Prepacking: A Simple Method for Fast Prefilling and Increased Throughput in Large Language Models

Siyan Zhao, Daniel Mingyi Israel, Guy Van den Broeck, Aditya Grover

Abstract

During inference for transformer-based large language models (LLM), prefilling is the computation of the key-value (KV) cache for input tokens in the prompt prior to autoregressive generation. For longer input prompt lengths, prefilling will incur a significant overhead on decoding time. In this work, we highlight the following pitfall of prefilling: for batches containing high-varying prompt lengths, significant computation is wasted by the standard practice of padding sequences to the maximum length. As LLMs increasingly support longer context lengths, variations in prompt lengths within a batch become more pronounced. To address this, we propose prepacking, a simple yet effective method to optimize prefilling computation. To avoid redundant computation on pad tokens, prepacking combines prompts of varying lengths into a sequence and packs multiple sequences into a compact batch using a bin-packing algorithm. It then modifies the attention mask and positional encoding to compute multiple prefilled KV-caches for multiple prompts within a single sequence. On standard curated dataset containing prompts with varying lengths, we obtain a significant speed and memory efficiency improvements as compared to the default padding-based prefilling computation within Huggingface across a range of base model configurations and inference serving scenarios.

Introduction

Large Language Model (LLM) inference typically proceeds in two distinct phases: prefilling and decoding. Prefilling involves processing the entire input prompt to generate the initial Key-Value (KV) cache, while decoding generates tokens autoregressively. A significant bottleneck in current inference systems is the "padding tax." Standard libraries, such as Hugging Face, handle batches of requests by padding all sequences to the length of the longest prompt in the batch.

Because Transformer computation scales quadratically with sequence length, this practice leads to massive waste. For example, in a batch where one prompt is 10 tokens and another is 500, the system processes 490 "empty" tokens for the shorter request. As context windows expand to millions of tokens, the variance in prompt lengths increases, making padding-based prefilling increasingly unsustainable.

Methodology: The Prepacking Approach

Prepacking is a high-level, PyTorch-based optimization that eliminates redundant computation on pad tokens. The method reorganizes data through three primary mechanisms:

  1. Bin-Packing: Using a "First-Fit Decreasing" algorithm, multiple short prompts are clustered into a single sequence (a "bin") of a fixed maximum length. This maximizes the density of meaningful tokens per batch.
  2. Independent Masking: To prevent cross-contamination between unrelated prompts within the same packed sequence, the attention mask is modified. This "independent mask" ensures tokens only attend to others within their specific prompt, creating logical boundaries within a single tensor.
  3. Restart Positional Encoding: The positional counter is reset to zero at the beginning of each new prompt in a packed sequence, preserving the semantic structure required by the model.
Overview of the Prepacking method, showing how multiple prompts are packed into a single sequence with independent masking.

After the prefilling pass, the resulting KV caches are "unpacked," allowing the model to transition to standard individual decoding for each prompt.

Experimental Setup

The researchers evaluated Prepacking across six diverse datasets—MMLU, SamSum, Alpaca, Wikitext (256 & 512), and Anthropic HH RLHF—to simulate varied user traffic. Testing was conducted on several open-source models, including Llama 2 (1.3B, 7B, and 13B) and Mistral 7B.

Results and Discussion

The empirical results demonstrate that the "padding tax" is a substantial burden. In the MMLU dataset, the padding ratio was reduced from 0.61 to 0.11 through prepacking. Contrary to the assumption that GPUs handle padding "for free" via parallelization, the study shows that increased batch sizes lead to latency spikes due to memory bandwidth and synchronization overhead.

Performance Gains

  • Speed: Prepacking achieved up to a 6x speedup in Time-to-First-Token (TTFT) compared to standard padding-based implementations.
  • Throughput: By optimizing memory usage, the method enabled batch sizes up to 16x larger before encountering Out-of-Memory (OOM) errors.
  • Synergy: Prepacking effectively complements FlashAttention 2. While FlashAttention 2 can underperform on sparse, heavily padded batches, Prepacking provides the dense data necessary for it to achieve peak performance.
Comparison of average prefilling time and Time-to-First-Token (TTFT) across various datasets and models, demonstrating significant speedups with Prepacking.

Conclusion and Future Work

Prepacking offers a simple, hardware-agnostic solution to a major inefficiency in LLM serving. By implementing these optimizations in pure PyTorch, the authors provide an accessible tool for developers to increase throughput and reduce latency. Future work includes extending this packing logic into the decoding phase and exploring hardware-specific CUDA kernels to further minimize the minimal (2.1%) bookkeeping overhead.

Summary updated

BibTeX
@inproceedings{zhao2025prepacking,
  author = {Siyan Zhao and Daniel Mingyi Israel and Guy Van den Broeck and Aditya Grover},
  title = {{Prepacking: A Simple Method for Fast Prefilling and Increased Throughput in Large Language Models}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2025},
  url = {https://openreview.net/forum?id=LBVD4krAq2},
}
Figure