Add Tokamax ring MHA attention for TPU#4266
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
050abe5 to
fb361d0
Compare
fb361d0 to
9b610fb
Compare
ce68360 to
f02b0bc
Compare
f02b0bc to
98393b6
Compare
# Conflicts: # src/maxtext/layers/attention_op.py
8219873 to
a54462b
Compare
| if keys["use_qk_clip"]: | ||
| raise ValueError("TPU Tokamax ring attention does not support QK-Clip statistics yet.") | ||
| if keys["enable_dropout"] and keys["dropout_rate"] > 0.0: | ||
| raise ValueError("TPU Tokamax ring attention does not support dropout yet.") |
There was a problem hiding this comment.
pyconfig_deprecated.py is no longer in use.
| if self.use_qk_clip: | ||
| raise ValueError("TPU Tokamax ring attention does not support QK-Clip statistics yet.") | ||
| if self.enable_dropout and self.dropout_rate > 0.0: | ||
| raise ValueError("TPU Tokamax ring attention does not support dropout yet.") |
There was a problem hiding this comment.
This is a great, exhaustive list of early stopping conditions (we need more of this!). As a quick refactoring idea: what do you think about mapping these out in a dictionary instead of using a long chain of if statements?
There was a problem hiding this comment.
That's a neat idea.
I saw people mostly use if....raise individually in this file so I didn't bring in more abstractions. I will make changes though if you insist but it's a P2 for now : )
| indexer_mask=indexer_mask, | ||
| use_ragged_attention=use_ragged_attention, | ||
| record_max_logits=record_max_logits, | ||
| ) |
There was a problem hiding this comment.
I would move the validation logic to initi of the module
| axis_names_kv=axis_names_kv, | ||
| dkv_dim_q=3, | ||
| dkv_dim_kv=3, | ||
| ) |
There was a problem hiding this comment.
I would move validation logic to init instead of call
| mha_generic_flash_cp_output = jax.device_get(mha_generic_flash_cp_output) | ||
|
|
||
| self.assertTrue( | ||
| jax.numpy.allclose(mha_generic_output, mha_generic_flash_cp_output, rtol=1e-01, atol=1e-01, equal_nan=False), |
There was a problem hiding this comment.
are 1e-01 the best we can do? maybe try using dtype=float32 and see if we can reduce them
There was a problem hiding this comment.
1e-1 was due to following other similar instances for comparing cross kernels. Some examples: attention_test.py line 674 (dot_product vs flash), 881 (dot_product vs flash_cp) , 1872 (MLA CP dot_product vs flash+cp).
Edited:
This is the best we can go afaik
# Forward
rtol=1e-2, atol=1e-2
# Backward
rtol=1e-2, atol=1e-7
| hooks: | ||
| - id: pyink | ||
| exclude: src/maxtext/input_pipeline/protos/ | ||
| exclude: src/maxtext/input_pipeline/protos/|src/maxtext/kernels/tokamax_splash_attention/(base|ring_attention_kernel|splash_attention_kernel|splash_attention_mask|splash_attention_mask_info)\.py |
There was a problem hiding this comment.
not necessary, please do not change this file
| - "src/maxtext/kernels/tokamax_splash_attention/ring_attention_kernel.py" | ||
| - "src/maxtext/kernels/tokamax_splash_attention/splash_attention_kernel.py" | ||
| - "src/maxtext/kernels/tokamax_splash_attention/splash_attention_mask.py" | ||
| - "src/maxtext/kernels/tokamax_splash_attention/splash_attention_mask_info.py" |
There was a problem hiding this comment.
I just wanted codecov to ignore the bulk from tokamax but ya we don't have to touch this
| from maxtext.kernels.attention import tokamax_ring_attention | ||
|
|
||
|
|
||
| class TokamaxRingAttentionTest(absltest.TestCase): |
There was a problem hiding this comment.
I think many of these tests are ran on CPU instead of TPU. For shape checks we could use AOT instead of real runs. We could chat more about details.
There was a problem hiding this comment.
for TPU previously we only had forward test and I added backward test.
45e5f89 to
37b508c
Compare
| ) | ||
| if context_parallel_size > 1 and self.context_parallel_strategy.lower() == "ring": | ||
| if "gpu" not in self.hardware: | ||
| context_parallel_strategy = self.context_parallel_strategy.lower() |
There was a problem hiding this comment.
nit: alternatively we can create Enum for context parallel strategy, e.g. in https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/common/common_types.py, to avoid mis-spell.
There was a problem hiding this comment.
that would touch several paths so perhaps a separate PR?
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Tokamax ring attention helpers.""" |
There was a problem hiding this comment.
please add references in tokamax
| # limitations under the License. | ||
| """Tokamax SplashAttention runtime. | ||
|
|
||
| These modules are adapted from OpenXLA Tokamax SplashAttention. |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== |
There was a problem hiding this comment.
Is this file necessary to clone? Looks like it is mask-unrelated? I am inclined to minimize cloning as much as possible.
There was a problem hiding this comment.
I'll respond below
| return f"splash_{attention_type}_{phase}{segments}{residuals}" | ||
|
|
||
|
|
||
| # Splash attention implementation |
There was a problem hiding this comment.
not empty. I didn't change anything about this part from tokamax 0.0.12. It just means it owns the stuff below
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== |
There was a problem hiding this comment.
is this file necessary to clone?
There was a problem hiding this comment.
I'll respond below
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== |
There was a problem hiding this comment.
is this file necessary to clone?
There was a problem hiding this comment.
I'll respond below
NuojCheng
left a comment
There was a problem hiding this comment.
Overall LGTM. I am a little scared by the scale of cloned codes. I have two suggestions on editing the PR description:
- Please specify reasons why cloning tokamax ring/splash and making local changes are necessary;
- Please provide example maxtext command for running ring attention, with performance comparison, see example description of PR#2783, also add link to your doc.
…kamax-ring-tpu # Conflicts: # src/maxtext/configs/pyconfig_deprecated.py # src/maxtext/layers/attention_op.py # tests/unit/configs_value_test.py
37b508c to
0eccf59
Compare
Description
Adds TPU support for
context_parallel_strategy=ringin Flash attention using Tokamax Splash.The ring path keeps Q sequence-sharded, rotates K/V blocks across the context axis, and combines the partial attention results using online softmax. This PR uses the Tokamax Splash components and includes the MaxText integration, config validation, and sharding checks.
Sequence packing and CP load balancing are not enabled in this PR.
Tests
Ran:
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.