Repository navigation
[tests] Keep compiled flex attention out of CPU unit tests - #6422
Conversation
|
@claude review |
|
Claude finished @tianyu-l's task in 1m 34s —— View job Approve -- |
There was a problem hiding this comment.
Verdict: Approve -- please address the item below before merging -- swapping in eager flex is the right fix for a test that only checks the LoRA lm_head and ChunkedLossWrapper, but a sibling test hits the same CPU failure.
What this PR does. test_lora_head_only_matches_cross_entropy runs a llama3 debug forward with attn_backend="flex". That forward reaches FlexInnerAttention._compiled_flex_attn (torch.compile(flex_attention), attention.py:763). Inductor can't lower that on CPUs without AVX2. The PR patches the class variable with eager flex_attention for this one test, the same swap distributed/utils.py:202 makes on ROCm.
Example.
# aarch64 / macOS CPU
main: model(tokens, positions=..., attention_metadata=...)
-> FlexInnerAttention._compiled_flex_attn -> Inductor lowering
-> NotImplementedError: torch.compile on current platform is not supported for CPU
PR: @patch.object(FlexInnerAttention, "_compiled_flex_attn", flex_attention)
-> eager flex_attention -> test passes; on x86 only the kernel used changes
Must fix
tests/unit_tests/cpu/models/common/test_lora.py:161--test_lora_forwardruns the same CPU forward:build_model_config("debugmodel", attn_backend="flex", ...), thenmodel(...)through_compiled_flex_attn.torch.no_grad()doesn't skip Inductor lowering, so it should fail on aarch64 and macOS in the same way. The PR's testing only coveredtest_loss.py, so this test was never run there. If it stays as is, the PR fixes one of two tests with the same root cause. Fix: add the same@patch.object(FlexInnerAttention, "_compiled_flex_attn", flex_attention)decorator. It works on a plain pytest function. Also mention the test in the description.
Details
- Gate. The problem is real: compiled flex on a CPU without AVX2. The test layer is the right place for the fix. Torchtitan core should not quietly fall back to eager flex on CPU (principle 8). Skipping the test would drop the coverage for no reason. The patch uses the existing hook, the class variable that
distributed/utils.pyand the graph_trainer tests already swap, so there is no new mechanism. - Patch target. The decorator replaces
_compiled_flex_attn, the class variable thatcompiled_flex_attn(attention.py:812) calls. That is the right target.patch.objectrestores it afterwards, so other tests in the process are unaffected. - Backward. The test's backward only flows through
lm_head, becausehidden_TD.requires_gradis False (the test asserts this). The eager flex backward never runs. - Other CPU tests. Several other CPU tests use
attn_backend="flex"(e.g.test_weight_tying.py,test_rope_cache_tying.py,qwen3_8/test_qwen3_8.py). Those only build or inspect configs and modules and don't run a model forward.test_lora_forwardis the only other full-model flex forward I found. - Overlap search. I searched open PRs for "flex attention CPU", "test_loss.py" and "aarch64" and found none touching this code.
Not verified: the AVX2/macOS condition in Inductor's check_cpu_supported (I couldn't run Python in this environment), and whether test_lora_forward actually fails on aarch64 (inferred from the shared code path).
Before merge: fill in the human-written summary.
d43188c to
a2fe85f
Compare
|
@claude review again |
|
Claude finished @tianyu-l's task in 50s —— View job Approve: the earlier must-fix item ( |
There was a problem hiding this comment.
Verdict: Approve -- the earlier must-fix item is resolved, and the new Qwen3.5 patch uses the same eager-flex swap with no new problems.
What this PR does. Three CPU tests run a model forward through FlexInnerAttention._compiled_flex_attn, which is torch.compile(flex_attention). On CPUs without AVX2 (aarch64, macOS), Inductor can't lower that call. Each test now temporarily swaps in eager flex_attention, the same swap distributed/utils.py:202 makes on ROCm. None of the three tests checks the attention kernel.
Example.
# aarch64 CPU
main: test_lora_forward -> model(...) -> FlexInnerAttention._compiled_flex_attn
-> InductorError: torch.compile on current platform is not supported for CPU
PR: @patch.object(FlexInnerAttention, "_compiled_flex_attn", flex_attention)
-> eager flex -> passes; on x86 only the kernel used changes, not what the test checks
Earlier must-fix items
test_lora.py:164,test_lora_forwardneeded the same patch: resolved. The decorator is added, and the stale comment "Flex supports the CPU forward ... but not backward" is removed. The description now lists the test.
Details
- Commits reviewed. The branch has one commit (
a2fe85f), force-pushed since the last review. I re-reviewed all of it:test_loss.py,test_lora.pyand the newtest_qwen3_5_mrope_positions.pyhunk. - Qwen3.5 patch. The vision encoder uses
FlexInnerAttentionwhatever the language model's backend is (vision_encoder.py:118), so this test needs the swap as well. It patches by string path ("torchtitan.models.common.attention.FlexInnerAttention._compiled_flex_attn") instead of withpatch.object. That matches the file, which imports torchtitan lazily inside_build_config_modules()so it can skip when an optional dependency is missing. The patch only wrapsmodel(...)and is undone afterwards, so other tests in the process are unaffected. - Patch target.
compiled_flex_attncallsFlexInnerAttention._compiled_flex_attnthrough the class (attention.py:812), so replacing the class variable is enough. - Coverage of other tests. Many CPU tests mention flex, but the author ran all of
tests/unit_tests/cpuon aarch64 and only these 5 failed. That is stronger evidence than my earlier grep for other full-model flex forwards. - Overlap search. I searched open PRs for "flex attention CPU", "aarch64" and "test_qwen3_5_mrope_positions" and found nothing that touches these tests.
Before merge: fill in the human-written summary.
Five CPU unit tests run a model forward through flex attention, which torchtitan calls through torch.compile and Inductor compiles on CPU only with AVX2 (x86, not macOS): on aarch64 and macOS they fail with "torch.compile on current platform is not supported for CPU". None of them tests attention: - test_lora_head_only_matches_cross_entropy checks the LoRA lm_head with the chunked loss; it now feeds the head random decoder outputs instead of running the frozen decoder. - test_lora_forward only checked a LoRA forward's output shape; deleted. - The Qwen3.5 M-RoPE routing tests need the forward (they check which positions reach the layers), and the vision encoder always uses flex attention, so they patch FlexInnerAttention._compiled_flex_attn with eager flex_attention, as distributed/utils.py does on ROCm.
a2fe85f to
badfd0c
Compare
|
@claude review again |
|
Claude finished @tianyu-l's task in 1m 5s —— View job Approve: the earlier must-fix item is resolved and there is no new must-fix item; one small should-consider (drop the now-unused |
There was a problem hiding this comment.
Verdict: Approve -- the rewritten commit takes compiled flex attention out of the two LoRA tests completely instead of patching it, and I found no new must-fix items.
What this PR does. Since the last review, the branch was force-pushed with a different approach for the two LoRA tests. test_lora_head_only_matches_cross_entropy no longer runs the frozen decoder. It feeds the LoRA lm_head a random hidden_TD of the right width and still compares the loss and adapter gradients against plain cross-entropy for 1, 2 and 4 chunks. test_lora_forward, which only checked the output shape, is deleted. The Qwen3.5 M-RoPE tests keep the eager-flex patch around model(...). They need a real forward, and the vision encoder always uses flex.
Example.
# aarch64 / macOS CPU, test_lora_head_only_matches_cross_entropy
main: model(tokens, ...) -> decoder -> FlexInnerAttention._compiled_flex_attn
-> InductorError: torch.compile on current platform is not supported for CPU
PR: hidden_TD = torch.randn(8, dim) -> ChunkedLossWrapper(lm_head) vs F.cross_entropy
-> no attention runs. The test still checks the same head loss and LoRA grads.
Earlier must-fix items
test_lora.py,test_lora_forwardhit the same CPU failure: resolved, now by deleting the test instead of patching it.
Should consider
tests/unit_tests/cpu/components/test_loss.py:1187--attn_backend="flex"doesn't do anything any more, because the test never runs a forward. It also suggests that the backend matters. Drop it and use the default (varlen). CPU tests already build varlen configs that way, e.g.deepseek_v3/test_deepseek_v3_moe.py:38.
Details
- Gate. Same as before: the failure is real on non-AVX2 CPUs, and the test layer is the right place to fix it. Not running the decoder at all is smaller than patching it. The head test used to depend on
_skip_lm_headand onhidden_TD.requires_gradbeing False. Both dependencies are gone, and the code under test (LoRAlm_headplusChunkedLossWrapper) is unchanged. - Hidden width.
model.lm_head.lora_a.weight.shape[1]is the decoder dim, becauselora_ais aLinear(dim, rank)with weight[rank, dim](models/common/lora.py:73). The other test in this file uses the same random-hidden pattern (hidden_TD = torch.randn(8, 5)). - Deleting
test_lora_forward. tests.md asks to name the retained coverage.test_transforms.pycovers LoRA structure. The integration testlora(tests/integration_tests/features.py:1175,llama3_debugmodel_lorawithwqkv/wo, TP2 PP2) runs a full LoRA forward and backward, so a wrong output shape can't regress silently. - Qwen3.5 hunk. Unchanged since my last review: patched by string path, scoped to
model(...). - Overlap search. I searched open PRs for "flex attention CPU" and "test_lora.py". #4576 (Kimi K3 LoRA) edits
tests/unit_tests/cpu/models/common/test_lora.pybut nottest_lora_forward, so the worst case is a trivial rebase conflict.
Before merge: fill in the human-written summary.
Summary (human-written)
Why
Five CPU unit tests fail on any CPU without AVX2 (aarch64, e.g. GB300 dev hosts, and macOS). Each runs a model forward through flex attention, which torchtitan always calls through
torch.compile(FlexInnerAttention._compiled_flex_attn), and Inductor's CPU flex attention template requires AVX2 and not macOS (torch/_inductor/kernel/flex/flex_cpu.py::check_cpu_supported). CPU CI runs on x86, so CI stays green; developers running the CPU suite on these machines see 5 failures unrelated to their change. (Flex is the only attention that runs on CPU at all: varlen is CUDA-only and sdpa is gone for language models.)Example (aarch64, main):
None of these tests is about attention, so the fix keeps compiled attention out of them rather than adding CPU attention coverage:
components/test_loss.py::...::test_lora_head_only_matches_cross_entropylm_headwithChunkedLossWrapper: loss and adapter gradients (the only test of it)models/common/test_lora.py::test_lora_forwardtest_transforms.py, and a wrong shape fails any runmodels/qwen3_5/test_qwen3_5_mrope_positions.py(3 tests)FlexInnerAttention._compiled_flex_attnwith eagerflex_attention(asdistributed/utils.pydoes on ROCm): they need the forward, and the vision encoder always uses flex attentionAlternatives considered
Testing
tests/unit_tests/cpupasses (before: these 5 failed, everything else passed).