Repository navigation
Description
Background
nemotron-h-30b decodes at ~0.60x of mlx-lm (55 vs 92 tok/s, from the #268 investigation). This issue profiles where that decode time actually goes, correcting an earlier guess that the gap was the SSM/attention path. The fused decode-MoE work (#268, steps 2b-3b) wired a squared-ReLU routed-expert kernel for nemotron behind MLXCEL_FUSED_MOE_RELU2 (PR #280) but it was performance-neutral; this issue explains why.
Profiling (M1 Ultra, nemotron-h-30b-4bit, single-token decode)
- Decode is GPU-bound: per step ~0.8 ms graph build + ~18.7 ms GPU eval (
MLXCEL_PROFILE_FORWARD). Decode uses the Rust per-layer path (forward_rust); the C++ full-forward (nemotron_decode_step) was already tried and disabled (no speedup: 19.2 ms C++ vs 18.9 ms Rust). - Per-block breakdown (
MLXCEL_PROFILE_BLOCKS, forced-eval-per-block ratios): MoE 58% (25.1 ms), Mamba2 33% (14.2 ms), attention 9% (4.0 ms). Note the forced-eval-per-block measurement overweights op-dense blocks (each block pays a sync), so MoE's share of the real 18.7 ms is somewhat lower, but it is still the largest component.
Why the MoE block dominates
Architecture (52 layers: 23 Mamba2, 6 attention, 23 MoE). Each MoE layer runs ~5 GEMVs plus many small ops:
- router:
[2688 -> 128]matmul + sigmoid + correction-bias + argpartition + gather + normalize + scale - routed experts: fc1
[2688 -> 1856]and fc2[1856 -> 2688]for top_k=6 of 128 experts, viagather_qmm, with relu² between - shared expert (n_shared=1): dense up
[2688 -> 3712]and down[3712 -> 2688]with relu², applied to every token, the largest dims in the block, currently UNFUSED - combine: expand + multiply by scores + sum
Why the routed-expert fusion (#280) was neutral
The fused squared-ReLU kernel only replaces the routed fc1/fc2 GEMV. That left the shared expert (the largest GEMVs), the router, the gather/scatter, and the combine untouched, so overall decode did not move. Also the routed gather_qmm is already bandwidth-efficient at this expert size (Dff=1856); the two-kernel fused approach's all-cores advantage (which gave +3.5% on qwen3-30b at Dff=768) shrinks for larger experts.
Conclusion
nemotron-h's 0.60x decode gap is not a single SSM/attention bottleneck. It is the MoE block's op density plus GEMV-heavy expert path (routed + shared) across 23 MoE layers, the same diffuse per-op-overhead class as the general #268 finding (mlxcel emits more AsType / small ops per token than mlx-lm on the same architecture). The Mamba2 path is already a fused C++ kernel (ssm_update_kernel) and efficient; attention is minor.
Suggested next steps (each likely modest, per the #268 / Gemma3n precedent of ~few% per lever)
- Finer intra-MoE-block profiling to split router vs routed vs shared-expert vs combine (instrument the C++
fused_moe_forward), to size each lever before building. - Fuse the dense shared expert (up/relu²/down) into fewer ops / a fused kernel, it is the largest unfused GEMV pair in the block.
- Fold router scoring + gather + combine to cut the MoE block's small-op count.
- Re-measure against mlx-lm after each lever; greedy temp-0 within the f16 jitter class.
Activity
update(nemotron-h): add intra-MoE phase profiling (lablup#284) (lablu…
update(nemotron-h): add intra-MoE phase profiling (lablup#284) (lablu…