Skip to content

[rl] Log the generator's CUDA graph padding fraction - #5945

Draft
felipemello1 wants to merge 3 commits into
mainfrom
felipemello1/cudagraph-padding-metrics
Draft

felipemello1 wants to merge 3 commits into
mainfrom
felipemello1/cudagraph-padding-metrics

Conversation

@felipemello1

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: A new metric, generator/cudagraph_padding_frac, shows how much of the generator's forward work is padding. Example: decodes of 3 and 5 requests replay the 4- and 8-token CUDA graphs, so 4 of 12 forward tokens are padding (0.33).

engine step   real tokens   padded tokens   paddings
decode x3     3             4               1
decode x5     5             8               3
-> generator/cudagraph_padding_frac/mean = (1 + 3) / (4 + 8) = 0.33

Problem

  1. vLLM pads every forward up to a CUDA graph capture size. Sequence-parallel rounding and DP equalization add more.
  2. max_num_seqs and the capture sizes decide how much padding a run gets.
  3. No metric reports it, so a bad capture ladder or cap stays invisible.

Solution

  1. TorchTitanGPUModelRunner counts real and padded tokens in its existing _determine_batch_execution_and_padding override. These are the numbers vLLM's own CUDAGraphStat uses (num_tokens vs batch_descriptor.num_tokens).
  2. Dummy forwards (profiling, capture, an idle DP rank's batch) are not counted. A flag set in the existing _dummy_run override keeps them out.
  3. VLLMEngine.step attaches the engine's counts since the last report to one finished request. Rank 0 already gets every DP rank's finished requests, so each rank's counts are added once.
  4. Rank 0 logs one engine-wide key, Mean(paddings, count=padded tokens). It covers every request in the engine, training and validation alike. Over a run, each forward is counted at most once: counts on a completion that is never logged (a failed group, or a training request that finishes after the trainer stops) are lost. One training step's value covers the completions that step took.
  5. Unchanged: no vLLM flag, no new config field.

You may ask: why not read vLLM's own stats?

  • vLLM computes CUDAGraphStat only with its cudagraph_metrics flag. Even then, LLMEngine.step() never returns it: it only goes to stat loggers.
  • Our vLLM stat logger, VllmOtelStatLogger, is off by default and exports to OpenTelemetry, not W&B. Getting the stat to W&B would need a new stat logger on each DP rank, plus the same hand-off through a finished request that this PR uses.
  • With the flag on, vLLM's default logging stat logger also prints a CUDA graph table every VLLM_LOG_STATS_INTERVAL seconds. Our engine runs with stats on, so that logger is always there.

Results

2 GPUs (trainer 1, generator 1), Qwen3.5-4B DAPO Math, FULL CUDA graphs, max_num_seqs 512, 3 steps,
16 validation prompts. W&B rows read back with the API.

W&B row               generator/cudagraph_padding_frac   in flight (max)
first pass (step 0)   0.039                                16
step 1                0.0067                               2,304
step 2                0.0073                               2,110
step 3                0.0055                               1,972
final pass            0.0036                               1,326
  • With about 2,000 requests in flight, most decode steps ran at the 512 cap, which is a capture size, so padding stays under 1%. The first pass ran with only 16 requests in flight, so its small batches padded more (3.9%). The final pass shared the engine with about 1,300 training requests still running.
  • No vLLM CUDA graph table appeared in the logs: the vLLM flag stays off.

NOTE:

  • The final validation pass logs at the last training step's number, so this key has two points at that step (training, then the final pass); the run summary keeps the second.
  • Steps that run eager count too. Their only padding is sequence-parallel rounding.
  • An idle DP rank's dummy forwards are not counted.
  • The counts ride on whichever request finishes next, so a row logged with a validation pass includes the training requests running at the same time.

Test plan

pytest tests/rl/unit_tests/cpu -q --ignore=tests/rl/unit_tests/cpu/test_vllm_wrapper.py
    # 552 passed, 7 skipped (main: 550 passed, 7 skipped)
pre-commit run --files <changed files>   # pyrefly from a checkout outside the dot-dir: 0 errors
  • New tests:
    • test_generator.py::test_cudagraph_padding_frac_weights_each_engine_step_by_its_tokens
    • test_vllm_dist_moe_padding_mask.py::test_runner_leaves_dummy_runs_out_of_the_token_counts
  • Extended: test_generator.py::test_step_returns_finished_requests_without_grad (counts attached to the finished request) and test_vllm_dist_moe_padding_mask.py::test_runner_publishes_the_unpadded_count_before_vllm_pads (the runner counts and resets).
  • test_vllm_wrapper.py is skipped above because this host's attn_gym has no mla module. Main fails to import it the same way.
  • Not run: the GPU unit tests (tests/rl/unit_tests/gpu). The counters are class attributes, so test_gdn_graph_metadata.py, which builds the runner without __init__, keeps working.

Felipe Mello added 3 commits November 7, 2026 22:27
The vLLM model runner pads each forward to a CUDA graph capture size (plus TP
rounding and DP equalization). Nothing reported how many forward tokens were
padding.

TorchTitanGPUModelRunner now counts real and padded tokens of real forwards
(dummy runs excluded). VLLMEngine attaches the counts to one finished request
per engine step, and rank 0 logs generator/cudagraph_padding_fraction, a mean
weighted by forward tokens.
Review round 1:
- Log generator/cudagraph_padding_frac whatever the carrying request's prefix:
  the counts cover every request in the engine, training and validation alike.
- Counters are class attributes, so runners built with object.__new__ in tests
  keep working.
- Count before the linear-attention TODO; name the fields num_engine_*.
- Test that dummy runs stay out of the counts.
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 8, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 8, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 3m 37s —— View job


Approve -- please fill in the human-written summary before merging. Review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- please address the items below before merging. The approach is the smallest one that gets an engine-wide vLLM stat to W&B: it adds no vLLM flag, no config field and no new channel. The counting matches vLLM's own CUDAGraphStat inputs. This touches the RL generator, so a maintainer still needs to review.

What this PR does. TorchTitanGPUModelRunner sums real and padded tokens for each real forward. It does this in its existing _determine_batch_execution_and_padding override, and a flag set in _dummy_run keeps dummy forwards out. When a VLLMEngine.step finishes at least one request, the step pops those sums and attaches them to the first finished request's RequestMetricsInputs. Rank 0 then emits them as generator/cudagraph_padding_frac = Mean(padded - real, count=padded). Because each DP rank pops its own counter, every forward is counted at most once across DP.

Example.

DP rank 1, engine steps since last pop: decode x3 -> graph 4, decode x5 -> graph 8
step finishes requests [r7, r9]  -> r7.metrics_inputs gets (unpadded=8, padded=12); r9 gets (0, 0)
rank 0 builds r7's Completion     -> Metric("generator/cudagraph_padding_frac", Mean(4, count=12))
trainer step that takes r7 + another DP rank's (3, 4) carrier -> (4+1)/(12+4) = 0.31

Must fix

  1. PR description -- "Summary (human-written)" is empty. The author needs to fill it in. Agents don't.

Should consider

  • torchtitan/rl/generator/dispatcher.py:439 -- the key name says "cudagraph", but the value also covers SP rounding and DP equalization, and it is logged in eager runs too (inline).
  • torchtitan/rl/generator/dispatcher.py:391-395 -- drop the speculative "a second such stat should..." sentence from the TODO (inline).
Details
  • Alternative considered: reading the counts in Generator.sync_log_step -> engine.log_stats(step). That path only runs on rank 0, on the RPC side rather than the engine-loop thread, so reading the runner's counters there would race with step(). It also can't see the other DP ranks' runners without a new collective, and it has no path into the controller's MetricsProcessor. Riding on a finished request avoids all three, so the piggyback is justified.
  • TP > 1: only TP rank 0 forwards finished requests (dispatcher.py:process_finished_requests), so each replica's counts are added once. Counters on TP ranks > 0 are never read, which is harmless.
  • Lost counts: counts can be lost on failed rollouts and on completions the trainer never takes. That only shrinks the sample. The carrier is whichever request finishes first in a step, which is independent of the request's outcome, so the ratio stays unbiased. DAPO-filtered groups still contribute their metrics through the batcher's metric-only groups.
  • Final step: validation runs only at the start and end, so the documented double point at the final step is the only collision.
  • Tests: the new and extended tests guard real invariants: dummy runs are excluded, the runner resets after a pop, and the Mean is weighted by padded tokens.

Not verified: that driver_worker.model_runner resolves through vLLM's WorkerWrapperBase and that batch_descriptor.num_tokens includes DP equalization in the pinned vLLM. vLLM isn't readable on this runner; the author's GPU run suggests both hold.

)
metrics.append(
m.Metric(
"generator/cudagraph_padding_frac",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The value covers all forward padding: CUDA graph capture sizes, sequence-parallel rounding and DP equalization. Your own comment above notes that eager steps count too. So a run with cuda_graph.mode="NONE" still logs cudagraph_padding_frac, where the number is really SP/DP padding. A name for what is measured, e.g. generator/forward_padding_frac or generator/padded_token_frac, would avoid that confusion. Since the key is new, now is the cheapest time to rename it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: Kept the name on purpose. Felipe asked for a CUDA graph padding metric, and in the default FULL mode almost all of this value is capture-size padding. The comment above the key says eager steps count only sequence-parallel rounding, so an eager run reads as near 0 rather than wrong. Happy to rename to generator/forward_padding_frac if a maintainer prefers.

Comment on lines +391 to +395
# in SchedulerStats / IterationStats, which only reach a registered
# vllm.v1.metrics.loggers.StatLoggerBase (VllmOtelStatLogger, when
# VLLMEngine.Config.stat_logger is set). To log one to W&B, carry it on a
# finished request like the CUDA graph padding counts (VLLMEngine.step). A second
# such stat should switch to one list of engine metrics built in VLLMEngine.step.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The last sentence ("A second such stat should switch to one list of engine metrics built in VLLMEngine.step") is a design note about future code. The comment should stay a TODO that describes where these stats live now and how to reach them. Suggest dropping that sentence. Whoever adds the second stat can decide the shape then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: A round of reviewers asked for that sentence. Without it, the next engine stat would copy this pattern and add two more fields to RequestMetricsInputs. It names the switch point rather than a design. I'll drop it if a maintainer disagrees.

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

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant