Skip to content

Measure first-call and post-update costs of continuous rollout CUDA Graphs - #8853

Open
nathon-lee wants to merge 1 commit into
deepspeedai:masterfrom
nathon-lee:bench/rollout-cuda-graph-first-call
Open

nathon-lee wants to merge 1 commit into
deepspeedai:masterfrom
nathon-lee:bench/rollout-cuda-graph-first-call

Conversation

@nathon-lee

@nathon-lee nathon-lee commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Background

Follow-up to #8732 and its
review suggestions.
Training updates invalidate the continuous rollout graph, so steady-state latency alone does not capture the cost of alternating training and generation.

Implementation status

The initial benchmark records CUDA-synchronized latency and throughput for eager / graph rollouts around one real SGD update. Each measured call's tokens, attention masks, and response offsets are checked against native Hugging Face generate() using the same weights.

Existing GPU measurements

Recorded on an RTX A4500 (20 GiB), PyTorch 2.9.1+cu128 / CUDA 12.8, Transformers 5.20.1, FP16, using a randomly initialized tiny Llama. Steady-state values are medians of 10 calls; first-call values are single samples.

Phase Eager (ms) CUDA Graph (ms)
First call 240.182 61.933
Steady state 228.014 45.424
First call after SGD update 226.304 58.800
Steady state after update 234.919 45.281

Graph first-call overhead was 16.509 ms before SGD and 13.519 ms afterward; the training update took 55.849 ms. These are synthetic observations, not production-model performance claims. The overhead values are end-to-end differences, not isolated capture timings.

Reproduce on the benchmark checkout with the Issue 4 fix:

python -m benchmarks.rollout.bench_cuda_graph_first_call \
    --requests 8 --batch-size 4 --prompt-len 32 --max-new-tokens 32 \
    --iterations 10 --output rollout-first-call.json

CUDA / model libraries were initialized without warming up the rollout graph; each phase measured eager before graph. The original combined working tree reported 60 passed, 1 skipped and passing pre-commit checks. The skip required an optional pretrained model. This benchmark was not rerun during the separate Issue 4 validation.

Remaining work and acceptance criteria

  • Separate cache / static-buffer allocation, warmup, and capture timings from total rollout latency, without double-counting.
  • Report per-phase capture counts, allocated / reserved and peak GPU memory, latency, and generated-token throughput.
  • Measure repeated training / rollout cycles with real optimizer updates; preserve the four phases and generation-parity checks.
  • Keep synchronized timing, fixed inputs, reproducible commands, and complete model / attention / hardware / software / workload metadata. Clearly label synthetic versus pretrained-model results; none of the latter are available yet.

Scope and dependencies

Depends on #8732 and, on Transformers 5.20.1, Issue 4. Keep the runtime fix separate. Measure existing behavior without weakening parameter-version checks. Benchmark code and usage belong in benchmarks/rollout/bench_cuda_graph_first_call.py and benchmarks/README.md, not the Issue 4 branch.

CC @delock

Base the benchmark on upstream master dc046b1, excluding merged rollout dependencies and keeping the query-offset runtime fix separate in deepspeedai#8852. Document Transformers 4.57.6 for the current runtime and count actual capture contexts rather than capability probes.

Validation: benchmark integration test passed on NVIDIA RTX A4000 (16 GB), PyTorch 2.9.1+cu128, CUDA 12.8, Transformers 4.57.6. The documented FP16 workload (8 requests, 4 slots, 32 prompt tokens, 32 new tokens, 10 steady-state calls) matched native generation in all four phases around a real SGD update. Modified-file pre-commit checks passed.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: nathon-lee <[email protected]>
@nathon-lee
nathon-lee force-pushed the bench/rollout-cuda-graph-first-call branch from 357d800 to d4756f8 Compare November 3, 2026 04:45
@nathon-lee
nathon-lee marked this pull request as ready for review November 3, 2026 04:46
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T04:52:18.365443Z d4756f8 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d4756f8be4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

])
# Unchanged shapes must reuse captures until the optimizer updates weights.
with patch.object(accelerator, "capture_to_graph", wraps=accelerator.capture_to_graph) as capture:
report = run_benchmark(args)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Guard the CUDA smoke test against Transformers 5.x

When this test runs in the required modal-torch-latest merge-queue GPU job, it executes with Transformers git main (.github/workflows/modal-torch-latest.yml lines 318-320), but the benchmark's own support note (benchmarks/README.md lines 13-15) says the current runtime must use 4.57.6 because 5.x needs #8852, which is intentionally absent here. Since the test calls run_benchmark after checking only for CUDA, current 5.x jobs reach the known query-offset mismatch during the exact output comparison and fail; pin or skip the unsupported version, or land the compatibility fix before enabling this smoke test.

