Commit fa63e99
perf(moe): speed up grouped MoE routing (NVIDIA#20)
## Summary
This PR tightens the routing/unrouting path in
`Qwen3VLMoeTextExpertsGroupedMm`:
- keep routed token indices as a 1D vector instead of expanding them
across the hidden dimension
- use `index_select` / `index_add_` for route and combine instead of
hidden-size-expanded `gather` / `scatter_add_`
- update the standalone MoE comparison harness so it calls the current
expert API and checks grouped output against the PyTorch reference
The old path was doing a lot of unnecessary index traffic. That is
mostly invisible for smaller routing configs, but it becomes very
noticeable when `top_k` and the expert count go up.
## Benchmarks
Environment: RTX 6000 Ada, `torch==2.10.0+cu128`, BF16 unless noted.
Focused MoE harness:
```text
python -m cosmos_framework.model.vfm.vlm.qwen3_vl_moe.moe_test
naive: 13.81 ms
optimized: 3.92 ms
speedup: 3.53x
relative diff: 0.00531
```
Synthetic decoder stack throughput, including attention + norms + MoE,
old grouped routing vs this PR:
```text
E128_K8_I768, 8 layers, seq=1024
old: 134.58 ms, 7,608.7 tok/s
new: 42.16 ms, 24,289.3 tok/s
speedup: 3.19x, +219.2%
E128_K8_I768, 12 layers, seq=1024
old: 136.89 ms, 7,480.3 tok/s
new: 63.33 ms, 16,170.2 tok/s
speedup: 2.16x, +116.2%
E128_K8_I1408, 8 layers, seq=1024
old: 103.66 ms, 9,878.7 tok/s
new: 52.81 ms, 19,391.2 tok/s
speedup: 1.96x, +96.3%
```
For the default-ish `E=60, top_k=4` shape, I did not see a meaningful
total-throughput improvement. The gain scales with routing pressure;
high-expert / high-`top_k` configurations are where the old
expanded-index route/combine path becomes expensive.
## Tests
```text
LD_LIBRARY_PATH= uv run --no-sync python -m ruff check \
cosmos_framework/model/vfm/vlm/qwen3_vl_moe/moe.py \
cosmos_framework/model/vfm/vlm/qwen3_vl_moe/moe_test.py
LD_LIBRARY_PATH= uv run --no-sync python -m cosmos_framework.model.vfm.vlm.qwen3_vl_moe.moe_test
LD_LIBRARY_PATH= uv run --no-sync python -m cosmos_framework.model.vfm.vlm.qwen3_vl_moe.moe_bench \
--compare --num-tokens 256 --num-iters 10 --num-warmup 3 --no-compile --dtype fp32
LD_LIBRARY_PATH= uv run --no-sync python -m cosmos_framework.model.vfm.vlm.qwen3_vl_moe.moe_bench \
--compare --num-tokens 2048 --num-iters 20 --num-warmup 5 --no-compile --dtype bf16
LD_LIBRARY_PATH= uv run --no-sync python -m cosmos_framework.model.vfm.vlm.qwen3_vl_moe.moe_bench \
--num-tokens 1024 --num-iters 5 --num-warmup 2 --dtype bf16 --backward
```
Signed-off-by: Shivanjan Chakravorty <shivanjanc@nvidia.com>
Co-authored-by: lfengad <liangf@nvidia.com>1 parent 19a00f8 commit fa63e99
2 files changed
Lines changed: 30 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
108 | 107 | | |
109 | 108 | | |
110 | 109 | | |
111 | 110 | | |
112 | 111 | | |
113 | 112 | | |
114 | 113 | | |
115 | | - | |
| 114 | + | |
116 | 115 | | |
117 | 116 | | |
118 | 117 | | |
| |||
125 | 124 | | |
126 | 125 | | |
127 | 126 | | |
128 | | - | |
| 127 | + | |
129 | 128 | | |
130 | 129 | | |
131 | 130 | | |
| |||
220 | 219 | | |
221 | 220 | | |
222 | 221 | | |
223 | | - | |
224 | | - | |
225 | | - | |
| 222 | + | |
| 223 | + | |
226 | 224 | | |
227 | 225 | | |
228 | 226 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
21 | | - | |
| 27 | + | |
| 28 | + | |
22 | 29 | | |
23 | 30 | | |
24 | 31 | | |
25 | 32 | | |
26 | | - | |
| 33 | + | |
| 34 | + | |
27 | 35 | | |
28 | 36 | | |
29 | 37 | | |
| |||
46 | 54 | | |
47 | 55 | | |
48 | 56 | | |
49 | | - | |
| 57 | + | |
50 | 58 | | |
51 | 59 | | |
52 | 60 | | |
| |||
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
61 | | - | |
| 69 | + | |
62 | 70 | | |
63 | 71 | | |
64 | 72 | | |
65 | 73 | | |
66 | 74 | | |
67 | 75 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
74 | 84 | | |
75 | 85 | | |
76 | 86 | | |
77 | 87 | | |
78 | 88 | | |
79 | 89 | | |
80 | | - | |
81 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
82 | 94 | | |
83 | 95 | | |
84 | 96 | | |
85 | 97 | | |
| 98 | + | |
86 | 99 | | |
87 | 100 | | |
88 | 101 | | |
| |||
0 commit comments