research26 min studyLesson 575 in Engineering

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

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.

Fuseful=4dh∑ene,Wexpert=2dhF_{\rm useful}=4dh\sum_e n_e,\qquad W_{\rm expert}=2dh
This forward-only convention assumes two dense linear maps d→h→d per expert, shared d and h, and counts each multiply-add as two FLOPs. W_expert counts scalar matrix weights per expert, excluding biases. Activation, gating, router, backward, and communication costs are omitted.↔ Scroll wide equations horizontally.

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.

Npadded=EC,Nblock=∑eb⌈neb⌉,ηrows=∑eneNscheduledN_{\rm padded}=EC,\qquad N_{\rm block}=\sum_e b\left\lceil\frac{n_e}{b}\right\rceil,\qquad \eta_{\rm rows}=\frac{\sum_e n_e}{N_{\rm scheduled}}
These are separate teaching schedules: fixed capacity C≥max n_e, or independent row rounding to positive block size b with zero-row groups contributing zero. η_rows measures useful scheduled-row fraction when N_scheduled>0; it is not achieved GPU utilization or a complete model of MegaBlocks kernels.↔ Scroll wide equations horizontally.

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.

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.

  1. Useful rows total six, giving 4×6×4×8=768 conventional forward matrix FLOPs, excluding activation and all other costs.

  2. Capacity-three scheduling allocates four times three, or twelve rows, giving 1,536 corresponding matrix FLOPs if every padded row is processed.

  3. 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.

The result

Useful work is fixed at 768 FLOPs while the stated schedules process different extra rows. Neither row ratio alone predicts device time.

Explore the model

Interactive lab

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.

A conditional intensity improvement is not a grouped-kernel benchmarkSynthetic 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. Current readings: Attainable ceiling: 640 GFLOP/s; Ridge intensity: 12.5 FLOP/byte; Ceiling regime: Memory-bound ceiling. All parameters are adjustable with the labeled controls below.Hypothetical roofline upper bound: 640 GFLOP per second at operational intensity 8 FLOP per byte and bandwidth 80 gigabytes per second.0.1110641101001000Operational intensity · FLOP/byte · log scaleCeiling · GFLOP/s · log scalePeak: 1000
Attainable ceiling / kernelBandwidth roof / traceCompute ceiling
Attainable ceiling
640 GFLOP/s
Ridge intensity
12.5 FLOP/byte
Ceiling regime
Memory-bound ceiling
t = 0 s

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.

Check your understanding

What must grouped expert matrix multiplication preserve?

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.

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.