Skip to main content
Back

LeetCUDA Hits 11.5k Stars Teaching GPU Kernel Writing the Hard Way

LeetCUDA is an open-source CUDA learning repo with 200+ kernels, PyTorch bindings, and curated GPU programming blogs that has grown to 11.5k stars

Pro

Repo, Full Access
  • What it is: LeetCUDA is an open-source CUDA learning repo with 200+ kernels, PyTorch bindings, and 100+ GPU programming blogs, now at 11.5k stars.
  • Difficulty ladder: Kernels are graded Easy to Hard++ covering element-wise ops, GEMM, and full FlashAttention-2 using raw MMA PTX assembly.
  • HGEMM performance: The included half-precision matrix multiply reaches 98–100% of cuBLAS TFLOPS on RTX 4090, L20, and RTX 3080 Laptop.
  • FlashAttention variants: Six progressively optimized FA-2 implementations reduce on-chip SRAM from 4×BrD down to O(Br×16), with the best variant spun out as ffpa-attn.
  • Interview mode: A dedicated kernels/interview/notes-v2.cu compiles to a self-contained verification harness covering 25+ kernels on Ada and Hopper GPUs.
  • Free and open: GPL-3.0 licensed, community contributions welcome; each kernel ships with a Python test and PyTorch binding.

LeetCUDA is an open-source CUDA learning resource built by DefTruth (xlite-dev) that has quietly become one of the most-starred GPU programming repositories on GitHub, crossing 11.5k stars and 1.2k forks. The premise is simple but rare: instead of a slide deck or a textbook, it gives you runnable, well-commented CUDA kernels that go from trivial element-wise ops all the way to hand-rolled FlashAttention-2 using raw MMA PTX assembly, each wired up to PyTorch via Python bindings so you can benchmark them immediately.

The gap it fills

The deep-learning stack is deeper than ever: Python on top, C++/CUDA at the bottom, and a large gap in between where most engineers get lost. LeetCUDA is a self-contained repo that tries to shrink that gap. It is not another "CUDA-101" slideshow , it is a living notebook that couples 200+ hand-written kernels with PyTorch integration, end-to-end benchmarks, and a curated blog roll that explains why every line of code looks the way it does.

The name is intentional. It borrows the LeetCode framing , graded difficulty, one problem at a time , and applies it to GPU kernel writing. The kernels guide you through a step-by-step progression, ranging from easy to very challenging topics, with the workflow for each topic being: custom CUDA kernel implementation, PyTorch Python bindings, then run tests.

What's inside

The difficulty ladder spans five tiers:

  • Easy / Medium: Element-wise ops, matrix transpose, warp/block reduce, NMS, ReLU, GELU, Swish, LayerNorm, RMSNorm, online softmax, dot product, and embedding , covering FP32, FP16, BF16, and FP8.
  • Hard: SGEMV, SGEMM, HGEMV, HGEMM, and FlashAttention , with numerous kernels implemented using Tensor Cores via pure MMA PTX.
  • Hard+ / Hard++: Full FlashAttention-2 variants with shared QKV SMEM, fine-grained QKV tiling, SMEM swizzle, and multi-stage pipelines.
  • Triton: OpenAI Triton kernels for softmax, LayerNorm, and attention state merging.
  • CUTLASS / CuTe: Higher-level abstractions via NVIDIA's CUTLASS library.

There is also a dedicated kernels/interview/ directory with a notes-v2.cu file that compiles into a self-contained verification harness , a single binary that runs 25+ kernels and prints a pass/fail table. It targets both Ada (sm_89) and Hopper (sm_90a) architectures, including WGMMA and TMA instructions for H100/H200.

nvcc -std=c++20 -O2 -arch=sm_89 -lcublas -lcuda notes-v2.cu -o notes_v2_sm89.bin
./notes_v2_sm89.bin
# Outputs: BlockReduce PASS, Dot PASS, ReLU PASS, HGEMM MMA PASS, FlashAttn-SplitQ PASS ...

The HGEMM and FlashAttention implementations

The HGEMM (half-precision matrix multiplication) implementation, using WMMA/MMA/CuTe, can achieve 98%–100% of cuBLAS's TFLOPS on NVIDIA L20, RTX 4090, and RTX 3080 Laptop. That is a meaningful benchmark , cuBLAS is NVIDIA's hand-tuned production library, and matching it from scratch is a legitimate teaching achievement.

The FlashAttention section is the most technically dense part of the repo. It implements six progressively more optimized variants of the FA-2 algorithm using raw MMA PTX, each reducing on-chip memory (SRAM) usage:

  1. Split KV , baseline FA-1 style, all QKV split across warps
  2. Split Q , FA-2 style, Q split across warps, KV shared
  3. Shared KV SMEM , K and V share the same SRAM buffer (1/2 the SRAM of FA-2)
  4. Shared QKV SMEM , Q, K, V all share SRAM (1/4 the SRAM of FA-2)
  5. QK Fine-grained Tiling , constant SRAM usage, supports head dimension up to 1024
  6. QKV Fine-grained Tiling , O(Br × 16) SRAM, near-constant regardless of head dimension

The most advanced variant , QKV fine-grained tiling , has been spun out into a separate project called ffpa-attn. FFPA extends FlashAttention-2 with a Split-D approach, achieving ~O(1) SRAM complexity for large head dimensions and 1.8x–3x speedup versus PyTorch's SDPA efficient attention.

The blog roll

Alongside the code, the repo curates 100+ technical blog posts (primarily in Chinese, written by DefTruth on Zhihu) covering GPU architecture deep dives, FlashAttention internals, vLLM operator development, TensorRT-LLM tuning, weight-only quantization (INT4/INT8), KV cache sharing strategies, and PTX assembly. The repository is primarily for learning and practice, with optimal performance not the final goal , LeetCUDA focuses on "using first, then using well," and for production use it recommends official implementations like cuBLAS, cuDNN, FlashAttention, and TensorRT.

Who it's for and how to use it

The repo is explicitly aimed at people who want to go from PyTorch user to GPU kernel writer. The interview directory makes the other target audience obvious: anyone preparing for ML systems or inference engineering roles where you are expected to write or reason about CUDA kernels. The workflow for each topic is: custom CUDA kernel implementation, PyTorch Python bindings, then run tests , meaning every kernel has a corresponding Python test you can run to verify correctness before profiling.

Getting started is straightforward:

git clone https://github.com/xlite-dev/LeetCUDA.git
git submodule update --init --recursive --force
cd kernels/elementwise
python3 elementwise.py  # runs the kernel and compares to PyTorch

The repo is free under GPL-3.0 and accepts community contributions. PRs are welcome to add new kernels. The contribution guide asks that each kernel follow the same structure as kernels/elementwise/ and pass pre-commit checks before submission.

Why this matters now

Demand for engineers who can write and optimize GPU kernels has grown sharply alongside the LLM infrastructure buildout. Most ML practitioners know PyTorch but have never touched a CUDA file. Resources like NVIDIA's official docs are comprehensive but not structured as a learning path. LeetCUDA's opinionated, difficulty-graded structure , with working code, benchmarks, and explanatory writing attached , fills a real gap that textbooks and documentation sites leave open. The 11.5k stars and 1.2k forks suggest the community agrees.