Useful? React with 👍 / 👎.

@tohtana tohtana added the needs-gpu-test GPU testing is recommended to validate the changes in this PR. label Nov 10, 2026
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Nov 13, 2026
…tdown (deepspeedai#9009)

## Summary

Keep the continuous-decode graph alive across RL rollout rounds, and add
an explicit way to release it.

| Change | Before | After |
| --- | --- | --- |
| Graph signature | Includes `prompt_len` and every parameter's
`(data_ptr, _version)`. | Keeps the decode batch size, cache length,
device and dtype, plus each parameter's and buffer's address, device,
dtype, shape and strides. Captured kernels read these tensors through
the address and layout fixed at capture, so in-place value updates are
replayed correctly. Replacing a parameter or buffer (for example a
re-registered rotary `inv_freq` or quant scale), or changing its layout
at the same address (for example `transpose_()`), triggers a recapture.
An unindexed device is normalized to the current device index. |
| Width changes | Every new prompt width recaptures, even when
`continuous_cache_capacity` pins the cache length. | With a pinned
capacity, one graph serves all widths that fit. The capacity check still
runs on every call. |
| Rebuild order | The new static cache and graph pool are allocated
while the old state is still alive. | Drop the old state before
allocating its replacement, avoiding overlapping ownership of both
states. |
| `HybridEngineRollout.shutdown()` | No API; the state lives until the
rollout object is collected. | Synchronizes and releases the graph and
static cache. The rollout stays usable and recaptures on the next graph
call. |

`shutdown()` implements the existing `RolloutEngine.shutdown()`
lifecycle hook. It matters for TP>1: in the NCCL reproducer below, a
persistent graph holding captured collectives makes
`destroy_process_group()` hang in `ProcessGroupNCCL.shutdown()`. Call
`shutdown()` on each rank before destroying the process group. The docs
describe reuse and release.

## Why dropping `_version` is safe

- With bf16, DeepSpeed's optimizer writes updated weights through flat
buffers that the parameters view via `.data`, which does not bump
`param._version`. On master, single-width bf16 rollouts already reused
the graph after `engine.step()`, and the replayed outputs changed with
the weights. The version check gave no protection there.
- On paths where a plain torch optimizer updates parameters in place
(for example fp32 ZeRO-0), `_version` changes on every step, so master
recaptured on every rollout round.
- The cases where storage really moves (AutoTP gather/partition or a
`.data =` swap) change `data_ptr`, which stays in the signature.
- A same-address layout change also invalidates the captured kernels.
Shape and strides are in the signature, so this recaptures too. This
also covers `param.data = param.data.t()`, which keeps both `data_ptr`
and `_version` and was therefore missed by the old signature.

## Related work / scope

- Depends on deepspeedai#9008 for TP>1 continuous graph. Without it, TP>1
continuous graph fails at the KV copy before this code matters. The two
PRs touch neighbouring lines in `_get_continuous_graph_state`; whichever
merges second needs a trivial rebase. TP2 results below use deepspeedai#9008 + this
PR.
- Builds on deepspeedai#8732 (persistent continuous graph), reusing its existing
fixed-capacity setting rather than adding another configuration option.
- deepspeedai#8853 only measures graph capture and keeps the version check. Its
`test_continuous_graph_first_call_benchmark_on_cuda` asserts
`capture.call_count == 2` because an in-place SGD step currently forces
a recapture. With this PR that step reuses the graph, so whichever of
the two merges second needs that expectation changed to 1 (and the
benchmark's post-update number no longer includes a recapture). deepspeedai#8786
buckets prefill and does not change graph reuse or release.
- Not covered: ZeRO-3, offload, HybridEngine kernel injection, LoRA
fuse/unfuse inside rollout.

## Validation

### After merging master

Merge `dedd6c840` includes master `a4c989b76`. On 1× NVIDIA L20 (driver
580.173.02, CUDA 13.0, torch 2.14.1+cu130, Transformers 5.17.0), the
tested files matched the merged worktree by SHA-256:

```bash
unset DEEPSPEED_PRETRAINED_ROLLOUT_MODEL
CUDA_VISIBLE_DEVICES=0 DS_ACCELERATOR=cuda OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 \
  HF_HUB_OFFLINE=1 timeout 600 python -m pytest -q -p no:cacheprovider -o addopts= \
  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
# 104 passed, 2 skipped: optional pretrained model and native inference extension

CUDA_VISIBLE_DEVICES=0 DS_ACCELERATOR=cuda OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 \
  HF_HUB_OFFLINE=1 DEEPSPEED_PRETRAINED_ROLLOUT_MODEL=Qwen/Qwen3-0.6B \
  timeout 300 python -m pytest -q -p no:cacheprovider -o addopts= \
  tests/unit/v1/test_hybrid_engine_rollout_v1.py::test_continuous_graph_matches_pretrained_model_when_enabled
# 1 passed, using cached Qwen3-0.6B weights
```

On CPU (AMD EPYC 9575F, 256 logical CPUs visible; Python 3.12.3, torch
2.14.0.dev20260804+xpu):

```bash
DS_ACCELERATOR=cpu OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 python -m pytest -q -p no:cacheprovider -o addopts= \
  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
# 78 passed, 28 skipped (accelerator-only cases and the optional pretrained test)
```

Pre-commit passed on the four PR files after resolving the conflict.

### Earlier validation

**CUDA:** 8× NVIDIA L20 available (1/2/4 devices used below), driver
580.173.02, CUDA 13.0, torch 2.14.1+cu130, Transformers 5.17.0. Base:
`d8241a9ba`. The three tested Python files on L20 have identical SHA-256
hashes to the then-current PR head `55384d86d`.

With `DEEPSPEED_PRETRAINED_ROLLOUT_MODEL` unset:

```bash
CUDA_VISIBLE_DEVICES=0 python -m pytest -q -p no:cacheprovider \
  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
# 61 passed, 2 skipped; exit 0
# Skips: optional pretrained model and native inference kernel unavailable in this invocation.
# Without the layout fix in 55384d8: the two lifecycle tests fail (unit: state reused after
# transpose_(); v1: graph tokens differ from eager after transpose_()).
```

The earlier run (before the layout fix) with the optional pretrained
model enabled and the native inference kernel available:

```bash
CUDA_VISIBLE_DEVICES=0 DEEPSPEED_PRETRAINED_ROLLOUT_MODEL=<Qwen3-0.6B> 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
# master + this PR's test files: 4 failed, 59 passed (3 incompatible/new regression tests + 1 pre-existing)
# this PR:            1 failed, 62 passed  (pre-existing only)
# this PR + deepspeedai#9008:    1 failed, 71 passed  (pre-existing only)
```

The pre-existing failure on baseline `d8241a9ba` was the opt-in
`test_continuous_graph_matches_pretrained_model_when_enabled` with
Transformers 5.17 (`'DeepSpeedStaticCache' object has no attribute
'get_query_offset'`, see deepspeedai#8852). The fix is now in master and the opt-in
test passes after the merge, as shown above; the test remains skipped in
CI without the model.

The unit test covers in-place updates, a same-address `transpose_()`,
parameter replacement, capacity changes and `shutdown()` with graph
capture mocked. On a real device the new test runs a tiny Llama with a
fixed capacity and widths 8 → 8 → 12 → 8. It applies an in-place weight
change before round 2 and a same-address `lm_head.weight.transpose_()`
before round 4. Each round matches plain eager token for token, the
output changes after the weight update, and there are exactly 2 captures
(initial + after the layout change). `shutdown()` frees at least the
cache size.

**Review follow-up (`2313079fa`, rebased on master `84481c333`).**
Buffers are now in the signature. The unit test also re-registers a
buffer, and the device test adds a fifth round that re-registers
`model.model.rotary_emb.inv_freq` at half its values: the output still
matches eager, the tokens change, and there are 3 captures. On 1× NVIDIA
L20 (torch 2.14.1+cu130):
`tests/unit/v1/test_hybrid_engine_rollout_v1.py` +
`tests/unit/runtime/rollout/test_hybrid_engine_rollout.py` give 102
passed, 6 skipped. With buffers removed from the signature, the device
test fails on the token comparison after the buffer swap. On 1× Arc Pro
B70 the same files give 71 passed, 37 skipped (graph-capture cases skip
on this XPU stack). Multi-GPU TP and the RL loop below were not rerun
for this change, which only adds buffers to the signature.

**RL loop on L20.** Qwen3-0.6B, TP1, 8 prompts, 4 slots, 32 new tokens,
greedy, 6 rounds. Each round does a graph rollout, then a real
`engine.backward()` + `engine.step()` with AdamW (lr 1e-4). Times are
wall-clock per rollout, including prefill.

| Setup | Captures (6 rounds) | Rollout ms (rounds 2–6) | Peak MiB
(rounds 2–6) |
| --- | --- | --- | --- |
| fp32 ZeRO-0, width 64 — master | 6 | 677–703 | 7069 → 7107 |
| fp32 ZeRO-0, widths 64/96, capacity 160 — this PR | 1 | 546–553 | 7107
/ 7070 alternating |
| bf16, widths 64/96, capacity 160 — master | 6 | 625–1331 | 8188 → 8220
|
| bf16, widths 64/96, capacity 160 — this PR | 1 | 401–542 | 8155 / 8137
alternating |
| bf16, width 64, no capacity — master / this PR | 1 / 1 | 386–388 /
390–396 | unchanged |

After the layout fix, the patched fp32 TP1, bf16 TP1 and fp32 TP2×DP2
runs were repeated: still 1 capture, the same per-round token-match
rates and the same `shutdown()` release. Optimizer steps do not change
parameter shape or strides.

These are exploratory short runs, not a controlled end-to-end speedup
claim. In particular the two fp32 rows use different prompt schedules
and cache capacities. No long-run convergence or training-throughput
claim is made.

**Correctness against plain eager generation.**

- **fp32:** every round is token-identical in the TP1 master and patched
runs, and in the patched TP2×DP2 run (with deepspeedai#9008).
- **bf16, TP1:** rounds 4–6 differ from plain eager after training
steps. The per-round token-match rates are the same on master and this
PR (0.9961 / 0.8984 / 0.9336). This is consistent with
precision-sensitive generation, but does not prove the root cause or
bitwise equivalence of the two graph runs.
- **bf16, TP2×DP2:** the patched run also differs from eager (minimum
match 0.5742 / 0.9805 / 0.9883 / 0.8047). There is no matched unpatched
TP2×DP2 run in this experiment, so this cell is **not validated for
token-exact correctness**.

**Tiny Llama fp32 runs:**

- ZeRO-2, widths 16/24: 1 capture, token-identical on every round.

**Release with TP2** (tiny Llama, deepspeedai#9008 + this PR):

| Exit path | Ranks that returned from `destroy_process_group()` |
| --- | --- |
| No `shutdown()` | 0/2 (killed by the 600 s timeout) |
| `shutdown()` first | 2/2 |

`shutdown()` released 116.5 MiB (Qwen3-0.6B bf16, TP1, capacity 160) and
43.7 MiB on rank 0 (TP2×DP2).

**Intel XPU** (1× Arc Pro B70, Intel compute runtime package
26.27.39122.11, Level Zero loader 1.28.2, torch 2.14.1+xpu, Transformers
5.22.1, on top of deepspeedai#9007, which adds XPU graph capture):

```bash
DS_ACCELERATOR=xpu python -m pytest tests/unit/v1/test_hybrid_engine_rollout_v1.py \
  tests/unit/runtime/rollout/test_hybrid_engine_rollout.py
# deepspeedai#9007 + this PR: 52 passed, 3 skipped; exit 0
# Without the layout fix, the v1 lifecycle test fails: graph tokens differ from eager after transpose_().
```

The earlier torch 2.15.0+xpu run also reported 52 passed / 3 skipped,
but **aborted during interpreter teardown (exit 134)**. A minimal
`torch.ones(16, device="xpu"); torch.xpu.synchronize()` program
reproduces that abort without importing DeepSpeed. It exits normally on
torch 2.14.1+xpu; the clean-exit test run above uses that version. The
earlier deepspeedai#9007-only comparison reached the expected assertion failure (3
captures rather than 1), but its process also used the affected 2.15
runtime.

**CPU:** Python 3.12.3, torch 2.14.0.dev20260804+xpu selected with
`DS_ACCELERATOR=cpu`.

```bash
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
# 57 passed, 6 skipped
pre-commit run --files <changed files>   # passed
```

**Compatibility:** fp32 ZeRO-0 at TP1/TP2/TP2×DP2 and fp32 ZeRO-2 at TP1
are token-exact on CUDA. Single-device fp16 graph inference matches
eager for the tested Qwen3 and tiny Llama cases after the merge.
Single-device fp32 graph tests pass on XPU with deepspeedai#9007. bf16 runs
establish capture/release behavior only; their eager-output
discrepancies are reported above. **Not validated:** ZeRO-1, ZeRO-3,
offload, fp16 training, autocast, quantization, HybridEngine kernel
injection, multi-device XPU graph, pipeline/expert/sequence parallelism,
or training convergence.

AI assistance: GitHub Copilot helped with implementation, documentation,
tests, review and validation. The original signed code commit is
preserved; this PR also includes signed documentation and
layout-signature follow-ups.

---------

Signed-off-by: Jin, Youzhi <[email protected]>
Co-authored-by: Copilot <[email protected]>
@tohtana tohtana removed the needs-gpu-test GPU testing is recommended to validate the changes in this PR. label Nov 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants