Skip to content

[Not Land][qwen3_5] Long-context 9B flavors, lean remat for CP attention, and a 1M recipe - #6286

Closed
fegin wants to merge 1 commit into
fegin/stack/46from
fegin/stack/47
Closed

fegin wants to merge 1 commit into
fegin/stack/46from
fegin/stack/47

Conversation

@fegin

@fegin fegin commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[Not Land][qwen3_5] Long-context 9B flavors, lean remat for CP attention, and a 1M recipe

Summary (human-written)

Only for testing and hand-off recipes.

Why

Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the inner_attention remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

Example.

main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)

What changed

  • Flavors 9B_1M and 9B_4M: YaRN factor 4 and 16 from 262144 to 220 and 222 positions. 9B is unchanged.
  • MRoPE scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
  • K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (attention.inner_attention.kernel*). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when inner_attention itself is saved, so existing policies are unchanged.
  • Recipe qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload: 9B_1M, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

Alternatives considered

  • Full remat: recomputes attention; 16% slower (below).
  • Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
  • PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

User-facing changes

Before After
9B max context 262144 9B_1M (220), 9B_4M (222)
- recipe qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload
- remat region attention.inner_attention.kernel* under CP

Numerics

  • 9B / 9B_mm configs are identical before and after; with scaling=None the MRoPE cache is bitwise unchanged.
  • YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
  • GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

Performance

8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:

Policy step time
full remat 21.68 s
lean 18.72 s
lean + offload 18.81 s
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
Nodes step time
--- ---
1 (CP 8) 54.7 s
16 (FSDP 16 x CP 8) 69.3 s
Offload keeps ~38 GiB per rank in pinned host memory.

Testing

  • GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
  • Recipe runs above (1 node and 16 nodes).

Known issues / follow-ups

  • 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
  • CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
  • TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Nov 20, 2026
fegin added a commit that referenced this pull request Nov 20, 2026
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
@github-actions
github-actions Bot requested a review from drisspg November 20, 2026 08:51
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 20, 2026
@fegin
fegin marked this pull request as draft November 20, 2026 09:16
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 20, 2026
@fegin
fegin changed the base branch from fegin/stack/46 to main November 21, 2026 08:11
fegin added a commit that referenced this pull request Nov 21, 2026
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
@fegin
fegin changed the base branch from main to fegin/stack/46 November 21, 2026 08:11
@fegin
fegin changed the base branch from fegin/stack/46 to main November 23, 2026 07:33
fegin added a commit that referenced this pull request Nov 23, 2026
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
@fegin
fegin changed the base branch from main to fegin/stack/46 November 23, 2026 07:33
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
@fegin
fegin changed the base branch from fegin/stack/46 to main November 23, 2026 07:42
@fegin
fegin changed the base branch from main to fegin/stack/46 November 23, 2026 07:42
fegin added a commit that referenced this pull request Nov 23, 2026
…#6285)

Stacked PRs:
 * #6286
 * __->__#6285


--- --- ---

### [CP] Support Ulysses CP when CP is larger than the number of KV
heads


## Summary (human-written)
Ulysses usually cannot work when the number of KV heads is smaller than
the CP degree. However, in certain cases, Ulysses is still the best
choice for full attention -- very long context on slower GPUs, which
makes computation the dominant factor. In such a case, a slight
imbalance can cause a dramatic slowdown.

This PR lets Ulysses support the case where the number of KV heads is
smaller than the CP degree by replicating KV heads.

## Why
Ulysses CP all-to-alls Q/K/V from sequence-sharded to head-sharded, so
every CP rank needs whole KV heads. Validation requires `n_kv_heads %
(tp * cp) == 0`. GQA models with few KV heads cannot use Ulysses at the
CP degree they need for long context. Qwen3.5 9B has 16 Q heads and 4 KV
heads, so Ulysses with CP 8 is rejected. The only option left is K/V
all-gather CP, whose head-tail balancing breaks once a row holds several
packed documents (1.9x attention imbalance across ranks, measured
below).

