Skip to content

fixed: Modified the topkgating function and modified the test_moe file for testing #7163

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions deepspeed/moe/sharded_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ def topkgating(
tp = 1 if groups.mpu is None else bwc_tensor_model_parallel_world_size(mpu=groups.mpu)
new_capacity = torch.ceil(new_capacity / tp).mul(tp).to(new_capacity.dtype)
capacity = new_capacity
locations = torch.cumsum(mask, dim=0) - 1

# normalize gates
gates_masked = gates * mask
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/moe/test_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ def check_equal(logits, cap, sparse_truth, res):
position_dispatch_res = topkgating(logits2, 3, 1, min_capacity=1, drop_policy='position')[2]
check_equal(logits2, 2, position_sec_sparse, position_dispatch_res)

#s=4 e=4 topk=2 drop_tokens=False
logits3 = torch.tensor([[0.95, 0.85, 0.90, 0.80], [0.70, 0.65, 0.75, 0.60], [0.50, 0.55, 0.45, 0.40],
[0.35, 0.30, 0.25, 0.20]])
logits3 *= dist.get_rank() + 1
dispatch_res = topkgating(logits3, 2, 1, min_capacity=1, drop_tokens=False)[2]
sec_sparse = torch.tensor([[0, 0, 0], [0, 2, 0], [1, 0, 1], [1, 2, 1], [2, 0, 2], [2, 1, 0], [3, 0, 3],
[3, 1, 1]])
check_equal(logits3, 4, sec_sparse, dispatch_res)


class TestExpertWeightGradWithZero(DistributedTest):
world_size = 2
Expand Down