Skip to content
This repository was archived by the owner on Mar 20, 2026. It is now read-only.

adding search.PrefixConstrainedBeamSearch#2646

Closed
nicola-decao wants to merge 5 commits intofacebookresearch:masterfrom
nicola-decao:add_PrefixConstrainedBeamSearch
Closed

adding search.PrefixConstrainedBeamSearch#2646
nicola-decao wants to merge 5 commits intofacebookresearch:masterfrom
nicola-decao:add_PrefixConstrainedBeamSearch

Conversation

@nicola-decao
Copy link
Copy Markdown
Contributor

@nicola-decao nicola-decao commented Sep 22, 2020

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos, doc improvements)
  • Did you read the contributor guideline?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

What does this PR do?

This adds a new decoding strategy search.PrefixConstrainedBeamSearch that limits the vocabulary of the next token generation given a prefix (that is the previously generated tokens during beam search). An end user has just to give the optional argument prefix_allowed_tokens_fn to .generate or .sample to activate PrefixConstrainedBeamSearch. prefix_allowed_tokens_fn(batch_id, tokens) is a callback function that given the batch_id and tokens returns the list of allowed token for the next generation step.

Did you have fun?

YES! 🙃

@nicola-decao
Copy link
Copy Markdown
Contributor Author

The test failed on something that is not part of the pull request

Copy link
Copy Markdown

@myleott myleott left a comment

Choose a reason for hiding this comment

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

You can ignore the test_translation_multi_simple_epoch test failure (the psutil import failure has been fixed in trunk).

But the test_ensemble_sequence_generator (tests.test_sequence_generator.TestJitSequeneceGenerator) failures seems related (see comment below)

Comment thread fairseq/sequence_generator.py Outdated
if num_remaining_sent == 0:
break
if isinstance(self.search, search.PrefixConstrainedBeamSearch) and step >= max_len:
if self.search.stop_on_max_len and step >= max_len:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@myleott has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@nicola-decao nicola-decao requested a review from myleott October 6, 2020 15:38
Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@myleott has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@nicola-decao
Copy link
Copy Markdown
Contributor Author

@myleott has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

I am not a Facebook employee so I cannot see the warnings and why this fails.

@fabiopetroni
Copy link
Copy Markdown

@myleott has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

I am not a Facebook employee so I cannot see the warnings and why this fails.

I'm taking care of this :)

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@myleott merged this pull request in 086fe1c.

jinyiyang-jhu pushed a commit to jinyiyang-jhu/fairseq-jyang that referenced this pull request Feb 26, 2021
Summary:
# Before submitting

- [ ] Was this discussed/approved via a Github issue? (no need for typos, doc improvements)
- [x] Did you read the [contributor guideline](https://github.com/pytorch/fairseq/blob/master/CONTRIBUTING.md)?
- [x] Did you make sure to update the docs?
- [x] Did you write any new necessary tests?

## What does this PR do?
This adds a new decoding strategy `search.PrefixConstrainedBeamSearch` that limits the vocabulary of the next token generation given a prefix (that is the previously generated tokens during beam search). An end user has just to give the optional argument `prefix_allowed_tokens_fn` to `.generate` or `.sample` to activate `PrefixConstrainedBeamSearch`. `prefix_allowed_tokens_fn(batch_id, tokens)` is a callback function that given the `batch_id` and `tokens` returns the list of allowed token for the next generation step.

## Did you have fun?
YES! �

Pull Request resolved: facebookresearch/fairseq#2646

Reviewed By: fabiopetroni

Differential Revision: D24006805

Pulled By: myleott

fbshipit-source-id: 40b1a866c6ea9f936272db27e2a020b18dbf8164
caltia pushed a commit to caltia/fairseq that referenced this pull request Jul 8, 2025
Summary:
# Before submitting

- [ ] Was this discussed/approved via a Github issue? (no need for typos, doc improvements)
- [x] Did you read the [contributor guideline](https://github.com/pytorch/fairseq/blob/master/CONTRIBUTING.md)?
- [x] Did you make sure to update the docs?
- [x] Did you write any new necessary tests?

## What does this PR do?
This adds a new decoding strategy `search.PrefixConstrainedBeamSearch` that limits the vocabulary of the next token generation given a prefix (that is the previously generated tokens during beam search). An end user has just to give the optional argument `prefix_allowed_tokens_fn` to `.generate` or `.sample` to activate `PrefixConstrainedBeamSearch`. `prefix_allowed_tokens_fn(batch_id, tokens)` is a callback function that given the `batch_id` and `tokens` returns the list of allowed token for the next generation step.

## Did you have fun?
YES! �

Pull Request resolved: facebookresearch/fairseq#2646

Reviewed By: fabiopetroni

Differential Revision: D24006805

Pulled By: myleott

fbshipit-source-id: 40b1a866c6ea9f936272db27e2a020b18dbf8164
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants