Capture GPT-2 decode into a CUDA Graph on a T4
Colab T4 · freenotebook120 minrequiredGoal · Measure the per-step latency of a batch-1 GPT-2 decode step eagerly versus replayed from a CUDA Graph, and count the kernels per step.
- 1Load gpt2 in float16 on the Colab GPU; build a static input_ids tensor of shape (1, 1) and a static past_key_values of fixed length.
- 2Time 100 eager forward calls with torch.cuda.Event, after 10 warmup calls.
- 3Use torch.profiler for one eager step and count the number of CUDA kernels launched.
- 4Warm up on a side stream, then capture one forward step with torch.cuda.graph(g) writing into static output tensors.
- 5Time 100 g.replay() calls and compare to eager; compute the ratio.
- 6Try torch.compile(model, mode="reduce-overhead") on the same step and record its time as a third column.
- 7Change the input shape to (2, 1) and observe what happens to the captured graph; write down why.
- Deliverable
- A table of eager, graph-replay and compiled per-step latency plus the kernel count.
- Check
- Graph replay is at least 1.5x faster than eager at batch 1; the kernel count per step is in the hundreds, explaining why.
- Fallback
- On CPU, measure Python dispatch overhead instead by timing 100 forward steps with and without torch.compile and comparing per-step time; the gap shows the same launch-overhead idea.