Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize get_module_leaves speed #2756

Merged
merged 3 commits into from
May 13, 2024
Merged

optimize get_module_leaves speed #2756

merged 3 commits into from
May 13, 2024

Conversation

BBuf
Copy link
Contributor

@BBuf BBuf commented May 9, 2024

Background

When I try to inference deepseek-v2 with transformers:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig

model_name = "deepseek-ai/DeepSeek-V2"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
# `max_memory` should be set based on your devices
max_memory = {i: "75GB" for i in range(8)}
# `device_map` cannot be set to `auto`
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, device_map="sequential", torch_dtype=torch.bfloat16, max_memory=max_memory, attn_implementation="eager")
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id

text = "An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is"
inputs = tokenizer(text, return_tensors="pt")
outputs = model.generate(**inputs.to(model.device), max_new_tokens=100)

result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(result)

I found that program with stuck...

Solve

In fact, the stuck appearing here .

图片

For DeepSeek V2, the length of module_sizes is 68185, and the code here has a complexity of O(N^2) (where N = module_sizes), which requires a very long time to execute, giving the illusion of being stuck. This PR optimizes the code to have a complexity of O(N), allowing it to quickly reach the stage of loading the large model. The inference results on an 8xA800 machine are also normal after this optimization.

img_v3_02an_91e8f57c-fb20-415f-84e8-8effc882f8bg

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@muellerzr muellerzr requested a review from SunMarc May 9, 2024 12:22
Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this great PR @BBuf ! Just a small nit.

src/accelerate/utils/modeling.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this much more efficient and fixing 🤗

@muellerzr muellerzr merged commit 6cf1cc0 into huggingface:main May 13, 2024
23 checks passed
yhna940 pushed a commit to yhna940/accelerate that referenced this pull request May 16, 2024
* optimize get_module_leaves

* fix format

* Update modeling.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants