-
Notifications
You must be signed in to change notification settings - Fork 81
feat: experimental two-phase (head-chunked) Ulysses all-to-all #428
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
Draft
csgoogle
wants to merge
1
commit into
main
Choose a base branch
from
sagarchapara/ulysses-two-phase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+33
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import contextlib | ||
| import functools | ||
| import math | ||
| import os | ||
| from typing import Optional, Callable, Tuple, Any, Dict | ||
| import flax.linen as nn | ||
| from flax import nnx | ||
|
|
@@ -605,6 +606,21 @@ def _ulysses_attention( | |
| "Ulysses attention requires the number of heads to be divisible by the context shard count, " | ||
| f"got heads={num_heads} and context_shards={num_shards}." | ||
| ) | ||
|
|
||
| # EXPERIMENTAL: split the all-to-all into `num_chunks` head-groups so XLA's | ||
| # async-collective scheduler can overlap one chunk's attention compute with | ||
| # the next chunk's all-to-all. Gated on an env var so it stays opt-in. The | ||
| # math is identical to the single-shot path (heads are independent); requires | ||
| # async-collective LIBTPU flags to actually overlap, and the per-chunk head | ||
| # count must still be shardable across the context axis. | ||
| num_chunks = int(os.environ.get("ULYSSES_ATTENTION_CHUNKS", "1")) | ||
|
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. Let's move this to config file to be used for any ulysses type kernel |
||
| if num_chunks > 1: | ||
| if num_heads % (num_shards * num_chunks) != 0: | ||
| raise ValueError( | ||
| "ULYSSES_ATTENTION_CHUNKS requires heads divisible by (context_shards * chunks), " | ||
| f"got heads={num_heads}, context_shards={num_shards}, chunks={num_chunks}." | ||
| ) | ||
|
|
||
| if not use_custom_kernel: | ||
| block_sizes = _select_flash_block_sizes(query, key, flash_block_sizes, dtype, "flash") | ||
|
|
||
|
|
@@ -721,7 +737,23 @@ def wrap_ulysses_attention(query, key, value): | |
| "Warning, batch dimension should be shardable among the devices in data and fsdp" | ||
| f" axis, batch dimension: {query.shape[0]}, devices_in_batch_sharding: {devices_in_batch_sharding}" | ||
| ) | ||
| x = wrap_ulysses_attention(query, key, value) | ||
| if num_chunks > 1: | ||
| # EXPERIMENTAL two-phase path: run the all-to-all + attention per head-group. | ||
| # Heads are independent, so this is numerically identical to the single-shot | ||
| # path; the goal is to let XLA overlap one group's compute with the next | ||
| # group's all-to-all (requires async-collective LIBTPU flags). | ||
| head_step = num_heads // num_chunks | ||
| chunk_outputs = [ | ||
| wrap_ulysses_attention( | ||
| query[:, i * head_step : (i + 1) * head_step], | ||
| key[:, i * head_step : (i + 1) * head_step], | ||
| value[:, i * head_step : (i + 1) * head_step], | ||
| ) | ||
| for i in range(num_chunks) | ||
| ] | ||
| x = jnp.concatenate(chunk_outputs, axis=1) | ||
| else: | ||
| x = wrap_ulysses_attention(query, key, value) | ||
| x = x[:, :, :orig_q_seq_len, :] | ||
| x = _reshape_heads_to_head_dim(x) | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
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.
Does this work on ulysses + ring as well?