Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.3k
feat: support Ulysses Anything Attention#12996
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
dd56429
feat: support Ulysses Anything Attention
DefTruth 123f526
feat: support Ulysses Anything Attention
DefTruth af9af62
feat: support Ulysses Anything Attention
DefTruth 40800e5
Merge branch 'main' into ulysses-anything
DefTruth 0918547
Merge branch 'main' into ulysses-anything
DefTruth b4d3f07
feat: support Ulysses Anything Attention
DefTruth ece9f15
Merge branch 'main' into ulysses-anything
DefTruth 7f9c412
Merge branch 'main' into ulysses-anything
DefTruth 403c204
fix UAA broken while using joint attn
DefTruth 9280e2b
update
DefTruth a5d2459
Merge branch 'main' into ulysses-anything
DefTruth de157c8
Merge branch 'main' into ulysses-anything
DefTruth 4caa87e
post check
DefTruth 140ece8
add docs
DefTruth 7f2ecf4
Merge branch 'main' into ulysses-anything
DefTruth 4cc56b9
add docs
DefTruth fd88b2d
Merge branch 'ulysses-anything' of https://github.com/xlite-dev/diffu…
DefTruth f1c32d9
Merge branch 'main' into ulysses-anything
DefTruth a74820b
remove lru cache
DefTruth d141801
Merge branch 'main' into ulysses-anything
DefTruth f8f2209
move codes
DefTruth 9924f8b
Merge branch 'main' into ulysses-anything
DefTruth e1d83eb
update
DefTruth e426b64
Merge branch 'ulysses-anything' of https://github.com/xlite-dev/diffu…
DefTruth cb6c4fd
Merge branch 'main' into ulysses-anything
DefTruth ef919b4
Merge branch 'main' into ulysses-anything
DefTruth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
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 |
|---|---|---|
| @@ -19,6 +19,7 @@ | ||
| from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Tuple, Union | ||
| import torch | ||
| import torch.distributed as dist | ||
| from ..utils import get_logger | ||
| @@ -67,6 +68,9 @@ class ContextParallelConfig: | ||
| convert_to_fp32: bool = True | ||
| # TODO: support alltoall | ||
| rotate_method: Literal["allgather", "alltoall"] = "allgather" | ||
| # Whether to enable ulysses anything attention to support | ||
| # any sequence lengths and any head numbers. | ||
| ulysses_anything: bool = False | ||
DefTruth marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| _rank: int = None | ||
| _world_size: int = None | ||
| @@ -94,6 +98,11 @@ def __post_init__(self): | ||
| raise NotImplementedError( | ||
| f"Only rotate_method='allgather' is supported for now, but got {self.rotate_method}." | ||
| ) | ||
| if self.ulysses_anything: | ||
| if self.ulysses_degree == 1: | ||
| raise ValueError("ulysses_degree must be greater than 1 for ulysses_anything to be enabled.") | ||
| if self.ring_degree > 1: | ||
| raise ValueError("ulysses_anything cannot be enabled when ring_degree > 1.") | ||
| @property | ||
| def mesh_shape(self) -> Tuple[int, int]: | ||
| @@ -257,3 +266,39 @@ def __repr__(self): | ||
| # | ||
| # ContextParallelOutput: | ||
| # specifies how to gather the input tensor in the post-forward hook in the layer it is attached to | ||
| # Below are utility functions for distributed communication in context parallelism. | ||
| def gather_size_by_comm(size: int, group: dist.ProcessGroup) -> List[int]: | ||
| r"""Gather the local size from all ranks. | ||
| size: int, local size return: List[int], list of size from all ranks | ||
| """ | ||
| # NOTE(Serving/CP Safety): | ||
| # Do NOT cache this collective result. | ||
| # | ||
| # In "Ulysses Anything" mode, `size` (e.g. per-rank local seq_len / S_LOCAL) | ||
| # may legitimately differ across ranks. If we cache based on the *local* `size`, | ||
| # different ranks can have different cache hit/miss patterns across time. | ||
| # | ||
| # That can lead to a catastrophic distributed hang: | ||
| # - some ranks hit cache and *skip* dist.all_gather() | ||
| # - other ranks miss cache and *enter* dist.all_gather() | ||
| # This mismatched collective participation will stall the process group and | ||
| # eventually trigger NCCL watchdog timeouts (often surfacing later as ALLTOALL | ||
| # timeouts in Ulysses attention). | ||
| world_size = dist.get_world_size(group=group) | ||
| # HACK: Use Gloo backend for all_gather to avoid H2D and D2H overhead | ||
| comm_backends = str(dist.get_backend(group=group)) | ||
| # NOTE: e.g., dist.init_process_group(backend="cpu:gloo,cuda:nccl") | ||
| gather_device = "cpu" if "cpu" in comm_backends else torch.accelerator.current_accelerator() | ||
| gathered_sizes = [torch.empty((1,), device=gather_device, dtype=torch.int64) for _ in range(world_size)] | ||
| dist.all_gather( | ||
| gathered_sizes, | ||
| torch.tensor([size], device=gather_device, dtype=torch.int64), | ||
| group=group, | ||
| ) | ||
| gathered_sizes = [s[0].item() for s in gathered_sizes] | ||
| # NOTE: DON'T use tolist here due to graph break - Explanation: | ||
| # Backend compiler `inductor` failed with aten._local_scalar_dense.default | ||
| return gathered_sizes | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is this from a failed eval? Can it be removed?
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.
I keep the
failedresults here to demonstrate that Ulysses Anything can handle cases that the standard Ulysses, Ring and USP fail to process.