fix: fallback to dot_product for autoselected attention on CPU to prevent Tokamax NotImplementedError - #4617
Open
chiajunglien wants to merge 1 commit into
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.
chiajunglien
requested review from
NuojCheng,
RissyRan,
aireenmei,
bvandermoon,
gagika,
gobbleturk,
huytransformer,
igorts-git,
jiangjy1982,
parambole,
richjames0,
shralex,
shuningjin,
suexu1025 and
xibinliu
as code owners
July 27, 2026 02:36
|
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! |
YixuanWang-99
approved these changes
Jul 27, 2026
SurbhiJainUSC
approved these changes
Jul 27, 2026
NuojCheng
reviewed
Jul 27, 2026
| 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") |
Collaborator
There was a problem hiding this comment.
this change gonna silently twist all results using AOT on CPU platform
Collaborator
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
Collaborator
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.