A modern CUDA + C++17 GPU kernel engineering suite targeting core LLM inference & training primitives: progressive SGEMM, warp-shuffle RMSNorm, Fused SwiGLU, single-pass Online Safe Softmax, SRAM-tiled FlashAttention Forward, and bank-conflict-free Warp Reduction & Hierarchical Blelloch Prefix Scan.
Every kernel is paired with:
- Gold-standard FP64-accumulating CPU reference implementations (
flash_ref) - Automated numerical parity verification (
max_abs_err,max_rel_err,RMSE,cosine_sim) across non-power-of-two shapes and extreme logit regimes - Analytical Roofline Performance Model (
RTX 4060,RTX 4090,A100 SXM4,H100 SXM5) - Nsight Compute (
ncu) profiling automation capturing SM/DRAM throughput, cache hit rates, and warp stall diagnostics
flash-kernel-cuda/
├── include/flash_kernel/
│ ├── cuda_utils.hpp # CUDA_CHECK, RAII DeviceBuffer<T>, CudaStream, CudaEventTimer
│ ├── kernels.cuh # Host launcher API for all CUDA kernels
│ ├── reference.hpp # FP64-accumulating CPU references + ParityMetrics
│ └── roofline.hpp # Analytical FLOPs, HBM traffic, AI, and GPU specs table
├── src/
│ ├── reference.cpp # CPU gold references & numerical parity checker
│ ├── roofline.cpp # Analytical roofline calculator & table/CSV formatter
│ ├── roofline_cli.cpp # CPU-only roofline estimator CLI (ENABLE_CUDA=OFF)
│ ├── bench_main.cu # Full CUDA verify / benchmark / roofline CLI (ENABLE_CUDA=ON)
│ └── kernels/
│ ├── sgemm.cu # Naive -> SMEM Tiled (+1 pad) -> 2D Register-Blocked float4
│ ├── rmsnorm_swiglu.cu # Warp-shuffle RMSNorm + Fused SwiGLU epilogue
│ ├── online_softmax.cu # Single-pass online safe (m, d) softmax
│ ├── flash_attention.cu # SRAM-tiled FlashAttention forward (4 threads/row, causal)
│ └── reduce_scan.cu # Two-level warp reduction + hierarchical Blelloch scan
├── tests/
│ └── test_reference.cpp # Zero-dependency C++17 CTest suite (+ CUDA linkage smoke)
├── scripts/
│ └── profile_ncu.sh # Nsight Compute roofline & warp-stall profiling script
└── .github/workflows/ci.yml # Dual-job CI: CPU CTest + CUDA 12.x SM 80/86/89 PTXAS build
| Kernel Family | Variant / Design | Key Hardware Optimizations |
|---|---|---|
SGEMM (sgemm.cu) |
1. sgemm_naive_kernel
|
Baseline global-memory dot product (16x16 CTAs) |
2. sgemm_smem_tiled_kernel
|
32x32 SRAM tiling with sA[32][33], sB[32][33] (+1 padding eliminates 32-way bank conflicts) |
|
3. sgemm_vectorized_2d_blocktiled_kernel
|
2D register blocking (BM=64, BN=64, BK=8, TM=4, TN=4), split-warp float4 cooperative loads, transposed sA[8][68] + sB[8][68], alignment/boundary fallbacks for arbitrary M, N, K
|
|
Normalization (rmsnorm_swiglu.cu) |
rmsnorm_kernel |
1 CTA/row, __shfl_xor_sync warp + block variance reduction, float4 vectorized global reads/writes when 16B aligned |
MLP Activation (rmsnorm_swiglu.cu) |
fused_swiglu_kernel |
Fuses silu(gate) * up in registers with overflow-safe silu and float4 grid-stride loop; cuts HBM traffic by 40% vs unfused (12 B/elem vs 20 B/elem) |
Softmax (online_softmax.cu) |
online_softmax_kernel |
Milakov–Gimelshein single-pass online (m, d) pair reduction via __shfl_xor_sync + SMEM; register-cached 1-HBM-read fast path when cols <= blockDim.x
|
Attention (flash_attention.cu) |
flash_attention_fwd_kernel |
SRAM-tiled (BR=32, BC=32, head_dim <= 64); 4 threads/row interleaved across d = 4k + sub (zero bank conflicts, zero local-memory register spills); sub-warp __shfl_xor_sync dot reduction; causal upper-tile early exit; avoids |
Collective Primitives (reduce_scan.cu) |
warp_reduce_sum_kernel |
float4 grid-stride accumulation + two-level warp-shuffle & shared-memory reduction |
blelloch_scan_kernel |
Work-efficient upsweep/downsweep prefix scan with CONFLICT_FREE_OFFSET bank padding + recursive multi-block offset propagation for arbitrary n
|
All numbers below are exact analytical lower bounds computed by flash_kernel_bench --roofline (no fabricated GPU timings). Hardware timings should be measured on your target GPU using --bench and scripts/profile_ncu.sh.
| GPU | FP32 Peak (TFLOP/s) | Memory BW (GB/s) | Ridge Point (FLOP/Byte) |
|---|---|---|---|
| NVIDIA GeForce RTX 4060 | 15.11 | 272 | 55.55 |
| NVIDIA GeForce RTX 4090 | 82.58 | 1008 | 81.92 |
| NVIDIA A100 SXM4 80GB | 19.50 | 2039 | 9.56 |
| NVIDIA H100 SXM5 80GB | 66.90 | 3350 | 19.97 |
| Kernel | Representative Shape | FLOPs (GFLOPs) | Min HBM Traffic (MB) | Arithmetic Intensity (FLOP/B) | Regime (H100) |
|---|---|---|---|---|---|
SGEMM |
2048x2048x2048 |
17.180 | 48.00 | 341.33 | Compute-Bound |
SGEMM |
4096x4096x4096 |
137.439 | 192.00 | 682.67 | Compute-Bound |
RMSNorm |
4096x4096 |
0.067 | 128.02 | 0.50 | Memory-Bound |
FusedSwiGLU |
4096 x 11008 |
0.225 | 516.00 | 0.42 | Memory-Bound |
OnlineSoftmax |
4096x4096 |
0.084 | 128.00 | 0.63 | Memory-Bound |
FlashAttentionFwd(causal) |
BH=32, N=1024, D=64 |
4.383 | 32.00 | 130.63 | Compute-Bound |
FlashAttentionFwd(causal) |
BH=32, N=2048, D=64 |
17.524 | 64.00 | 261.13 | Compute-Bound |
WarpReduceSum |
16,777,216 |
0.017 | 64.00 | 0.25 | Memory-Bound |
BlellochScan |
4,194,304 |
0.008 | 32.00 | 0.25 | Memory-Bound |
Notice how SRAM tiling keeps
FlashAttentionFwdHBM traffic strictly linear in sequence length ($16 \cdot BH \cdot N \cdot D$ bytes), causing arithmetic intensity to scale linearly with$N$ and transition solidly into the compute-bound regime at long context lengths.
cmake -B build-cpu -DENABLE_CUDA=OFF -DCMAKE_BUILD_TYPE=Release
cmake --build build-cpu --parallel
ctest --test-dir build-cpu --output-on-failure
# Run analytical roofline estimator for any supported GPU spec
./build-cpu/flash_kernel_bench --roofline --gpu h100
./build-cpu/flash_kernel_bench --roofline --gpu rtx4090 --csvcmake -B build-cuda \
-DENABLE_CUDA=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CUDA_ARCHITECTURES="80;86;89"
cmake --build build-cuda --parallel
ctest --test-dir build-cuda --output-on-failure# Verify every CUDA kernel against FP64 CPU references (non-power-of-two shapes + 1e4 logit stability)
./build-cuda/flash_kernel_bench --verify --kernel all
# Benchmark all kernels with CUDA event timing + live % of roofline efficiency
./build-cuda/flash_kernel_bench --bench --kernel all --gpu rtx4090 --warmup 5 --iters 20
# Emit machine-readable CSV for plotting
./build-cuda/flash_kernel_bench --verify --bench --kernel attention --csvchmod +x scripts/profile_ncu.sh
./scripts/profile_ncu.sh all ./build-cuda/flash_kernel_bench ./profilesThis generates:
profiles/flash_kernel_<kernel>.ncu-rep(open in Nsight Compute GUI for full roofline & source-level stall attribution)profiles/flash_kernel_<kernel>_metrics.csv(sm__throughput,gpu__dram_throughput,l1tex__t_sector_hit_rate,lts__t_sector_hit_rate, active warps, and long-scoreboard/barrier/math-pipe stall percentages)
MIT License — see LICENSE.