-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Clamp capacity to num_tokens in MoE gating drop branches (complete #5353) #8155
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,6 +224,10 @@ def top1gating(logits: Tensor, | |
| new_capacity = torch.ceil(new_capacity / tp).mul(tp).to(new_capacity.dtype) | ||
| # Make sure the capacity value does not exceed the number of tokens. | ||
| capacity = min(new_capacity, torch.tensor(mask1.size(0)).to(new_capacity.device)) | ||
| else: | ||
| # Same guard for the drop branch: capacity feeds torch.topk(..., dim=0) over the token | ||
| # dimension in _top_idx below, which requires capacity <= num_tokens. | ||
| capacity = min(capacity, torch.tensor(mask1.size(0)).to(capacity.device)) | ||
|
|
||
| # Compute l_aux | ||
| me = torch.mean(gates, dim=0) | ||
|
|
@@ -403,6 +407,11 @@ def topkgating( | |
| if drop_tokens: | ||
| # Calculate configured capacity and remove locations outside capacity from mask | ||
| capacity = _capacity(gates, torch.tensor(capacity_factor * k), torch.tensor(min_capacity)) | ||
| # Make sure the capacity value does not exceed the number of tokens. The drop_policy=='probs' | ||
| # branch below selects tokens with torch.topk(..., k=capacity, dim=0) over the token | ||
| # dimension, which requires capacity <= num_tokens; #5353 added this same clamp to | ||
| # top1gating's no-drop branch but not to the drop branches. | ||
| capacity = min(capacity, torch.tensor(gates.size(0)).to(capacity.device)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When callers use Useful? React with 👍 / 👎. |
||
| # update mask and locations by capacity | ||
|
|
||
| if drop_policy == 'probs': | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When tensor-model parallelism is enabled,
MOELayer.forwardlater callsdrop_tokenson the capacity dimension, anddrop_tokensasserts that this dimension is divisible by the TP world size (deepspeed/moe/mappings.py:64-65). For top-1 withdrop_tokens=True,capacity_factor > num_experts, and a local token count that is not TP-divisible, this line sets the dispatch capacity tonum_tokens;_top_idxsucceeds, but the subsequent TP drop still fails. Clamp only thekpassed to_top_idx(or repad the dispatch capacity) instead of reusing the clamped value for_one_hot_to_float.Useful? React with 👍 / 👎.