Log inStart free
LESSON 27.05 · 5 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Streams, events and asynchronous execution

Watch the CPU queue GPU work, wait for one event and run ahead.


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

Go Pro · $8.25/mo
EXPLAINER · 27.05 · STREAM BAYSTEP 1 · CPU 24 µs · GPU 70.8 µs
01
Stage 1 of 5 · Queue kernels
Each launch puts a kernel in stream 1, a queue on the GPU. The CPU goes on after 2 µs.
Explain it simply

This is asynchronous execution: the CPU does not wait for a kernel to finish. It queues all 12 kernels of step 1 in 24 microseconds. The GPU runs them one by one and ends at 70.8 microseconds. Each ochre line shows how long one kernel sits in the queue.

The CPU lane holds 12 launch calls that end at 24.0 µs. Stream 1 runs the 12 kernels until 70.8 µs.

kernelCPU call, host worktoken copyidle GPUCPU waits
CPUstream 1stream 2050100150200250time (µs)25911step 1: queue the kernelsstep 1CPU done at 24 µs · GPU done at 70.8 µsCPUstream 1stream 2050100150200250time (µs)25911step 1: queue the kernelsstep 1CPU done at 24 µs · GPU done at 70.8 µs
Toy: 3 decode steps of 12 kernels, 2 µs per CUDA call
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
si=max⁡(qi, ei−1)\sy{s}{data}{s_i} = \max(\sy{q}{param}{q_i},\ \sy{e}{data}{e_{i-1}})
q12=12⋅2=24 μs,e12=70.8 μsq_{12} = 12 \cdot 2 = 24\ \mu\text{s},\quad e_{12} = 70.8\ \mu\text{s}

A kernel starts once it is queued and the kernel before it has ended.

Common mix-ups

MYTHA second stream makes the GPU work faster by itself.

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

Predict, then reveal
3 CHECKS · PRO
Q01At batch 16 with 2 streams, do 3 tokens take more or less than 260 µs with synchronize?

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