Skip to content

Commit 0eacef7

Browse files
authored
docs: add disagg tuning guide (ai-dynamo#413)
1 parent 68efb48 commit 0eacef7

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

docs/guides/disagg_perf_tuning.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!--
2+
SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
# Dynamo Disaggregation: Performance Tuning
19+
20+
Disaggregation gains performance by separating the prefill and decode into different engines to reduce interferences between the two. However, performant disaggregation requires careful tuning of the inference parameters. Specifically, there are three sets of parameters that needs to be tuned:
21+
22+
1. Engine knobs (e.g. parallelization mapping, maximum number of tokens, etc.).
23+
1. Disaggregated router knobs.
24+
1. Number of prefill and decode engines.
25+
26+
This guide will walk you through the process of tuning these parameters.
27+
28+
## Engine Knobs
29+
30+
The most important engine knob to tune is the parallelization mapping. For most dense models, the best setting is to use TP within node and PP across nodes. For example, for llama 405b w8a8 on H100, TP8 on a single node or TP8PP2 on two nodes is usually the best choice. The next thing to decide is how many numbers of GPU to serve the model. Typically, the number of GPUs vs the performance follows the following pattern:
31+
32+
Number of GPUs | Performance
33+
--- | ---
34+
Cannot hold weights in VRAM | OOM
35+
(Barely hold weights in VRAM) | (KV cache is too small to maintain large enough sequence length or reasonable batch size)
36+
Minimum number with fair amount of KV cache | Best overall throughput/GPU, worst latency/user
37+
Between minimum and maximum | Tradeoff between throughput/GPU and latency/user
38+
Maximum number limited by communication scalability | Worst overall throughput/GPU, best latency/user
39+
More than maximum | Communication overhead dominates, poor performance
40+
41+
Note that for decode-only engines, sometimes larger number of GPUs has to larger kv cache per GPU and more decoding requests running in parallel, which leads to both better throughput/GPU and better latency/user. For example, for llama3.3 70b NVFP4 quantization on B200 in vllm with 0.9 free GPU memory fraction:
42+
43+
TP Size | KV Cache Size (GB) | KV Cache per GPU (GB) | Per GPU Improvement over TP1
44+
--- | --- | --- | ---
45+
1 | 113 | 113 | 1.00x
46+
2 | 269 | 135 | 1.19x
47+
4 | 578 | 144 | 1.28x
48+
49+
The best number of GPUs to use in the prefill and decode engines can be determined by running a few fixed isl/osl/concurrency test using [GenAI-Perf](https://github.com/triton-inference-server/perf_analyzer/tree/main/genai-perf) and compare with the SLA. GenAI-Perf is pre-installed in the dynamo container.
50+
51+
Besides the parallelization mapping, other common knobs to tune are maximum batch size, maximum number of tokens, and block size. For prefill engines, usually a small batch size and large max_num_token is preferred. For decode engines, usually a large batch size and medium max_num_token is preferred. More details on tuning the max_num_token and max_batch_size will be covered in the next section.
52+
53+
For block size, if the block size is too small, it leads to small memory chunks in the P->D KV cache transfer and poor performance. Too small block size also leads to memory fragmentation in the attention calculation, but the impact is usually insignificant. If the block size is too large, it leads to low prefix cache hit ratio. For most dense models, we find block size 128 is a good choice.
54+
55+
## Disaggregated Router
56+
57+
Disaggregated router decides whether to prefill a request in the remote prefill engine or locally in the decode engine using chunked prefill. For most frameworks, when chunked prefill is enabled and one forward iteration gets a mixture of prefilling and decoding request, three kernels will be launched:
58+
1. The attention kernel for context tokens (context_fmha kernel in trtllm).
59+
2. The attention kernel for decode tokens (xqa kernel in trtllm).
60+
3. Dense kernel for the combined active tokens in prefills and decodes.
61+
62+
### Prefill Engine
63+
64+
In the prefill engine, the best strategy is to operate at the smallest batch size that saturates the GPUs so that the average TTFT is minimized. For example, for llama3.3 70b NVFP4 quantization on B200 TP1 in vllm, the below figure shows the prefill time with different isl (prefix caching is turned off):
65+
66+
![Prefill Time](../images/prefill_time.png)
67+
68+
For isl less than 1000, the prefill efficiency is low because the GPU is not fully saturated. For isl larger than 4000, the prefill time per token increases because the attention takes longer to compute with a longer history.
69+
70+
Currently, prefill engines in Dynamo operate at a batch size of 1. To make sure prefill engine is saturated, users can set `max-local-prefill-length` to the saturation point to make sure prefill engine is optimal.
71+
72+
73+
### Decode Engine
74+
75+
In the decode engine, maximum batch size and maximum number of tokens affects the size of intermediate tensors. With a larger batch size and number of tokens, the size of intermediate tensors increases and the size of KV cache decreases. Trtllm has a good [summary](https://nvidia.github.io/TensorRT-LLM/reference/memory.html) on the memory footprint where similar ideas also applies to other llm frameworks.
76+
77+
With chunked prefill enabled, the maximum number of tokens controls the longest prefill that can be piggybacked to decode and control the ITL. For the same prefill requests, a large maximum number of tokens leads to fewer but longer stalls in the generation, while a small maximum number of tokens leads to more but shorter stalls in the generation. However, chunked prefill is currently not supported in Dynamo (vllm backend). Hence, the current best strategy is to set the maximum batch size to the optimized kv cache size and set the maximum number of tokens to the maximum local prefill length + maximum batch size (since one decode request has one active token).
78+
79+
## Number of Prefill and Decode Engines
80+
81+
The best dynamo knob choices depends on the operating condition of the model. Based on the load, we define three operating conditions:
82+
1. **Low load**: The endpoint is hit by a single user (single-stream) most of the time.
83+
2. **Medium load**: The endpoint is hit by multiple users, but the KV cache of the decode engines is never fully utilized.
84+
3. **High load**: The endpoint is hit by multiple users and the requests are queued up due to no available KV cache in the decode engines.
85+
86+
At low load, disaggregation would not benefit much as prefill and decode are usually computed separately. It is usually better to use a single monolithic engine.
87+
88+
At medium load, disaggregation allows better ITL compared with prefill-prioritized and chunked prefill engines and better TTFT compared with chunked prefill engine and decode-only engine for each user. Dynamo users can adjust the number of prefill and decode engines based on TTFT and ITL SLA.
89+
90+
At high load where KV cache capacity is the bottleneck, disaggregation has the following effect on the KV cache usage in the decode engines:
91+
* Increase the total amount of KV cache:
92+
* Being able to use larger TP in decode engines leads to more KV cache per GPU and higher prefix cache hit rate.
93+
* When the requests is prefilled remotely, the decode engine does not need to maintain its KV cache (currently not implemented in Dynamo).
94+
* Lower ITL reduces the decode time and allow the same amount of KV cache to serve more requests.
95+
* Decrease the total amount of KV cache:
96+
* Some GPUs are configured as prefill engines whose KV cache is not used in the decode phase.
97+
98+
Since Dynamo current allocates the KV blocks immediately when the decode engine get the requests, it is advisable to use as few decode engines as possible (even no prefill engine) to maximize the KV cache utilization. To prevent queueing at prefill engines, users can set a large `max-local-prefill-length` and piggyback more prefill requests at decode engines.

docs/images/prefill_time.png

66 KB
Loading

0 commit comments

Comments
 (0)