-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[WIP] Merging AutoSP into DeepSpeed #7860
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
neeldani
wants to merge
9
commits into
deepspeedai:master
Choose a base branch
from
neeldani:autosp
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b57ccb8
add autosp backend
neeldani 4df32d1
add benchmarking script
neeldani fad4846
Merge remote-tracking branch 'upstream/master' into autosp
neeldani 10d9cc0
Merge branch 'master' into autosp
sfc-gh-truwase ecbc6ea
move bench scripts to DeepSpeedExamples
neeldani a38674e
move constants and apis to deepspeed library
neeldani fea194c
add zero-1 interoperability to autosp
bd916b7
fix early termination of gradients issue when using autosp
d6a0aaa
Merge pull request #1 from neeldani/staging
spikerheado1234 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
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import torch | ||
| import torch.distributed as dist | ||
| from .sp_dp_registry import get_group, is_setup, sp_size, dp_size | ||
|
|
||
| @torch.library.custom_op("autosp::all_to_all", mutates_args=()) | ||
| def all_to_all( | ||
| input: torch.Tensor, | ||
| scatter_idx: int, | ||
| gather_idx: int, | ||
| name: str, | ||
| ) -> torch.Tensor: | ||
| """ | ||
| All-to-all collective for SDPA tensors [B, N, S, H]. | ||
|
|
||
| For QKV (scatter_idx=1, gather_idx=2): | ||
| [B, N, S/P, H] -> [B, N/P, S, H] | ||
| For O (scatter_idx=2, gather_idx=1): | ||
| [B, N/P, S, H] -> [B, N, S/P, H] | ||
| """ | ||
| assert is_setup(), 'Incorrect initialization of SP/DP mesh.' | ||
| B, dim1, dim2, H = input.shape | ||
| gid = dist.get_rank() // sp_size() | ||
| group = get_group(gid) | ||
|
|
||
| if scatter_idx == 1: | ||
| N, local_S = dim1, dim2 | ||
| input_t = input.reshape(B, sp_size(), N // sp_size(), local_S, H) | ||
| input_t = input_t.permute(1, 0, 2, 3, 4).contiguous() | ||
|
|
||
| output = torch.empty_like(input_t) | ||
| dist.all_to_all_single(output, input_t, group=group) | ||
|
|
||
| output = output.permute(1, 2, 0, 3, 4).contiguous() | ||
| output = output.reshape(B, N // sp_size(), sp_size() * local_S, H) | ||
| else: # scatter_idx == 2, O: scatter sequence, gather heads | ||
| local_N, S = dim1, dim2 | ||
| input_t = input.reshape(B, local_N, sp_size(), S // sp_size(), H) | ||
| input_t = input_t.permute(2, 0, 1, 3, 4).contiguous() | ||
|
|
||
| output = torch.empty_like(input_t) | ||
| dist.all_to_all_single(output, input_t, group=group) | ||
|
|
||
| output = output.permute(1, 0, 2, 3, 4).contiguous() | ||
| output = output.reshape(B, sp_size() * local_N, S // sp_size(), H) | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| @torch.library.register_fake("autosp::all_to_all") | ||
| def all_to_all_fake(input: torch.Tensor, scatter_idx: int, gather_idx: int, name: str): | ||
| B, dim1, dim2, H = input.shape | ||
| if scatter_idx == 1: | ||
| return input.new_empty(B, dim1 // sp_size(), dim2 * sp_size(), H) | ||
| else: | ||
| return input.new_empty(B, dim1 * sp_size(), dim2 // sp_size(), H) | ||
|
|
||
|
|
||
| def _all_to_all_backward_setup(ctx, inputs, output): | ||
| _, scatter_idx, gather_idx, name = inputs | ||
| ctx.scatter_idx = gather_idx | ||
| ctx.gather_idx = scatter_idx | ||
| ctx.name = name + "_grad" | ||
|
|
||
|
|
||
| def _all_to_all_backward(ctx, grad): | ||
| return ( | ||
| all_to_all(grad, ctx.scatter_idx, ctx.gather_idx, ctx.name), | ||
| None, None, None, None | ||
| ) | ||
|
|
||
|
|
||
| torch.library.register_autograd( | ||
| "autosp::all_to_all", _all_to_all_backward, setup_context=_all_to_all_backward_setup | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import torch | ||
| import torch.distributed as dist | ||
|
|
||
| GROUP_REGISTRY = {} # int -> dist.ProcessGroup | ||
|
|
||
| def register_groups(groups): | ||
| """groups: List[List[int]], e.g. [[0,1],[2,3]]""" | ||
| for gid, ranks in enumerate(groups): | ||
| if gid not in GROUP_REGISTRY: | ||
| GROUP_REGISTRY[gid] = dist.new_group(ranks) | ||
|
|
||
| def get_group(gid: int): | ||
| return GROUP_REGISTRY[gid] if gid is not None else dist.group.WORLD | ||
|
|
||
| def get_registry(): | ||
| return GROUP_REGISTRY | ||
|
|
||
| def is_setup(): | ||
| return GROUP_REGISTRY['is_reg'] if 'is_reg' in GROUP_REGISTRY else False | ||
|
|
||
| def sp_size(): | ||
| assert 'SP_SIZE' in GROUP_REGISTRY, 'SP_SIZE not init properly.' | ||
|
|
||
| return GROUP_REGISTRY['SP_SIZE'] | ||
|
|
||
| def dp_size(): | ||
| assert 'DP_SIZE' in GROUP_REGISTRY, 'DP_SIZE not init properly' | ||
|
|
||
| return GROUP_REGISTRY['DP_SIZE'] | ||
|
|
||
| def populate_registry(SP_SIZE, DP_SIZE): | ||
| """ Populate rank to SP/DP mesh index. """ | ||
|
|
||
| if GROUP_REGISTRY.get('is_reg', False): | ||
| return | ||
|
|
||
| group_listing = [] | ||
| offset = 0 | ||
| for _ in range(DP_SIZE): | ||
| group_listing.append([i + offset for i in range(SP_SIZE)]) | ||
| offset += SP_SIZE | ||
|
|
||
| register_groups(group_listing) | ||
|
|
||
| ## Extraneous metadata required for proper instatiation. ## | ||
| GROUP_REGISTRY['SP_SIZE'] = SP_SIZE | ||
| GROUP_REGISTRY['DP_SIZE'] = DP_SIZE | ||
| GROUP_REGISTRY['is_reg'] = True |
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import torch | ||
| from torch.fx import GraphModule | ||
| from .passes.sp_compile import apply_autosp | ||
|
|
||
| def init_autosp(compile_config): | ||
| def backend_fn(gm: GraphModule, real_inputs): | ||
| apply_autosp(gm, real_inputs, debug=False, sp_size=compile_config.sp_size, dp_size=compile_config.dp_size) | ||
| return torch._inductor.compile(gm, real_inputs) | ||
| return backend_fn |
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.
(Not about this file) Don't we need
__init__.pyincustom_ops?