Log inStart free
LESSON 27.14 · 5 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Batched and grouped GEMM

Watch eight small expert GEMMs become one kernel launch, in two ways.


Pro lesson. Stage 1 is free to watch. Pro opens all 5 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 27.14 · GEMM BAY16 TOKENS · 8 EXPERTS · MAX 4 PER EXPERT
01
Stage 1 of 5 · Tokens per expert
The router sends 16 tokens to 8 experts. Each expert runs its own GEMM with 0 to 4 rows.
Explain it simply

Each chip at the top is one token. Its line shows the expert the router picked. Each expert multiplies its token rows by its own weights, the ochre square. Here the busiest expert gets 4 tokens and the quietest gets 0.

16 token chips with lines to 8 expert columns. The columns hold 2, 1, 0, 4, 2, 4, 3, 0 token rows.

token rowsweights, tiles, launchespadding, start cost, idle GPUrun time, tile time
16 tokens, 8 expert GEMMsthe GPU: 4 SMs, no work yet0123456789101112131415W0W1W2W3W4W5W6W7E0m 2E1m 1E2m 0E3m 4E4m 2E5m 4E6m 3E7m 0SM 0SM 1SM 2SM 3m = 2, 1, 0, 4, 2, 4, 3, 0 · sum 16 tokens16 tokens, 8 expert GEMMsthe GPU: 4 SMs, no work yet0123456789101112131415W0W1W2W3W4W5W6W7E0m 2E1m 1E2m 0E3m 4E4m 2E5m 4E6m 3E7m 0SM 0SM 1SM 2SM 3m = 2, 1, 0, 4, 2, 4, 3, 0 · sum 16 tokens
Toy: 16 tokens, 8 experts, a GPU with 4 SMs, 5 µs per launch
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Ye=Xe We,Xe∈Rme×8\sy{y}{out}{Y_e} = \sy{m}{data}{X_e}\,\sy{w}{param}{W_e}, \quad \sy{m}{data}{X_e} \in \mathbb{R}^{m_e \times 8}
me=2, 1, 0, 4, 2, 4, 3, 0; ∑me=16m_e = 2,\ 1,\ 0,\ 4,\ 2,\ 4,\ 3,\ 0;\ \textstyle\sum m_e = 16

Each expert multiplies its own token rows by its own weights.

Common mix-ups

MYTHBatched GEMM can run experts with different token counts.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With even routing, which finishes first: batched or grouped GEMM?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.