Log inStart free
LESSON 29.24 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Sampling on the GPU

Watch one GPU thread block turn 8 logits into one sampled token.


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

Go Pro · $8.25/mo
EXPLAINER · 29.24 · SAMPLING BAY8 TOKENS · 8 THREADS · 1 BLOCK
01
Stage 1 of 6 · Logits in HBM
The last layer writes 8 logits to HBM. One thread of the sampling kernel reads each logit.
Explain it simply

Each box on top is one token with its logit, a raw score from the model. A block of threads works on the row at the same time. Here one thread holds one logit. A real block has 1,024 threads, so with 128,256 tokens each thread reads about 125 logits.

Eight token boxes in a row, each with its logit and one thread, t0 to t7.

The logits are toy values, not from a trained model.
logits ztemperature T, draw ukernel resultslogit copy, avoided
thread block, one per requestlogits in HBMlogits za real block: 1,024 threads, about 125 logits each at 128,256 tokenst0box0.4t1mat3.1t2dog−1.3t3rug2.6t4cat−0.5t5floor2.2t6sofa0.9t7bed1.28 logits · 8 threads · one block per requestthread block, one per requestlogits in HBMlogits za real block: 1,024 threads, about 125 logits each at 128,256 tokenst0box0.4t1mat3.1t2dog−1.3t3rug2.6t4cat−0.5t5floor2.2t6sofa0.9t7bed1.28 logits · 8 threads · one block per request
Toy: 8 tokens, 8 threads, 64 requests in the copy
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
z=(z0,…,z7)\sy{z}{data}{z} = (\sy{z}{data}{z_0}, \dots, \sy{z}{data}{z_7})
z=(0.4,3.1,−1.3,2.6,−0.5,2.2,0.9,1.2)z = (0.4, 3.1, -1.3, 2.6, -0.5, 2.2, 0.9, 1.2)

One logit per token, and one thread per logit.

Common mix-ups

MYTHSampling needs the logits sorted first.

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

Predict, then reveal
3 CHECKS · PRO
Q01If the temperature drops to 0.5, does the range of mat get wider or narrower?

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

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 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.