[Hybrid Engine] Enable graph capture for HybridEngine on non-CUDA accelerators #8933
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
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 #8933"
general
weight: 0.35
delock
Summary
The HybridEngine graph-capture paths (per-position decode graphs from #8271 and the continuous-rollout graphs from #8732) were gated on CUDA in several places: a device-name check, an input device-type check, and hard-coded "CUDA graph" naming. This PR replaces them with accelerator capability hooks so any backend that implements graph capture (
create_graph()returning a graph,capture_to_graph,replay_graph, andis_current_stream_capturing) takes the same code path as CUDA.Two accelerator-specific fixes ride along to make the NPU path actually work:
NPU inference attention switches to the graph-mode flash attention variant (
npu_fusion_attention_v3): the v1/v2 variants launch work on an internal secondary stream that CANN capture rejects atcapture_end(error 107025).The NPU inference shim grows its KV-cache table on demand:
layer_idis derived fromDeepSpeedSelfAttention.num_layers, a process-wide counter that only ever grows, so any process that builds a second engine would otherwise index out of bounds.Design
Capability gates instead of name gates.
validate_cuda_graph_supportbecomesvalidate_graph_capture_supportand rejects backends withget_accelerator().create_graph() is Noneinstead of comparingdevice_name()with"cuda". The continuous-rollout path uses the same capability check. Its capture guard (is_current_stream_capturing, used byDeepSpeedStaticCacheto skip host-side bounds checks while recording) already routes through the accelerator abstraction since #8732 (CUDA implementation, safeFalsedefault); this PR adds the NPU implementation.Naming. Internal names, docstrings, and error messages drop the "CUDA" prefix (
validate_graph_capture_support, "continuous graph capture", "per-position graph"). The config key is renamed tohybrid_engine.enable_graph_captureso the flag reads as a backend capability rather than a CUDA-specific feature, following the rename request in #8271's thread. The oldhybrid_engine.enable_cuda_graphkey still works as a deprecated alias: its value migrates to the new field with a warning (the existing DeepSpeed deprecated-field machinery), so existing config files keep working.NPU flash attention.
NPUInference._softmax_contextcallstorch_npu.npu_fusion_attention_v3, the graph-mode variant. The v1/v2 variants rejected capture with "capture model contains a stream that was not joined to the original stream" (107025); after the failed capture the fallback eager path then hit a secondary failure (561103) because host-side counters had already advanced. v3 captures cleanly in the BSH layout (the docs only promise BNSD), and both failure modes disappear together.Safety
The existing refusals are preserved and remain device-agnostic: ZeRO stage 3,
release_inference_cache,inference_tp_size > 1, and unpinned generation lengths all refuse graphs with a warning and fall back to eager, as do capture failures at runtime. The continuous-rollout path keeps its explicit rejections of aligned decode fronts, cache trimming, andflash_attention_2implementations.Results
Old container path (
#8271methodology)facebook/opt-1.3blocal weights, ZeRO-2, FP16, single Ascend NPU, 256-token prompt, 128 new tokens:| batch | ms / decode step | tok/s |
| ----: | ---------------: | ----: |
| 1 | 7.16 | 140 |
| 4 | 10.86 | 368 |
| 8 | 12.27 | 652 |
| 16 | 15.77 | 1015 |
| 32 | 22.37 | 1431 |
Throughput scales 10.2× over a 32× batch increase. Batch 8, averaged over 4 measured iterations of an RLHF-style loop (eval → generate → train → forward/backward/step):
| phase | eager | graph | speedup |
| ----------- | -------- | -------- | ------- |
| generate | 5436.0 ms | 1578.4 ms | 3.44× |
| train step | 797.7 ms | 797.7 ms | 1.00× |
| iteration | 6233.7 ms | 2376.1 ms | 2.62× |
Generated tokens are unchanged: 1024/1024 token agreement with the eager path (8 sequences × 128 tokens, same input).
Peak memory: 8.94 → 9.04 GiB (+0.10 GiB for 127 captured positions).
One-time capture cost: 8.9 s for the first generation; every later generation replays.
Continuous rollouts (
#8732methodology)gpt2local weights, FP16, batch 1, 32-token prompt, 32 new tokens, greedy, continuous batching on a single Ascend NPU, two warmup calls and five steady-state measurements:| Mode | Median latency | Throughput |
| ----------------- | -------------: | ---------: |
| Eager | 1060.44 ms | 30 tokens/s |
| Graph reuse | 177.59 ms | 180 tokens/s |
That is a 5.97× steady-state speedup, with the first call (including capture) at 395.5 ms reported separately. Two different prompts (seeds 1234/1235) produced token-identical eager and graph outputs (32/32 tokens each) while reusing one capture.
Tests
tests/unit/hybrid_engine/test_he_graph.py(renamed fromtest_he_cuda_graph.py):16 CPU-only tests covering the generation-length gate, the ZeRO-3 /
release_inference_cache/inference_tp_sizerejections, the dispatch state machine, capture-failure fallback, and invalidation on length change.One end-to-end test (
seq_inferencemarker) asserting a graphed generation is token-identical to the same generation run eagerly.Continuous-rollout suite, run on NPU with Transformers 4.57.6:
NPU end-to-end validation ran locally on a single Ascend NPU (PyTorch 2.10 + torch_npu 2.10.0.post7; Transformers 4.57.6): both paths capture and replay correctly with token-identical outputs, and the eager path is unchanged. The three HybridEngine scripts in DeepSpeedExamples (TODO: fill in the script names) were also run end-to-end on NPU with graph capture enabled.
Scope
The graph flag remains opt-in and off by default; nothing changes for CUDA, whose eager path measured identically before and after. Only ZeRO-2 with
inference_tp_size=1was benchmarked on the container path, matching the upstream guards. Known limitations carried over or found while measuring:The NPU train step was measured over 2 iterations: ZeRO-2 on NPU retains ~4.9 GiB of live device memory per train step (not freed by
del/GC), so a 4-step loop exhausts the 32 GB card. This is unrelated to graph capture and tracked separately.Generate and train were measured in separate processes (iteration = sum of means; the eval↔train switch overhead is ~0.07% per #8271).
The continuous path on Transformers 5.x still needs the
get_query_offsetCache-API compatibility work, tracked in #8852; this PR does not include it.Related work
#8271 added the per-position decode graphs for CUDA; this PR implements its comment thread's
enable_graph_capturerename request.#8732 added the continuous-rollout graphs for CUDA and routed the capture-state check through the accelerator abstraction.
#8852 (open) adds the Transformers 5.x
get_query_offsetsupport this PR depends on for newer Transformers versions.Fixes #8932 — the accompanying feature request ([REQUEST][Hybrid Engine] support hybrid engine for non-CUDA), which frames the motivation and the hard-coding analysis.