deepspeedai / deepspeedai/DeepSpeed

[BUG] VRAM increasing after each call to model

Open
#3,073 1 comment 0 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'm using Deepspeed to speed up inference. I'm facing three issues:

  1. VRAM (GPU memory) consumption is increasing after each call to the model (each response generation). Without using Deepspeed this is not the case. I'm already using torch.cuda.empty_cache(), please see below.
  2. Did I miss some important arguments or steps to further improve inference time? Is it correct to first create the model (GPTJForCausalLM.from_pretrained) and then feed this to Deepspeed (deepspeed.init_inference)? When I try to directly instantiate the Deepspeed model, it fails with the error 'str' object has no attribute 'text_encoder'.

To Reproduce

import transformers
from transformers import GPTJForCausalLM
import torch
import deepspeed
tokenizer = transformers.AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B", pad_token='<|endoftext|>', eos_token='<|endoftext|>', truncation_side='left')
model = GPTJForCausalLM.from_pretrained(
            "EleutherAI/gpt-j-6B"
            revision="float16",
            torch_dtype=torch.float16,
            low_cpu_mem_usage=True,
            use_cache=True,
            gradient_checkpointing=True,
)
model = deepspeed.init_inference(
            model=model,
            mp_size=1,
            dtype=model.dtype,
            replace_method="auto",
            replace_with_kernel_inject=True
)
device = 'cuda'
model.to(device)

prompt = tokenizer(text, return_tensors='pt', truncation="only_first", max_length=2048)
prompt = {key: value.to(device) for key, value in prompt.items()}
out = model.generate(**prompt,
                                  min_length=16,
                                  max_new_tokens=100,
                                  do_sample=True,
                                  top_k=35,
                                  top_p=0.6,
                                  temperature=1,
                                  no_repeat_ngram_size=4,
                                  use_cache=True,
                                  pad_token_id=tokenizer.eos_token_id,
                                  stopping_criteria=StoppingCriteriaList([stopping_criteria])  # Here I provide some stopping criteria
)
res = tokenizer.decode(out[0])
torch.cuda.empty_cache()

Expected behavior

  1. Not increasing VRAM.
  2. Lower inference time and direct instantiation with deepspeed.init_inference(...).

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-dev package with apt
[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 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 .. [NO] ....... [OKAY]
utils .................. [NO] ....... [OKAY]

DeepSpeed general environment info:
torch install path ............... ['/home/myUsername/anaconda3/envs/huggingface/lib/python3.11/site-packages/torch']
torch version .................... 2.0.0+cu117
deepspeed install path ........... ['/home/myUsername/anaconda3/envs/huggingface/lib/python3.11/site-packages/deepspeed']
deepspeed info ................... 0.8.3, unknown, unknown
torch cuda version ............... 11.7
torch hip version ................ None
nvcc version ..................... 11.7
deepspeed wheel compiled w. ...... torch 2.0, cuda 11.8

System info (please complete the following information):

  • OS: Ubuntu 22.04 LTS
  • One machine with 1x Nvidia RTX 3090
  • Deepspeed 0.8.3
  • Transformers 4.27.2
  • Accelerate 0.17.1
  • Python v3.11

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 supplied reproduction around deepspeed.init_inference, model.generate, and torch.cuda.empty_cache(), then compare the reported ds_report environment. Determine whether repeated generation causes the VRAM growth and separately assess the requested inference setup and direct-instantiation error; done means the reproduction has an identified cause or confirmed limitation for each reported problem.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.