Add position_ids to RoFormerForCausalLM forward pass#44705
Open
saivedant169 wants to merge 1 commit intohuggingface:mainfrom
Open
Add position_ids to RoFormerForCausalLM forward pass#44705saivedant169 wants to merge 1 commit intohuggingface:mainfrom
saivedant169 wants to merge 1 commit intohuggingface:mainfrom
Conversation
Threads position_ids through RoFormerForCausalLM -> RoFormerModel -> RoFormerEncoder -> RoFormerSinusoidalPositionalEmbedding, enabling explicit position control for flash attention packed sequence optimization and API consistency with other CausalLM models. Closes huggingface#32937 (for RoFormer) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: roformer |
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 part of #32937
What does this PR do?
RoFormer introduced rotary position embeddings, but its
ForCausalLMforward method doesn't acceptposition_ids— which means callers can't specify custom positions for packed sequences or flash attention without padding.The interesting bit is that
RoFormerSinusoidalPositionalEmbedding.forward()already accepts aposition_idsargument. It just was never wired up from the model's public API.This PR threads
position_idsthrough the full call chain:RoFormerForCausalLM→RoFormerModel→RoFormerEncoder→RoFormerSinusoidalPositionalEmbeddingWhen
position_idsisNone, behaviour is identical to before (sequential positions are generated internally). When provided, the embedding layer uses them directly.Shape handling during generation
GenerationMixinpasses 2Dposition_idsof shape[batch_size, seq_len], which makes the embedding output 3D instead of the usual 2D. The encoder now checks the output dimensionality and reshapes accordingly —[1, 1, seq_len, dim]for the broadcast case,[batch, 1, seq_len, dim]when batch-specific positions are provided.How was it tested?
test_for_generate_causal_lmpasses (this exercises theGenerationMixin→position_idspath)make styleandmake fix-repoboth cleanCoordination
Commented on #32937 here. This covers RoFormer specifically — other models missing
position_idscan be addressed in separate PRs.