fix: fallback to dot_product for autoselected attention on CPU to prevent Tokamax NotImplementedError - #4617
Conversation
The tokamax migration (AI-Hypercomputer#4384) introduced a strict hardware check that causes jax.eval_shape (used heavily during checkpoint conversion) to crash with NotImplementedError when hardware=cpu is passed. This commit intercepts target_hardware == "cpu" inside attention_op.py when attention_kernel="autoselected". It safely falls back to native JAX apply_attention_dot, ensuring that CPU shape tracing and mock evaluations complete without invoking unsupported GPU-specific flash attention logic.
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| self.attention_kernel == "dot_product" | ||
| or (self.attention_kernel == "autoselected" and model_mode == MODEL_MODE_AUTOREGRESSIVE) | ||
| or (self.attention_kernel == "autoselected" and length < 128) | ||
| or (self.attention_kernel == "autoselected" and target_hardware == "cpu") |
There was a problem hiding this comment.
this change gonna silently twist all results using AOT on CPU platform
There was a problem hiding this comment.
I doubt this change can pass https://github.com/AI-Hypercomputer/maxtext/blob/main/tests/integration/aot_identical_test.py
There was a problem hiding this comment.
somehow aot identical test does not show in https://github.com/AI-Hypercomputer/maxtext/actions/runs/30232605541/job/89875651384?pr=4617
There was a problem hiding this comment.
Hi @NuojCheng, thanks for the review! I've double-checked the AOT compilation behavior, and this change works safely without twisting results.
1. AOT correctly bypasses the fallback:
Even when cross-compiling on a CPU host, AOT (via train_compile.py) uses get_topology_desc(platform="tpu") to generate mock devices. These mock devices strictly retain device.platform == "tpu". Thus, target_hardware remains "tpu", properly preserving the flash or autoselected kernels without triggering a fallback to dot_product.
2. aot_identical_test.py passes flawlessly:
I just ran the test manually on my TPU VM and can confirm the generated HLO graphs remain identical and it passes perfectly:
tests/integration/aot_identical_test.py::AotHloIdenticalTest::test_default_hlo_match PASSED
Log: https://paste.googleplex.com/4608946768838656
If it’s missing in recent CI job logs, it’s likely because the test classes are wrapped with @pytest.mark.skip_on_tpu7x. If a GitHub Action runner happens to execute on a Trillium (tpu7x) node, Pytest naturally skips it.
Let me know if this clears up the concerns!
Description
This PR addresses a bug where running tasks that utilize
jax.eval_shape(e.g., checkpoint conversions viato_maxtext.py) on CPU-only hardware crashes abruptly due to an unsupported attention kernel device check.DAG Error log
Shortened Error Trace:
Root Cause
The default configuration for attention in MaxText is
attention="autoselected". Whenautoselectedfalls into the non-TPU branch for the prefill / train mode, it attempts to use the GPU flash attention fallback.jax.experimental.pallas.ops.gpu.attention.mha, which gracefully allowed dummy shape tracing (jax.eval_shape) on CPU devices.tokamax_pallas_triton). Tokamax has strict hardware validations in its tracing logic. When it detectsdevice.device_kind == 'cpu', it immediately raisesNotImplementedError: Not supported on cpu, entirely breaking abstract shape evaluation on CPU nodes.Solution
Modified the routing logic in
src/maxtext/layers/attention_op.py(apply_attention).When
attention_kernelis"autoselected"andtarget_hardwareevaluates to"cpu", it now safely falls back to standard JAX dot-product attention (apply_attention_dot). This completely bypasses the Tokamax flash attention branch on CPU devices, preventing unexpected crashes while maintaining standard performance optimizations for TPUs and GPUs.Tests
Log: https://paste.googleplex.com/6472437859483648
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.