Pidoku

FlashInfer (2025)

Module 30 Lesson 30.x 40 min Difficulty 4/5

Prerequisites 30.f, 30.o, 30.r, 5.f


Before you start: words you need#

WordPlain meaning
Attention engineA library of attention kernels plus the planning code that decides how to run them for each batch, rather than one fixed kernel.
Block-sparse matrix (BSR)A sparse matrix stored as small dense blocks plus an index of where the non-empty blocks are.
Page table as a sparse matrixFlashInfer’s key view: which KV pages each query reads can be written as a block-sparse matrix of queries × KV positions.
JIT compilationCompiling a kernel when it is first needed, specialised to the exact settings (head size, mask, data types), instead of shipping every combination prebuilt.
Load balancingSplitting uneven work (one request with 100k tokens of context, many with 100) evenly across the GPU’s SMs.
CUDA GraphA recorded sequence of kernel launches replayed with one call to remove launch overhead; it requires fixed launch settings (5.f, 13.k).
Plan / runFlashInfer’s two-step API: plan computes a schedule on the CPU from the batch’s lengths; run executes kernels with that schedule.
Cascade attentionComputing attention over a shared prefix once for many requests, then over each request’s own suffix, and merging the two results.

1. The paper#

  • Title: “FlashInfer: Efficient and Customizable Attention Engine for LLM Inference Serving”
  • Authors: Zihao Ye, Lequn Chen, Ruihang Lai, Wuwei Lin, Yineng Zhang, Stephanie Wang, Tianqi Chen, Baris Kasikci, Vinod Grover, Arvind Krishnamurthy, Luis Ceze (University of Washington, NVIDIA, Perplexity AI, Carnegie Mellon and others)
  • Venue: MLSys 2025, where it received the conference’s top paper award (listed as “Outstanding Paper”; reported as Best Paper by the authors’ universities). arXiv January 2025; the library itself has been public since 2023.
  • Links: arXiv:2501.01005 · code: flashinfer-ai/flashinfer

2. The problem#

FlashAttention (30.f, 30.n) is superb for training-style attention: dense, contiguous, equal-length batches. Serving attention is messier:

  • Many KV layouts: paged caches with different page sizes (30.o), radix-tree prefix caches (30.r), shared prefixes, tree-shaped speculative drafts (30.s), sparse patterns.
  • Many variants: GQA group sizes, MLA, sliding windows, logit soft-capping, custom masks, attention sinks, fused RoPE.
  • Very uneven batches that change every step: a continuous batch (30.g) mixes long and short sequences, prefill chunks and single decode tokens.
  • CUDA Graphs want fixed launch settings, while the batch changes shape every step.

Writing and tuning a separate kernel for every combination does not scale. Each engine was doing it again.


3. The idea#

Three ideas:

  1. One storage abstraction: block-sparse matrices. Any KV layout — paged, radix tree, shared prefix, token tree — can be expressed as a block-sparse matrix saying which query rows read which KV columns. Blocks can be as narrow as one token wide (“vector-sparse”), so page size 1 still runs efficiently, because the kernel gathers the needed rows into shared memory first. Composable formats let a shared prefix use bigger blocks (several queries share one KV load) and each request’s suffix use small ones.
  2. One customisable kernel template, compiled just in time. The attention variant is defined by a few small functions (query/key transforms, logits transform, mask) plugged into a FlashAttention-2/3 style template, and JIT-compiled for the exact settings.
  3. A load-balanced, CUDA-Graph-friendly scheduler. Before each step, plan on the CPU splits the work (including splitting long KV sequences into chunks, as in Flash-Decoding) into evenly sized tiles for a fixed number of thread blocks, writes the plan into a workspace buffer on the GPU, and the run step reads it. Launch settings stay constant, so the kernels can live inside a CUDA Graph while the batch changes.
