Skip to content
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

Distributed Expand #18126

Merged
merged 5 commits into from
Oct 28, 2023
Merged

Distributed Expand #18126

merged 5 commits into from
Oct 28, 2023

Conversation

wschin
Copy link
Contributor

@wschin wschin commented Oct 27, 2023

This PR implements DistributedExpand for llama 2.

Representative Examples of DistributedExpand:

  • [shard on non-expanded axis] input tensor (shape=[8, 1], spec=S[0]R, device_mesh=[0,1]) -> Expand(target_shape=[8, 2] -> output tensor (shape=[8, 2], spec=S[0]R, device_mesh=[0,1])
  • [sharding expanded axis is invalid since it must have dim=1 and axis with dim=1 cannot be sharded] input tensor (shape=[1, 8], spec=S[0]R, device_mesh=[0,1]) -> Expand(target_shape=[2, 8] -> output tensor (shape=[2, 8], spec=S[0]R, device_mesh=[0,1])

From those examples, we observe a few important behaviors.

  • The output sharding spec is always the same to the input sharding spec.
  • Expanding always happen on axis with dimension=1. Otherwise, it will violate the broadcasting rule.
  • No communication is needed since all computation can happen locally. Let's consider the first example again. If you put the first half tensor (shape: [4, 1]) on device 0 and the second half (shape: [4, 1]) on device 1, then Expand it with target shape [4, 2] , these two local tensors (shape: [4, 2]) are exactly the same as the one described by output sharding spec.

Algorithm:

  • Compute logical (i.e., unsharded) shapes of input and output.
  • Compute sharded output shape from logical output.
  • Call Expand to broadcast local input to sharded output shape.

How to review?

  • Start with changes in onnxruntime_test_distributed.py. Those tests are good examples for using this op.
  • Read expand.h/expand.cc. Theose changes are for exposing functionalities in Expand to DistributedExpand.
  • Read distributed_expand.h/distributed_expand.cc. It follows the algorithm described above. The commit 68ac301 first sketches the definition of DistributedExpand. The next commit 0eb9330 adds real implementation.

@wschin wschin marked this pull request as ready for review October 27, 2023 19:26
@wschin wschin requested a review from souptc October 27, 2023 19:31
@wschin wschin closed this Oct 28, 2023
@wschin wschin reopened this Oct 28, 2023
@wschin wschin merged commit 24f9c1a into main Oct 28, 2023
97 of 101 checks passed
@wschin wschin deleted the wechi/d-expand branch October 28, 2023 07:44
kleiti pushed a commit to kleiti/onnxruntime that referenced this pull request Mar 22, 2024
This PR implements DistributedExpand for llama 2.

Representative Examples of DistributedExpand:
- [shard on non-expanded axis] `input tensor (shape=[8, 1], spec=S[0]R,
device_mesh=[0,1]) -> Expand(target_shape=[8, 2] -> output tensor
(shape=[8, 2], spec=S[0]R, device_mesh=[0,1])`
- [sharding expanded axis is invalid since it must have dim=1 and axis
with dim=1 cannot be sharded] `input tensor (shape=[1, 8], spec=S[0]R,
device_mesh=[0,1]) -> Expand(target_shape=[2, 8] -> output tensor
(shape=[2, 8], spec=S[0]R, device_mesh=[0,1])`

From those examples, we observe a few important behaviors.

- The output sharding spec is always the same to the input sharding
spec.
- Expanding always happen on axis with dimension=1. Otherwise, it will
violate the broadcasting rule.
- No communication is needed since all computation can happen locally.
Let's consider the first example again. If you put the first half tensor
(shape: [4, 1]) on device 0 and the second half (shape: [4, 1]) on
device 1, then `Expand` it with target shape [4, 2] , these two local
tensors (shape: [4, 2]) are exactly the same as the one described by
output sharding spec.

Algorithm:
- Compute logical (i.e., unsharded) shapes of input and output.
- Compute sharded output shape from logical output.
- Call Expand to broadcast local input to sharded output shape.

How to review?
- Start with [changes in
onnxruntime_test_distributed.py](microsoft@ea33392).
Those tests are good examples for using this op.
- [Read
expand.h/expand.cc](microsoft@e4c4998).
Theose changes are for exposing functionalities in Expand to
DistributedExpand.
- Read distributed_expand.h/distributed_expand.cc. It follows the
algorithm described above. The commit
microsoft@68ac301
first sketches the definition of DistributedExpand. The next commit
microsoft@0eb9330
adds real implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants