Skip to content

Commit 97ce934

Browse files
HJSangclaude
andcommitted
Test that attention gets the real max_length; state what FlashAttention-4 needs
The actor test's pad segment was as long as its longest document, so it could not tell get_batch's max_seqlen from the real one. The docs now list every architecture that passes the boundaries and say the rest cannot use flash_attention_4 yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2ef7854 commit 97ce934

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎docs/user-guide/training-backend.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,9 @@ targets are Megatron-only: both `--offload-train-target disk` and
381381
- `--attn-implementation` picks the `transformers` attention backend: `flash_attention_2` by
382382
default, with `flash_attention_3`, `sdpa` and `eager` passed straight through.
383383
`flash_attention_4` uses the FlashAttention-4 that SGLang installs, so it trains without a
384-
`flash-attn` build. It needs an adapter that passes the packed-document boundaries: Qwen3,
385-
Qwen3-MoE, Qwen3-VL, GLM-4.7-Flash and Qwen3.5.
384+
`flash-attn` build. It needs the packed-document boundaries from the architecture's adapter:
385+
Qwen3, Qwen3-MoE, Qwen3-VL, GLM-4.7-Flash, Qwen3.5, Qwen3.5-MoE and Qwen3-Next pass them; other
386+
architectures (no adapter, or remote code) do not and cannot use it yet.
386387
- An architecture with fussier numerics can register its own policy, see
387388
[when an HF model needs help](#going-deeper-when-an-hf-model-needs-help).
388389

‎miles/backends/fsdp_utils/adaptations/arch_adapter.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ArchAdapter:
2626
verified: bool = False
2727
# None means the architecture has no MoE router to replay.
2828
routing_replay: RoutingReplayAdapter | None = None
29-
# stock HF attention reads the varlen boundaries; remote code may not accept them
29+
# stock HF modeling takes the packed boundaries (FlashAttention-4 needs them); remote code may not
3030
passes_attention_packing_kwargs: bool = False
3131

3232
def resolve_precision(self, base: PrecisionPolicy, args: Namespace) -> PrecisionPolicy:

‎tests/fast/backends/fsdp_utils/test_actor.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,22 @@ def test_packing_kwargs_follow_the_batch_boundaries_so_padding_stays_one_segment
9191
"""Pad tokens carry position 0; boundaries derived from `position_ids` would make each one a document."""
9292
actor = object.__new__(actor_module.FSDPTrainRayActor)
9393
actor.arch_adapter = resolve_arch_adapter(SimpleNamespace(model_type="qwen3_5_text"))
94-
cu_seqlens_host = (0, 3, 5, 8) # two documents, then the 3-token pad segment get_batch appends
94+
cu_seqlens_host = (0, 3, 5, 9) # two documents, then the 4-token pad segment get_batch appends
9595
batch = {
96-
"tokens": torch.zeros(1, 8, dtype=torch.long),
97-
"position_ids": torch.tensor([[0, 1, 2, 0, 1, 0, 0, 0]]),
96+
"tokens": torch.zeros(1, 9, dtype=torch.long),
97+
"position_ids": torch.tensor([[0, 1, 2, 0, 1, 0, 0, 0, 0]]),
9898
"cu_seqlens": torch.tensor(cu_seqlens_host, dtype=torch.int32),
9999
"cu_seqlens_host": cu_seqlens_host,
100-
"max_seqlen": 3,
100+
"max_seqlen": 4, # get_batch's, which counts the pad segment
101101
"unconcat_tokens": [torch.zeros(3, dtype=torch.long), torch.zeros(2, dtype=torch.long)],
102102
}
103103

104104
model_args = actor._get_model_inputs_args(batch)
105105

106106
assert model_args["cu_seq_lens_q"] is batch["cu_seqlens"]
107+
# the longest real document: the pad segment changes FlashAttention's launch plan
107108
assert model_args["max_length_q"] == 3
108-
assert model_args["seq_idx"].tolist() == [[0, 0, 0, 1, 1, 2, 2, 2]]
109+
assert model_args["seq_idx"].tolist() == [[0, 0, 0, 1, 1, 2, 2, 2, 2]]
109110

110111

111112
class TestFSDPTrainParallelConfigWiring:

0 commit comments

Comments
 (0)