Skip to content

[tests] Keep compiled flex attention out of CPU unit tests - #6422

Merged
tianyu-l merged 1 commit into
mainfrom
lora-test-flex-eager
Nov 28, 2026
Merged

tianyu-l merged 1 commit into
mainfrom
lora-test-flex-eager

Conversation

@tianyu-l

@tianyu-l tianyu-l commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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):

pytest tests/unit_tests/cpu/components/test_loss.py -k test_lora_head_only_matches_cross_entropy
E  torch._inductor.exc.InductorError: LoweringException: NotImplementedError:
E    torch.compile on current platform is not supported for CPU.  target: flex_attention

None of these tests is about attention, so the fix keeps compiled attention out of them rather than adding CPU attention coverage:

Test What it guards Change
components/test_loss.py::...::test_lora_head_only_matches_cross_entropy the LoRA lm_head with ChunkedLossWrapper: loss and adapter gradients (the only test of it) feeds the head random decoder outputs instead of running the frozen decoder
models/common/test_lora.py::test_lora_forward a LoRA forward's output shape deleted: LoRA structure is tested in test_transforms.py, and a wrong shape fails any run
models/qwen3_5/test_qwen3_5_mrope_positions.py (3 tests) which positions reach the layers (M-RoPE vs 1D), a silent-numerics contract patch FlexInnerAttention._compiled_flex_attn with eager flex_attention (as distributed/utils.py does on ROCm): they need the forward, and the vision encoder always uses flex attention

Alternatives considered

  • Patch eager flex attention into all five: keeps a model forward in a test that only needs the head, and a shape-only test.
  • Stub the Qwen3.5 vision encoder like the tests already stub the text layers: drops the real image path from a test about image positions.
  • Skip without AVX2: drops the coverage on those machines.

Testing

  • aarch64 (GB300 host): the 3 files pass (86 tests); the rest of tests/unit_tests/cpu passes (before: these 5 failed, everything else passed).
  • x86 CI runs the same tests.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 27, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 27, 2026
@tianyu-l

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 34s —— View job


Approve -- test_lora_forward hits the same CPU failure and needs the same eager-flex patch before merging. See the review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. tests/unit_tests/cpu/models/common/test_lora.py:161 -- test_lora_forward runs the same CPU forward: build_model_config("debugmodel", attn_backend="flex", ...), then model(...) 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 covered test_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.py and the graph_trainer tests already swap, so there is no new mechanism.
  • Patch target. The decorator replaces _compiled_flex_attn, the class variable that compiled_flex_attn (attention.py:812) calls. That is the right target. patch.object restores it afterwards, so other tests in the process are unaffected.
  • Backward. The test's backward only flows through lm_head, because hidden_TD.requires_grad is 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_forward is 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.

@tianyu-l
tianyu-l force-pushed the lora-test-flex-eager branch from d43188c to a2fe85f Compare November 28, 2026 00:00
@tianyu-l tianyu-l changed the title [tests] Run the LoRA lm_head loss test with eager flex attention [tests] Run CPU forward tests with eager flex attention Nov 28, 2026
@tianyu-l

Copy link
Copy Markdown
Contributor Author

@claude review again

@claude

claude Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 50s —— View job


Approve: the earlier must-fix item (test_lora_forward) is resolved, and there are no new findings. See the review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. test_lora.py:164, test_lora_forward needed 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.py and the new test_qwen3_5_mrope_positions.py hunk.
  • Qwen3.5 patch. The vision encoder uses FlexInnerAttention whatever 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 with patch.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 wraps model(...) and is undone afterwards, so other tests in the process are unaffected.
  • Patch target. compiled_flex_attn calls FlexInnerAttention._compiled_flex_attn through 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/cpu on 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.
@tianyu-l
tianyu-l force-pushed the lora-test-flex-eager branch from a2fe85f to badfd0c Compare November 28, 2026 04:22
@tianyu-l tianyu-l changed the title [tests] Run CPU forward tests with eager flex attention [tests] Keep compiled flex attention out of CPU unit tests Nov 28, 2026
@tianyu-l

Copy link
Copy Markdown
Contributor Author

@claude review again

@claude

claude Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

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 attn_backend="flex" in test_loss.py). See the review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. test_lora.py, test_lora_forward hit 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_head and on hidden_TD.requires_grad being False. Both dependencies are gone, and the code under test (LoRA lm_head plus ChunkedLossWrapper) is unchanged.
  • Hidden width. model.lm_head.lora_a.weight.shape[1] is the decoder dim, because lora_a is a Linear(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.py covers LoRA structure. The integration test lora (tests/integration_tests/features.py:1175, llama3_debugmodel_lora with wqkv/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.py but not test_lora_forward, so the worst case is a trivial rebase conflict.

Before merge: fill in the human-written summary.

@tianyu-l
tianyu-l merged commit 1ae4b48 into main Nov 28, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

2 participants