Add fused MRotaryEmbedding contrib op for Qwen mRoPE variants - #31728
Merged
Conversation
…le attribute Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
kunal-vaishnavi
August 8, 2026 00:07
View session
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
kunal-vaishnavi
marked this pull request as ready for review
August 8, 2026 23:38
Tianlei Wu (tianleiwu)
requested changes
Aug 10, 2026
Tianlei Wu (tianleiwu)
left a comment
Contributor
There was a problem hiding this comment.
The CPU/CUDA split and independent scalar test oracle make the normal rank/layout paths straightforward to verify. I found three correctness blockers around CUDA cache bounds, odd rotary dimensions, and empty CUDA tensors, plus two input-validation inconsistencies. Details are inline; please add focused negative and empty-input coverage with the fixes.
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Tianlei Wu (tianleiwu)
pushed a commit
that referenced
this pull request
Aug 11, 2026
## Description Add WebGPU Execution Provider support for the Microsoft-domain MRotaryEmbedding contrib op introduced for CPU and CUDA in #31728. The WebGPU kernel: - supports rank-3 BSNH and rank-4 BNSH inputs - supports sectioned and interleaved mRoPE layouts - supports interleaved and non-interleaved rotary pairing - supports float and float16 data, scaling, and partial rotary dimensions - preserves non-rotary tail values and invalid/out-of-range position IDs - validates full int64 position IDs by reading both u32 storage words - implements the shader as a parameterized WGSL template The operator tests now explicitly exercise WebGPU and extend the formerly CUDA-only negative-attribute, out-of-bounds position ID, and empty-input cases to both GPU providers. ## Testing - Release WebGPU build of onnxruntime_provider_test - onnxruntime_provider_test.exe --gtest_filter="ContribOpMRotaryEmbeddingTest.*" (9 tests passed) - WGSL template generation across the WebGPU source tree (52 templates generated) - lintrunner on all changed C++, header, test, and WGSL template files - git diff --check ## Notes The standalone WGSL template Python suite has two pre-existing golden-file mismatches for core subgroup-matrix templates. That smoke test scans core/providers/webgpu only; this PR adds its template under contrib_ops/webgpu and does not affect those mismatches.
Tianlei Wu (tianleiwu)
pushed a commit
that referenced
this pull request
Aug 12, 2026
## Summary `docs/ContribOperators.md` disagrees with the schema that generates it for the `MRotaryEmbedding` operator, causing the **Windows GPU Kernel Documentation Validation** CI check to fail on every PR merged to `main`. ## Root cause PR #31728 (`e415ef9afd`) added the fused `MRotaryEmbedding` contrib op. The checked-in doc contains: ``` setting `mrope_section` to a single full-width section (or omitting it) reduces this op to standard RoPE. ``` but the schema in `onnxruntime/core/graph/contrib_ops/bert_defs.cc` (and correspondingly the output of `gen_contrib_doc.py`) says: ``` setting `mrope_section` to a single full-width section reduces this op to standard RoPE. ``` The parenthetical `(or omitting it)` is also semantically incorrect — `mrope_section` is declared as a **required** attribute (`AttributeProto::INTS` with no default), so it cannot be omitted. ## Fix Remove the inaccurate parenthetical from the checked-in doc to match the schema/generator output. ```diff - (or omitting it) reduces this op to standard RoPE. + reduces this op to standard RoPE. ``` This is a hand-edit matching exactly what the generator (`tools/ci_build/gen_contrib_doc.py --domains com.microsoft`) produces. I did not run the generator because it requires built Python bindings. ## Impact Unblocks the documentation validation CI check for all open PRs targeting `main`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Aug 12, 2026
Closed
This was referenced Aug 15, 2026
Open
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.
Description
Introduces a new
com.microsoftcontrib op,MRotaryEmbedding, that fuses the multimodal RoPE (mRoPE) subgraphs used by the Qwen vision-language model family into a single op, replacing the multi-nodeShape/Gather/Unsqueeze/Expand/MatMul/Concat/Cos/Sin/Split/Gather/Slice/Neg/Concatchain currently built per Q/K tensor by the onnxruntime-genai model builder.Schema (
bert_defs.cc,ms_opset.h)RotaryEmbedding:position_idsbecomes a required 3D(3, batch_size, sequence_length)tensor stacking Temporal/Height/Width position streams.mrope_section(3 ints, sum ==rotary_embedding_dim/2).mrope_layout(0 = Sectioned/Chunked — Qwen2-VL/2.5-VL, 1 = Interleaved — Qwen3-VL/3.5 family).scale,interleaved,rotary_embedding_dim,num_heads,is_packed_batchingfromRotaryEmbeddingso a single full-width section reduces to standard RoPE.CPU kernel (
contrib_ops/cpu/bert/mrotary_embedding*)ComputeDimAssignmentsprecomputes, per cos/sin column, which T/H/W stream owns it (based onmrope_section/mrope_layout), independent of runtime data.scale, then rotates viaMlasRotaryEmbedOneRow.CUDA kernel (
contrib_ops/cuda/bert/mrotary_embedding*)RotaryEmbeddingBSNHlauncher (grid/block/shared-memory-for-in-place strategy).Both kernels are registered for float/MLFloat16 (CPU) and float/MLFloat16/BFloat16 (CUDA).
Motivation and Context
Qwen2-VL, Qwen2.5-VL, Qwen3-VL(-MoE), and Qwen3.5(-MoE) each require a distinct, hand-built mRoPE subgraph (~15-20 ops per Q/K rotary application) in the ONNX graph produced by the model builder. Fusing this into one op removes that per-layer overhead, reduces graph size, and gives a single, versioned contract for all current and future mRoPE layout variants.