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

fix jamba slow foward for multi-gpu #30418

Merged
merged 4 commits into from
Apr 24, 2024
Merged

Conversation

SunMarc
Copy link
Member

@SunMarc SunMarc commented Apr 23, 2024

What does this PR do ?

This PR makes the slow forward of Jamba model compatible with multi-gpu setup with use_cache=True. The issue is that the cache is not initialized on the right device. Not sure we want to refactor the cache since the key_cache and and value_cache are handled correctly. This is just specific to ssm_states. Moving the tensor manually should only impact the first forward also. After the first forward, the cache will be correctly initialized.
Fixes #30367

Code:

import os
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig

prompt = "def quicksort(arr):\n"
tokenizer = AutoTokenizer.from_pretrained("ai21labs/Jamba-tiny-random")
tokenizer.pad_token = tokenizer.eos_token
config = AutoConfig.from_pretrained("ai21labs/Jamba-tiny-random")
# change the following to switch between fast and slow forward 
config.use_mamba_kernels=False 
model = AutoModelForCausalLM.from_pretrained(
    "ai21labs/Jamba-tiny-random",
    device_map="auto",
    config = config
)
print(model.hf_device_map)
print(model.config)
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(input_ids=inputs.to(model.device), max_new_tokens=512, do_sample=False)
print(tokenizer.decode(outputs[0]))

@SunMarc SunMarc requested a review from ArthurZucker April 23, 2024 11:06
@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.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Seems like it's the best tradeoff:

could be changed but would need to accomodate for every layers? (or is the PKV handled by accelerate separately? )

@SunMarc
Copy link
Member Author

SunMarc commented Apr 24, 2024

could be changed but would need to accommodate for every layers? (or is the PKV handled by accelerate separately? )

Yes, we would need to accommodate for every layer but that can become quite complicated if we start to introduce layers that are offloaded to the disk and cpu. Hence, I think that it is not worth investing too much effort on that. The PKV is not handled by accelerate separately.

@SunMarc SunMarc merged commit 37fa1f6 into huggingface:main Apr 24, 2024
18 checks passed
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.

model.generate fail for Jamba with device_map="auto"
3 participants