AI Skills AI技能 3d ago Updated 3d ago 更新于 3天前 48

What a Kernel Is, and Why Everyone Is Writing New Ones 什么是内核,以及为什么每个人都在编写新内核

GPU kernels are the critical layer between silicon and software, where the real performance bottlenecks of LLM inference are solved through memory optimization rather than arithmetic FlashAttention eliminates the O(S²) memory footprint of attention by processing tiles in shared memory with online softmax, achieving exact results while reducing held memory by ~250x at 64K tokens The GPU memory hierarchy has one dramatic step—a 1,600-fold gap between L2 cache (50 MB) and HBM (80 GB)—making shared GPU推理瓶颈不在算力而在内存带宽,kernel层的核心任务是减少数据在HBM与片上内存之间的搬运 FlashAttention通过tile-based计算和online softmax,将注意力机制的内存占用从8.6GB降至34MB,实现精确计算而非近似 GPU内存层级呈阶梯状而非平滑梯度,L2与HBM之间存在1,600倍带宽鸿沟,shared memory(每block仅48KiB)是关键优化目标 Triton存在双重含义:一是GPU kernel编写语言(PyTorch编译器底层使用),二是NVIDIA推理服务器(已更名为Dynamo Triton) FlashInfer通过统一稀疏矩阵

68
Hot 热度
72
Quality 质量
65
Impact 影响力

Analysis 深度分析

TL;DR

  • GPU kernels are the critical layer between silicon and software, where the real performance bottlenecks of LLM inference are solved through memory optimization rather than arithmetic
  • FlashAttention eliminates the O(S²) memory footprint of attention by processing tiles in shared memory with online softmax, achieving exact results while reducing held memory by ~250x at 64K tokens
  • The GPU memory hierarchy has one dramatic step—a 1,600-fold gap between L2 cache (50 MB) and HBM (80 GB)—making shared memory (228 KiB per SM) the key optimization target
  • FlashInfer unifies multiple cache layouts into a single block-sparse format and performs dynamic batch planning per forward pass, making it the default for serving on Blackwell GPUs
  • The two "Tritons" (kernel language vs. inference server) share only a name; NVIDIA renamed Triton Inference Server to Dynamo Triton in March 2025 to reduce confusion

Why It Matters

Understanding this kernel layer is essential for anyone deploying LLMs in production, as it explains why attention was the bottleneck and how modern libraries achieve order-of-magnitude memory reductions without sacrificing correctness. For AI practitioners, knowing which kernel stack their engine uses—and why—directly impacts throughput, memory efficiency, and hardware utilization across different GPU generations.

Technical Details

  • GPU Memory Hierarchy: Four tiers on a log scale—registers (33 MiB), shared memory (29 MiB), L2 cache (50 MB), and HBM3 (80 GB)—with a 1,600-fold bandwidth gap between L2 and HBM being the critical boundary; each SM has only 228 KiB of shared memory, with 48 KiB per block unless explicitly opted in
  • FlashAttention Mechanism: Processes attention in tiles loaded into shared memory, using online softmax (Milakov & Gimelshein, 2018) to maintain running max and sum without materializing the full S×S score matrix; computes exact attention (not approximate), trading extra FLOPs in rescaling for massive memory savings
  • FlashInfer Architecture: Treats every KV cache layout (paged, shared prefixes, sliding windows) as a single block-sparse matrix format, generating kernels from templates; performs dynamic batch planning by inspecting ragged serving batches and rebuilding schedules every step, spreading planning cost across 36 layers on CPU while GPU computes
  • Triton Language: A Python-like kernel language from Harvard/OpenAI that compiles to GPU code via PyTorch's torch.compile; users control block sizes, thread lockstep, and pipeline depth, producing roughly a tenth the code of traditional CUDA with most of the performance
  • CUTLASS 3.x: NVIDIA's template library for GEMM and variants, including CuTe layout system used by FlashAttention-3; critical for grouped GEMM operations powering mixture-of-experts layers, with gaps over naive implementations far exceeding marginal optimizations

Industry Insight

  • Kernel selection is now a primary deployment decision: FlashInfer is becoming the default for serving on Blackwell while FlashAttention remains preferred on H100, meaning infrastructure choices must be GPU-generation-aware rather than assuming one-size-fits-all
  • The shift from bandwidth-bound to compute-bound attention means performance tuning should now focus on tensor core utilization and tiling strategies rather than memory bandwidth optimization, fundamentally changing how teams approach inference profiling
  • The convergence of libraries (TensorRT-LLM shipping kernels into FlashInfer, vLLM's Blackwell-first-choice switch) suggests the ecosystem is consolidating around a few high-quality kernel implementations rather than fragmenting, making it increasingly important to understand which backend your stack actually uses

TL;DR

  • GPU推理瓶颈不在算力而在内存带宽,kernel层的核心任务是减少数据在HBM与片上内存之间的搬运
  • FlashAttention通过tile-based计算和online softmax,将注意力机制的内存占用从8.6GB降至34MB,实现精确计算而非近似
  • GPU内存层级呈阶梯状而非平滑梯度,L2与HBM之间存在1,600倍带宽鸿沟,shared memory(每block仅48KiB)是关键优化目标
  • Triton存在双重含义:一是GPU kernel编写语言(PyTorch编译器底层使用),二是NVIDIA推理服务器(已更名为Dynamo Triton)
  • FlashInfer通过统一稀疏矩阵表示和运行时调度规划,覆盖多种cache布局,已成为vLLM和SGLang的默认kernel库

为什么值得看

本文系统梳理了LLM推理优化栈中kernel层的技术脉络,澄清了FlashAttention等核心技术的真实原理与性能边界,帮助从业者理解"为什么字节比浮点运算更昂贵"这一关键认知。

技术解析

  • GPU内存层级结构:寄存器(33MiB)> 共享内存(29MiB)> L2缓存(50MB)> HBM3(80GB),其中L2到HBM存在1,600倍带宽跃迁,是性能优化的关键断点。每个SM仅分配228KiB共享内存,单block默认48KiB。

  • FlashAttention核心机制:不物化完整注意力矩阵,将序列分块(tile)加载到shared memory中计算,配合online softmax(维护运行最大值和累加和)实现精确计算。在64K序列长度下,单头单层的内存占用从8.6GB降至34MB,数据搬运量减少4倍。

  • Triton语言与编译器:由哈佛和OpenAI发起的GPU kernel开发语言,代码量约为传统CUDA的十分之一。PyTorch的torch.compile默认生成Triton代码处理element-wise和求和操作,矩阵乘法仍走cuBLAS。

  • FlashInfer设计哲学:将所有cache布局(paged cache、shared prefixes、sliding windows)统一表示为块稀疏矩阵,通过模板生成单一kernel;运行时先分析batch的ragged特性,规划调度后再启动计算,规划成本分摊到36层的前向传播中。

行业启示

  • 推理优化重心已从算力转向内存管理:随着GPU算力持续提升,带宽成为瓶颈,kernel层的tile-based和online算法是突破长序列推理的关键路径。
  • 技术选型需区分GPU代际:FlashInfer在Blackwell上成为vLLM首选,但在H100上FlashAttention仍占主导,部署时需根据硬件版本选择最优kernel组合。
  • 术语混淆成本高昂:Triton等名称存在多重含义,团队沟通时应明确上下文("Triton kernels" vs "Triton server"),避免跨团队理解偏差。

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

GPU GPU Inference 推理 LLM 大模型 Chip 芯片 Programming 编程