**Example.** 16 Q heads, 4 KV heads, TP 1, CP 8:
```
main:  ValueError: n_kv_heads (4) must be divisible by ... (8)
PR:    k (T/8, 4, K) -> repeat_interleave(2) -> (T/8, 8, K) = [kv0 kv0 kv1 kv1 kv2 kv2 kv3 kv3]
       after the all-to-all, rank r holds Q heads [2r, 2r+1] and KV head r // 2
```
GQA maps Q head h to KV head h // 4, so each rank gets the KV head its Q
heads use.

## What changed
- `UlyssesInnerAttention` repeats each local KV head `cp / n_kv_heads`
times (adjacent copies) before the all-to-all. The divisible case takes
no new code path.
- Config validation accepts full-attention Ulysses when `n_kv_heads`
divides `tp * cp` and is divisible by `tp`. GDN, KDA, and MLA Ulysses
keep the strict rule.
- The rule is documented in the `UlyssesInnerAttention` docstring; the
Qwen3.5 README points to it.

## Alternatives considered
- K/V all-gather CP: works today, but attention work is uneven across
ranks with packed documents.
- All-gather K/V within each group of ranks that share a KV head instead
of copying: saves some all-to-all bytes, but adds code, and the
all-to-all is only 11-29 ms per step.

## User-facing changes
| Before | After |
| --- | --- |
| Ulysses with `cp % n_kv_heads == 0`, `n_kv_heads < cp` raises
`ValueError` | Runs, with KV head copies |

## Numerics
| Config | GPUs | Result |
| --- | --- | --- |
| Qwen3.5 debugmodel, Ulysses CP 2 (divisible case), seed 42,
deterministic, 10 steps | 2 | loss and grad_norm bitwise identical to
`b1c259cd3` |
| Qwen3.5 debugmodel, 4 Q / 2 KV heads, TP 2 x CP 2, Ulysses vs K/V
all-gather | 4 | step 1 loss and grad_norm identical (12.94527 /
1.3714); later steps differ in the 4th-5th digit |
| Qwen3.5 debugmodel, 4 Q / 2 KV heads, CP 4, flex, Ulysses vs K/V
all-gather | 4 | max diff 4.6e-5 loss, 3.6e-5 grad_norm over 10 steps |
Kernel check (document-masked, bf16, varlen and flex) at CP 4 and CP 8
against no-CP and an fp32 oracle: same output error as no-CP.

## Performance
8 H100, one CP 8 group, Qwen3.5 9B shape with 4 layers (2 GDN + 2 full
attention), 256K tokens of packed documents, FA3, steps 6-15:
| CP method | step time | flash kernel time per rank (min-max) |
| --- | --- | --- |
| Ulysses + KV copies | 1.152 s | 352-369 ms |
| K/V all-gather, head-tail | 1.346 s | 278-525 ms |
| K/V all-gather, contiguous | 1.453 s | 143-613 ms |

## Testing
- CPU unit test: per-CP-rank GQA after the KV copy matches full GQA,
with and without TP. It fails if the copy is tiled (`repeat`) instead of
adjacent (`repeat_interleave`).
- Validation tests: the new rule accepts `cp=4, tp=2, n_heads=16,
n_kv_heads=4`, and rejects 3 KV heads at CP 4 and KV heads not divisible
by TP (on Qwen3.5 attention, which has no fused QKV, so the CP check is
what fires).
- The Qwen3.5 Ulysses H100 integration test now runs TP 2 x CP 2 (1
TP-local KV head copied to 2 CP ranks). It uses 4 GPUs instead of 2.

## Known issues / follow-ups
- The integration config was verified locally on the text dataset; the
multimodal dataset does not load in the local env (torchvision CUDA
mismatch).
acisseJZhong pushed a commit that referenced this pull request Nov 24, 2026
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
acisseJZhong pushed a commit that referenced this pull request Nov 24, 2026
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
acisseJZhong pushed a commit that referenced this pull request Nov 25, 2026
…ion, and a 1M recipe

## Summary (human-written)
Only for testing and hand-off recipes.

## Why
Qwen3.5 9B cannot train past 262144 tokens today: the flavor caps context there and has no RoPE scaling. At 1M, full attention is 85-95% of step time, so recomputing it in backward is expensive. Saving the `inner_attention` remat region instead keeps the CP-gathered K/V (all-gather) or the post-all-to-all Q/K/V (Ulysses), several times the memory of the kernel output.

