Fix Qwen3 RotaryEmbedding fusion emitting invalid position_ids shape#29632
Open
Sammy-Dabbas wants to merge 1 commit into
Open
Fix Qwen3 RotaryEmbedding fusion emitting invalid position_ids shape#29632Sammy-Dabbas wants to merge 1 commit into
Sammy-Dabbas wants to merge 1 commit into
Conversation
The on-the-fly RoPE fusion added for Qwen3 in microsoft#27590 wires the traced position_ids into com.microsoft.RotaryEmbedding without adapting its shape. A real Qwen3 export has no position_ids input, so HuggingFace builds one with a hardcoded batch axis of 1, shape 1 by sequence_length, which the kernel rejects for batch_size greater than 1. Reduce it to an equivalent single-offset tensor. Adds a regression test.
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.
The on-the-fly RoPE fusion added for Qwen3 in #27590 wires the traced position_ids into the com.microsoft.RotaryEmbedding node without adapting its shape. A real Qwen3 export has no position_ids input, so HuggingFace builds one with a hardcoded batch axis of 1, shape 1 by sequence_length. The kernel only accepts a scalar or single-element position_ids, or an exact batch_size by sequence_length tensor, so any request with batch_size greater than 1 fails with "Input 'position_ids' dimension 0 should be of size batch_size, got 1". The existing tests missed it because the Qwen3 test generator declared position_ids as an already correct batch_size by sequence_length input.
In on-the-fly RoPE the position_ids is just the contiguous arange, and the kernel's scalar path derives every position from the first element, which reproduces that arange exactly and is batch-independent. This flattens the traced tensor and slices its first element to an equivalent single-offset input, so the fused node is correct at any batch size. A standalone check confirms the reduced output is bit-identical to the intended full arange, and a regression test verifies the fusion now emits it.
Fixes #29618.