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: remove near-duplicate sample/greedy copy #30773

Merged
merged 4 commits into from
May 13, 2024

Conversation

gante
Copy link
Member

@gante gante commented May 13, 2024

What does this PR do?

We have two pairs of functions that are near copy of each other. These pairs are exactly the same, except for the token selection part (argmax vs multinomial sampling):

  • _greedy_search/_sample
  • _beam_search/_beam_sample

This PR keeps the most popular version of each pair (_sample and _beam_search), making the appropriate changes to preserve all functionality.

@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.

@gante gante marked this pull request as ready for review May 13, 2024 14:16
Comment on lines +2254 to +2266
logger.warning_once(
"Calling `._greedy_search()` directly is deprecated and will be removed in v4.42. Use `._sample()` "
"instead, passing the same arguments."
)
return self._sample(
input_ids=input_ids,
logits_processor=logits_processor,
stopping_criteria=stopping_criteria,
generation_config=generation_config,
synced_gpus=synced_gpus,
streamer=streamer,
**model_kwargs,
)
Copy link
Member Author

Choose a reason for hiding this comment

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

There is no hard need for deprecations (these are private fns), but I know they are used in other internal libraries -- this should make the transition smoother!

@gante gante requested a review from ArthurZucker May 13, 2024 14:17
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.

great cleanup!

next_token_scores, max(2, 1 + n_eos_tokens) * num_beams, dim=1, largest=True, sorted=True
)
n_tokens_to_keep = max(2, 1 + n_eos_tokens) * num_beams
if do_sample:
Copy link
Collaborator

Choose a reason for hiding this comment

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

very nice thaat all the do_sample flag expose the real changed / diff between the two!

@gante gante merged commit de2f722 into huggingface:main May 13, 2024
23 checks passed
@gante gante deleted the rm_sample branch May 13, 2024 14:48
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