Commit 15f8231
feat(rope_fix): Hoist layer-invariant RoPE indexing out of decoder subfunctions for cached text models (quic#928)
## Summary
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.
## What changed
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.
## Tests
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>1 parent 5231e91 commit 15f8231
20 files changed
Lines changed: 183 additions & 399 deletions
File tree
- QEfficient/transformers/models
- falcon
- gemma2
- gemma
- gpt_oss
- granitemoe
- granite
- llama_swiftkv
- llama
- mistral
- mixtral_moe
- mllama
- olmo2
- phi3
- qwen2_5_vl
- qwen2
- qwen3_moe
- qwen3_vl_moe
- qwen3_vl
- qwen3
- tests/transformers/subfunction
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 71 | | |
82 | 72 | | |
83 | 73 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | 74 | | |
88 | 75 | | |
89 | 76 | | |
| |||
127 | 114 | | |
128 | 115 | | |
129 | 116 | | |
130 | | - | |
| 117 | + | |
131 | 118 | | |
132 | 119 | | |
133 | 120 | | |
| |||
308 | 295 | | |
309 | 296 | | |
310 | 297 | | |
| 298 | + | |
| 299 | + | |
311 | 300 | | |
312 | 301 | | |
313 | 302 | | |
| |||
326 | 315 | | |
327 | 316 | | |
328 | 317 | | |
329 | | - | |
330 | | - | |
| 318 | + | |
| 319 | + | |
331 | 320 | | |
332 | 321 | | |
333 | 322 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | 73 | | |
84 | 74 | | |
85 | 75 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | 76 | | |
90 | 77 | | |
91 | 78 | | |
| |||
144 | 131 | | |
145 | 132 | | |
146 | 133 | | |
147 | | - | |
148 | | - | |
149 | | - | |
| 134 | + | |
150 | 135 | | |
151 | 136 | | |
152 | 137 | | |
| |||
321 | 306 | | |
322 | 307 | | |
323 | 308 | | |
| 309 | + | |
| 310 | + | |
324 | 311 | | |
325 | 312 | | |
326 | 313 | | |
| |||
335 | 322 | | |
336 | 323 | | |
337 | 324 | | |
338 | | - | |
339 | | - | |
| 325 | + | |
| 326 | + | |
340 | 327 | | |
341 | 328 | | |
342 | 329 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | 76 | | |
87 | 77 | | |
88 | 78 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | 79 | | |
93 | 80 | | |
94 | 81 | | |
| |||
151 | 138 | | |
152 | 139 | | |
153 | 140 | | |
154 | | - | |
155 | | - | |
156 | | - | |
| 141 | + | |
157 | 142 | | |
158 | 143 | | |
159 | 144 | | |
| |||
358 | 343 | | |
359 | 344 | | |
360 | 345 | | |
| 346 | + | |
| 347 | + | |
361 | 348 | | |
362 | 349 | | |
363 | 350 | | |
| |||
373 | 360 | | |
374 | 361 | | |
375 | 362 | | |
376 | | - | |
377 | | - | |
| 363 | + | |
| 364 | + | |
378 | 365 | | |
379 | 366 | | |
380 | 367 | | |
| |||
Lines changed: 14 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
541 | 541 | | |
542 | 542 | | |
543 | 543 | | |
544 | | - | |
| 544 | + | |
545 | 545 | | |
546 | 546 | | |
547 | 547 | | |
| |||
558 | 558 | | |
559 | 559 | | |
560 | 560 | | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | 561 | | |
574 | 562 | | |
575 | 563 | | |
576 | 564 | | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | 565 | | |
581 | 566 | | |
582 | 567 | | |
| |||
703 | 688 | | |
704 | 689 | | |
705 | 690 | | |
706 | | - | |
707 | | - | |
708 | | - | |
| 691 | + | |
709 | 692 | | |
710 | 693 | | |
711 | 694 | | |
| |||
787 | 770 | | |
788 | 771 | | |
789 | 772 | | |
790 | | - | |
791 | | - | |
792 | | - | |
| 773 | + | |
793 | 774 | | |
794 | 775 | | |
795 | 776 | | |
| |||
868 | 849 | | |
869 | 850 | | |
870 | 851 | | |
871 | | - | |
872 | | - | |
873 | | - | |
| 852 | + | |
874 | 853 | | |
875 | 854 | | |
876 | 855 | | |
| |||
1044 | 1023 | | |
1045 | 1024 | | |
1046 | 1025 | | |
| 1026 | + | |
| 1027 | + | |
1047 | 1028 | | |
1048 | 1029 | | |
1049 | 1030 | | |
| |||
1059 | 1040 | | |
1060 | 1041 | | |
1061 | 1042 | | |
1062 | | - | |
1063 | | - | |
| 1043 | + | |
| 1044 | + | |
1064 | 1045 | | |
1065 | 1046 | | |
1066 | 1047 | | |
| |||
1085 | 1066 | | |
1086 | 1067 | | |
1087 | 1068 | | |
1088 | | - | |
1089 | | - | |
| 1069 | + | |
| 1070 | + | |
1090 | 1071 | | |
1091 | 1072 | | |
1092 | 1073 | | |
| |||
1144 | 1125 | | |
1145 | 1126 | | |
1146 | 1127 | | |
| 1128 | + | |
| 1129 | + | |
1147 | 1130 | | |
1148 | 1131 | | |
1149 | 1132 | | |
| |||
1160 | 1143 | | |
1161 | 1144 | | |
1162 | 1145 | | |
1163 | | - | |
1164 | | - | |
| 1146 | + | |
| 1147 | + | |
1165 | 1148 | | |
1166 | 1149 | | |
1167 | 1150 | | |
| |||
Lines changed: 6 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 71 | | |
82 | 72 | | |
83 | 73 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | 74 | | |
88 | 75 | | |
89 | 76 | | |
| |||
137 | 124 | | |
138 | 125 | | |
139 | 126 | | |
140 | | - | |
141 | | - | |
142 | | - | |
| 127 | + | |
143 | 128 | | |
144 | 129 | | |
145 | 130 | | |
| |||
320 | 305 | | |
321 | 306 | | |
322 | 307 | | |
| 308 | + | |
| 309 | + | |
323 | 310 | | |
324 | 311 | | |
325 | 312 | | |
| |||
335 | 322 | | |
336 | 323 | | |
337 | 324 | | |
338 | | - | |
339 | | - | |
| 325 | + | |
| 326 | + | |
340 | 327 | | |
341 | 328 | | |
342 | 329 | | |
| |||
0 commit comments