-
Notifications
You must be signed in to change notification settings - Fork 322
[mxfp8 moe training] add triton kernel for blocked swizzled 3d weight scales #2894
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
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
160 changes: 160 additions & 0 deletions
160
benchmarks/prototype/moe_training/benchmark_3d_blocked_swizzle_scale_kernels.py
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 |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD 3-Clause license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# this benchmarking script is a modified version of the original script from: https://github.com/drisspg/transformer_nuggets/blob/main/transformer_nuggets/utils/benchmark.py | ||
|
||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
import torch | ||
from tabulate import tabulate | ||
from tqdm import tqdm | ||
|
||
from benchmarks.utils import benchmark_cuda_function_in_microseconds | ||
from torchao.prototype.moe_training.kernels.mxfp8_blocked_scales import ( | ||
torch_to_blocked_per_group_3d, | ||
triton_mx_block_rearrange_per_group_3d, | ||
) | ||
|
||
device = torch.device("cuda") | ||
|
||
# Needed since changing args to function causes recompiles | ||
torch._dynamo.config.cache_size_limit = 1000 | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExperimentConfig: | ||
input_shape: tuple[int] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExperimentResult: | ||
torch_time_us: float | ||
triton_time_us: float | ||
torch_mem_bw_gbps: float | ||
triton_mem_bw_gbps: float | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Experiment: | ||
config: ExperimentConfig | ||
result: ExperimentResult | ||
|
||
|
||
def get_configs() -> List[ExperimentConfig]: | ||
# Llama4 shapes. Input activations are scaled along K dim. | ||
block_size = 32 | ||
input_shapes = [ | ||
# w1, w3 scaled along K (fwd) | ||
(1, 8192, 5120 // block_size), | ||
(2, 8192, 5120 // block_size), | ||
(4, 8192, 5120 // block_size), | ||
(8, 8192, 5120 // block_size), | ||
(16, 8192, 5120 // block_size), | ||
# w2 scaled along K (fwd) | ||
(1, 5120, 8192 // block_size), | ||
(2, 5120, 8192 // block_size), | ||
(4, 5120, 8192 // block_size), | ||
(8, 5120, 8192 // block_size), | ||
(16, 5120, 8192 // block_size), | ||
] | ||
configs = [] | ||
for shape in input_shapes: | ||
configs.append( | ||
ExperimentConfig( | ||
input_shape=shape, | ||
) | ||
) | ||
return configs | ||
|
||
|
||
def run_experiment(config: ExperimentConfig) -> ExperimentResult: | ||
input_tensor = torch.randint( | ||
low=0, | ||
high=256, | ||
size=config.input_shape, | ||
dtype=torch.uint8, | ||
device=device, | ||
) | ||
|
||
def warmup(fn, *args, **kwargs): | ||
for _ in range(5): | ||
fn(*args, **kwargs) | ||
|
||
E, N, K = config.input_shape | ||
|
||
# bench torch | ||
compiled_run_torch = torch.compile(torch_to_blocked_per_group_3d) | ||
warmup(compiled_run_torch, input_tensor) | ||
torch_time_us = benchmark_cuda_function_in_microseconds( | ||
compiled_run_torch, | ||
input_tensor, | ||
) | ||
|
||
# bench triton | ||
triton_out_scales = triton_mx_block_rearrange_per_group_3d(input_tensor) | ||
warmup(triton_mx_block_rearrange_per_group_3d, input_tensor) | ||
triton_time_us = benchmark_cuda_function_in_microseconds( | ||
triton_mx_block_rearrange_per_group_3d, | ||
input_tensor, | ||
) | ||
|
||
# mem bw calculations | ||
bytes_per_input_el = torch.finfo(torch.float8_e8m0fnu).bits / 8 | ||
bytes_per_output_el = torch.finfo(torch.float8_e4m3fn).bits / 8 | ||
|
||
read_bytes = input_tensor.numel() * bytes_per_input_el | ||
write_bytes = triton_out_scales.numel() * bytes_per_output_el | ||
|
||
torch_mem_bw_gbps = ((read_bytes + write_bytes) / 1e9) / (torch_time_us / 1e6) | ||
triton_mem_bw_gbps = ((read_bytes + write_bytes) / 1e9) / (triton_time_us / 1e6) | ||
|
||
return ExperimentResult( | ||
torch_time_us=torch_time_us, | ||
triton_time_us=triton_time_us, | ||
torch_mem_bw_gbps=torch_mem_bw_gbps, | ||
triton_mem_bw_gbps=triton_mem_bw_gbps, | ||
) | ||
|
||
|
||
def print_results(experiments: List[Experiment]): | ||
headers = [ | ||
"input_shape", | ||
"torch_time_us", | ||
"triton_time_us", | ||
"torch_mem_bw_gbps", | ||
"triton_mem_bw_gbps", | ||
"triton_speedup", | ||
] | ||
rows = [] | ||
for experiment in experiments: | ||
input_shape = f"({experiment.config.input_shape[0]}, {experiment.config.input_shape[1]}, {experiment.config.input_shape[2]})" | ||
rows.append( | ||
[ | ||
input_shape, | ||
experiment.result.torch_time_us, | ||
experiment.result.triton_time_us, | ||
round(experiment.result.torch_mem_bw_gbps, 3), | ||
round(experiment.result.triton_mem_bw_gbps, 3), | ||
f"{experiment.result.torch_time_us / experiment.result.triton_time_us:.2f}x", | ||
] | ||
) | ||
print(tabulate(rows, headers=headers)) | ||
|
||
|
||
def main(): | ||
torch.random.manual_seed(123) | ||
configs = get_configs() | ||
results = [] | ||
for config in tqdm(configs): | ||
result = run_experiment(config) | ||
results.append(Experiment(config=config, result=result)) | ||
|
||
# Use Tabulate to print results | ||
print_results(results) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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.
For this one we can just call the other triton kernl impl though right?
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.
We could potentially modify the other impl but the result would be rather ugly IMO, see below (from PR description, sorry it was a bit long):
"I could have potentially expanded the existing dense model kernel to do this since there's no variable group sizes, but i didn't because (1) kernel needs different launch grid and additional params passed into it, so we'd have a weird if-else launching different kernels with different grids and different params. And (2) I would like to keep all the MoE training code in the prototype/moe_training to make the code base as self-contained as possible for now."
What do you think?
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.
maybe... you could also just factor out the main kernel into a series of triton jit functions and then the kernel builds them up
non blocking
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.
Hmm, that could work. Sounds good