Grouped expert GEMM: schedule irregular rows without changing the experts
Count useful expert arithmetic, distinguish grouped and block-sparse execution, and inspect how row counts, padding, and precision constrain kernel efficiency.
On this page
The question for today
What you’ll understand
- Compute useful FLOPs for explicitly specified expert networks.
- Distinguish grouped scheduling from fixed-capacity padded computation.
- Design shape-aware correctness and performance comparisons for irregular expert batches.
After dispatch, each local expert owns a different number of token rows. Launching one tiny matrix operation after another can waste scheduling opportunities, while padding every expert to the busiest shape wastes arithmetic. Grouped or block-sparse implementations address this irregularity with different mechanisms. The experiment must preserve each expert's parameters, actual row count, and numerical semantics while measuring the whole permutation-and-compute path.
The useful matrix work follows accepted row counts
For a simple two-linear-layer expert with width d, hidden width h, and n_e accepted rows, conventional matrix work is four times n_e d h. The count includes two multiply-add matrix products under the usual two-FLOP convention and excludes nonlinearities, biases, routing, and backward work. A gated expert with two input projections and one output projection has a different count, typically six times n_e d h under matching dimensions. Name the architecture instead of assigning one universal FLOP formula to every feed-forward expert.
Experts keep distinct parameters even if a scheduler groups their matrix operations. Grouping is not averaging their weights or concatenating token rows into one shared dense expert. Each row must be multiplied by its assigned expert matrices, with the appropriate leading dimensions and output mapping. A zero-row expert requires no useful matrix arithmetic but may still appear in metadata or a grouped API. Empty groups, odd widths, tail rows, and mixed local row counts need explicit support rather than relying on a balanced benchmark.
Grouped and block-sparse schedules solve related problems differently
A grouped matrix interface presents several independent products to a scheduling mechanism so it can use resources more effectively than an unfavorable sequence of tiny launches. Backend implementations differ: kernel grouping, concurrent streams, persistent scheduling, or other strategies may be involved. Megatron-Core 0.15.0 documents its Transformer Engine grouped path for multiple local experts. Inspect the selected backend and version before claiming one launch shape or universal speedup. Row skew can leave a few long groups dominating completion even when launch overhead falls.
MegaBlocks instead reformulates expert computation using block-sparse operations that accommodate uneven assignment while avoiding token dropping. That mechanism is not identical to every grouped-GEMM library. Sparse block granularity, internal padding, metadata, and permutation still cost resources. Fixed-capacity dense padding and block-rounded sparse work have different wasted-row patterns. Compare useful rows against actually scheduled rows, and include temporary storage and preparation. Dropless refers to preserving assignments, not to the absence of all padding, zero work, or memory limits.
Tune the irregular portfolio rather than one balanced shape
Record the distribution of rows per expert, number of local experts, hidden widths, dtypes, and selected precision path. Tiny groups may be dominated by overhead, while larger groups can reach a different compute or bandwidth regime. Quantized or specialized matrix paths can impose alignment and padding requirements that change the scheduled work. Check output and gradient agreement after reordering groups or changing accumulation. A speed comparison must include dispatch permutations and any required weight or activation conversion at its declared boundary.
The roofline lab isolates only a conditional reuse argument. Reset, set I=4 and B=80, predict ceiling 320 GFLOP/s, then Play or Step. Separately Reset, set I=8 and B=80, predict 640 GFLOP/s, then Play or Step. Its compute peak stays at 1,000 GFLOP/s. The instrument has no expert row distribution, grouped scheduler, padding, or sparse matrix representation. Use it to check the units of an intensity hypothesis, then test that hypothesis on actual irregular shapes rather than treating the curve as a grouped-kernel result.
Work it through
Compare useful rows with two teaching padding schedules
Four local experts receive [3,1,0,2] rows. Each has a two-linear-layer network d=4,h=8. Compare useful work, capacity-three dense scheduling, and independent row rounding to block size two.
Useful rows total six, giving 4×6×4×8=768 conventional forward matrix FLOPs, excluding activation and all other costs.
Capacity-three scheduling allocates four times three, or twelve rows, giving 1,536 corresponding matrix FLOPs if every padded row is processed.
Block-two rounding gives [4,2,0,2], or eight scheduled rows and 1,024 corresponding FLOPs. This invented row-rounding comparison illustrates accounting; it does not specify a real backend's full sparse layout.
Useful work is fixed at 768 FLOPs while the stated schedules process different extra rows. Neither row ratio alone predicts device time.
The living notebook
Explore the model
Interactive lab · roofline
A conditional intensity improvement is not a grouped-kernel benchmark
Reset, set I=4 and B=80, predict 320 GFLOP/s, then Play or Step. Separately Reset, set I=8 and B=80, predict 640 GFLOP/s, then Play or Step. The fixed synthetic ceiling has no expert groups, padding, or kernel launches.
- Attainable ceiling
- 640 GFLOP/s
- Ridge intensity
- 12.5 FLOP/byte
- Ceiling regime
- Memory-bound ceiling
Synthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forUseful arithmetic, scheduled arithmetic, and measured throughput need separate counters.
Pause & reason
Check your understanding
Your turn
Try it yourself
Expert row counts are [5,0,3,2], with d=16,h=32. Find useful forward matrix FLOPs for a two-linear-layer expert and for a matching three-projection gated expert.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
There are ten useful rows; use four or six FLOPs per row per d×h weight rectangle under the stated conventions.
Compare with the solution
The two-linear-layer count is 4×10×16×32=20,480 FLOPs. The three-projection gated count is 6×10×16×32=30,720 FLOPs, excluding elementwise gating and activation. A report using the smaller numerator for the gated network would misstate its conventional arithmetic rate.
Keep the idea
Take it with you
- Expert architecture and accepted row counts determine useful matrix work.
- Grouped scheduling and block-sparse computation are distinct implementation mechanisms.
- Row efficiency is accounting evidence, not a complete GPU performance metric.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.