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

Add Beam Search sampler #618

Merged
merged 3 commits into from
Feb 9, 2024
Merged

Add Beam Search sampler #618

merged 3 commits into from
Feb 9, 2024

Conversation

rlouf
Copy link
Member

@rlouf rlouf commented Feb 6, 2024

Closes #258.

In order to implement Beam Search I had to make a few changes to the samplers, they now:

  • Update the sequences' weights (log-probability)
  • Return the new token_ids's ancestors, i.e. the sequence to which they need to be added. While trivial for greedy and multinomial sampling, this is what will allow us to update "beams" in beam search.

token_ids, attention_masks, kv_cache, fsm and fsm_states are now updated using the ancestors information. GenerationState contains the sequence's weights and ancestors so we can inspect the sampling process.

I also simplified the get_generated_token_ids method of SequenceGenerator. We should soon do a cleaning pass on this class: remove deprecated init arguments, make some methods independant functions and test them.

@rlouf rlouf added enhancement transformers Linked to the `transformers` integration labels Feb 6, 2024
@rlouf rlouf force-pushed the beam-search-implementation branch 10 times, most recently from 137740a to a06f4b5 Compare February 8, 2024 12:32
@rlouf rlouf marked this pull request as ready for review February 8, 2024 13:29
Copy link
Collaborator

@lapp0 lapp0 left a comment

Choose a reason for hiding this comment

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

Included a few questions and a documentation fix.

Nice to see how straightforward a sampler implementation can be when accompanied with a well designed SequenceGenerator.

from outlines import models, generate, samplers


model = models.transformers("mistralai/Mistral-7B-0.1")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Typo, it should be mistralai/Mistral-7B-v0.1

However I think we should be recommending mistralai/Mistral-7B-Instruct-v0.2

docs/reference/samplers.md Show resolved Hide resolved
outlines/generate/api.py Show resolved Hide resolved
outlines/generate/generator.py Outdated Show resolved Hide resolved
outlines/generate/generator.py Show resolved Hide resolved
outlines/generate/generator.py Show resolved Hide resolved
outlines/samplers.py Show resolved Hide resolved
@rlouf
Copy link
Member Author

rlouf commented Feb 9, 2024

I overlooked something: some Beam Search implementations clone each beam K times and then down sample them to preserve some kind of sample diversity, see this implementation for instance. This can be done in another PR.

@rlouf rlouf force-pushed the beam-search-implementation branch 2 times, most recently from d23d3fd to c269c0b Compare February 9, 2024 17:09
@rlouf rlouf merged commit 94e2a38 into main Feb 9, 2024
5 checks passed
@rlouf rlouf deleted the beam-search-implementation branch February 9, 2024 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement transformers Linked to the `transformers` integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add beam search
2 participants