Skip to content

feat(rope_fix): Hoist layer-invariant RoPE indexing out of decoder su…#936

Closed
quic-rishinr wants to merge 2 commits intoquic:release/v1.21.6from
quic-rishinr:cherry-pick/pr-928-to-v1.21.6
Closed

feat(rope_fix): Hoist layer-invariant RoPE indexing out of decoder su…#936
quic-rishinr wants to merge 2 commits intoquic:release/v1.21.6from
quic-rishinr:cherry-pick/pr-928-to-v1.21.6

Conversation

@quic-rishinr
Copy link
Copy Markdown
Contributor

…bfunctions for cached text models (#928)

This change moves layer-invariant RoPE cos/sin indexing out of repeated decoder-layer subfunctions and into model-level forward paths.

For cached decoder models, we were repeatedly doing:

cos = cos[position_ids].unsqueeze(1)
sin = sin[position_ids].unsqueeze(1)

inside each decoder attention block. With ONNX subfunctions enabled, that indexing becomes part of the exported repeated subfunction body and contributes to the on-device regression we observed after the single-subfunction Rope Fix work #880 .

This patch hoists that work once per forward pass and passes the already-shaped cos/sin tensors into each decoder layer.

Applied the refactor to the applicable QEff model families that thread static cached RoPE tensors through repeated decoder layers, including:

  • Llama
  • Llama SwiftKV
  • Gemma
  • Gemma2
  • Mistral
  • Falcon
  • GPT-OSS
  • Granite
  • GraniteMoE
  • Mllama text path
  • Mixtral
  • Olmo2
  • Phi3
  • Qwen2
  • Qwen3
  • Qwen3 MoE
  • Qwen2.5 VL text path
  • Qwen3 VL text path
  • Qwen3 VL MoE text path

For the Qwen VL text towers, the same idea is applied to the indexed/interleaved MRoPE preparation: the already-indexed cos/sin tensors are prepared once before the decoder-layer loop and reused across layers.

Added a TinyLlama regression test to assert that export with subfunctions still produces a single decoder-layer ONNX function.

Verified:

python -m pytest -q tests/unit_test/models/test_model_quickcheck.py -n auto


…bfunctions for cached text models (quic#928)

This change moves layer-invariant RoPE cos/sin indexing out of repeated
decoder-layer subfunctions and into model-level forward paths.

For cached decoder models, we were repeatedly doing:

```
cos = cos[position_ids].unsqueeze(1)
sin = sin[position_ids].unsqueeze(1)
```

inside each decoder attention block. With ONNX subfunctions enabled,
that indexing becomes part of the exported repeated subfunction body and
contributes to the on-device regression we observed after the
single-subfunction Rope Fix work quic#880 .

This patch hoists that work once per forward pass and passes the
already-shaped cos/sin tensors into each decoder layer.

Applied the refactor to the applicable QEff model families that thread
static cached RoPE tensors through repeated decoder layers, including:

- Llama
- Llama SwiftKV
- Gemma
- Gemma2
- Mistral
- Falcon
- GPT-OSS
- Granite
- GraniteMoE
- Mllama text path
- Mixtral
- Olmo2
- Phi3
- Qwen2
- Qwen3
- Qwen3 MoE
- Qwen2.5 VL text path
- Qwen3 VL text path
- Qwen3 VL MoE text path

For the Qwen VL text towers, the same idea is applied to the
indexed/interleaved MRoPE preparation: the already-indexed cos/sin
tensors are prepared once before the decoder-layer loop and reused
across layers.

Added a TinyLlama regression test to assert that export with
subfunctions still produces a single decoder-layer ONNX function.

Verified:

`python -m pytest -q tests/unit_test/models/test_model_quickcheck.py -n
auto`

---------

Signed-off-by: vbaddi <vbaddi@qti.qualcomm.com>
Signed-off-by: Rishin Raj <rishinr@qti.qualcomm.com>
Co-authored-by: Rishin Raj <rishinr@qti.qualcomm.com>
@quic-rishinr quic-rishinr requested a review from vbaddi April 22, 2026 04:13
Signed-off-by: Rishin Raj <rishinr@qti.qualcomm.com>
@vbaddi
Copy link
Copy Markdown
Contributor

vbaddi commented Apr 22, 2026

duplicate #928 not need in release branch, closing the PR

@vbaddi vbaddi closed this Apr 22, 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.

2 participants