Skip to content

[rl] Qwen3.5 Unified Path in RL (bitwise parity, cudagraph, TP+DP/EP) - #3957

Merged
wwwjn merged 21 commits into
mainfrom
wwwjn/qwen35-gdn-vllm-clean
Aug 20, 2026
Merged

wwwjn merged 21 commits into
mainfrom
wwwjn/qwen35-gdn-vllm-clean

Conversation

@wwwjn

@wwwjn wwwjn commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

In collaboration with and based on @yichuan-w 's works #3756

What & why

Runs torchtitan's hybrid Qwen3.5 (GatedDeltaNet linear-attention + full-attention [+ MoE]) inside vLLM for generation, borrowing only vLLM's paged conv/ssm cache and using the SAME fla kernels the trainer uses. Under batch-invariant mode the generator is bitwise-identical to the trainer forward -- the prerequisite for on-policy RL.

Supported Feature

  • gdn.py VLLMGatedDeltaNetCore: parameterless core owning only vLLM cache plumbing + fla kernels. conv + gated-delta recurrence run against the paged conv/ssm state (fla-only; no vLLM conv kernel -- it differs ~4e-4 and breaks parity). Recurrent-everywhere (prefill == decode) with fp32 recurrence + materialized initial state under BI. Registered as a real vLLM custom op (graph-split boundary).
  • Swappable core: GatedDeltaNet.core is a dense GatedDeltaCore in training, swapped to VLLMGatedDeltaNetCore in vLLM; both share the [B,L,...] + cu_seqlens signature. cu_seqlens sourced from packed positions ([RL][qwen3_5] GDN: reset conv/recurrent state at packed sample boundaries #3892) resets state per sample.
  • Tensor parallelism: GDN runs head-sharded via a single local_map (training) and a DTensor<->local bridge (generator); qwen3_5 sharding now honors enable_sequence_parallel (trainer SP on, generator SP off).
  • cudagraph: FULL_DECODE_ONLY (decode captured, prefill eager).
  • Prefix caching: align-mode mamba prefix caching.
  • Swappable core: GatedDeltaNet.core is a dense GatedDeltaCore in training, swapped to VLLMGatedDeltaNetCore in vLLM; both share the [B,L,...] + cu_seqlens signature. cu_seqlens sourced from packed positions ([RL][qwen3_5] GDN: reset conv/recurrent state at packed sample boundaries #3892) resets state per sample.
  • Tensor parallelism: GDN runs head-sharded via a single local_map (training) and a DTensor<->local bridge (generator); qwen3_5 sharding now honors enable_sequence_parallel (trainer SP on, generator SP off).
  • cudagraph: FULL_DECODE_ONLY (decode captured, prefill eager).
  • Prefix caching: align-mode mamba prefix caching.

Validation

  • Batch-invariant parity: trainer<->generator prefill bitwise at TP=1 and TP=2 (5/5 seqs, 0.0); trainer batch-invariance bitwise. TP=1 is fully bitwise incl. decode.
  • cudagraph (FULL_DECODE_ONLY) captures + coherent output at TP=2.
  • TP=2 generator via generate.py: coherent, correct output.
  • TP + DP + EP (MoE flavor debugmodel_moe): fwd+bwd OK for DP + TP + EP.
  • Non-computation numerics preserved: pretraining conv path unchanged.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 21, 2026
@wwwjn
wwwjn force-pushed the wwwjn/qwen35-gdn-vllm-clean branch 4 times, most recently from 452fb33 to 9173d86 Compare July 22, 2026 18:03
@wwwjn
wwwjn force-pushed the wwwjn/qwen35-gdn-vllm-clean branch 5 times, most recently from 6c426ca to 21848e7 Compare July 22, 2026 20:02
Comment thread torchtitan/models/qwen3_5/model.py Outdated
activation: str = "silu"
kernel: GatedDeltaKernel.Config

def __init__(self, config: Config):

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.

Add a new layer of abstraction in pretraining torchtitan which helps sort out the GDN computation, and can be replaced in vllm_wrapper.py

@wwwjn
wwwjn marked this pull request as ready for review July 22, 2026 22:13
@wwwjn wwwjn changed the title [WIP][rl] Qwen3.5 Unified Path in RL (bitwise parity, cudagraph, TP+DP/EP) [rl] Qwen3.5 Unified Path in RL (bitwise parity, cudagraph, TP+DP/EP) Jul 22, 2026
Comment thread torchtitan/models/qwen3_5/model.py Resolved
Comment thread torchtitan/models/qwen3_5/model.py Resolved
Comment thread torchtitan/experiments/rl/models/gdn.py Resolved
Comment thread torchtitan/experiments/rl/models/gdn.py Resolved
Comment thread torchtitan/models/qwen3_5/model.py Outdated
return result[0]


class GatedDeltaCore(Module):

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.

Is Core a name widely accepted?

  • now it's kernel inside core
  • why not core inside kernel?

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.

Not really, the name "core" is not widely used. And we have two options here:

  1. Wrap and replace the whole GDN class with input/output projection
  2. Only wrap and replace the "core" part like we did for inner_attention(), but have a better name , eg GatedDeltaInnerAttention or GatedDeltaConvRecurrence

From vllm side, it doesn't really matter which option we choose, because vllm only require the state cache read and write part register enough information, it doesn't really care about input/output projection

@wwwjn
wwwjn requested review from yichuan-w and removed request for drisspg, liangel-02 and shuhuayu July 23, 2026 02:30
Comment thread torchtitan/experiments/rl/models/gdn.py Resolved
@wwwjn
wwwjn force-pushed the wwwjn/qwen35-gdn-vllm-clean branch from 49dac2f to 5c0773b Compare July 23, 2026 15:09
@wwwjn
wwwjn force-pushed the wwwjn/qwen35-gdn-vllm-clean branch 2 times, most recently from a26aff9 to 6d6d65c Compare August 19, 2026 20:47
wwwjn and others added 21 commits August 19, 2026 16:31
…ity, cudagraph, TP/EP)

Runs torchtitan's hybrid Qwen3.5 (GatedDeltaNet linear-attention + full-attention
[+ MoE]) inside vLLM for generation, borrowing only vLLM's paged conv/ssm cache and
using the same fla kernels the trainer uses. Under batch-invariant mode the generator
is bitwise-identical to the trainer forward -- the prerequisite for on-policy RL.

