deepspeedai / deepspeedai/DeepSpeed

Deepspeed Inference not working on llama when input has padding and using kernel injection

Open
#3,960 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug inference
Dominant language
Python
Stars
43.1k
Forks
5k
Avg merge
4d 15h
Merged PRs (30d)
112

Description

Describe the bug
I am tryting to do batch inference, so the inputs needs padding. When using replace_with_kernel_inject=True, the engine output is incorrect. setting replace_with_kernel_inject=False produces correct output.

I also tried to disable the padding, it also works.

So it seems that kernel injection does not support well when input has padding.

To Reproduce
Script:

import os

import torch
import torch.distributed as dist
import deepspeed
from transformers import AutoModelForCausalLM, GenerationConfig, AutoTokenizer


dist.init_process_group("nccl", world_size=int(os.environ["WORLD_SIZE"]))
# setup_distributed_slurm()

model_name="lmsys/vicuna-13b-v1.3" #'gpt2'   #
model = AutoModelForCausalLM.from_pretrained(model_name).cuda()
tokenizer = AutoTokenizer.from_pretrained(model_name)


texts = ["what is lemon?","tell me a story about moon", "what is kv cache in decoder-only transformoer models?", "explain e=mc^2",]

input_ids =[]
attention_masks=[]
for text in texts:
    model_inputs = tokenizer(text, max_length=96, padding="max_length",truncation=True,
                            return_tensors="pt")
    # if not padding, works fine
    # model_inputs = tokenizer(text,
    #                          return_tensors="pt")
    input_ids.append(model_inputs["input_ids"])
    attention_masks.append(model_inputs["attention_mask"])

input_ids = torch.cat(input_ids, dim=0).cuda()
attention_masks = torch.cat(attention_masks, dim=0).cuda()

generation_config = GenerationConfig(
    bos_token_id=1,
    do_sample=True,
    top_k=20,
    top_p=0.9,
    temperature=0.7,
    max_new_tokens=1024,
)

model = deepspeed.init_inference(model,
                        mp_size=1, dtype=torch.half,
                        checkpoint=None,
                        max_out_tokens = 1024,
                        replace_with_kernel_inject=True)    # replace_with_kernel_inject=False works fine

gen_out = model.generate(input_ids=input_ids,
                    attention_mask=attention_masks,
                    generation_config=generation_config,)
outputs = tokenizer.batch_decode(gen_out, skip_special_tokens=True)

for out in outputs:
    print(outputs)

Run this file with torch distributed.

Expected behavior
Expect the output to be nomal text like:

['what is lemon?\n\nLemon is a fruit, which is commonly used as a flavoring agent in food and drinks. It is a small, round citrus fruit that is typically yellow when ripe, but can also be green when unripe. The lemon tree is a small evergreen tree that is native to Asia, but is now grown in many other regions of the world, including the United States, Europe, and Africa. Lemons are typically grown in warm, sunny climates, and they are often grown in orchards or groves. Lemons are often used in cooking and baking, ....

False output: (when kernel injection=True)

['what is lemon?\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

ds_report output

DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
      runtime if needed. Op compatibility means that your system
      meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. [OKAY]
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
 [WARNING]  async_io requires the dev libaio .so object and headers but these were not found.
 [WARNING]  async_io: please install the libaio-devel package with yum
 [WARNING]  If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found.
async_io ............... [NO] ....... [NO]
cpu_adagrad ............ [NO] ....... [OKAY]
cpu_adam ............... [NO] ....... [OKAY]
fused_adam ............. [NO] ....... [OKAY]
fused_lamb ............. [NO] ....... [OKAY]
quantizer .............. [NO] ....... [OKAY]
random_ltd ............. [NO] ....... [OKAY]
 [WARNING]  sparse_attn requires a torch version >= 1.5 and < 2.0 but detected 2.0
 [WARNING]  using untested triton version (2.0.0), only 1.0.0 is known to be compatible
sparse_attn ............ [NO] ....... [NO]
spatial_inference ...... [NO] ....... [OKAY]
transformer ............ [NO] ....... [OKAY]
stochastic_transformer . [NO] ....... [OKAY]
transformer_inference .. [YES] ...... [OKAY]
--------------------------------------------------
No CUDA runtime is found, using CUDA_HOME='/mnt/cuda-11.8'
DeepSpeed general environment info:
torch install path ............... ['/mnt/miniconda3/envs/pt20llm/lib/python3.9/site-packages/torch']
torch version .................... 2.0.1+cu118
deepspeed install path ........... ['/mnt/miniconda3/envs/pt20llm/lib/python3.9/site-packages/deepspeed']
deepspeed info ................... 0.10.0+aef6c65, aef6c65, master
torch cuda version ............... 11.8
torch hip version ................ None
nvcc version ..................... 11.8
deepspeed wheel compiled w. ...... torch 2.0, cuda 11.8

System info (please complete the following information):

  • OS: [Centos 7]
  • GPU count and types [ 1 machines x1 A100s ]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided batch-inference script and compare deepspeed.init_inference with replace_with_kernel_inject set to true and false, using the padded inputs and attention masks shown. Trace the kernel-injection inference entry point to determine why padded Llama inputs produce incorrect generation, then verify that injected and non-injected runs produce equivalent normal text.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.