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

Siyan Zhao, Daniel 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, potentially up to 10 million tokens, 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 datasets containing prompts with varying lengths, we obtain 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

In the current landscape of Large Language Model (LLM) inference, the process is divided into two primary stages: prefilling and generation. Prefilling involves processing the input prompt to generate a Key-Value (KV) cache, while generation produces new tokens autoregressively. Because Transformers exhibit quadratic runtime complexity relative to sequence length, prefilling is a major computational bottleneck.

The industry standard for batching requests, often referred to as "Full Batching," pads all sequences in a batch to match the length of the longest prompt. For example, a batch with one 1,000-token prompt and several 10-token prompts would see the shorter prompts padded with 990 meaningless tokens. This "padding tax" results in wasted GPU cycles and memory, a problem that scales poorly as LLMs move toward supporting context windows of millions of tokens.

The Prepacking Method

To address this inefficiency, we introduce Prepacking, a software-level optimization that eliminates redundant computation on pad tokens through a three-step process:

  1. Bin-Packing: We employ a "First-Fit Decreasing" heuristic to pack multiple prompts of varying lengths into a single sequence of a fixed maximum length. This significantly reduces the total number of sequences (bins) the GPU must process.
  2. Independent Masking: To maintain the independence of prompts within a single packed sequence, we modify the 2D attention mask. This ensures that each prompt only attends to its own tokens and cannot "see" other prompts in the same pack.
  3. Restart Positional Encoding: We reset positional indices to zero at the start of every new prompt within a packed sequence. This preserves the relative positional information necessary for the model to correctly interpret each individual query. This is possible because Transformers are largely permutation-equivariant, allowing for restarted encodings without confusing the model.
The Prepacking process: prompts of varying lengths are packed into compact sequences, with attention masks modified to prevent cross-prompt interaction.

This method requires no changes to the underlying model weights and can be implemented directly in standard frameworks like PyTorch.

Experimental Evaluation

We evaluated Prepacking across six diverse datasets—including MMLU, SamSum, Alpaca, Wikitext, and Anthropic HH RLHF—to simulate a variety of real-world traffic patterns. Tests were conducted on models ranging from 1.3B to 13B parameters, such as Llama 2 and Mistral 7B.

Key Results

  • Prefilling Speedups: Prepacking achieved a 6x speedup for Llama2-1.3B and a 4.5x speedup for Llama2-7B compared to standard HuggingFace batching.
  • Throughput and Memory: On an NVIDIA A6000 GPU, Prepacking enabled batch sizes up to 16x larger than standard methods before reaching memory limits.
  • Reduced Latency Variance: By minimizing padding overhead, the time-to-first-token (TTFT) became more predictable, showing significantly lower standard deviations across runs.
  • Generation Phase Gains: Preliminary tests indicate the concept also benefits the generation phase, yielding 56% less peak GPU memory usage and 35% faster generation times.

Discussion and Conclusion

Our analysis confirms that padding is a significant "silent killer" of throughput in LLM inference. The performance gains from Prepacking are highly correlated with the "Max Absolute Deviation" of prompt lengths within a batch; the more diverse the requests, the more computation is recovered.

While this study primarily focuses on the prefilling stage, the results suggest that independent masking and positional restarts are powerful tools for optimizing the entire inference pipeline. Future work will explore full-scale implementations for autoregressive decoding and further optimization of the packing algorithm for ultra-high-traffic, low-latency environments.

Summary updated

BibTeX
@inproceedings{zhao2025prepacking,
  author = {Siyan Zhao and Daniel 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