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

GenerationConfig validate both constraints and force_words_ids #29163

Conversation

FredericOdermatt
Copy link
Contributor

What does this PR do?

There is currently two ways to pass constraints to constrained beam search, either constraints or force_words_ids. They get merged here which is after the GenerationConfig initialization. I was following the blog on constrained decoding (using force_words_ids and was checking for myself which other options constrained beam search can be combined with (Ex: can I sample and constrained beam search? [no])

This snippet raised a ValueError:

    generated_tokens = self.model.generate(
        **encoded_source,
        forced_bos_token_id=forced_bos_token_id,
        num_beams=5,
        num_return_sequences=1,
        constraints=constraints,
        do_sample=True
    )

while this snipped didn't:

    generated_tokens = self.model.generate(
        **encoded_source,
        forced_bos_token_id=forced_bos_token_id,
        num_beams=5,
        num_return_sequences=1,
        force_words_ids=force_words_ids,
        do_sample=True
    )

I've added the additional check to GenerationConfig.validate() to include force_words_ids and I've added two new tests:

        with self.assertRaises(ValueError):
            # dummy constraint
            GenerationConfig(do_sample=True, num_beams=2, constraints=["dummy"])
        with self.assertRaises(ValueError):
            GenerationConfig(do_sample=True, num_beams=2, force_words_ids=[[[1, 2, 3]]])

Without the change only the first test passes, with the change both tests pass as they now both raise ValueErrors (Expected behavior).

This is my first time opening a PR here, so let me know what I can improve. I've followed the docs on contribution for this PR, though after running all tests after installation there were 5 tests that failed before I changed anything.

Cheers! :)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

maybe @gante

@FredericOdermatt FredericOdermatt changed the title GenerationConfig validate both options for constrained decoding: cons… GenerationConfig validate both constraints and force_words_ids Feb 21, 2024
Copy link
Member

@gante gante left a comment

Choose a reason for hiding this comment

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

Thank you for noticing this issue and for fixing it 💛

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.

Thanks!

@ArthurZucker ArthurZucker merged commit 871ba71 into huggingface:main Feb 27, 2024
20 checks passed
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.

None yet

3 participants