Skip to content

Raise a clear error when a token is both forced and suppressed - #47511

Merged
ArthurZucker merged 2 commits into
mainfrom
fix-suppress-forced-token-nan
Sep 1, 2026
Merged

ArthurZucker merged 2 commits into
mainfrom
fix-suppress-forced-token-nan

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

CI

Small low priority contribution:

Using the same token in forced_eos_token_id/forced_bos_token_id and suppress_tokens is contradictory: at the forcing step every logit but the forced token is set to -inf, then the shared token is suppressed to -inf too — leaving all logits -inf, producing nan probabilities and the cryptic RuntimeError: probability tensor contains inf/nan.

GenerationConfig.validate() now rejects this with an actionable error. It fires only when every forced token is also suppressed (a guaranteed crash); partial overlap leaves a surviving forced token and is allowed. This also covers generate() (via validate()) and save_pretrained.

Before

model.generate(**inputs, forced_eos_token_id=eos, suppress_tokens=[eos])
# RuntimeError: probability tensor contains either `inf`, `nan` or element < 0

After

model.generate(**inputs, forced_eos_token_id=eos, suppress_tokens=[eos])
# ValueError: Every token in `forced_eos_token_id` ([2]) is also in `suppress_tokens`.
# Forcing a token while suppressing it sets all logits to `-inf` at the forcing step, which
# produces `nan` probabilities and crashes generation. Remove the overlapping token(s) from
# either `forced_eos_token_id` or `suppress_tokens` (if you meant to prevent an early EOS
# token, use `min_new_tokens` instead).

Still allowed — partial overlap leaves a usable forced token, so no crash:

GenerationConfig(forced_eos_token_id=[2, 5], suppress_tokens=[2])  # ok

Closes #24099.

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

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 30142558377:1
Result: success | Jobs: 16 | Tests: 165,742 | Failures: 0 | Duration: 14h 22m

@ArthurZucker
ArthurZucker added this pull request to the merge queue Sep 1, 2026
Merged via the queue into main with commit be255fb Sep 1, 2026
112 checks passed
@ArthurZucker
ArthurZucker deleted the fix-suppress-forced-token-nan branch September 1, 2026 06:20
davidnichols-ops added a commit to davidnichols-ops/transformers that referenced this pull request Sep 10, 2026
Resolve merge conflict with the suppress/forced-token overlap check
(huggingface#47511): keep both checks, renumber the pad/eos overlap check to 2.8.
Preserves the PR's final intent: warning-only via logger.warning_once
(not added to minor_issues), multi-token eos lists only, and
validate(strict=True) must not raise so save_pretrained keeps working
for models that ship with pad_token_id in eos_token_id.
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Sep 16, 2026
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.

generation error when same token in "forced_eos_token_id" and "supress_token" parameter.

3 participants