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

Triton's block programming model

Watch one Triton kernel add two vectors, a whole block at a time.


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 · 27.15 · KERNEL BAYN 20 · BLOCK 8 · 3 PROGRAMS · 4 LANES OFF
01
Stage 1 of 6 · Add two vectors
Vector addition adds x and y at each position. The 20 sums go into out.
Explain it simply

You see two rows of 20 digits, x and y. Vector addition adds the two digits at each position. The sum goes into the row out, which starts at zero. Every position works on its own, so all of them can run at the same time.

Rows x and y hold 20 digits each. Row out holds zeros.

x, yprograms, offsetsoutpast the endmasked lane, or past N
x + y = outxyout@triton.jitdef add_kernel(x_ptr, y_ptr, out_ptr, n, BLOCK: tl.constexpr): pid = tl.program_id(0) offsets = pid * BLOCK + tl.arange(0, BLOCK) mask = offsets < n x = tl.load(x_ptr + offsets, mask=mask) y = tl.load(y_ptr + offsets, mask=mask) tl.store(out_ptr + offsets, x + y, mask=mask)add_kernel[(3,)](x, y, out, 20, BLOCK=8)290060600110120450020360440100700300640460050040560660020130memory order: x, y, out, zx + y = outxyout@triton.jitdef add_kernel(x_ptr, y_ptr, out_ptr, n, BLOCK: tl.constexpr): pid = tl.program_id(0) offsets = pid * BLOCK + tl.arange(0, BLOCK) mask = offsets < n x = tl.load(x_ptr + offsets, mask=mask) y = tl.load(y_ptr + offsets, mask=mask) tl.store(out_ptr + offsets, x + y, mask=mask)add_kernel[(3,)](x, y, out, 20, BLOCK=8)290060600110120450020360440100700300640460050040560660020130memory order: x, y, out, z
Toy: seeded digits in a flat toy memory; programs run in pid order
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
outi=xi+yi\sy{out}{out}{\mathrm{out}_i} = \sy{x}{data}{x_i} + \sy{y}{data}{y_i}
out0=2+9=11\mathrm{out}_0 = 2 + 9 = 11

Each output value is the sum of x and y at one position.

Common mix-ups

MYTHA Triton program handles one value, like a CUDA thread.

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

Predict, then reveal
3 CHECKS · PRO
Q01With N = 20 and BLOCK 4, how many lanes fall past the end?

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.