-
Notifications
You must be signed in to change notification settings - Fork 459
Make token group alignment size configurable #1503
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
|
||
|
||
from functools import partial | ||
from typing import Callable | ||
from typing import Callable, Literal | ||
|
||
import torch | ||
import torch.distributed as dist | ||
|
@@ -24,6 +24,33 @@ | |
from torch.distributed.tensor.placement_types import Placement | ||
|
||
|
||
TOKEN_GROUP_ALIGN_SIZE_M = 8 | ||
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. OK for now. Later we may want to set this as private field and provide a getter function too. |
||
ValidTokenGroupAlignmentSize = Literal[8, 16, 32] | ||
|
||
|
||
def set_token_group_alignment_size_m( | ||
alignment_size: ValidTokenGroupAlignmentSize, | ||
) -> None: | ||
""" | ||
Set the token group alignment size for token groups in MoE. This is implemented by | ||
padding each token group size to the next multiple of TOKEN_GROUP_ALIGN_SIZE_M. | ||
|
||
Valid values are: 8, 16, or 32. | ||
Different values are needed for different cases: | ||
|
||
* For bf16, 8 is enough (16 byte alignment / 2 bytes per elem = 8 elements). | ||
* For fp8, 16 byte alignment / 1 byte per elem = 16 elements. | ||
* For mxfp8, we need 32 (or block_size) because scaling block size is (1 x 32), | ||
so when doing per-token-group quantization on each logically distinct subtensor, | ||
we need to ensure the contracting dim is divisible by block_size. | ||
In the backward pass, grad_weight = (grad_output_t @ input).t() has gemm dims | ||
of (N, M) @ (M, K) so M is the contracting dim, and group offsets are along M, | ||
so we need 32 element alignment. | ||
""" | ||
global TOKEN_GROUP_ALIGN_SIZE_M | ||
TOKEN_GROUP_ALIGN_SIZE_M = alignment_size | ||
|
||
|
||
# implementation of Tensor Parallel for the GroupedExperts in MoE | ||
class TensorParallel(ParallelStyle): | ||
def _partition_fn(self, name, module, device_mesh): | ||
|
@@ -251,6 +278,7 @@ def wrapper( | |
x: torch.Tensor, | ||
num_tokens_per_expert: torch.Tensor | None = None, | ||
) -> torch.Tensor: | ||
global TOKEN_GROUP_ALIGN_SIZE_M | ||
if isinstance(w1, DTensor): | ||
w1 = w1.to_local() | ||
w2 = w2.to_local() | ||
|
@@ -264,7 +292,6 @@ def wrapper( | |
experts_per_ep_rank = w1.shape[0] | ||
num_ep_ranks = num_tokens_per_expert.shape[0] // experts_per_ep_rank | ||
|
||
ALIGN_SIZE_M = 16 | ||
with torch.no_grad(): | ||
( | ||
permuted_indices, | ||
|
@@ -274,8 +301,8 @@ def wrapper( | |
num_tokens_per_expert, | ||
experts_per_ep_rank, | ||
num_ep_ranks, | ||
x.shape[0] + experts_per_ep_rank * ALIGN_SIZE_M, | ||
ALIGN_SIZE_M, | ||
x.shape[0] + experts_per_ep_rank * TOKEN_GROUP_ALIGN_SIZE_M, | ||
TOKEN_GROUP_ALIGN_SIZE_M, | ||
) | ||
|
||
x = torch.vstack((x, x.new_zeros((x.shape[-1])))) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
don't we need to do this for Float8 as well, as IIRC it supports grouped gemm too
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.
Yes but the default (16) is what is needed for float8, so no need to manually set it.
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.
I'm not sure if we should use 16 as default.
For bf16, is 16 enough or is 8 enough?
I think we should still set it, in case the default changes later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Actually yeah I think you're right.
block_size
) because scaling block size is (1 x 32), so when doing per-token-group quantization on each logically distinct subtensor, we need to ensure the contracting dim is divisible by block_size. In the backward pass,grad_weight = (grad_output_t @ input).t()
has gemm dims (N, M) @ (M, K) so M is the contracting dim, and group offsets are along M, so we need 32 element alignment.Updated this accordingly.