-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
Batch inputs get different result to single input for llama model. #30378
Comments
According to my debug, the generate mask is not correct. for example, there should be some large negative values in mask, but there is only 0 now.
|
Hey @liangan1 ! There are a few things that have to be changed for generation to work properly in batched form. Firstly, tt is recommended to use the left padding side in generation if you are using a decoder-only models. Also, attention mask needs to be passed into the from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf", padding_side='left') # left padding for generation
model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
inputs = tokenizer(["how are you?", "what is the best the AI algorithm?"], return_tensors="pt",padding=True)
output = model.generate(**inputs, max_new_tokens=10) # pass in not only input ids, but also attention mask
out_text = tokenizer.batch_decode(output, skip_special_tokens=True)
print(out_text)
input_ids = tokenizer(["how are you?"], return_tensors="pt",padding=True).input_ids
output = model.generate(input_ids, max_new_tokens=10)
out_text = tokenizer.batch_decode(output, skip_special_tokens=True)
print(out_text) |
@zucchini-nlp thanks. why user need to create mask by themself? |
@liangan1 you don't have to create it manually. The tokenizer returns attention mask, which should be passed into generate.
I will close the issue as resolved. For any further questions it is recommended to ask in the forum 🤗 |
Thanks for your help. |
@liangan1 to complement the answer above: there are a few seemingly innocuous differences that may result in slightly different LLM outputs, such as batching. To understand why it happens (and why it is unavoidable), have a look at this comment :) |
Thanks for your info. |
System Info
Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points.
transformers
version: 4.40.0Who can help?
No response
Information
Tasks
examples
folder (such as GLUE/SQuAD, ...)Reproduction
import torch
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
#import intel_extension_for_pytorch as ipex
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf", padding_side='right')
model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
input_ids = tokenizer(["how are you?", "what is the best the AI algorithm?"], return_tensors="pt",padding=True).input_ids
#model = ipex.llm.optimize(model, deployment_mode=False)
output = model.generate(input_ids, max_new_tokens=10)
out_text = tokenizer.batch_decode(output, skip_special_tokens=True)
print(out_text)
input_ids = tokenizer(["how are you?"], return_tensors="pt",padding=True).input_ids
output = model.generate(input_ids, max_new_tokens=10)
out_text = tokenizer.batch_decode(output, skip_special_tokens=True)
print(out_text)
Expected behavior
For batch=2, the output should be ['how are you? I'm doing well, thanks for asking!', 'what is the best the AI algorithm?\n\nThere is no single "best" A'].
The text was updated successfully, but these errors were encountered: