WMMA fp16 GEMM via load_inline versus cuBLAS on a T4
Colab T4 · freenotebook180 minrequiredGoal · A tensor-core GEMM written with the WMMA API, compiled through torch.utils.cpp_extension.load_inline, verified against torch.matmul and benchmarked against it and against a scalar fp32 kernel.
- 1On a Colab T4, write a scalar fp32 tiled GEMM kernel (shared-memory tiles of 32x32) with load_inline and verify it for 1024x1024.
- 2Write wmma_gemm using nvcuda::wmma fragments of 16x16x16 fp16 with fp32 accumulators; each warp computes a 16x16 output tile, with a block of 4 warps covering 32x32 and a shared-memory staged K loop.
- 3Compile with -arch=sm_75 and extra flags -lineinfo; verify against torch.matmul in fp16 at rtol 1e-2 for 256, 1024 and 4096 square sizes, including a non-multiple-of-16 size with padding.
- 4Benchmark all three (scalar, wmma, torch.matmul) with CUDA events at 1024, 2048 and 4096; compute TFLOPs as 2*M*N*K divided by time.
- 5Dump PTX with --ptx (or cuobjdump on the built extension) and grep for wmma.mma.sync; count the instructions in the inner loop.
- 6Increase the warp tile to 32x32 (four fragments per warp) and re-measure; explain the change in a markdown cell using register and shared-memory reuse.
- 7Record the fraction of T4 peak (about 65 TFLOPs fp16 tensor core) that each kernel reaches.
- Deliverable
- A notebook with the three kernels, correctness asserts, the TFLOPs table and the PTX excerpt.
- Check
- The PTX contains wmma.mma.sync instructions; the wmma kernel is at least 4x faster than the scalar fp32 kernel at 4096 and passes the rtol 1e-2 check for all sizes; the 32x32 warp tile is faster than the 16x16 one.
- Fallback
- Use a Kaggle T4. Without any GPU, write the wmma kernel anyway, compile to PTX on Colab's CPU runtime with nvcc --ptx if available, and verify the instruction appears; skip the timings.