Skip to content

[qwen3_8_fn] 3/n Add pipeline stages, remat and numerical integration coverage - #5785

Closed
shuhuayu wants to merge 1 commit into
pytorch:users/shuhuayu/qwen3-8-fn-03-tp-epfrom
shuhuayu:qwen3-8-fn-05-pipeline
Closed

shuhuayu wants to merge 1 commit into
pytorch:users/shuhuayu/qwen3-8-fn-03-tp-epfrom
shuhuayu:qwen3-8-fn-05-pipeline

Conversation

@shuhuayu

@shuhuayu shuhuayu commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Why

Stacked on #5783; GDN prerequisite #5784 is already in its ancestry.

A pipeline stage without PLE should own no lookup weights, optimizer moments or lookup collectives. Interleaved scheduling can put PLE in a later local model part, while the shared engine preprocesses inputs through the first part. Local-only optimizer regex validation also rejects valid global patterns on stages with no PLE.

Example. The eight-GPU recipe uses DP2, TP2, EP4, PP2 and four virtual stages:

PP rank 0: [token/vision embedding]  [layers 1-3, including PLE]
PP rank 1: [layer 0]                [layers 4-7, norm, stream mixer, LM head]

PP rank 0's first part prepares routing through a weak reference to its second part's embedding. Only the second part registers table parameters. PP rank 1 has no table or table moments.

What changed

  • Bind preprocessing to the actual PLE owner after pipeline splitting/parallelization, without registering a borrowed module or duplicate state keys. Only that stage owns native DTensor table weights and moments; non-owning stages have no lookup collectives.
  • Validate existing optimizer regex configurations over all local parts and a named 1D PP mesh. Skip locally empty optimizers before constructing their schedulers, and reject globally missing or fully shadowed patterns. Audit shared callers, including TorchFT.
  • Compose the model's stage-local residual/mixer, FSDP, TP, EP and full/selective remat through the existing pipeline engine.
  • Add the alternate-split CPU ownership/checkpoint/optimizer/scheduler regression and registered GB300 numerical integration with a pinned provenance record.

User-facing changes

Before After
PP configurations are rejected Real interleaved PP supports a later local PLE owner and stages without table state.
Optimizer patterns are validated per local part Patterns are validated over all parts and PP ranks; locally empty groups are skipped.

Numerics and testing

  • 126 targeted CPU tests and six two-GB300 tests pass on the native DTensor stack, covering shared optimizer patterns, model lifecycle, remat and pipeline ownership. The alternate split uses public model.pipeline()/preprocess_inputs, checks stage-owned native DTensor chunk coordinates and globally partitioned model/optimizer keys, and restores optimizer/LR state after two updates.

  • The registered TP2/SP/EP2 GB300 C4 regression matches all twenty committed full-precision loss/norm values bitwise; the golden is unchanged. Ten updates, seed 42 and strict determinism, PyTorch 2.16.0.dev20261030+cu132, revision bf270c3251d8e84723cedbf5b60eaa60cacaa3f2.

  • Eight DCT GB300s, actual C4 Trainer and native asynchronous checkpoints: DP2/TP2/EP4/PP2 with four Interleaved1F1B stages and full remat matches A/B on all twenty loss/norm values and all 2,866 final checkpoint entries (763 tensors). A repeated native run and B resume from A's step-5 checkpoint reach the identical final state. Loss: 7.70045804977417 -> 5.246838569641113.

  • HSDP2x2/TP2/EP4/PP1 with full remat and SPMD type checking matches A/B on all twenty loss/norm values and all 2,874 final entries (763 tensors). Loss: 7.747829437255859 -> 5.494491100311279.

  • All 48 child training processes exit zero and verify the October 30 runtime. The job wrapper then fails because it expects tb_baseline instead of the recipe's tb; local postprocessing of the saved full-precision events and native checkpoints passes. No training source or runtime patch is used. The committed provenance records the recovery explicitly.

  • All-file pre-commit run --all-files: pass.

NGPU=8 MODULE=torchtitan_recipes.tests.models.qwen3_8_flash_next CONFIG=qwen38_flash_next_debugmodel_all_parallelism ./run_train.sh
python -m tests.integration_tests.run_tests OUTPUT --test_suite gb300 --execution_mode real_pg --gpu_arch gb300 --ngpu 2 --test_name qwen3_8_flash_next_tp+ep_numerics

Precision and scope

Explicit fsdp_defer_gradient_reduction is rejected. Real PP still uses PyTorch's group-level FSDP finalization: backbone gradients accumulate in FP32 on requesters before DP reduction. PLE sums each microbatch's BF16 requester gradients in FP32 on row owners, then accumulates owner gradients. Both compute the same globally token-normalized sum with different floating-point association. PP coverage qualifies native determinism and C4 training; bitwise parity with the former full-table FSDP path is not claimed for PP.

