← Back to Pull requests

Size HybridEngine rollout KV caches from local prefill shapes under AutoTP #9008

Closed
unknown closed this pull request without merging on Nov 11, 2026 09:20 UTC
View on GitHub
unknown unknown commented on Nov 04, 2026 12:50 UTC
Author

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 makes DeepSpeedStaticCache initialize from module.config up front, so the lazy per-layer setup never runs. The prefill copy then fails with 4 vs 2 heads, 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_layer initializes 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_initialization gains an optional max_batch_size, which the continuous path uses because its capacity is the slot count. cache_memory_bytes is 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

  • #9000 / #9004 fix attention-mask construction during capture, not KV shape allocation. This PR is independent of them. For real Hugging Face model validation, #9004's runtime hunk was applied to both the baseline and the fixed tree.

  • #8602 divides cache heads by the global world size, which breaks TP×DP (for example TP2×DP4). This fix reads the tensors directly instead of inferring the TP group.

  • On Transformers 5.17, continuous eager also needs #8852 (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.

  • Known limitation, not changed here: a persistent continuous graph (#8732) that contains NCCL ops makes torch.distributed.destroy_process_group() hang in ProcessGroupNCCL.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.

    CUDA_VISIBLE_DEVICES=0 python -m pytest --forked tests/unit/v1/test_hybrid_engine_rollout_v1.py \
      -k "static_graph_uses_prefill_kv_shape or continuous_generation_uses_local_kv_shape"
    # baseline: 6 failed, 3 passed; this PR: 9 passed
    
    CUDA_VISIBLE_DEVICES=0 python -m pytest --forked tests/unit/v1/test_hybrid_engine_rollout_v1.py \
      tests/unit/runtime/rollout/test_hybrid_engine_rollout.py tests/unit/utils/test_static_cache.py
    # baseline: 6 failed, 64 passed, 1 skipped; this PR: 70 passed, 1 skipped
    

    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 2 error → pass |
    | TP4, world 4 | shape error → pass | 2 vs 8 error → pass |
    | TP2×DP4, world 8 | shape error → pass | 4 vs 2 error → pass |
    | TP4×DP2, world 8 | not run → pass | 2 vs 8 error → 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 via deepspeed.initialize (autotp_size=2) on a tiny Llama, 3 requests on 2 continuous slots, tokens compared with generate() on the same sharded model. On 2× NVIDIA H20, torch 2.9.0a0+50eac811a6.nv25.09, Transformers 4.57.6, Python 3.12.3:

    CUDA_VISIBLE_DEVICES=0,1 python -m pytest tests/unit/v1/test_hybrid_engine_rollout_v1.py -k TestAutoTPContinuousRollout
    # baseline (rollout + static cache from d8241a9): 2 failed (eager `4 vs 2`, graph `[2, 2, 16]` vs `[2, 4, 16]`); this PR: 2 passed
    

    DS_ACCELERATOR=cpu python -m pytest tests/unit/runtime/rollout/test_hybrid_engine_rollout.py \
      tests/unit/utils/test_static_cache.py tests/unit/v1/test_hybrid_engine_rollout_v1.py
    # 60 passed, 11 skipped; Python 3.12.3 / torch 2.14.0.dev20260804+xpu
    
    pre-commit run --files deepspeed/runtime/rollout/hybrid_engine_rollout.py \
      deepspeed/utils/static_cache.py tests/unit/v1/test_hybrid_engine_rollout_v1.py
    # passed
    

    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):

    DS_ACCELERATOR=xpu python -m pytest tests/unit/v1/test_hybrid_engine_rollout_v1.py \
      -k "static_graph_uses_prefill_kv_shape or continuous_generation_uses_local_kv_shape"
    # #9007 alone: 6 failed, 3 passed; #9007 + this PR: 9 passed
    
    DS_ACCELERATOR=xpu python -m pytest tests/unit/v1/test_hybrid_engine_rollout_v1.py \
      tests/unit/runtime/rollout/test_hybrid_engine_rollout.py
    # #9007 + this PR: 60 passed, 3 skipped; process exit 0
    

    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 when DEEPSPEED_PRETRAINED_ROLLOUT_MODEL is 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.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jinyouzhi Review duration: 9870m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%