2 Background
In this section, we describe the generation and serving procedures of typical LLMs and the iteration-level scheduling
used in LLM serving.
612
2.1 Transformer-Based Large Language Models
The task of language modeling is to model the probability
of a list of tokens (𝑥1, . . . , 𝑥𝑛). Since language has a natural
sequential ordering, it is common to factorize the joint probability over the whole sequence as the product of conditional
probabilities (a.k.a. autoregressive decomposition [3]):
𝑃 (𝑥) = 𝑃 (𝑥1) · 𝑃 (𝑥2 | 𝑥1) · · · 𝑃 (𝑥𝑛 | 𝑥1, . . . , 𝑥𝑛−1). (1)
Transformers [53] have become the de facto standard architecture for modeling the probability above at a large scale.
The most important component of a Transformer-based language model is its self-attention layers. For an input hidden
state sequence (𝑥1, . . . , 𝑥𝑛) ∈ R
𝑛×𝑑
, a self-attention layer
first applies linear transformations on each position 𝑖 to get
the query, key, and value vectors:
𝑞𝑖 = 𝑊𝑞𝑥𝑖
, 𝑘𝑖 = 𝑊𝑘𝑥𝑖
, 𝑣𝑖 = 𝑊𝑣𝑥𝑖
. (2)
Then, the self-attention layer computes the attention score
𝑎𝑖𝑗 by multiplying the query vector at one position with all
the key vectors before it and compute the output 𝑜𝑖 as the
weighted average over the value vectors:
𝑎𝑖𝑗 =
exp(𝑞
⊤
𝑖
𝑘𝑗 /
√
𝑑)
Í𝑖
𝑡=1
exp(𝑞
⊤
𝑖
𝑘𝑡 /
√
𝑑)
, 𝑜𝑖 =
∑︁
𝑖
𝑗=1
𝑎𝑖𝑗𝑣𝑗
. (3)
Besides the computation in Eq. 4, all other components
in the Transformer model, including the embedding layer,
feed-forward layer, layer normalization [2], residual connection [22], output logit computation, and the query, key, and
value transformation in Eq. 2, are all applied independently
position-wise in a form of 𝑦𝑖 = 𝑓 (𝑥𝑖).
2.2 LLM Service & Autoregressive Generation
Once trained, LLMs are often deployed as a conditional generation service (e.g., completion API [34] or chatbot [19, 35]).
A request to an LLM service provides a list of input prompt
tokens (𝑥1, . . . , 𝑥𝑛), and the LLM service generates a list of
output tokens (𝑥𝑛+1, . . . , 𝑥𝑛+𝑇 ) according to Eq. 1. We refer to
the concatenation of the prompt and output lists as sequence.
Due to the decomposition in Eq. 1, the LLM can only sample and generate new tokens one by one, and the generation
process of each new token depends on all the previous tokens
in that sequence, specifically their key and value vectors. In
this sequential generation process, the key and value vectors
of existing tokens are often cached for generating future
tokens, known as KV cache. Note that the KV cache of one
token depends on all its previous tokens. This means that the
KV cache of the same token appearing at different positions
in a sequence will be different.
Given a request prompt, the generation computation in
the LLM service can be decomposed into two phases:
The prompt phase takes the whole user prompt (𝑥1, . . . , 𝑥𝑛)
as input and computes the probability of the first new token 𝑃 (𝑥𝑛+1 | 𝑥1, . . . , 𝑥𝑛). During this process, also generates the key vectors 𝑘1, . . . , 𝑘𝑛 and value vectors 𝑣1, . . . , 𝑣𝑛.
Since prompt tokens 𝑥1, . . . , 𝑥𝑛 are all known, the computation of the prompt phase can be parallelized using matrixmatrix multiplication operations. Therefore, this phase can
efficiently use the parallelism inherent in GPUs.
The autoregressive generation phase generates the remaining new tokens sequentially. At iteration 𝑡, the model
takes one token 𝑥𝑛+𝑡 as input and computes the probability
𝑃 (𝑥𝑛+𝑡+1 | 𝑥1, . . . , 𝑥𝑛+𝑡) with the key vectors 𝑘1, . . . , 𝑘𝑛+𝑡 and
value vectors𝑣1, . . . , 𝑣𝑛+𝑡
. Note that the key and value vectors
at positions 1 to 𝑛 + 𝑡 − 1 are cached at previous iterations,
only the new key and value vector 𝑘𝑛+𝑡 and 𝑣𝑛+𝑡 are computed at this iteration. This phase completes either when the
sequence reaches a maximum length (specified by users or
limited by LLMs) or when an end-of-sequence () token
is emitted. The computation at different iterations cannot
be parallelized due to the data dependency and often uses
matrix-vector multiplication, which is less efficient. As a result, this phase severely underutilizes GPU computation and
becomes memory-bound, being responsible for most portion
of the latency of a single request.
2.3 Batching Techniques for LLMs
The compute utilization in serving LLMs can be improved
by batching multiple requests. Because the requests share
the same model weights, the overhead of moving weights is
amortized across the requests in a batch, and can be overwhelmed by the computational overhead when the batch
size is sufficiently large. However, batching the requests
to an LLM service is non-trivial for two reasons. First, the
requests may arrive at different times. A naive batching strategy would either make earlier requests wait for later ones
or delay the incoming requests until earlier ones finish, leading to significant queueing delays. Second, the requests may
have vastly different input and output lengths (Fig. 11). A
straightforward batching technique would pad the inputs
and outputs of the requests to equalize their lengths, wasting
GPU computation and memory.
To address this problem, fine-grained batching mechanisms, such as cellular batching [16] and iteration-level scheduling [60], have been proposed. Unlike traditional methods
that work at the request level, these techniques operate at
the iteration level. After each iteration, completed requests
are removed from the batch, and new ones are added. Therefore, a new request can be processed after waiting for a
single iteration, not waiting for the entire batch to complete.
Moreover, with special GPU kernels, these techniques eliminate the need to pad the inputs and outputs. By reducing
the queueing delay and the inefficiencies from padding, the
fine-grained batching mechanisms significantly increase the
throughput of LLM serving.3 Memory Challenges in LLM Serving
Although fine-grained batching reduces the waste of computing and enables requests to be batched in a more flexible
way, the number of requests that can be batched together is
still constrained by GPU memory capacity, particularly the
space allocated to store the KV cache. In other words, the
serving system’s throughput is memory-bound. Overcoming this memory-bound requires addressing the following
challenges in the memory management:
Large KV cache. The KV Cache size grows quickly with the
number of requests. As an example, for the 13B parameter
OPT model [62], the KV cache of a single token demands 800
KB of space, calculated as 2 (key and value vectors) × 5120
(hidden state size) × 40 (number of layers) × 2 (bytes per
FP16). Since OPT can generate sequences up to 2048 tokens,
the memory required to store the KV cache of one request
can be as much as 1.6 GB. Concurrent GPUs have memory
capacities in the tens of GBs. Even if all available memory
was allocated to KV cache, only a few tens of requests could
be accommodated. Moreover, inefficient memory management can further decrease the batch size, as shown in Fig. 2.
Additionally, given the current trends, the GPU’s computation speed grows faster than the memory capacity [17]. For
example, from NVIDIA A100 to H100, The FLOPS increases
by more than 2x, but the GPU memory stays at 80GB maximum. Therefore, we believe the memory will become an
increasingly significant bottleneck.
Complex decoding algorithms. LLM services offer a range
of decoding algorithms for users to select from, each with
varying implications for memory management complexity.
For example, when users request multiple random samples
from a single input prompt, a typical use case in program
suggestion [18], the KV cache of the prompt part, which
accounts for 12% of the total KV cache memory in our experiment (§6.3), can be shared to minimize memory usage.
On the other hand, the KV cache during the autoregressive
generation phase should remain unshared due to the different sample results and their dependence on context and
position. The extent of KV cache sharing depends on the
specific decoding algorithm employed. In more sophisticated
algorithms like beam search [49], different request beams
can share larger portions (up to 55% memory saving, see
§6.3) of their KV cache, and the sharing pattern evolves as
the decoding process advances.
Scheduling for unknown input & output lengths. The
requests to an LLM service exhibit variability in their input
and output lengths. This requires the memory management
system to accommodate a wide range of prompt lengths. In
addition, as the output length of a request grows at decoding,
the memory required for its KV cache also expands and may
exhaust available memory for incoming requests or ongoing
generation for existing prompts. The system needs to make
scheduling decisions, such as deleting or swapping out the
KV cache of some requests from GPU memory.
3.1 Memory Management in Existing Systems
Since most operators in current deep learning frameworks
[33, 39] require tensors to be stored in contiguous memory,
previous LLM serving systems [31, 60] also store the KV
cache of one request as a contiguous tensor across the different positions. Due to the unpredictable output lengths from
the LLM, they statically allocate a chunk of memory for a
request based on the request’s maximum possible sequence
length, irrespective of the actual input or eventual output
length of the request.
Fig. 3 illustrates two requests: request A with 2048 maximum possible sequence length and request B with a maximum of 512. The chunk pre-allocation scheme in existing
systems has three primary sources of memory wastes: reserved slots for future tokens, internal fragmentation due to
over-provisioning for potential maximum sequence lengths,
and external fragmentation from the memory allocator like
the buddy allocator. The external fragmentation will never
be used for generated tokens, which is known before serving
a request. Internal fragmentation also remains unused, but
this is only realized after a request has finished sampling.
They are both pure memory waste. Although the reserved
memory is eventually used, reserving this space for the entire request’s duration, especially when the reserved space
is large, occupies the space that could otherwise be used to
process other requests. We visualize the average percentage
of memory wastes in our experiments in Fig. 2, revealing
that the actual effective memory in previous systems can be
as low as 20.4%Although compaction [54] has been proposed as a potential solution to fragmentation, performing compaction in a
performance-sensitive LLM serving system is impractical
due to the massive KV cache. Even with compaction, the
pre-allocated chunk space for each request prevents memory
sharing specific to decoding algorithms in existing memory
management systems.