Only the stage containing PLE adds table capacity; other PP stages join no lookup collectives. CP, CPU offload, released-model capacity and production throughput remain outside the qualified configurations.

Qualification source bdc26ae0e, preserved A stack a725047d4; DCT job qwen38-fn-01a1b222-dct-gb300-dtensor-stack-v2-sb702lrl, two hosts with four GB300s each. Those GPU results apply to the recorded pre-rebase sources. The subsequent HF bookkeeping follow-up and current rebase have targeted CPU coverage; the eight-GPU model qualification has not been repeated after the rebase. The registered golden is unchanged.

Rebase validation

Rebased onto main at c62c6a7552137d10d4fc3927aa20dcd69925aaf2, including the upstream RL golden-reference fix in #5909. The Flash-Next compatibility edits narrow config types and initialize the BF16 router test through the current meta-materialization lifecycle; they do not change model equations.

  • Runtime: PyTorch 2.16.0.dev20261108+cu132, revision 1392e1f8e4903657d4391f3bd8fcc9ca8a74b758; Attention Gym 0.0.19.dev10+g26beb363c, pinned revision 26beb363c6d1328851bfc5c9c8691fa2066bd1fa.
  • All-file pre-commit, including typechecking, passes on 0/n and the complete rebased stack. Typechecking uses this runtime and isolated stubs for optional dependencies.
  • 123 targeted CPU tests pass on the complete stack: row-sharded embedding, distributed utilities, Flash-Next, optimizer groups, schedulers and integration definitions.
  • The retained GDN deterministic-backward regression passes on a GB300 in both ordinary and batch-invariant modes.

The earlier two/eight-GPU model qualification results above apply to their recorded pre-rebase sources and October 30 runtime. Those model training comparisons have not been repeated after this rebase. GitHub CI for the new heads is pending.

Stack

Prerequisite: #5784, shared GDN deterministic normalization, independently based on main. Merge it first.

  1. [qwen3_8_fn] 1/n Add Flash-Next model, DTensor PLE, FSDP and EP #5782: Flash-Next model, native DTensor PLE, FSDP and EP, based on the prerequisite.
  2. [qwen3_8_fn] 2/n Add tensor and sequence parallelism #5783: TP/SP and PLE feature-column sharding, based on [qwen3_8_fn] 1/n Add Flash-Next model, DTensor PLE, FSDP and EP #5782.
  3. [qwen3_8_fn] 3/n Add pipeline stages, remat and numerical integration coverage #5785: PP, remat and registered numerical integration, based on [qwen3_8_fn] 2/n Add tensor and sequence parallelism #5783.

Local all-file hooks pass; Pyrefly uses the pinned validation runtime and isolated optional-dependency stubs. GitHub CI is pending.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 3, 2026
@shuhuayu shuhuayu changed the title [qwen3_8_fn] 5/n Add pipeline stages, remat and numerical integration coverage [qwen3_8_fn] 4/n Add pipeline stages, remat and numerical integration coverage Nov 3, 2026
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from 48795a8 to 7b33941 Compare November 4, 2026 01:54
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from b2aca89 to 71eb967 Compare November 4, 2026 01:54
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 4, 2026
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from 7b33941 to 964e954 Compare November 4, 2026 21:15
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from 71eb967 to 223be19 Compare November 4, 2026 21:15
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from 964e954 to a5efda3 Compare November 5, 2026 08:21
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from 223be19 to 51b450b Compare November 5, 2026 08:21
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from a5efda3 to 2addbca Compare November 5, 2026 20:05
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch 2 times, most recently from 6608e4e to 72d0de7 Compare November 5, 2026 20:14
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch 2 times, most recently from 84d08ed to 3585676 Compare November 6, 2026 00:55
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from 72d0de7 to bc22fd5 Compare November 6, 2026 00:55
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from 3585676 to a725047 Compare November 6, 2026 03:39
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from bc22fd5 to f72e254 Compare November 6, 2026 03:40
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from a725047 to 9b07f3f Compare November 7, 2026 02:07
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from f72e254 to ba80b84 Compare November 7, 2026 02:07
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from 9b07f3f to 5682a74 Compare November 9, 2026 07:55
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-04-determinism branch from ba80b84 to f521fcb Compare November 9, 2026 07:55
@shuhuayu shuhuayu changed the title [qwen3_8_fn] 4/n Add pipeline stages, remat and numerical integration coverage [qwen3_8_fn] 3/n Add pipeline stages, remat and numerical integration coverage Nov 9, 2026
@shuhuayu
shuhuayu changed the base branch from users/shuhuayu/qwen3-8-fn-04-determinism to users/shuhuayu/qwen3-8-fn-03-tp-ep November 9, 2026 07:55
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from 5682a74 to ccae348 Compare November 9, 2026 10:21
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-03-tp-ep branch from 62df501 to b07872a Compare November 9, 2026 10:21
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-05-pipeline branch from ccae348 to 0d9fb8d Compare November 9, 2026 10:46
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-03-tp-ep branch from b07872a to 92ab189 Compare November 9, 2026 10:46
shuhuayu added a commit that referenced this pull request Nov 10, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
     this approach, and what you checked yourself. -->

