Skip to content

Ensure num_logits_to_keep is always set to 1 for generate calls#1681

Merged
xenova merged 7 commits into
mainfrom
num_logits_to_keep-fix
May 8, 2026
Merged

Ensure num_logits_to_keep is always set to 1 for generate calls#1681
xenova merged 7 commits into
mainfrom
num_logits_to_keep-fix

Conversation

@xenova
Copy link
Copy Markdown
Collaborator

@xenova xenova commented May 8, 2026

Closes #1666

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

@xenova xenova merged commit 3dc58cb into main May 8, 2026
4 checks passed
@xenova xenova deleted the num_logits_to_keep-fix branch May 8, 2026 19:30
IdkwhatImD0ing added a commit to emilyLi2020/WAVE that referenced this pull request May 14, 2026
@huggingface/transformers@4.2.0's decoder_forward() unconditionally
overwrites num_logits_to_keep to 0n for Gemma 4 sessions, so every decode
step computes logits over the full prompt sequence (262144 vocab × every
prompt token) instead of just the last token. For 200-500-token framing
prompts this is a 200-500x extra MatMul on lm_head per token, explaining
browser tok/s near 6 on a card that should be much faster.

Fix is upstream in PR huggingface/transformers.js#1681
(merged 2026-05-08) but not yet in any published release (latest is 4.2.0
from 2026-04-23). We vendor a one-line backport via pnpm patch until 4.2.1+
ships.

Patch covers all four bundle paths referenced from package.json#exports:
  dist/transformers.node.cjs   (Node CJS - SSR)
  dist/transformers.node.mjs   (Node ESM)
  dist/transformers.web.js     (Browser)
  dist/transformers.js         (legacy)

To remove after the bump: drop client/patches/, drop the
patchedDependencies entry in pnpm-workspace.yaml, bump
@huggingface/transformers in client/package.json, regenerate lockfile.

Full write-up: docs/transformers-js-gemma4-perf.md
Reported: huggingface/transformers.js#1666
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.

Gemma 4 generation passes num_logits_to_keep=0 in decoder_forward, causing full-prompt logits memory blowup

2 participants