Skip to content

Add unigram sampling (alpha, nbest_size)#1994

Merged
ArthurZucker merged 8 commits intohuggingface:mainfrom
kennethsible:unigram-sampling
Apr 8, 2026
Merged

Add unigram sampling (alpha, nbest_size)#1994
ArthurZucker merged 8 commits intohuggingface:mainfrom
kennethsible:unigram-sampling

Conversation

@kennethsible
Copy link
Copy Markdown
Contributor

I noticed that models.Unigram doesn't support sampling, which enables subword regularization (arguably one of the main reasons to choose the Unigram model). I checked GitHub and there are multiple closed issues on this topic (#730, #849). In one of these issues, it was mentioned that the sampling code has already been implemented in lattice.rs and simply needs to be exposed through Python. I filled in the missing details and added a sample_nbest function for parity with Google's implementation. I also copied the interface for BPE dropout as closely as possible, including getters and setters for the new sampling parameters.

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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.

Copy link
Copy Markdown
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! Do you mind adding a test on python side bindings/python/tests/bindings/test_models.py!

Also update the doc of PyUnigram to mention these new args and the new behavior!

I am not super super familiar with unigram / nbest etc but happy to have parity!

@kennethsible
Copy link
Copy Markdown
Contributor Author

Also update the doc of PyUnigram to mention these new args and the new behavior!

@ArthurZucker Is updating the docstring sufficient? I wasn't sure if the documentation is autogenerated.

Copy link
Copy Markdown
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.

Yep that's good

@ArthurZucker ArthurZucker merged commit 2827113 into huggingface:main Apr 8, 2026
35 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.

3 participants