Size HybridEngine rollout KV caches from local prefill shapes under AutoTP #9008
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #9008"
general
weight: 0.35
jinyouzhi
Summary
Size HybridEngine rollout KV caches from each rank's own prefill KV tensors, not from the global model config. Under AutoTP, each rank holds only its share of the KV heads. Today the cache is allocated for the full model's KV-head count, so TP>1 rollout fails on all three decode paths:
| Path | TP>1 failure today | Change |
| --- | --- | --- |
| Plain graph (
_generate_graph) | Passing batch size, device and dtype makesDeepSpeedStaticCacheinitialize frommodule.configup front, so the lazy per-layer setup never runs. The prefill copy then fails with4 vs 2heads, or broadcasts silently when the rank has 1 local head. | Drop those up-front arguments, so the existing lazy setup sizes each layer from the prefill tensors before capture. || Continuous eager (
_generate_continuous) | The same up-front setup from config fails on the first prefill copy (4 vs 2,2 vs 8). | Create the cache lazily._continuous_cache_layerinitializes each layer from the prefill tensors, sized to the slot count. The slot count is the decode batch capacity, not the prefill batch. || Continuous graph | The graph state cache is initialized from config, and capture happens before any prefill. | Create the cache lazily and run one decode forward on the current stream before warmup, so every layer is initialized with local shapes. Without this, those tensors would be allocated on the warmup side stream. |
lazy_initializationgains an optionalmax_batch_size, which the continuous path uses because its capacity is the slot count.cache_memory_bytesis now computed after decode, once the layers exist. No model config is changed and no TP/world group is consulted, so TP×DP layouts work with no extra logic.Related work / scope
get_query_offset), which is still open, so the continuous eager HF runs below apply #8852 to both baseline and fixed trees. The new unit tests use a local model and do not depend on #8852.torch.distributed.destroy_process_group()hang inProcessGroupNCCL.shutdown()at exit. Releasing the rollout (del+gc.collect()+synchronize()) before destroying avoids it. Before this PR, TP>1 continuous graph could not run, so the hang was not reachable. A release/close API is left for a follow-up.Validation
Hardware/software: 8× NVIDIA L20, driver 580.173.02, CUDA runtime 13.0, torch 2.14.1+cu130, Transformers 5.17.0, Python 3.10.22. Baseline:
d8241a9ba266405e1a58fadf19e24b2370f6d7fa.The new tests write head-dependent cache contents, compare tokens with an independent oracle, and count real graph replays:
| Local / config KV heads | Plain graph | Continuous eager | Continuous graph |
| --- | --- | --- | --- |
| 4 / 4 | pass → pass | pass → pass | pass → pass |
| 2 / 4 | fail → pass | fail → pass | fail → pass |
| 1 / 4 | fail → pass | fail → pass | fail → pass |
Multi-GPU tiny Llama, continuous path. 2 layers, 4 KV heads, fp32, 4 requests per rank, 2 slots. Tokens were compared with plain eager generation on every rank.
| Layout | Continuous graph (before → after) | Continuous eager + #8852 (before → after) |
| --- | --- | --- |
| TP1, world 1 | pass → pass | pass → pass |
| TP2, world 2 | shape error → pass |
4 vs 2error → pass || TP4, world 4 | shape error → pass |
2 vs 8error → pass || TP2×DP4, world 8 | shape error → pass |
4 vs 2error → pass || TP4×DP2, world 8 | not run → pass |
2 vs 8error → pass |Plain graph with real models, with #9004 applied to both trees:
| Model / precision | Layouts | Result |
| --- | --- | --- |
| Tiny Llama / fp32 | TP1×DP1, TP1×DP4, TP2×DP2, TP2×DP4 | All pass; graph tokens and masks equal native eager generation |
| Qwen3-8B / bf16 | TP2×DP4 | 4 prompts × 16 new tokens; graph tokens and masks equal native eager generation |
On the plain-graph runs, each rank made three generation calls: 3 captures and 45 replays, and repeated graph outputs matched. The baseline TP2×DP4 Llama run failed at the KV copy (
4 vs 2). The Qwen3 graph calls took 0.464 / 0.439 s, including prefill and capture, with 8.25 GiB max allocated per GPU. These numbers come from correctness runs; no speedup is claimed.Distributed AutoTP integration test (
TestAutoTPContinuousRollout, world size 2): real AutoTP viadeepspeed.initialize(autotp_size=2) on a tiny Llama, 3 requests on 2 continuous slots, tokens compared withgenerate()on the same sharded model. On 2× NVIDIA H20, torch 2.9.0a0+50eac811a6.nv25.09, Transformers 4.57.6, Python 3.12.3:Intel XPU (1× Arc Pro B70, torch 2.14.1+xpu, Transformers 5.22.1, Intel compute runtime 26.27.39122.11, Level Zero loader 1.28.2). Master has no XPU graph backend, so the new graph tests skip there. On top of #9007 (XPU graph capture):
This clean-exit torch 2.14.1 rerun confirms all nine new tests pass; the same tests against #9007 without this PR give 6 failed / 3 passed (exit 1). The earlier torch 2.15.0+xpu run printed passing tests but aborts during interpreter teardown on this host (exit 134). A minimal XPU tensor program reproduces that abort without DeepSpeed, so that earlier run is not counted as a clean test pass. The earlier master-based tree reported 52 passed / 10 skipped, with graph coverage skipped as expected.
The opt-in
test_continuous_graph_matches_pretrained_model_when_enabled(only runs whenDEEPSPEED_PRETRAINED_ROLLOUT_MODELis set) fails the same way on master and on this PR with Transformers 5.17:'DeepSpeedStaticCache' object has no attribute 'get_query_offset', which #8852 addresses.Validated: ZeRO-0, no offload, fp32/bf16, AutoTP with data-parallel replicas on CUDA, plain and continuous decode, eager and graph; single-device graph tests on XPU with #9007. Not validated: a full training-to-rollout cycle, ZeRO-1/2/3, offload, pipeline/expert/sequence parallelism, older HF preallocated cache APIs, and multi-device XPU graph. No claim is made about training convergence or performance.
AI assistance: GitHub Copilot helped with the implementation, the tests and the validation. I reviewed every changed line before signing off.