-
Notifications
You must be signed in to change notification settings - Fork 554
Add Tokamax ring MHA attention for TPU #4266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9b610fb
98393b6
a54462b
0eccf59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3261,15 +3261,60 @@ def calculate_global_batch_sizes(per_device_batch_size, expansion_factor, num_de | |
| context_parallel_size = getattr(self, f"ici_{self.context_sharding}_parallelism", 1) * getattr( | ||
| self, f"dcn_{self.context_sharding}_parallelism", 1 | ||
| ) | ||
| 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() | ||
| if ( | ||
| context_parallel_strategy == "ring" | ||
| and "gpu" not in self.hardware | ||
| and "tpu" not in self.hardware | ||
| and context_parallel_size > 1 | ||
| ): | ||
| raise ValueError( | ||
| "Ring context parallelism strategy (context_parallel_strategy='ring') is only supported on GPUs " | ||
| "or TPU with attention=flash and use_tokamax_splash=True." | ||
| ) | ||
| if context_parallel_strategy == "ring" and "gpu" not in self.hardware and "tpu" in self.hardware: | ||
| if context_parallel_size <= 1: | ||
| raise ValueError("TPU Tokamax ring attention requires context_parallel_size > 1.") | ||
| if self.context_sharding != "context": | ||
| raise ValueError("TPU Tokamax ring attention requires context_sharding='context'.") | ||
| if self.dq_reduction_steps not in (0, 3): | ||
| raise ValueError("TPU Tokamax ring attention requires dq_reduction_steps to be 0 or 3.") | ||
| if self.max_target_length % (context_parallel_size * context_parallel_size) != 0: | ||
| raise ValueError( | ||
| "Ring context parallelism strategy (context_parallel_strategy='ring') is only supported on GPUs." | ||
| "TPU Tokamax ring attention requires max_target_length to be divisible by context_parallel_size squared." | ||
| ) | ||
| if self.attention != "flash": | ||
| raise ValueError("TPU ring context parallelism requires attention=flash.") | ||
| if not self.use_tokamax_splash: | ||
| raise ValueError("TPU ring context parallelism requires use_tokamax_splash=True.") | ||
| if self.use_jax_splash: | ||
| raise ValueError("TPU ring context parallelism requires use_jax_splash=False.") | ||
| if self.attention_type != "global": | ||
| raise ValueError("TPU Tokamax ring attention is initially supported only for global causal attention.") | ||
| if self.packing: | ||
| raise ValueError("TPU Tokamax ring attention does not support packing yet.") | ||
| if self.context_parallel_load_balance: | ||
| raise ValueError("TPU Tokamax ring attention does not support context_parallel_load_balance yet.") | ||
| if self.use_ragged_attention: | ||
| raise ValueError("TPU Tokamax ring attention does not support ragged attention.") | ||
| if self.attention_sink: | ||
| raise ValueError("TPU Tokamax ring attention does not support attention sinks.") | ||
| if self.use_indexer: | ||
| raise ValueError("TPU Tokamax ring attention does not support sparse indexer masks.") | ||
| if self.use_chunked_prefill: | ||
| raise ValueError("TPU Tokamax ring attention does not support chunked prefill yet.") | ||
| if self.moba: | ||
| raise ValueError("TPU Tokamax ring attention does not support MoBA.") | ||
| if self.use_multimodal: | ||
| raise ValueError("TPU Tokamax ring attention does not support multimodal attention.") | ||
| 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.") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a neat idea. |
||
| # STRIPED reorder strategy is a Transformer Engine feature and is GPU-only. | ||
| # The AUTO + packing case (which training resolves to STRIPED) is not validated here | ||
| # because test code paths may load the same config but use a different reorder path. | ||
| # Training's runtime path in max_utils.reorder_causal_load_balanced enforces this. | ||
| # The AUTO + packing case, which training resolves to STRIPED, is not | ||
| # validated here because test code paths may load the same config but use a | ||
| # different reorder path. Training's runtime path enforces this. | ||
| if ( | ||
| context_parallel_size > 1 | ||
| and "gpu" not in self.hardware | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would touch several paths so perhaps a separate PR?