Overview
Introduced by Sarathi-Serve, Chunked Prefill is enabled by default in Mini-SGLang. This feature addresses the memory challenges of processing very long input sequences by breaking them into manageable pieces.Chunked Prefill is particularly important for long-context models where a single prefill batch could consume excessive GPU memory, starving the decode phase and reducing overall throughput.
The Problem: Memory Spikes
During the prefill phase, the system must:- Compute attention over the entire input sequence
- Store KV cache for all input tokens
- Store intermediate activations for the forward pass
- Memory spikes that exceed GPU capacity
- OOM errors that crash the serving system
- Starvation of decode requests as memory is monopolized by prefill
Traditional Approach
Chunked Prefill Approach
How It Works
Prefill Manager
ThePrefillManager handles chunking logic in /home/daytona/workspace/source/python/minisgl/scheduler/prefill.py:116:
Chunked Request Class
Chunked requests are marked with a special class to prevent premature completion:- Are not sampled for output tokens
- Stay in prefill queue until all chunks are processed
- Don’t enter decode phase prematurely
Scheduling Logic
The scheduler processes chunks across multiple iterations:Chunked requests are prioritized in the pending list to ensure all chunks of a request are processed before moving to new requests. This prevents head-of-line blocking.
Configuration
Max Prefill Length
Control the chunk size with--max-prefill-length:
max-prefill-length is also accessible via SchedulerConfig.max_extend_tokens.
The
token_budget in the prefill adder is set to max_extend_tokens and shared across all requests in a batch. This ensures the total number of tokens processed in a single iteration stays within limits.Choosing the Right Value
- Too large: May cause OOM on long sequences
- Too small: Excessive overhead from many small batches
- Recommended: Start with default (8192) and adjust based on:
- Available GPU memory
- Typical sequence lengths
- Model size and architecture
Benefits
Prevents OOM Errors
By limiting the maximum tokens processed in a single forward pass, chunked prefill prevents memory spikes that could crash the server. Without chunked prefill:Enables Long-Context Serving
Models with large context windows (32K, 128K, or even 1M tokens) can be served effectively:Better Resource Utilization
By controlling prefill memory usage, more memory remains available for:- Decode requests: Maintains responsiveness
- KV cache: Supports more concurrent requests
- Batch processing: Higher throughput
Memory Estimation
The prefill adder estimates memory requirements before processing:reserved_size accounts for in-flight decode requests to prevent starvation:
Integration with Radix Cache
Chunked Prefill works seamlessly with Radix Cache:- First chunk: Matches prefix in cache, only processes uncached portion
- Subsequent chunks: Continue from cached state
- After completion: Full sequence is cached for future reuse
Performance Considerations
Trade-offs
Benchmarking
For performance testing without chunked prefill:--max-prefill-length values to find the optimal setting for your workload.
Example: Processing a Long Document
Related Concepts
Radix Cache
Learn how KV cache reuse works with chunked prefill
Architecture
Understand the scheduler’s role in chunked prefill
Overlap Scheduling
See how chunking interacts with scheduling optimizations