TL;DR
FlashInfer is an open-source, highly efficient, and customizable attention engine designed specifically for Large Language Model (LLM) inference serving. By introducing a unified block-sparse KV-cache format, a JIT-compiled attention template, and a dynamic load-balanced scheduler, it slashes inter-token latency by up to 69% and seamlessly adapts to diverse, evolving LLM application patterns.
What Problem Does This Paper Address?
Deploying LLMs in production environments requires maximizing throughput while minimizing latency. At the heart of this challenge lies the attention mechanism, which must constantly read from and write to the KV-cache (the memory storing historical context).
However, standard attention kernels (like FlashAttention) designed for model training fall short in inference serving due to two major challenges:
- Workload Dynamism: Inference requests arrive unpredictably with highly variable sequence lengths. Advanced serving techniques like prefix-sharing, speculative decoding, and multi-token parallel generation introduce complex, irregular attention patterns.
- Storage Heterogeneity: To prevent memory fragmentation, modern serving frameworks use non-contiguous storage layouts like paged attention or radix trees.
Existing solutions typically hardcode optimizations for a narrow subset of these patterns, leading to high maintenance overhead and an inability to easily support new attention variants (e.g., specialized masking or logit soft-capping).
Methods
FlashInfer tackles these challenges through a decoupled system architecture that separates hardware-specific compute optimizations from runtime scheduling.
- Unified Block-Sparse Format: FlashInfer unifies fragmented memory layouts (paged attention, radix trees, sparse masks) under a single Block Compressed Sparse Row (BSR) matrix abstraction.
- Composable Formats: Inspired by sparse compilers, FlashInfer allows developers to decompose the KV-cache. For instance, a shared system prompt (prefix) can be processed using a kernel optimized for large block sizes to maximize shared memory reuse, while the unique user query uses a fine-grained block size.
- JIT Compiler for Attention Variants: Instead of hand-writing CUDA kernels for every new model variant, users define mathematical modifications (like
LogitsTransform) in Python/CUDA code strings. FlashInfer’s Just-In-Time (JIT) compiler automatically injects these into highly optimized templates. - Dynamic Load-Balanced Scheduling: To handle variable sequence lengths without sacrificing GPU utilization, FlashInfer introduces a runtime scheduler that splits long KV-caches into deterministic chunks. Crucially, it pre-allocates a fixed-size workspace buffer, making it fully compatible with the static execution requirements of
CUDAGraph.
Datasets
The authors evaluated FlashInfer using both synthetic and real-world workloads:
- ShareGPT: A widely used dataset of real user-chatbot conversations to simulate production traffic.
- Variable Synthetic Workloads: Workloads with sequence lengths uniformly distributed between 512–2,048 tokens and 4,096–16,384 tokens to rigorously test input dynamism.
- MT-Bench: Used to evaluate end-to-end multi-turn conversation performance within the Streaming-LLM framework.
Key Insights and Ideas
- System Layouts as Sparse Matrices: The core conceptual leap is realizing that complex system-level memory management structures (like page tables) are mathematically equivalent to sparse matrices. This allows hardware-level tensor core optimizations to be applied directly to memory management.
- Decoupling Scheduling from Compute: Traditional kernels bake the scheduling logic directly into the GPU code. By moving scheduling to a lightweight CPU runtime plan, FlashInfer can dynamically rebalance workloads per generation step without needing to recompile or re-capture the GPU execution graph.
Key Results
FlashInfer was integrated into leading frameworks like SGLang, vLLM, and MLC-Engine, demonstrating substantial performance gains on NVIDIA A100 and H100 GPUs:
- End-to-End Serving: Integrated with SGLang, FlashInfer achieved a 29% to 69% reduction in inter-token latency (ITL) compared to SGLang's highly optimized, closed-source Triton backend.
- Long-Context Acceleration: For
Streaming-LLM(million-token inference), FlashInfer's fused RoPE-attention kernel delivered a 28% to 30% end-to-end latency reduction and improved memory bandwidth utilization by up to 3.7x. - Parallel Generation: When generating multiple tokens simultaneously in MLC-Engine, composable formats yielded up to a 17.4% speedup in ITL and a 22.8% speedup in Time-To-First-Token (TTFT).
- Fine-Grained Sparsity: On the
Questalgorithm (which prunes KV-caches at a fine granularity), FlashInfer outperformed PyTorch SDPA and FlexAttention by up to 20x.
Limitations and Future Work
- Forward Pass Only: FlashInfer currently only supports the forward pass for inference. Extending its customizable template design to support the backward pass for training and fine-tuning is left for future work.
- Hardware-Specific Constraints: Hopper’s Tensor Memory Accelerator (TMA) does not natively support non-affine sparse gathering, forcing FlashInfer to fall back to Ampere-style asynchronous copies for sparse layouts.
- Host-Side Overhead: In frameworks like vLLM, heavy Python-side array operations during integration caused minor regressions in certain dense workloads. The authors plan to port the runtime scheduler entirely to C++ to eliminate this bottleneck.
Summary updated