Core:
- gdn.py VLLMGatedDeltaNetCore: parameterless core owning only vLLM cache plumbing +
  fla kernels; conv + gated-delta recurrence run against the paged conv/ssm state
  (fla-only, no vLLM conv kernel). Recurrent-everywhere (prefill == decode) with fp32
  recurrence + always-materialized initial state under BI. Registered as a real vLLM
  custom op (graph-split boundary).
- Swappable core: GatedDeltaNet.core is a dense GatedDeltaCore in training, swapped to
  VLLMGatedDeltaNetCore in vLLM; both share the [B,L,...] + cu_seqlens signature.
  cu_seqlens from packed positions (#3892) resets state per packed sample.
- Tensor parallelism: GDN head-sharded via a single local_map (training) and a
  DTensor<->local bridge (generator); qwen3_5 sharding honors enable_sequence_parallel
  (trainer SP on, generator SP off). TP + DP + EP compose for MoE flavors.
- cudagraph FULL_DECODE_ONLY; align-mode mamba prefix caching.
- Both paged states kept fp32 for bitwise parity: the ssm state AND the conv state
  (mamba_cache_dtype defaults to bf16, which makes decode round-trip the conv state
  through bf16 each step and drift from the single-pass prefill conv).

Tests performed:
- Batch-invariant parity (TestBitwiseParityQwen35Varlen): trainer<->generator prefill
  and generator decode-vs-prefill both bitwise (0.0) at TP=1 and TP=2; trainer batch
  invariance bitwise.
- Generator TP=2 via generate.py: coherent, correct output; cudagraph FULL_DECODE_ONLY
  captured.
- TP + DP + EP (debugmodel_moe) fwd+bwd OK (FSDP-DP and replicate-DP).
- Non-BI pretraining conv path unchanged (identical loss).

Co-authored-by: yichuan-w <[email protected]>
- Rename GatedDeltaCore -> GatedDeltaNetCore for the dense training core
  (model.py, __init__.py, and callsite comments).
- Add get_first_inner_attention_config() and use it in the generator,
  trainer, controller, generate.py, and the bitwise-parity test in place of
  ad-hoc layer-0 / inline scans (hybrid models have no layer-0 attention).
- Extract _configure_gdn_hybrid_model() for vLLM's hybrid-state contract.
- Reword gdn.py into a paged-cache adapter; note that
  fused_recurrent_gated_delta_rule is shape-specialized on sequence length,
  which is why batch-invariant mode casts recurrence inputs to fp32.
- Trim now-redundant comments/docstrings in config_registry, tests, and CI.
Run FLA's gated-delta recurrence once over the whole batch (decode and
prefill sequences delimited by cu_seqlens) instead of two per-segment calls.
torchtitan already runs recurrent-everywhere and the kernel processes each
sequence independently, so the merge is free; in batch-invariant mode (fp32
inputs) the recurrent kernel is shape-invariant, so every sequence's result
is bitwise-unchanged.

