Profile a GPT-2 forward pass with the PyTorch profiler
Colab T4 · freenotebook90 minrequiredGoal · A key_averages table and a Perfetto trace for GPT-2 small on a T4 at batch 1 and batch 32, with the GPU idle fraction computed for each.
- 1Load gpt2 in float16 on a T4; run 5 warm-up forward passes on a 128-token input.
- 2Wrap 10 forward passes in torch.profiler.profile(activities=[CPU, CUDA], record_shapes=True) and print key_averages().table sorted by cuda_time_total.
- 3Export a Chrome trace with prof.export_chrome_trace and open it in ui.perfetto.dev; screenshot one layer's kernels.
- 4Sum total CUDA kernel time and divide by wall time of the profiled region to get GPU busy fraction for batch 1.
- 5Repeat with batch 32 and compare busy fraction and top kernels.
- 6Write three sentences on which kernels dominate at each batch size and whether the workload is launch-bound.
- Deliverable
- Notebook with both tables, the busy fractions and the trace screenshot.
- Check
- Batch 1 GPU busy fraction is noticeably lower than batch 32; the top kernel by CUDA time at batch 32 is a GEMM (names containing gemm, cutlass or sgemm/hgemm).
- Fallback
- Run on Kaggle; or profile on CPU with the same API and compare operator time distribution, noting that the idle-gap analysis needs a GPU.