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

Reduce buffer copying by using one device to reduce and distribute #957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions sharktank/sharktank/ops/sharded_impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ def all_gather_split(
def all_reduce_split_or_unreduced(
input: Union[SplitPrimitiveTensor, UnreducedTensor],
) -> ReplicatedTensor:
# For each device move the shards to it and do a reduction.
# If we don't move first, common sub-expression elimination is free to collapse all
# reductions into one and then copy to all devices, which is not what we want.
reduced = functools.reduce(
lambda x, y: elementwise(torch.add, x, y),
[
(
transfer_to_logical_device(shard, 0)
if i != 0
else barrier_on_logical_device(shard, 0)
)
for i, shard in enumerate(input.shards)
],
)
shards = [
functools.reduce(
lambda x, y: elementwise(torch.add, x, y),
[
(
barrier_on_logical_device(shard, i)
if i == j
else transfer_to_logical_device(shard, i)
)
for j, shard in enumerate(input.shards)
],
(
transfer_to_logical_device(reduced, i)
if i != 0
else barrier_on_logical_device(reduced, 0)
)
for i in range(input.shard_count)
]
Expand Down
Loading