r/CUDA•
Grand-Bed6510

I wrote a tiny FlashAttention kernel in CUDA C++: ~250 lines, up to 4.5x faster than naive PyTorch

I built a small educational FlashAttention-style forward pass in CUDA C++.

Repo: https://github.com/lavawolfiee/mini-flash-attention

The goal was to make something much easier to read than the official highly optimized kernels, but still fast enough to be interesting.

There are two implementations:

  • flash_attn_wmma_cuda.cu: ~150 lines, mostly plain CUDA + WMMA. Tensor Cores for Q @ K^T, blockwise online softmax, simpler P @ V.
  • flash_attn_cuda.cu: ~250 lines, CuTe/CUTLASS version. Tensor Core MMA for both Q @ K^T and P @ V, register-resident accumulators, and swizzled shared-memory layouts.

Current scope:

  • forward only
  • fp16
  • head dim 64
  • non-causal attention
  • input layout [B x H, N, D]

Benchmarked on RTX A4000, B=1, H=8, D=64.

Median latency:

NPyTorchWMMACuTe
10240.835 ms0.395 ms0.248 ms
20482.637 ms1.451 ms0.706 ms
409610.461 ms4.445 ms2.740 ms
819243.271 ms17.783 ms9.510 ms

So the CuTe version is up to ~4.5x faster than naive PyTorch on this setup, while not materializing the full N x N attention matrix.

Official FlashAttention is still much faster, of course, but that is kind of the point: the code is small enough to read, understand and play with.

This is also my first project using CuTe, so I'd really love some feedback from people who have written CUDA/CuTe kernels!

Posted Jun 3, 2026, 11:39 PM UTC

453
Loading comments…