Add fused MRotaryEmbedding contrib op for Qwen mRoPE variants - #31728
Draft
kunal-vaishnavi with Copilot wants to merge 3 commits into
Draft
Add fused MRotaryEmbedding contrib op for Qwen mRoPE variants#31728kunal-vaishnavi with Copilot wants to merge 3 commits into
kunal-vaishnavi with Copilot wants to merge 3 commits into
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. |
| if (position_ids_dims.size() != 3 || position_ids_dims[0] != 3) { | ||
| return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | ||
| "MRotaryEmbedding: 'position_ids' is expected to have shape " | ||
| "(3, batch_size, sequence_length), got ", position_ids_dims.size(), " dimensions", |
Contributor
There was a problem hiding this comment.
Suggested change
| "(3, batch_size, sequence_length), got ", position_ids_dims.size(), " dimensions", | |
| "(3, batch_size, sequence_length), got ", | |
| position_ids_dims.size(), " dimensions", |
Comment on lines
+42
to
+43
| int mrope_section[3]; // section sizes for T, H, W (sum == rotary_embedding_dim / 2) | ||
| MRopeLayout mrope_layout; // how the 3 sections are combined |
Contributor
There was a problem hiding this comment.
Suggested change
| int mrope_section[3]; // section sizes for T, H, W (sum == rotary_embedding_dim / 2) | |
| MRopeLayout mrope_layout; // how the 3 sections are combined | |
| int mrope_section[3]; // section sizes for T, H, W (sum == rotary_embedding_dim / 2) | |
| MRopeLayout mrope_layout; // how the 3 sections are combined |
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
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.