Skip to content

Add fused MRotaryEmbedding contrib op for Qwen mRoPE variants - #31728

Merged
kunal-vaishnavi merged 13 commits into
mainfrom
copilot/mrope-variants-support
Aug 11, 2026
Merged

Add fused MRotaryEmbedding contrib op for Qwen mRoPE variants#31728
kunal-vaishnavi merged 13 commits into
mainfrom
copilot/mrope-variants-support

Conversation

Copilot AI commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Description

Introduces a new com.microsoft contrib op, MRotaryEmbedding, that fuses the multimodal RoPE (mRoPE) subgraphs used by the Qwen vision-language model family into a single op, replacing the multi-node Shape/Gather/Unsqueeze/Expand/MatMul/Concat/Cos/Sin/Split/Gather/Slice/Neg/Concat chain currently built per Q/K tensor by the onnxruntime-genai model builder.

Schema (bert_defs.cc, ms_opset.h)

  • Generalizes RotaryEmbedding: position_ids becomes a required 3D (3, batch_size, sequence_length) tensor stacking Temporal/Height/Width position streams.
  • New required attr mrope_section (3 ints, sum == rotary_embedding_dim/2).
  • New optional attr mrope_layout (0 = Sectioned/Chunked — Qwen2-VL/2.5-VL, 1 = Interleaved — Qwen3-VL/3.5 family).
  • Retains scale, interleaved, rotary_embedding_dim, num_heads, is_packed_batching from RotaryEmbedding so a single full-width section reduces to standard RoPE.

CPU kernel (contrib_ops/cpu/bert/mrotary_embedding*)

  • ComputeDimAssignments precomputes, per cos/sin column, which T/H/W stream owns it (based on mrope_section/mrope_layout), independent of runtime data.
  • Per-token: gathers position id for all 3 streams, builds combined cos/sin buffers by column selection, applies scale, then rotates via MlasRotaryEmbedOneRow.

CUDA kernel (contrib_ops/cuda/bert/mrotary_embedding*)

  • Per-thread stream selection (contiguous-range for Sectioned, modulo-3 for Interleaved) avoids needing a lookup table on GPU.
  • Mirrors the existing RotaryEmbeddingBSNH launcher (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.

…le attribute

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread onnxruntime/contrib_ops/cpu/bert/mrotary_embedding_helper.h Fixed

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cpu/bert/mrotary_embedding_helper.h Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/mrotary_embedding_helper.h Outdated
Copilot AI and others added 2 commits August 8, 2026 00:31
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
kunal-vaishnavi marked this pull request as ready for review August 8, 2026 23:38

@tianleiwu Tianlei Wu (tianleiwu) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread onnxruntime/contrib_ops/cuda/bert/mrotary_embedding_impl.cu
Comment thread onnxruntime/contrib_ops/cpu/bert/mrotary_embedding_helper.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/mrotary_embedding_impl.cu Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/mrotary_embedding_helper.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/mrotary_embedding.cc Outdated
Copilot AI and others added 3 commits August 10, 2026 18:04
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 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants