Repository navigation
Commit c683d0f
perf(moe): use a plain tensor add for the MoE tail combine
MoE layers with both shared and routed experts merge the two outputs once per
layer at the end of the block. The Ascend runner mirrors upstream's
``MoERunner.forward`` and performs that combine with
``torch._foreach_add([shared_output], [fused_output])[0]``.
Both operands are ordinary activation tensors, so this is a plain elementwise
add. ``_foreach_add`` exists to amortise dispatch across a tensor list; with a
one-element list there is nothing to amortise, and on NPU the multi-tensor path
still launches a dedicated ``ForeachAddList`` kernel. That launch costs
~13-17us regardless of tensor size, once per MoE layer, on every decode step:
for a 45-layer MoE model this is ~0.75ms of pure launch overhead sitting on the
token-to-token critical path.
The multi-tensor form is still required in the graph modes: the superkernel
generation path only accepts this combine through ``torch._foreach_add``, and a
plain ``aclnnAdd`` disconnects the graph. So the combine uses whichever form
the mode requires:
* ``ascend_compilation_config.enable_super_kernel`` or
``enable_static_kernel`` set -> keep ``torch._foreach_add``, unchanged
behaviour;
* otherwise -> ``shared_output + fused_output``, which is bit-identical (same
dtype, same operand order, elementwise add) and saves one op launch per MoE
layer.
No new configuration key or environment variable: the mode is read from the
existing compilation config once in ``__init__`` and cached on the instance, so
the hot path never re-reads it and no graph recompilation happens mid-run. A
runner built without an initialized Ascend config (unit tests) keeps the
graph-safe foreach path.
The combine is extracted into ``_combine_shared_and_routed_output`` so both
branches are directly unit-tested.
Co-authored-by: westbook1006 <1210973614@qq.com>
Co-authored-by: zmc1997 <742133686@qq.com>
Co-authored-by: mengguangxin <mgx0018@163.com>
Co-authored-by: liuwj-research <liuwj.2008@tsinghua.org.cn>
Co-authored-by: dailiang16 <dailiang0110@yeah.net>
Signed-off-by: 2luckydog <2981945956@qq.com>1 parent 695e939 commit c683d0f
2 files changed
Lines changed: 171 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
86 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
87 | 111 | | |
88 | 112 | | |
89 | 113 | | |
| |||
126 | 150 | | |
127 | 151 | | |
128 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
129 | 157 | | |
130 | 158 | | |
131 | 159 | | |
| |||
180 | 208 | | |
181 | 209 | | |
182 | 210 | | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
189 | 216 | | |
190 | 217 | | |
191 | | - | |
192 | | - | |
| 218 | + | |
| 219 | + | |
193 | 220 | | |
194 | 221 | | |
195 | 222 | | |
| |||
279 | 306 | | |
280 | 307 | | |
281 | 308 | | |
282 | | - | |
| 309 | + | |
283 | 310 | | |
284 | 311 | | |
285 | 312 | | |
286 | 313 | | |
287 | 314 | | |
288 | 315 | | |
289 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
290 | 333 | | |
291 | 334 | | |
292 | 335 | | |
| |||
0 commit comments