The convolution stays split by request type (decode -> single-token update
kernel, prefill -> varlen kernel): FULL-captured pure decode cannot run the
varlen conv, whose chunk-index prep host-syncs, which CUDA-graph capture
forbids. The unified initial-state gather uses plain integer indexing on the
captured pure-decode path (every decode resumes) and the boolean-mask gather
only on the eager prefill/mixed path, so no data-dependent indexing / host
sync enters a captured graph.

Also add docstrings noting get_state_{dtype,shape} are required by vLLM's
MambaBase interface, and expand the cu_seqlens-rebasing comments.

Verified: TestBitwiseParityQwen35Varlen (Qwen3.5-9B, TP=2, FULL_AND_PIECEWISE
cudagraph) -- all 3 subtests bitwise-identical (max_delta=0.00e+00), including
test_vllm_decode_vs_prefill, with 0 cudagraph-capture errors.
The GDN recurrence ran the sequential recurrent kernel for prefill and decode
alike. Batch-invariant mode requires that (the trainer's BI forward uses the
recurrent kernel, so the generator must match it bitwise), but non-BI prefill
left throughput on the table.

_run_recurrence now takes a use_chunk flag and calls
fla.ops.gated_delta_rule.chunk_gated_delta_rule (parallel chunked scan) when it
is set; _forward sets it for non-batch-invariant prefill/mixed batches
(num_prefills > 0 and not batch-invariant). Batch-invariant mode and pure decode
keep the recurrent kernel (decode is a single token and stays
cudagraph-capturable). Both kernels share the same
(q, k, v, g, beta, initial_state, output_final_state, cu_seqlens) interface and
final-state layout, so the paged gather/scatter is unchanged.

Verified: chunk vs recurrent numerically equivalent on a mixed varlen batch
(bf16 inputs + fp32 initial_state); BI parity unchanged
(TestBitwiseParityQwen35Varlen, 9B TP=2, all subtests max_delta=0.00e+00).
@wwwjn
wwwjn force-pushed the wwwjn/qwen35-gdn-vllm-clean branch from 6d6d65c to 94678a4 Compare August 19, 2026 23:35
@wwwjn
wwwjn merged commit 745373f into main Aug 20, 2026
22 checks passed
fegin added a commit that referenced this pull request Aug 20, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* __->__ #4242

Both #3957 and
#4238 try to fix
test_validate.py, which then cause merge conflict and cause duplicated
fields.
wwwjn added a commit that referenced this pull request Sep 9, 2026
## Why

The Qwen3.5 debug integration test in
https://github.com/pytorch/torchtitan/actions/runs/34312737649/job/102342893821
generated rollouts indefinitely without producing a training step.
Random-init policies can give every rollout in a group the same reward.
With `drop_zero_std_reward_groups=True`, every such group is discarded,
so the generator keeps replenishing work while the trainer waits forever
for a batch.

This is the same invariant addressed for earlier debug configs in #3802;
the Qwen3.5 debug config added in #3957 omitted the override.

## What

- Set `drop_zero_std_reward_groups=False` for the Qwen3.5 debug config.
- Add the corresponding CLI override centrally to every command produced
by the RL CI integration-test launcher, including multi-run tests.
- Record consecutive untrainable groups at the batcher boundary:
- each complete batch-sized block of untrainable groups emits a warning;
  - any trainable group resets the count;
- 10 consecutive untrainable batches raise `RuntimeError` instead of
hanging indefinitely.
- Remove the resolved silent-hang TODO and add regression tests for the
CI override, warnings, reset behavior, and terminal error.

This follows Prime-RL’s generalized no-progress protection:
https://github.com/PrimeIntellect-ai/prime-rl/blob/main/src/prime_rl/orchestrator/train_sink.py

## Testing

- `python -m pytest
torchtitan/experiments/rl/tests/test_async_controller.py
torchtitan/experiments/rl/tests/test_rollout_utils.py -q` (27 passed)
- Targeted watchdog tests after the final naming change (3 passed).
- Targeted pre-commit hooks passed (Pyrefly skipped because RL
experiments/tests are excluded by repository configuration and the
configured local PyTorch tree is unavailable).
- Python compilation passed for all changed files.
- Validated that all 8 commands currently produced by the RL CI launcher
contain the override.

The focused launcher pytest could not be collected locally because this
checkout environment lacks `grain`; CI installs the project
requirements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl ciflow/8gpu 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.

6 participants