Skip to content

[qwen3_8_fn] 2/n Add tensor and sequence parallelism - #5783

Closed
shuhuayu wants to merge 1 commit into
pytorch:users/shuhuayu/qwen3-8-fn-02-modelfrom
shuhuayu:qwen3-8-fn-03-tp-ep
Closed

shuhuayu wants to merge 1 commit into
pytorch:users/shuhuayu/qwen3-8-fn-02-modelfrom
shuhuayu:qwen3-8-fn-03-tp-ep

Conversation

@shuhuayu

@shuhuayu shuhuayu commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Why

Stacked on #5782; the main-based GDN prerequisite #5784 is already in its ancestry.

Flash-Next needs TP for its projections and attention. #5782 provides FSDP, EP and DP row ownership for PLE. Its lexical lookup must partition feature columns as well as the DP-owned rows added in #5782; replicating that table over TP wastes the available capacity.

Example. For an eight-row, 160-feature table with DP2 and TP2, each rank stores [4, 80] weights. A request for row 6 goes to DP owner 1 within each TP column group. The selected 80-feature responses gather across TP to form the token's 160 features. No operation gathers table rows or the whole table.

What changed

  • Declarative TP/SP placements for gated streams, PLE projections, QSA heads, dense/GDN projections and routed/shared experts. Residual/PLE activations remain replicated over TP.
  • DP row ownership times TP feature-column ownership reuses the native DTensor parameter and selected-row lookup from [qwen3_8_fn] 1/n Add Flash-Next model, DTensor PLE, FSDP and EP #5782. Hash IDs stay replicated over TP; only selected vectors gather their feature columns.
  • QSA reduces detached per-head teacher probabilities across TP before block pooling, and the replicated indexer contributes its KL gradient once.
  • Native DTensor chunk coordinates provide the global row and feature-column offsets for sharded HF loading/export. No new backend or dependency.

User-facing changes

Before After
TP/SP configurations are rejected TP2/SP composes with EP from #5782; DP-owned table rows also partition feature columns over TP.

Numerics and testing

  • The native DTensor stack matches the unchanged registered TP2/SP/EP2 GB300 golden bitwise on all twenty loss/norm values. Ten C4 updates, seed 42, strict determinism, and PyTorch 2.16.0.dev20261030+cu132; the same shared GDN policy is used throughout. The golden is not regenerated.
  • Actual-model two-GB300 TP2/SP/EP2 tests pass: persistent DP-row/TP-column DTensor storage, accumulation, active global clipping, fused AdamW, full remat and async native model/optimizer restore.
  • Real distributed HF safetensors save, consolidation and load cover DTensor row/feature intersections, empty owners and BF16 conversion. Format conversion performs zero collectives.
  • Independent eight-rank CPU/Gloo probes cover DP4/TP2, HSDP2x2/TP2 and TP-only table storage. Public chunk coordinates, lookup/backward, native clipping and AdamW match the plain-owned-row baseline.
  • Eight DCT GB300s, HSDP2x2/TP2/EP4 with full remat and type checking: native DTensor and plain owned-row storage match all twenty ten-step C4 loss/norm values and all 763 final checkpoint tensors bitwise. The stacked pipeline configuration, repeat and A-to-B resume also match; [qwen3_8_fn] 3/n Add pipeline stages, remat and numerical integration coverage #5785 records the saved-artifact postprocessing and source provenance.
  • The restacked model/distributed/optimizer/pipeline suites pass 126 CPU tests and six two-GB300 tests. All-file lint/typechecking pass.
NGPU=2 MODULE=torchtitan_recipes.tests.models.qwen3_8_flash_next CONFIG=qwen38_flash_next_debugmodel_tp2 ./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
python -m pytest -q tests/unit_tests/gpu/models/qwen3_8_flash_next

Limits

CP is rejected. Full released-model capacity and throughput remain unqualified. Strict shared GDN normalization comes from prerequisite #5784; pipeline ownership and registered numerical integration follow in #5785.

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] 3/n Add tensor and expert parallelism with batched QSA teachers [qwen3_8_fn] 2/n Add tensor and expert parallelism with batched QSA teachers Nov 3, 2026
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from b292f9c to cbe9b3c Compare November 4, 2026 01:54
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from e0aceb3 to a925141 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-03-tp-ep branch from cbe9b3c to e15a1f6 Compare November 4, 2026 21:15
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from a925141 to f387642 Compare November 4, 2026 21:15
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from e15a1f6 to b4b0b68 Compare November 5, 2026 08:21
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from f387642 to 1232b90 Compare November 5, 2026 08:21
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from b4b0b68 to c339afc Compare November 5, 2026 20:05
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from 1232b90 to 3f39d6f Compare November 5, 2026 20:06
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from c339afc to ac5a16a Compare November 5, 2026 20:14
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from ac5a16a to 7bba104 Compare November 6, 2026 00:55
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from 3f39d6f to 48314a2 Compare November 6, 2026 00:55
@shuhuayu shuhuayu changed the title [qwen3_8_fn] 2/n Add tensor and expert parallelism with batched QSA teachers [qwen3_8_fn] 2/n Add tensor, sequence and expert parallelism Nov 6, 2026
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from 7bba104 to ff98eea Compare November 6, 2026 03:39
@shuhuayu shuhuayu changed the title [qwen3_8_fn] 2/n Add tensor, sequence and expert parallelism [qwen3_8_fn] 2/n Add tensor and sequence parallelism Nov 6, 2026
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from ff98eea to d9413c0 Compare November 7, 2026 02:07
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from 0f6481b to 3bb96e0 Compare November 7, 2026 02:07
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from d9413c0 to 62df501 Compare November 9, 2026 07:55
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from 3bb96e0 to 0972e3a Compare November 9, 2026 07:55
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from 62df501 to b07872a Compare November 9, 2026 10:21
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from 0972e3a to 06c11e0 Compare November 9, 2026 10:21
@shuhuayu
shuhuayu force-pushed the qwen3-8-fn-03-tp-ep branch from b07872a to 92ab189 Compare November 9, 2026 10:46
@shuhuayu
shuhuayu force-pushed the users/shuhuayu/qwen3-8-fn-02-model branch from 06c11e0 to 2854b18 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