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

Avoid duplicate .cpu() call #37

Merged
merged 9 commits into from
Nov 6, 2023
Merged
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
9 changes: 8 additions & 1 deletion megablocks/layers/moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def parallel_forward_once(self, x, expert_weights, top_experts):
# TODO(tgale): It might be faster to do this on the GPU and
# then communicate the results back to the host.
send_counts = repeated_tokens_per_expert.cpu().sum(dim=-1)
recv_counts = parallel_tokens_per_expert.cpu().sum(dim=-1)
parallel_tokens_per_expert_cpu = parallel_tokens_per_expert.cpu()
recv_counts = parallel_tokens_per_expert_cpu.sum(dim=-1)

# Convert the send/recv counts to lists.
send_counts = send_counts.tolist()
Expand Down Expand Up @@ -374,6 +375,12 @@ def parallel_forward_once(self, x, expert_weights, top_experts):

# Locally permute the tokens and perform the expert computation.
# Block to make sure that the cross-device permutation is complete.
if isinstance(self.mlp, mlp.GroupedMLP):
# GroupedMLP requires counts on CPU. We can use the tensor already
# moved to CPU for the prior all_to_all, which avoids an extra
# device synchronization.
parallel_tokens_per_expert = parallel_tokens_per_expert_cpu.sum(
dim=0, dtype=torch.int)
parallel_x_handle.wait()
parallel_x = self.permute_and_compute(
parallel_x,
Expand Down