Fix left-padding token selection in BioGptForSequenceClassification#46782
Merged
Rocketknight1 merged 1 commit intoJun 22, 2026
Merged
Conversation
BioGptForSequenceClassification was the last decoder classification head still using the legacy pooling index (input_ids != pad).sum(-1) - 1, which is the last real token only under right padding. With left padding the real tokens are shifted to the end, so the head pooled an earlier token and returned silently wrong logits and loss. Use the same left/right-padding safe selection as the other decoders (from huggingface#35911). Signed-off-by: Ting Sun <[email protected]>
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: biogpt |
Contributor
|
CI Dashboard: View test results in Grafana |
Rocketknight1
approved these changes
Jun 22, 2026
Rocketknight1
left a comment
Member
There was a problem hiding this comment.
Yep, LGTM! Glad we can finally clean out the old code
Rocketknight1
enabled auto-merge
June 22, 2026 13:34
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
BioGptForSequenceClassificationis the last decoder classification head still using the legacy pooling index:(num_non_pad - 1)equals the index of the last real token only when padding is on the right. With left padding the real tokens are shifted to the end, so this selects a token in the front of the sequence and the head pools the wrong position. The classification logits and loss are then silently wrong, with no error or warning. Right padding is unaffected.#35911 moved every other
ForSequenceClassificationdecoder (gpt2, llama, ...) onto a left/right-padding-safe selection, but biogpt was missed. This PR brings biogpt in line by using the same block as gpt2:which takes the rightmost non-pad index and is correct for both padding sides. This is the full block from gpt2, so it also adds the same
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")guard forpad_token_id is Nonewithbatch_size > 1(previously biogpt silently pooled index-1); this matches every other decoder after #35911. The edit is inmodular_biogpt.py, regenerated intomodeling_biogpt.pywith the modular converter.biogpt is the only remaining model with this pattern (
grep "torch.ne(input_ids, self.config.pad_token_id).sum"matches biogpt only).Reproduction (CPU, fp32, no GPU)
The check stays inside a single forward to remove the position-embedding shift that left padding introduces: the head's
pooled_logitsshould equal the per-position logits at the true last real token.Before fix (selects index 4, the buggy
num_non_pad - 1):After fix (selects index 7, the real last token):
gpt2 already returns
0.0here; right padding returns0.0for biogpt before and after.Added regression test
test_biogpt_sequence_classification_left_padding: it assertspooled_logitsequals the last-real-token logits from the same forward. It fails on the current code and passes with the fix. Fulltests/models/biogpt/test_modeling_biogpt.pypasses.Code Agent Policy
Before submitting
Who can review?
cc @Rocketknight1