Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

450 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vitamin-CUDA

🍎 One Kernel a Day, Keeps High Latency Away.

A hands-on CUDA learning path from novice to expert.

Welcome to your daily dose of GPU performance fuel. Vitamin-CUDA is a curated collection of 170+ hand-tuned CUDA kernels, ranging from basic grid-stride loops for beginners to cutting-edge asynchronous TMA pipelines for performance enthusiasts(SGEMM, HGEMM Flash Attention/Decoding). Every kernel is seamlessly wrapped as a PyTorch C++ extension, delivering raw hardware performance with zero-overhead Python integration.

No over-engineered abstractions. No black boxes. Just pure hardware saturation.

News

  • [2026.05.21] topk_topp_sampling -Released template-specialized (split-K) sampling kernels optimized for small-K/BS regimes using register-level insertion sort and hierarchical merge. Performance insights from this implementation helped drive a 2~4x speedup optimization in FlashInfer(see #3389).
  • [2026.04.26] flash_decode_sm120 — Released a custom sm120 decode kernel leveraging TMA + double Ks buffers + single Vs buffer to maximize memory bandwidth utilization.
  • [2026.04.16] flash_attn_sm120 — Released a custom sm120 kernel that outperforms torch.sdpa(FA2) across all prefill sequence lengths(1.2x in best case). Leverages TMA to drastically reduce register pressure and maximize L2 cache locality.
  • [2026.04.04] hgemm_120 — FP16/BF16 Tensor-Core kernel pushing the absolute compute limit of the GPU. Outperforms cuBLAS via TMA copy + 3-stage pipeline + double register buffer + swizzle + ldmatrix + mma.
  • [2026.03.18] hgemm — FP16/BF16 Tensor-Core kernel outperforming cuBLAS via cp.async + double SMEM buffer + swizzle + ldmatrix + mma.
  • [2026.03.10] sgemm_tf32 — TF32 Tensor-Core kernel outperforming cuBLAS via cp.async + double SMEM buffer + swizzle + ldmatrix + mma.
  • [2026.02.27] sgemm — SIMT kernel outperforming cuBLAS via SMEM + swizzle + double buffer + coalesced read/write.

Contents

Prerequisites

  • NVIDIA GPU (Compute Capability 6.0+)
  • CUDA Toolkit 11.0+
  • C++ Compiler (GCC / Clang / MSVC)
  • CMake 3.18+ (optional but recommended)
  • PyTorch (for Python bindings and performance comparison)

Quick Start — We recommend using the NVIDIA PyTorch NGC Docker image for a ready-to-go environment.

Kernels (170+)

All kernels were tested on an RTX 5060 Laptop GPU (unless otherwise noted) and benchmarked against PyTorch 2.9+.

Easy ⭐️ ~ ⭐️⭐️

  • elementwise: element-wise add
    • elementwise_add — FP32 / FP16
    • elementwise_add_fp16x2 — FP16 vectorized (×2)
    • elementwise_add_fp16x8 — FP16 vectorized (×8)
    • elementwise_add_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • sigmoid
    • sigmoid — FP32 / FP16
    • sigmoid_fp16x2 — FP16 vectorized (×2)
    • sigmoid_fp16x8 — FP16 vectorized (×8)
    • sigmoid_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • swish
    • swish — FP32 / FP16
    • swish_fp16x2 — FP16 vectorized (×2)
    • swish_fp16x8 — FP16 vectorized (×8)
    • swish_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • relu
    • relu — FP32 / FP16
    • relu_fp16x2 — FP16 vectorized (×2)
    • relu_fp16x8 — FP16 vectorized (×8)
    • relu_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • relu6
    • relu6 — FP32 / FP16
    • relu6_fp16x2 — FP16 vectorized (×2)
    • relu6_fp16x8 — FP16 vectorized (×8)
    • relu6_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • elu
    • elu — FP32 / FP16
    • elu_fp16x2 — FP16 vectorized (×2)
    • elu_fp16x8 — FP16 vectorized (×8)
    • elu_fp16x8 — FP16 vectorized (×8, packed r/w; ~2× speedup via half2 intrinsics)
    • PyTorch op binding & correctness check
  • gelu
    • gelu — FP32 / FP16
    • gelu_fp16x2 — FP16 vectorized (×2)
    • gelu_fp16x8 — FP16 vectorized (×8)
    • gelu_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • hardswish
    • hardswish — FP32 / FP16
    • hardswish_fp16x2 — FP16 vectorized (×2)
    • hardswish_fp16x8 — FP16 vectorized (×8)
    • hardswish_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • embedding
    • embedding — FP32 / FP16
    • embedding_fp32x4 — FP32 vectorized (×4)
    • embedding_fp32x4 — FP32 vectorized (×4, packed r/w)
    • embedding_fp16x2 — FP16 vectorized (×2)
    • embedding_fp16x8 — FP16 vectorized (×8)
    • embedding_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • rope — Rotary Position Embedding
    • PyTorch naive RoPE
    • PyTorch RoPE with cos/sin look-up table
    • rope_fp32 — ~10× faster than the PyTorch naive implementation
    • rope_fp32x4 — FP32 vectorized; tens of times faster at larger sizes
    • PyTorch op binding & correctness check

Medium ⭐️⭐️ ~ ⭐️⭐️⭐️

  • reduce — Warp-shuffle-based reductions
    • reduce_sum — FP32 / FP16
    • reduce_sum_fp16x2 — FP16 vectorized (×2)
    • reduce_sum_fp16x8_packed — FP16 vectorized (×8, packed r/w)
    • reduce_sum — INT8
    • reduce_sum_i8x16_packed — INT8 vectorized (×16, packed r/w)
    • reduce_sum_i8x16_packed — INT8 vectorized (×16, packed r/w, dp4a; tens of times faster than naive PyTorch)
    • reduce_sum_i8x64_packed — INT8 vectorized (×64, packed r/w, dp4a)
    • PyTorch op binding & correctness check
  • dot_product
    • dot_product — FP32 / FP16
    • dot_product_fp32x4 — FP32 vectorized (×4)
    • dot_product_fp16x2 — FP16 vectorized (×2)
    • dot_product_fp16x8 — FP16 vectorized (×8, packed r/w)
    • PyTorch op binding & correctness check
  • cumsum - Warp-shuffle-based scan
    • naive Torch cumsum
    • cumsum_fp32
    • cumsum_fp32x4 — FP32 vectorized (×4)
    • cumsum_bf16
    • cumsum_bf16x8_packed — BF16 vectorized (×8, packed r/w)
    • cumsum_fp32x4_split_k — split-k two-pass
    • cumsum_fp32x4_multi_cta_scan — multi-CTA single kernel (decoupled look-back)
    • pytorch op bindings && diff check
  • normalization - (x - mean) / std
    • naive Torch norm
    • norm_fp32
    • norm_fp32x4 — FP32 vectorized (×4)
    • norm_fp16
    • norm_fp16x8_packed — FP16 vectorized (×8, packed r/w)
    • norm_fp32x4_split_k — split-k two-pass
    • pytorch op bindings && diff check
  • softmax — Safe online softmax
    • One-pass
      • Safe online softmax — FP32 / FP16
      • Safe online softmax — FP32 vectorized (×4)
      • Safe online softmax — FP16 vectorized (×8, pure register, packed r/w)
      • Safe online softmax medium — FP16 vectorized (moderate register + SMEM, packed r/w)
      • Safe online softmax extreme — FP16 vectorized (max register + SMEM, packed r/w)
    • Two-pass (large rows beyond single-block capacity)
      • Safe online softmax arbitrary — FP16 vectorized (max register + SMEM, packed r/w)
      • Safe online softmax split-k — FP16 vectorized (max register + SMEM, packed r/w)
    • PyTorch op binding & correctness check
  • rmsnorm
    • PyTorch naive RMSNorm (baseline)
    • rmsnorm — FP32 / FP16
    • rmsnorm_fp32x4 — FP32 vectorized (×4)
    • rmsnorm_fp32x4_smem — with shared memory
    • rmsnorm_fp16x8 — FP16 vectorized (×8, packed r/w)
    • rmsnorm_fp16x8_smem — FP16 vectorized (×8, packed r/w, shared memory)
    • PyTorch op binding & correctness check
  • transpose — Matrix transpose
    • transpose_coalesced_read — coalesced global loads (input-centric)
    • transpose_coalesced_write — coalesced global stores (output-centric)
    • transpose_smem — shared-memory tiled transpose
    • transpose_smem_bcf — SMEM bank-conflict-free
    • transpose_smem_packed_bcf — SMEM bank-conflict-free, float4 vectorized r/w
    • transpose_smem_swizzled_packed — SMEM swizzled, float4 vectorized r/w
    • PyTorch op binding & correctness check

Hard ⭐️⭐️⭐️~⭐️⭐️⭐️⭐️

  • sgemv — Single-precision GEMV
    • gemv_fp32 — baseline
    • gemv_fp32x4 — vectorized loads (×4)
    • gemv_fp32x4_split_k — split-k two-pass
    • PyTorch op binding & correctness check
    • PyTorch op binding & correctness check
  • sgemm — Single-precision GEMM (SIMT)
    • sgemm_cublas — cuBLAS FP32 baseline
    • sgemm_tiling — vectorized r/w + block tiling with SMEM
    • sgemm_at_tiling — A-transpose into SMEM (4-way bank conflict, inner-loop float4 loads)
    • sgemm_at_bcf_swizzling — A-transpose + swizzle, bank-conflict-free
    • sgemm_at_bcf_swizzling_rw — + coalesced C write-back
    • sgemm_at_bcf_swizzling_dbf_rw — + double buffer pipeline, outperforms cuBLAS
    • PyTorch op binding & correctness check
  • sgemm_tf32 — TF32 Tensor-Core GEMM
    • sgemm_cublas_tf32 — cuBLAS TF32 baseline
    • sgemm_tf32_bt — vectorized loads for A/B, B-transpose into SMEM, ldmatrix + mma
    • sgemm_tf32_bt_swizzle — + A-SMEM zero bank conflicts
    • sgemm_tf32_bt_swizzle_dbf — + grid swizzling, 97–102% of cuBLAS
    • sgemm_tf32_swizzle_bcfcp.async, warp-shuffle B-register transpose, A/B SMEM conflict-free, grid swizzling
    • sgemm_tf32_swizzle_bcf_dbf — + double buffer, outperforms cuBLAS
    • PyTorch op binding & correctness check
  • hgemm
    • hgemm_cublas — cuBLAS BF16/FP16 baseline
    • hgemm_naive — BF16/FP16, ldmatrix + mma
    • hgemm_bcf — BF16/FP16, ldmatrix + mma, A/B SMEM swizzle conflict-free, 95–99% of cuBLAS
    • hgemm_bcf_dbf — + double buffer, outperforms cuBLAS
    • hgemm_bcf_dbf_rw — + coalesced GMEM read/write, outperforms cuBLAS
    • PyTorch op binding & correctness check
  • hgemm_sm120
    • hgemm_cublas baseline
    • hgemm_bcf_dbf_rw baseline: cp.async + ldmatrix + mma + double buffer + coalesced gmem r/w
    • hgemm_k_stages: cp.async + ldmatrix + mma + 2/3 stages buffer + coalesced gmem
    • hgemm_tma_r_k_stages_64: TMA read + ldmatrix + mma: one block, 3 stages, 128x128x64, double buffer register
    • hgemm_tma_r_k_stages_32: TMA read + ldmatrix + mma: two blocks, 3 stages, 128x128x32, double buffer register
    • pytorch op bindings && diff check
  • flash_attantion_sm120
    • fmha: sigle buffer (BMxBN = 64x64, better for small seq_len size)
    • pytorch op bindings && diff check
  • flash_decoding_sm120
    • flash_decode_tma_128
    • pytorch op bindings && diff check
  • topk_topp_sampling
    • sampling_topk_topp_batched
    • sampling_topk_topp_split_k
    • pytorch op bindings && diff check

DSL kernels

  • vector_add:
    • tilelang/triton kernels
    • pytorch/cuda op compare && diff check
  • softmax
    • tilelang online softmax (naive two pass)
    • pytorch/cuda op compare && diff check

Samples

Blogs (10+)

blogs

📖type 📖blog link 📖author 📖star rating
CUDA basics A Deep Dive into DeviceQuery: Understanding Your GPU Hardware @WingEdge777 ⭐️⭐️
CUDA basics Numbers Every CUDA Developer Should Know @WingEdge777 ⭐️⭐️
CUDA basics Understanding CUDA's "Nonexistent" Memory Tier: Local Memory @WingEdge777 ⭐️⭐️
CUDA Practice [CUDA in Practice] Matrix Transpose — From Padding to XOR Swizzle: The Art of Shared Memory Access Optimization @WingEdge777 ⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] SGEMM — Beating cuBLAS: A Deep Dive into Peak-Performance Matrix Multiplication in Pure CUDA C++ @WingEdge777 ⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] Safe Online Softmax — A Must-Know for Interviews: Arbitrary hidden_size, One/Two Pass, Trade-offs, Split-K @WingEdge777 ⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] SGEMM TF32 — Beating cuBLAS with Tensor Cores, cp.async, ldmatrix & mma @WingEdge777 ⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] HGEMM — Beating cuBLAS: Tensor Core, cp.async, ldmatrix, mma @WingEdge777 ⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] HGEMM SM120 — Micro-Sculpture Warfare in 100KB SMEM: Tensor Core, TMA, ldmatrix, mma @WingEdge777 ⭐️⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] FMHA on SM120: Beating torch.sdpa (FlashAttention-2) @WingEdge777 ⭐️⭐️⭐️⭐️
CUDA Practice [CUDA in Practice] Hand-Rolled Flash Decoding on SM120: Beating flashinfer.single_decode_with_kv_cache @WingEdge777 ⭐️⭐️⭐️⭐️

