-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Tensor parallelism for Mixture of Experts #2074
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
Merged
Changes from 40 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
347d450
add tensor parallelism support for non-expert groups
siddharth9820 4330936
non-expert tensor parallelism - drop tokens before a2a
siddharth9820 2643c18
support tensor parallelism for non-experts
siddharth9820 f96e0a0
fix formatting
siddharth9820 2dfd09c
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 7af3e87
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 1c4e8a7
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 763fb19
migrate code for dropping tokens from megatron
siddharth9820 0a797fe
change gather function name
siddharth9820 32063d9
fall back to previous error message
siddharth9820 b3e2fd8
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 1d2d975
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 ef4feb1
formatting changes
siddharth9820 ed731d0
change function names
siddharth9820 7712d90
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 eb6dd0a
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 9b4cd1f
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 fda3714
Merge branch 'master' into moe-tensor-parallelism
tjruwase 7962586
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 8006a1d
fix number of local experts
siddharth9820 4378112
Merge branch 'moe-tensor-parallelism' of github.com:microsoft/DeepSpe…
siddharth9820 c9fa997
fix documentation
siddharth9820 474c932
correct log statement
siddharth9820 4fe355d
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 555ad4f
roll back ep-size setting code
siddharth9820 1299a50
Merge branch 'moe-tensor-parallelism' of github.com:microsoft/DeepSpe…
siddharth9820 ad0a147
add detailed comments
siddharth9820 e29ddf6
restore function in groupy.py
siddharth9820 f8469b7
better comments
siddharth9820 f4f217c
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 baa9812
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 9bdeb41
remove code that changes ep_size and convert it to asserts
siddharth9820 d34f69c
correct groups
siddharth9820 01c61b8
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 dd999c0
correction
siddharth9820 382da2e
add copyright
siddharth9820 d00c742
correction
siddharth9820 6a56180
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 da5a688
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 0f3f398
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 725c66b
Merge branch 'master' into moe-tensor-parallelism
awan-10 ae0030d
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 3d6a136
formatting changes
siddharth9820 870dfd0
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 c5acd1c
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 a1c470e
Merge branch 'master' into moe-tensor-parallelism
tjruwase 43216ca
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 b6dd6ea
Merge branch 'master' into moe-tensor-parallelism
tjruwase 372c663
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 5379a21
add unit tests
siddharth9820 50ae30b
small change
siddharth9820 5f040c8
Merge branch 'master' into moe-tensor-parallelism
siddharth9820 8dfe33d
remove amp from tests
siddharth9820 f918175
Merge branch 'moe-tensor-parallelism' of github.com:microsoft/DeepSpe…
siddharth9820 0afa114
Merge branch 'master' into moe-tensor-parallelism
tjruwase 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
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,95 @@ | ||
| ''' | ||
|
siddharth9820 marked this conversation as resolved.
|
||
| Copyright 2022 The Microsoft DeepSpeed Team | ||
| ''' | ||
|
|
||
| # The file has been adapted from the following Megatron-LM file: | ||
| # https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/mpu/mappings.py | ||
| # Git commit hash: 9dc3c42a84aa656f583703cf8b6b4f79f712b796 | ||
| # We retain the following copyright from the original files: | ||
|
|
||
| # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # 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. | ||
|
|
||
| import torch | ||
| import deepspeed | ||
|
|
||
|
|
||
| def _gather_tokens(input_, dim=0): | ||
| """Gather tensors and concatenate them along a dimension""" | ||
| mpu = deepspeed.utils.groups.mpu | ||
| if mpu.get_tensor_model_parallel_world_size() == 1: | ||
| return input_ | ||
|
|
||
| input_ = input_.contiguous() | ||
| # Size and dimension. | ||
| rank = mpu.get_tensor_model_parallel_rank() | ||
|
|
||
| tensor_list = [ | ||
| torch.empty_like(input_) | ||
| for _ in range(mpu.get_tensor_model_parallel_world_size()) | ||
| ] | ||
| tensor_list[rank] = input_ | ||
| deepspeed.comm.all_gather(tensor_list, | ||
| input_, | ||
| group=mpu.get_tensor_model_parallel_group()) | ||
|
|
||
| # Note: torch.cat already creates a contiguous tensor. | ||
| output = torch.cat(tensor_list, dim=dim).contiguous() | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| def _drop_tokens(input_, dim=0): | ||
| """Divide a tensor among the tensor parallel ranks""" | ||
| mpu = deepspeed.utils.groups.mpu | ||
| if mpu.get_tensor_model_parallel_world_size() == 1: | ||
| return input_ | ||
| total_chunks = mpu.get_tensor_model_parallel_world_size() | ||
| this_chunk = mpu.get_tensor_model_parallel_rank() | ||
| assert input_.shape[dim] % total_chunks == 0, f"input dimension {dim} ({input_.shape[dim]}) is not divisible by tensor parallel world size ({total_chunks})" | ||
| chunk_size = input_.shape[dim] // total_chunks | ||
|
|
||
| return torch.narrow(input_, dim, this_chunk * chunk_size, chunk_size) | ||
|
|
||
|
|
||
| class _GatherTokens(torch.autograd.Function): | ||
| """All gather tokens among the tensor parallel ranks""" | ||
| @staticmethod | ||
| def symbolic(graph, input_, dim): | ||
| return _gather_tokens(input_, dim) | ||
|
|
||
| @staticmethod | ||
| def forward(ctx, input_, dim): | ||
| ctx.dim = dim | ||
| return _gather_tokens(input_, dim) | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, grad_output): | ||
| return _drop_tokens(grad_output, ctx.dim), None | ||
|
|
||
|
|
||
| class _DropTokens(torch.autograd.Function): | ||
| "Divide tokens equally among the tensor parallel ranks" | ||
|
|
||
| @staticmethod | ||
| def symbolic(graph, input_, dim): | ||
| return _drop_tokens(input_, dim) | ||
|
|
||
| @staticmethod | ||
| def forward(ctx, input_, dim): | ||
| ctx.dim = dim | ||
| return _drop_tokens(input_, dim) | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, input_): | ||
| return _gather_tokens(input_, ctx.dim), None | ||
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.
Uh oh!
There was an error while loading. Please reload this page.