A Queueing Theoretic Perspective on Low-Latency LLM Inference with Variable Token Length

Yuqing Yang, Lei Jiao, Yuedong Xu

Introduction

In the deployment of Large Language Models (LLMs), request variability presents a significant challenge for system efficiency. Unlike traditional web services, LLM inference is autoregressive; the time required to fulfill a request is directly proportional to the number of tokens generated. A user requesting a brief summary and another requesting a long-form essay impose vastly different computational loads. This variability, if unmanaged, leads to the "head-of-line blocking" problem in First-Come-First-Serve (FCFS) systems, where short requests are delayed by a few "heavy" tasks. This paper introduces a mathematical framework based on queueing theory to optimize LLM inference latency through strategic clipping and elastic batching.

Background: LLM Architecture and Inference

Modern LLMs like LLaMA-2 utilize a transformer-based architecture that relies on Key-Value (KV) caching to avoid redundant computations during the autoregressive decoding phase. As shown in Figure 2, the inference process is divided into a "prefill" stage (processing the input prompt) and a "decode" stage (generating output tokens one by one).

LLaMA-2 architecture and KV cache mechanism for prefill and decode stages.

The decode stage is the primary source of variable latency, as the total service time depends on the stochastic length of the generated sequence. Traditional batching methods often pad shorter sequences to match the longest one in a batch, leading to significant computational waste and increased latency for all requests within that batch.

Methodology

The authors bridge systems engineering and probability theory by modeling LLM inference as an M/G/1 queueing system. This model assumes Poisson arrivals (M) and a general service time distribution (G) determined by the output token length. Key components of the methodology include:

  • Max-Token Clipping Optimization: An optimization problem is formulated to balance user utility (the value of a complete response) against the cost of waiting. By identifying an optimal cutoff point for the longest responses, the system can significantly reduce average wait times.
  • Bulk Queue and Elastic Batching: To analyze batching, the authors use a bulk queue model. They specifically advocate for "elastic batching" (iteration-level scheduling), where finished requests are released immediately, effectively shrinking the batch size dynamically and minimizing the lower bound of queueing delay.

Empirical Validation and Results

The mathematical models were validated using real-world datasets like Instruction-in-Wild and models including LLaMA-2-7b-chat and ChatGLM3-6b. A critical empirical finding is the linear relationship between the number of output tokens and inference latency, as illustrated in Figure 6(a).

Inference latency vs. output tokens (a) and throughput/latency vs. batch size (b).

Key Findings:

  • Latency Reduction: For LLaMA-2-7b-chat, implementing an optimal max token limit of 1600 reduced the mean queueing delay by 58.93% (from 56s to 23s).
  • The Clipping Paradox: Enforcing limits on only the top 1-5% of the longest requests—the "heavy tail"—drastically stabilizes the queue with minimal impact on overall response quality.
  • Batching Efficiency: Figure 6(b) demonstrates how throughput increases with batch size, but so does the latency for the first output token. Elastic batching was shown to consistently outperform traditional dynamic batching by eliminating intra-batch waiting.

Discussion and Future Work

While this framework provides a robust tool for capacity planning, it primarily focuses on the decode stage. The authors note that the "prefill" stage also contributes to latency, though its relationship with input length is less linear due to parallel processing. Future research will explore extending these queueing models to multi-node, distributed inference environments where load balancing across clusters adds another layer of complexity. Furthermore, the hardware-specific constants used in these models would require recalibration for different GPU architectures or memory bandwidths.

Summary updated

Abstract

Large language models (LLMs) propel the prosperity of interactive AI applications showcased by ChatGPT that demand timely response of inference services. However, LLM inference is computation intensive and memory intensive, and improper parameter configuration at LLM platforms may exacerbate the inference time. In this paper, we analyze the impact of LLM output token distribution on the inference queueing delay, where the max-token clipping and the batched inference are considered. By formulating an M/G/1 model, we observe that enforcing a maximum output token limit on a very small fraction of inference requests can significantly reduce the queueing delay, and our model facilitates the selection of the optimal limit. For the batch inference, we model the service process as a bulk queue in which the batch processing time is affected by the batch size and the maximum token size inside this batch jointly. The queueing delays of the batching of all buffered requests (dynamic batching), the batching of constant number of requests (fixed batching), and the batching without intra-batch waiting (elastic batching) are derived. Experimental results show that our mathematical models coincide with the event-driven simulations well.
BibTeX
@inproceedings{yang2024queueing,
  author = {Yuqing Yang and Lei Jiao and Yuedong Xu},
  title = {{A Queueing Theoretic Perspective on Low-Latency LLM Inference with Variable Token Length}},
  booktitle = {WiOpt},
  year = {2024},
  url = {https://openreview.net/forum?id=9HMShNHkiM},
}
Figure