博客

📖类型 📖文章链接 📖作者 📖推荐
CUDA入门 深度解读 DeviceQuery:理解你的 GPU 硬件属性 @WingEdge777 ⭐️⭐️
CUDA入门 CUDA 开发者应该熟悉的数 @WingEdge777 ⭐️⭐️
CUDA入门 [CUDA 入门] L1/TEX/SMEM - 再识bank conflict @WingEdge777 ⭐️⭐️
CUDA入门 [CUDA 入门] 认识 CUDA “不存在的存储层级” - local memory @WingEdge777 ⭐️⭐️
CUDA实战 [CUDA 优化实战] 矩阵转置 - 从 Padding 到 Swizzle:基础共享内存优化的艺术 @WingEdge777 ⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] RoPE - 手写算子的作用之 kernel fusion:减少访存次数、减少启动开销的优化技巧 @WingEdge777 ⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] sgemm - 超越 cuBLAS:带你学会极致优化的矩阵乘法 cuda c++ 实现 @WingEdge777 ⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] sgemm tf32 - 超越 cuBLAS:cp.async、ldmatrix、mma、Tensor Core加速 @WingEdge777 ⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] hgemm - 超越 cuBLAS:Tensor-core、cp.async、ldmatrix、mma @WingEdge777 ⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] safe online softmax - 面试必问:任意 hidden_size、one/two pass、trade-off、split-k @WingEdge777 ⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] hgemm sm120 - 100KB SMEM 中的“微雕”战争:Tensor-core、TMA、ldmatrix、mma @WingEdge777 ⭐️⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] 纯手搓 flash attention (fmha) - sm120 : 超越 torch.sdpa(FA2) @WingEdge777 ⭐️⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] 纯手搓 flash decoding sm120 (上): 拉爆显存带宽的cuda c++实现 @WingEdge777 ⭐️⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] 手搓 flash decoding sm120(下) : 超越 flashinfer的c++实现 @WingEdge777 ⭐️⭐️⭐️⭐️
CUDA实战 [CUDA 优化实战] LLM 推理的最后一公里 - topk_topp_sampling - 乱拳打死老师傅 :暴力插排 topK、block merge @WingEdge777 ⭐️⭐️⭐️

Star History

Star History Chart

References

About

🍎 One kernel a day keeps high latency away. A hands-on CUDA learning path featuring a rich collection of kernels, from the basics to peak performance, seamlessly integrated as PyTorch C++ extensions.

Topics

Resources

Stars

207 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages