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

Siyan Zhao, Daniel Israel, Guy Van den Broeck, Aditya Grover
4/15/2024
cs.LGcs.AIcs.CL

No official code found. Best implementation: siyan-zhao/prepacking (62 stars, last commit 2024-10-11).

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 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.

View on arXivView PDF

Code Implementations(1)

The source code of our work "Prepacking: A Simple Method for Fast Prefilling and Increased Throughput in Large Language Models" [AISTATS 2025]

627Python, Jupyter NotebookApr 15, 20242 years ago

Similar Papers

Cite this paper

@article{zhao2024prepacking,
  title  = {Prepacking: A Simple Method for Fast Prefilling and Increased Throughput in Large Language Models},
  author = {Siyan Zhao and Daniel Israel and Guy Van den Broeck and Aditya Grover},
  year   = {2024},
  eprint = {2404.09529},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2404.09529v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2404.09529.svg)](https://codeofpaper.com/paper/2404.09529)

Discussion