flowchart LR
  B["This step's batch: lengths, pages, prefixes"] --> PL["plan (CPU): balanced tiles for a fixed grid"]
  PL --> WS["Workspace buffer on GPU"]
  V["Attention variant spec (mask, transforms)"] --> JIT["JIT-compiled kernel"]
  WS --> RUN["run: kernels read plan; captured in a CUDA Graph"]
  JIT --> RUN
  RUN --> M["Merge partial results (log-sum-exp)"]
  class B io
  class PL,WS neutral
  class V,JIT memory
  class RUN,M compute

4. How it works#

  • Merging partial attention. Attention over a set of keys can be split into parts, each returning an output and a log-sum-exp; the parts merge exactly (the same algebra as online softmax). This one operator enables split-KV decoding, cascade attention for shared prefixes, and combining cached and new parts.
  • Prefix sharing. With a shared prefix, the queries of all requests that share it are grouped into one tall block, so the prefix’s K and V are read once for all of them, not once per request.
  • Kernels. Built on CUTLASS/CuTe, with FlashAttention-2-style kernels for Ampere and FlashAttention-3-style kernels for Hopper; tile sizes chosen per head size, GQA group and workload.
  • Integration. Engines call plan once per step and run once per layer; the paper reports integrations in SGLang, vLLM and MLC-Engine.

5. Results#

From the abstract and section 4 (A100 and H100):

ComparisonResult
Inter-token latency vs SGLang’s Triton attention backend (Llama 3.1 8B on 1 × H100, 70B on 4 × H100)29–69% lower
Long-context inference (with StreamingLLM-style fused kernels)28–30% lower latency
Serving with parallel generation (shared prefixes)13–17% faster

These are end-to-end serving gains, which are smaller than kernel-only speed-ups because attention is only part of each step. The paper’s kernel benchmarks show the load-balanced scheduler matters most for skewed batches (a few long sequences among many short ones).


6. What held up and what did not#

Held up

  • Attention as an engine, not a kernel. FlashInfer became a default dependency of SGLang and a core backend in vLLM; in vLLM v0.32.0 it is the default attention backend on Blackwell data-centre GPUs (18.c).
  • Plan/run with a GPU workspace is how serving engines reconcile dynamic batches with CUDA Graphs (18.d).
  • JIT-specialised variants made it fast to support new models (soft-capping, sinks, MLA, sliding window) without waiting for a new kernel release.
  • Beyond attention. FlashInfer grew into a kernel library for serving: sampling, MoE (fused and grouped GEMMs), communication (all-reduce), and NVIDIA’s own TensorRT-LLM kernels (XQA, trtllm-gen) packaged for other engines.

Did not, or changed

  • The fastest kernel is often someone else’s. On Blackwell, FlashInfer routes many cases to NVIDIA’s trtllm-gen kernels; FlashAttention-4 (30.n), FlashMLA and cuDNN compete per case. FlashInfer’s value is the abstraction and dispatch as much as its own kernels.
  • JIT compile time shows up as slow first requests and cold starts; engines precompile common variants (20.g).
  • Block-sparse generality is used mostly for paging and prefixes; arbitrary sparse attention (30.y) usually gets dedicated kernels.

7. Where it lives in 2026#

  • Taught in depth: vLLM’s attention backends in 18.c, CUDA graphs and compilation in 18.d, SGLang in 15.b, attention kernels in 25.f.
  • flashinfer-ai/flashinfer is actively developed with NVIDIA as a major contributor; it ships attention, sampling, MoE and communication kernels for NVIDIA GPUs.

Checked on 13 November 2026.


8. Questions#

  1. How can a paged KV cache be represented as a block-sparse matrix?
  2. Why does FlashInfer gather KV rows into shared memory rather than requiring large pages?
  3. Explain how attention over two parts of the context can be computed separately and merged exactly.
  4. How does sharing a prefix across requests reduce memory traffic in FlashInfer?
  5. Why does CUDA Graph capture conflict with dynamic batches, and how do plan/run resolve it?
  6. What does JIT compilation buy, and what does it cost in production?
  7. On Blackwell, vLLM defaults to FlashInfer but FlashInfer often runs trtllm-gen kernels. What does that tell you about where FlashInfer’s value lies?

9. Further reading#

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in