this pr is to add `deterministic ` option to gdn backend (an existing
bug) so gdn has reproducible numerics.

## Why

Independent prerequisite based on `main`. Merge this before #5782; the
model stack is based on this branch.

`torch.use_deterministic_algorithms(True)` does not reach Attention
Gym's GDN L2 normalization policy. Its forward schedule is fixed, but
backward autotuning benchmarks different tile schedules. Two
strict-deterministic runs can therefore choose different backward
schedules and train differently.

**Example.** The eight-GB300 Flash-Next first-step diagnostic observes a
difference in `kda_l2norm_bwd` before the PLE output gradient or any
lookup-gradient exchange. Hash IDs and selected vectors are identical.
Disabling normalization autotuning under PyTorch's existing
deterministic flag pins the schedule.

```python
autotune = not torch.are_deterministic_algorithms_enabled()
normalized_q = l2norm(q, cu_seqlens=cu_seqlens, autotune=autotune)
```

## What changed

Pass that policy through all shared GDN normalization callsites:
ordinary chunk/recurrent forward, recurrent backward's chunk-gradient
recomputation, and CP GDN. Nondeterministic runs retain autotuning. No
model flag or alternate kernel is added.

## User-facing changes

| Before | After |
| --- | --- |
| Strict determinism still allows GDN backward normalization autotuning
| The existing PyTorch deterministic flag selects a fixed schedule. |

## Numerics and testing

- The CUDA regression checks that deterministic backward bypasses the L2
autotuner and produces finite, nonzero gradients in ordinary and
batch-invariant GDN modes. It does not assert how many times upstream
internals run in nondeterministic mode.
- The shared GDN computation in this PR is unchanged by the PLE storage
refactor. Both native DTensor and plain-owned-row qualification sources
include this same committed policy.
- Two-GB300 controlled A/B runs match first-step logits and 190 gradient
shards per rank, all twenty ten-step loss/norm values, and all 763 final
model/optimizer tensors bitwise. Full remat and checkpoint resume
preserve the result.
- The restacked TP2/SP/EP2 registered GB300 regression matches all
twenty unchanged golden values bitwise. Runtime: PyTorch
`2.16.0.dev20261030+cu132`, revision
`bf270c3251d8e84723cedbf5b60eaa60cacaa3f2`; seed 42 and strict
determinism. No production kernel or runtime patch is used.
- Existing GDN integration coverage exercises the default path; all-file
lint/typechecking pass.

```bash
python -m pytest -q tests/unit_tests/gpu/models/qwen3_5/test_qwen3_5_deltanet.py
```

The fix intentionally chooses a fixed backward schedule in deterministic
mode; it does not claim bitwise equivalence to every autotuned schedule.
#5785 carries the registered strict numerical integration. EP remains in
#5782 and TP/SP in #5783.

## Rebase validation

Rebased onto `main` at `c62c6a7552137d10d4fc3927aa20dcd69925aaf2`. This
includes #5909's upstream RL golden-reference fix. All ten losses from
the old failing RL job exactly match that corrected reference; this PR
does not edit the RL golden.

- Runtime: PyTorch `2.16.0.dev20261108+cu132`, revision
`1392e1f8e4903657d4391f3bd8fcc9ca8a74b758`; Attention Gym
`0.0.19.dev10+g26beb363c`, pinned revision
`26beb363c6d1328851bfc5c9c8691fa2066bd1fa`.
- The retained
`test_deterministic_backward_bypasses_normalization_autotuner` passes on
a GB300, covering ordinary and batch-invariant GDN forward/backward with
finite, nonzero gradients.
- All-file pre-commit, including typechecking, passes on this PR and the
complete rebased stack. Typechecking uses this runtime and isolated
stubs for optional dependencies.
- The complete stack's targeted CPU suite passes 123 tests.

The earlier multi-GPU model qualification results above apply to their
recorded pre-rebase sources and October 30 runtime. They have not been
repeated after this rebase. GitHub CI for the new head is pending.

## Stack

Prerequisite: #5784, shared GDN deterministic normalization,
independently based on `main`. Merge it first.

1. #5782: Flash-Next model, native DTensor PLE, FSDP and EP, based on
the prerequisite.
2. #5783: TP/SP and PLE feature-column sharding, based on #5782.
3. #5785: PP, remat and registered numerical integration, based on
#5783.

Local all-file hooks pass; Pyrefly uses the pinned validation runtime
and isolated optional-dependency stubs. GitHub CI is pending.
@shuhuayu

Copy link
Copy Markdown
Contributor Author

Closing as stale: this was based on an older revision of 1/n. The Flash-Next stack now lives on upstream branches managed with gh stack, starting from #6046; this change will be restacked on top later.

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