**Example.**
```
main:  build_model_config("9B", seq_len=2**20)  -> ValueError: exceeds max context length 262144
PR:    build_model_config("9B_1M", ...)         -> YaRN factor 4, 2**20 positions

remat save region   kept per full-attention layer (K/V all-gather CP, 256K tokens)
inner_attention     gathered K/V + kernel output + LSE   (6x)
...kernel           kernel output + LSE                  (1x)
```

## What changed
- Flavors `9B_1M` and `9B_4M`: YaRN factor 4 and 16 from 262144 to 2**20 and 2**22 positions. `9B` is unchanged.
- `MRoPE` scales the cos/sin cache by the YaRN attention factor, as HF does. Qwen3.5 rotates only 64 of 256 channels per head, so the factor cannot fold into the softmax scale. Without scaling the factor is 1.0.
- K/V all-gather varlen CP and Ulysses CP give the local attention kernel its own remat region (`attention.inner_attention.kernel*`). Saving it keeps only the output and LSE; projections, all-gather, and all-to-alls are replayed. The region is inert when `inner_attention` itself is saved, so existing policies are unchanged.
- Recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload`: `9B_1M`, CP 8 (Ulysses with KV head copies for full attention, CP GDN), FSDP over the remaining ranks, lean remat (save block inputs and attention kernel outputs) with CPU offload, last block kept on GPU, FA3, CUDA graphs off, chunked loss with 4096 tokens per chunk per CP rank.

## Alternatives considered
- Full remat: recomputes attention; 16% slower (below).
- Lean remat without offload: 78.2 GiB at 1M, too tight on 80 GB H100.
- PP to cut memory: worse (61-69 GiB vs 44.5 GiB at 1M), because stage 0 still holds one microbatch per stage under 1F1B and FSDP keeps stage params and grads unsharded across microbatches.

## User-facing changes
| Before | After |
| --- | --- |
| 9B max context 262144 | `9B_1M` (2**20), `9B_4M` (2**22) |
| - | recipe `qwen3_5_9b_1m_cp8_ulysses_lean_remat_offload` |
| - | remat region `attention.inner_attention.kernel*` under CP |

## Numerics
- `9B` / `9B_mm` configs are identical before and after; with `scaling=None` the MRoPE cache is bitwise unchanged.
- YaRN inverse frequencies and attention factors (1.13863, 1.27726) match HF transformers exactly at rotary dim 64.
- GPU test: with the kernel region saved, gradients are bitwise equal to full recompute, on both CP paths.

## Performance
8 H100, one CP 8 group, 9B with 8 layers, 1M single-document rows:
| Policy | step time |
| --- | --- |
| full remat | 21.68 s |
| lean | 18.72 s |
| lean + offload | 18.81 s |
Recipe end to end, full 32 layers, packed long-document rows (2-3 documents per 1M row), H100 80 GB:
| Nodes | step time | tokens/s per GPU | peak memory |
| --- | --- | --- | --- |
| 1 (CP 8) | 54.7 s | ~2,400 | 58.2 GiB |
| 16 (FSDP 16 x CP 8) | 69.3 s | ~1,890 | 43.1 GiB |
Offload keeps ~38 GiB per rank in pinned host memory.

## Testing
- GPU test for both CP paths: only the kernel output and LSE are retained, and gradients match full recompute. It fails without the kernel region.
- Recipe runs above (1 node and 16 nodes).

## Known issues / follow-ups
- 4M does not fit an 80 GB H100 yet (best tested: Ulysses CP 16 + lean + offload at 80.2 GiB). Next: share one RoPE cache across layers (16 GiB at 4M), then chunked MLP replay or a segmented GDN backward.
- CUDA graphs crash at capture with the current PyTorch build in this setup, so the recipe turns them off.
- TorchTitan's logged MFU overcounts causal, document-masked attention for this model.

stack-info: PR: #6286, branch: fegin/stack/47
@fegin fegin closed this Nov 30, 2026
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