Serving
How to benchmark the trained model with Megatron's dynamic inference engine, in a 512-request mode on 8 GPUs and a one-request mode on one GPU, and how to size the KV cache.
What you will do
- Build a fixed prompt file from GSM8K test questions.
- Benchmark Megatron’s dynamic inference engine twice: 512 requests at once on 8 GPUs, then one request at a time on one GPU.
- Read the time to first token, the time per output token, the throughput, and the engine’s KV-cache summary.
Three terms. Time to first token (TTFT) is the wait until the first answer token, including any queueing. Time per output token (TPOT) is the average gap between later tokens. The KV cache stores every past token’s keys and values in each layer, so the model does not recompute them.
Hardware and time
The concurrent mode uses all 8 GPUs with expert parallelism 8. The one-request mode loads the whole model on one GPU with expert parallelism 1; in bf16 it is about 14 GB. Our two runs took 625 s and 159 s.
Steps
python -I code/scripts/make_serving_prompts.py $CURRICULUM_DIR/gsm8k_test.jsonl /workspace/serve_prompts.jsonl 512
export SERVE_CKPT=/workspace/ckpt/gspo SERVE_PROMPTS=/workspace/serve_prompts.jsonl
SERVE_MODE=concurrent run serve_concurrent bash $M/serve_bench.sh # 8 GPUs, 512 requests at once
SERVE_MODE=batch1 run serve_batch1 bash $M/serve_bench.sh # 1 GPU, 16 requests one at a time, warm-up pass
python -I code/scripts/serving_report.py /workspace/runs/serve_*/serve_*.json
code/scripts/megatron/serve_bench.sh makes the runs comparable:
- greedy decoding (
--top_k 1), with bf16 weights and KV cache; - every answer exactly 256 tokens long, because
--termination-id -1turns the end token off; - the engine’s own limits: at most 512 requests and 16,384 tokens in flight.
The concurrent mode runs Megatron’s offline_inference.py with its coordinator, which expert parallelism needs. The
one-request mode runs gpt_dynamic_inference.py twice over 16 prompts; the first pass is a warm-up.
Both run in eager mode, as the RL rollouts did; the CUDA-graph variant is not tested with expert parallelism here.
If the one-request mode cannot load the checkpoint at expert parallelism 1, take its latency from
SERVE_MODE=concurrent SERVE_REQUESTS=1 instead.
Size the KV cache
where \(b_{\text{KV}}\) is the cache’s bytes per token, \(n_{\text{L}}\) the layers, 2 the key and the value, \(n_{\text{KV}}\) the key-value heads, \(d_{\text{head}}\) the head dimension, and \(b_{\text{value}}\) the bytes per number (2 in bf16).
In words: every token stores one key and one value vector per key-value head in every layer.
Ours: \(16 \times 2 \times 4 \times 64 \times 2 = 16{,}384\) bytes, 16 KiB per token. Grouped-query attention keeps this small: 32 query heads share 4 key-value heads. The engine stores the cache in blocks of 256 tokens, \(256 \times 16\ \text{KiB} = 4\ \text{MiB}\) each.
On our node it had 5,119 usable blocks, room for 2,559 requests of two blocks each. The batch was capped at the configured 512 requests, far below that.
The benchmark’s report computes TPOT from each request’s timings:
where \(t_{\text{latency}}\) is the request’s total time, \(t_{\text{TTFT}}\) its time to first token, and \(n_{\text{tokens}}\) the tokens it generated.
In words: the decode time after the first token, divided by the tokens it produced.
What to expect
| Mode | Requests | Time to first token | Time per output token | Throughput |
|---|---|---|---|---|
| One at a time (1 GPU, expert parallel 1) | 16 | 149 ms (median) | 68.3 ms | 14.6 tokens/s |
| All at once (8 GPUs, expert parallel 8) | 512 | median 6.5 s, p90 37.4 s | median 271 ms | 1,731 tokens/s (216 per GPU) |
The 512 first tokens fell into two groups: 375 came at about 6.5 s and 137 at about 37.4 s. Yet all 512 requests finished together, at 75.7 s, after the same 259 engine steps. So the slow first tokens are not ordinary queueing; their cause is open.
- 68 ms per token at batch 1 is slow for a model with 1.22B active parameters. This path runs without CUDA graphs, so kernel launches dominate small batches.
- With 64 requests per GPU, the per-token time is the all-to-all and the experts’ grouped matrix multiplies at expert parallelism 8.
- On one H100, the shrunk rehearsal model gave a TTFT of 137 ms and a TPOT of 71.2 ms, and a measured KV cache of 1,024 bytes per token, the 1 KiB the formula gives for 2 layers and 2 KV heads.
These numbers are a baseline, not a tuned server. CUDA graphs for decoding, and serving through vLLM or SGLang, are the next steps; neither was done.
Check that it worked
-
tokens_minequalstokens_maxequals 256 in the report: every answer ran to the full length. -
The engine’s KV-cache summary in each log shows blocks of 256 tokens at 4 MiB, 16 KiB per token.
-
The concurrent run used all 8 GPUs, and the one-request run one GPU.
Common problems
| Symptom | Cause | Fix |
|---|---|---|
max_tokens (16384) must be >= max_requests (81916) at engine start |
without its own limits the engine admits as many requests as the KV buffer has blocks | set --inference-dynamic-batching-max-requests and --max-tokens (in serve_bench.sh) |
| answers of different lengths make runs incomparable | the model stops at its end token | --termination-id -1 for the benchmark only |
| the KV cache would be twice the expected size (found in the code) | the cache takes the parameter dtype | pass --bf16 |
| the one-request mode may fail to load the checkpoint (ours loaded) | the checkpoint was saved at expert parallelism 8 | use the concurrent mode with one request |
| a 4-bit ModelOpt checkpoint hangs | ModelOpt’s attention cannot run in this engine | serve the bf16 model; see 4-bit |
Read more
- Part 11: serving and the inference engine explains the engine, the KV cache and CUDA graphs.
- Code: code/scripts/megatron/serve_bench.sh, code/scripts/make_serving_prompts.py, code/scripts/serving_report.py.