Allow position_ids_start=2 on DataCollatorWithFlattening for RoBERTa etc.#47525
Open
tomaarsen wants to merge 1 commit into
Open
Allow position_ids_start=2 on DataCollatorWithFlattening for RoBERTa etc.#47525tomaarsen wants to merge 1 commit into
tomaarsen wants to merge 1 commit into
Conversation
Contributor
CI recapDashboard: View test results in Grafana |
|
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. |
vasqu
reviewed
Jul 24, 2026
| indices_q = (position_ids == 0).nonzero().view(-1) | ||
| # Packed sequences all restart from the same first position id, but it is not always 0 | ||
| # (RoBERTa-like models start at padding_idx + 1) | ||
| indices_q = (position_ids == position_ids.min()).nonzero().view(-1) |
Collaborator
There was a problem hiding this comment.
Can we add an integration test for roberta that tests this case implicitly? It's ok to require FA or kernels
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.
Fixes #47496
What does this PR do?
Pull Request overview
position_ids_startargument toDataCollatorWithFlatteningposition_ids == position_ids.min()instead ofposition_ids == 0Details
As described in #47496,
DataCollatorWithFlatteningnumbers positions from 0, but RoBERTa-like architectures (roberta,xlm_roberta,camembert,mpnet,esmwith absolute position embeddings, and more) compute their position ids starting atpadding_idx + 1(usually 2) when none are passed. Feeding these models the collator's 0-basedposition_idssilently shifts the position embeddings and degrades the outputs. This affects some very common checkpoints, e.g.xlm-roberta.This PR adds a
position_ids_startargument (default 0, existing behavior is unchanged) so users can match the numbering of their model, e.g.position_ids_start=2for XLM-R. The collator cannot reliably derive this from a config:mpnetfor example hardcodespadding_idx = 1regardless ofconfig.pad_token_id. The docstring now documents all arguments and warns about the pitfall.The new argument alone turned out to be insufficient on the path where FlashAttention infers the varlen kwargs from
position_idsat runtime (i.e. withoutreturn_flash_attn_kwargs=True, like the reproduction in the issue).prepare_fa_kwargs_from_position_idsdetected sequence starts viaposition_ids == 0, which never matches offset positions, after which it crashes on an emptycu_seq_lens.diff().max(). I changed the detection toposition_ids == position_ids.min(), mirroring_is_packed_sequencewhich already handles a non-zero start viaposition_ids.min(). Whenever a 0 position is present, this is behavior-identical, so existing models are unaffected.I verified that with
position_ids_start=2the collator output exactly matchesRobertaEmbeddings.create_position_ids_from_input_idson unpadded sequences, and that the RoBERTa embedding layer produces identical outputs for the flattened batch and for the individual sequences. The flattening collator tests now cover the new argument as well as the boundary inference on collator output.Because of this
prepare_fa_kwargs_from_position_idspatch, the PR got a bit bigger than I originally intended in #47496 (comment), but it does seem important.Code Agent Policy
The tests and docstrings were written by Fable and reviewed by myself.
Before submitting
Pull Request checks?
to it if that's the case.
Who can review?
@vasqu