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

Generate: nudge towards do_sample=False when temperature=0.0 #25722

Merged
merged 3 commits into from
Aug 24, 2023

Conversation

gante
Copy link
Member

@gante gante commented Aug 24, 2023

What does this PR do?

Related issue: meta-llama/llama#687

Improves the error message when temperature=0.0, which asymptotically corresponds to greedy decoding... except that it results in numerical problems :D


test run:

from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
model = AutoModelForCausalLM.from_pretrained("distilgpt2")

inputs = tokenizer(["The quick brown"], return_tensors="pt")
gen_out = model.generate(**inputs, do_sample=True, temperature=0.0)

yields

ValueError: `temperature` (=0.0) has to be a strictly positive float, otherwise your next token scores will be invalid. If you're looking for greedy decoding strategies, set `do_sample=False`.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Aug 24, 2023

The documentation is not available anymore as the PR was closed or merged.

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.

We could also just set do_sample = False in case temperature = 0. Will let you decide !

raise ValueError(f"`temperature` has to be a strictly positive float, but is {temperature}")
except_msg = (
f"`temperature` (={temperature}) has to be a strictly positive float, otherwise your next token "
"scores will be invalid."
Copy link
Collaborator

Choose a reason for hiding this comment

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

do you mean that it will be nan?

Copy link
Member Author

Choose a reason for hiding this comment

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

depends on the value the user places here (e.g. a negative float will not generate nans, but make the scores enter uncharted territory), hence the vague message

@gante
Copy link
Member Author

gante commented Aug 24, 2023

We could also just set do_sample = False in case temperature = 0. Will let you decide !

I agree we should do that! But I'm going to leave that for the generate refactor, as it implies significant code changes to do it right :)

@gante gante merged commit 0a365c3 into huggingface:main Aug 24, 2023
21 checks passed
@gante gante deleted the temp_msg branch August 24, 2023 13:15
parambharat pushed a commit to parambharat/transformers that referenced this pull request Sep 26, 2023
blbadger pushed a commit to blbadger/transformers that referenced this pull request Nov 8, 2023
EduardoPach pushed a commit to EduardoPach/transformers that referenced this pull request Nov 18, 2023
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.

3 participants