-
Notifications
You must be signed in to change notification settings - Fork 311
[moe training] add fp8 rowwise kernels for expert weights #2696
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
+340
−15
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
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
251 changes: 251 additions & 0 deletions
251
torchao/prototype/moe_training/kernels/float8_rowwise.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,251 @@ | ||
# 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. | ||
|
||
|
||
from typing import Tuple | ||
|
||
import torch | ||
import triton | ||
import triton.language as tl | ||
|
||
EPS = 1e-12 | ||
|
||
FP8_DTYPE_MAP = { | ||
torch.int8: tl.int8, | ||
torch.int16: tl.int16, | ||
torch.int32: tl.int32, | ||
torch.int64: tl.int64, | ||
torch.float8_e4m3fn: tl.float8e4nv, | ||
torch.float8_e5m2: tl.float8e5, | ||
torch.float16: tl.float16, | ||
torch.bfloat16: tl.bfloat16, | ||
torch.float32: tl.float32, | ||
torch.float64: tl.float64, | ||
} | ||
|
||
block_sizes = [16] | ||
num_warps = [4] | ||
num_stages = [2] | ||
kernel_configs_2D = [ | ||
triton.Config( | ||
{"BLOCK_SIZE_N": block_size, "BLOCK_SIZE_K": block_size * 2}, | ||
num_warps=warps, | ||
num_stages=stages, | ||
) | ||
for block_size in block_sizes | ||
for warps in num_warps | ||
for stages in num_stages | ||
] | ||
|
||
from torch.library import triton_op, wrap_triton | ||
|
||
|
||
@triton_op("torchao::triton_fp8_rowwise_transpose_rhs", mutates_args={}) | ||
def triton_fp8_rowwise_3d_transpose_rhs( | ||
hp_tensor: torch.Tensor, # (E, K, N) | ||
output_dtype: torch.dtype = torch.float8_e4m3fn, | ||
round_scales_to_power_of_2: bool = False, | ||
) -> Tuple[torch.Tensor, torch.Tensor]: | ||
assert hp_tensor.ndim == 3, "input tensor must be 3D" | ||
|
||
num_elements = hp_tensor.numel() | ||
tl_input_dtype = FP8_DTYPE_MAP[hp_tensor.dtype] | ||
tl_output_dtype = FP8_DTYPE_MAP[output_dtype] | ||
|
||
fp8_dtype_min = torch.finfo(output_dtype).min | ||
fp8_dtype_max = torch.finfo(output_dtype).max | ||
|
||
e, k, n = hp_tensor.shape | ||
|
||
# allocate on-device buffers for output and scales | ||
# output shape = input.transpose(-2, -1).shape = (E, N, K) in column major layout | ||
output_buffer = torch.empty((e, k, n), dtype=output_dtype, device=hp_tensor.device) | ||
output_buffer = output_buffer.transpose(-2, -1) | ||
scales_buffer = torch.full( | ||
(e, k), float("inf"), dtype=torch.float32, device=hp_tensor.device | ||
) | ||
|
||
# parallelize across experts, and for each expert, parallelize across rows and cols | ||
grid = lambda meta: ( | ||
e, | ||
triton.cdiv(k, meta["BLOCK_SIZE_K"]), | ||
triton.cdiv(n, meta["BLOCK_SIZE_N"]), | ||
) | ||
|
||
# compute scales | ||
wrap_triton(_triton_fp8_rowwise_3d_transpose_scales_rhs_kernel)[grid]( | ||
hp_tensor, | ||
hp_tensor.stride(0), | ||
hp_tensor.stride(1), | ||
hp_tensor.stride(2), | ||
scales_buffer, | ||
scales_buffer.stride(0), | ||
scales_buffer.stride(1), | ||
e, | ||
n, | ||
k, | ||
num_elements, | ||
fp8_dtype_min, | ||
fp8_dtype_max, | ||
tl_input_dtype, | ||
round_scales_to_power_of_2=round_scales_to_power_of_2, | ||
EPS=EPS, | ||
) | ||
|
||
# perform casting | ||
wrap_triton(_triton_fp8_rowwise_3d_transpose_cast_rhs_kernel)[grid]( | ||
hp_tensor, | ||
hp_tensor.stride(0), | ||
hp_tensor.stride(1), | ||
hp_tensor.stride(2), | ||
output_buffer, | ||
output_buffer.stride(0), | ||
output_buffer.stride(1), | ||
output_buffer.stride(2), | ||
scales_buffer, | ||
scales_buffer.stride(0), | ||
scales_buffer.stride(1), | ||
e, | ||
n, | ||
k, | ||
num_elements, | ||
fp8_dtype_min, | ||
fp8_dtype_max, | ||
tl_input_dtype, | ||
tl_output_dtype, | ||
) | ||
return output_buffer, scales_buffer | ||
|
||
|
||
@triton.autotune(configs=kernel_configs_2D, key=["num_elements"]) | ||
@triton.jit | ||
def _triton_fp8_rowwise_3d_transpose_scales_rhs_kernel( | ||
input_ptr, | ||
stride_input_dim0: int, | ||
stride_input_dim1: int, | ||
stride_input_dim2: int, | ||
scales_ptr, | ||
stride_scales_dim0: int, | ||
stride_scales_dim1: int, | ||
E: int, | ||
N: int, | ||
K: int, | ||
num_elements: int, | ||
fp8_dtype_min: tl.constexpr, | ||
fp8_dtype_max: tl.constexpr, | ||
input_dtype: tl.constexpr, | ||
round_scales_to_power_of_2: tl.constexpr, | ||
BLOCK_SIZE_N: tl.constexpr, | ||
BLOCK_SIZE_K: tl.constexpr, | ||
EPS: tl.constexpr, | ||
): | ||
# parallelize across experts, rows, and cols | ||
expert_idx = tl.program_id(0) | ||
k_block_idx = tl.program_id(1) | ||
n_block_idx = tl.program_id(2) | ||
|
||
# compute offsets for each dimension | ||
k_offs = k_block_idx * BLOCK_SIZE_K + tl.arange(0, BLOCK_SIZE_K) | ||
n_offs = n_block_idx * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) | ||
|
||
# load block of input data, shape (K, N) | ||
input_offs = ( | ||
expert_idx * stride_input_dim0 | ||
+ k_offs[:, None] * stride_input_dim1 | ||
+ (n_offs[None, :] * stride_input_dim2) | ||
) | ||
input_mask = (k_offs[:, None] < K) & (n_offs[None, :] < N) | ||
input_data = tl.load(input_ptr + input_offs, mask=input_mask, other=0.0).to( | ||
input_dtype | ||
) | ||
|
||
# compute scales with local amax, using axis=0 because for each expert, | ||
# we are reading the non-transposed input, and want to compute the scales | ||
# along axis=1 for the transposed input. | ||
amaxes = tl.max(tl.abs(input_data), axis=1).to(tl.float64) # (K,) | ||
scales = (fp8_dtype_max / tl.clamp(amaxes, min=EPS, max=float("inf"))).to( | ||
tl.float32 | ||
) | ||
if round_scales_to_power_of_2: | ||
scales = tl.exp2(tl.floor(tl.log2(scales))) | ||
|
||
# compute global scales using atomics with local scales - shape (1, K) | ||
scales_offs = ( | ||
expert_idx[:, None] * stride_scales_dim0 + k_offs[None, :] * stride_scales_dim1 | ||
) | ||
scales_mask = k_offs[None, :] < K | ||
tl.atomic_min(scales_ptr + scales_offs, scales[None, :], mask=scales_mask) | ||
|
||
|
||
@triton.autotune(configs=kernel_configs_2D, key=["num_elements"]) | ||
@triton.jit | ||
def _triton_fp8_rowwise_3d_transpose_cast_rhs_kernel( | ||
input_ptr, | ||
stride_input_dim0: int, | ||
stride_input_dim1: int, | ||
stride_input_dim2: int, | ||
output_ptr, | ||
stride_output_dim0: int, | ||
stride_output_dim1: int, | ||
stride_output_dim2: int, | ||
scales_ptr, | ||
stride_scales_dim0: int, | ||
stride_scales_dim1: int, | ||
E: int, | ||
N: int, | ||
K: int, | ||
num_elements: int, | ||
fp8_dtype_min: tl.constexpr, | ||
fp8_dtype_max: tl.constexpr, | ||
input_dtype: tl.constexpr, | ||
output_dtype: tl.constexpr, | ||
BLOCK_SIZE_N: tl.constexpr, | ||
BLOCK_SIZE_K: tl.constexpr, | ||
): | ||
# parallelize across experts, rows, and cols | ||
expert_idx = tl.program_id(0) | ||
k_block_idx = tl.program_id(1) | ||
n_block_idx = tl.program_id(2) | ||
|
||
# compute offsets for each dimension | ||
k_offs = k_block_idx * BLOCK_SIZE_K + tl.arange(0, BLOCK_SIZE_K) | ||
n_offs = n_block_idx * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) | ||
|
||
# load block of input data for this expert - shape (K, N) | ||
input_offs = ( | ||
expert_idx * stride_input_dim0 | ||
+ k_offs[:, None] * stride_input_dim1 | ||
+ (n_offs[None, :] * stride_input_dim2) | ||
) | ||
input_mask = (k_offs[:, None] < K) & (n_offs[None, :] < N) | ||
input_data = tl.load(input_ptr + input_offs, mask=input_mask, other=0.0).to( | ||
input_dtype | ||
) | ||
input_data = input_data.trans(1, 0) # (K, N) -> (N, K) | ||
|
||
# load global scales for this block of the given expert - shape (1, K) | ||
scales_offs = ( | ||
expert_idx[:, None] * stride_scales_dim0 + k_offs[None, :] * stride_scales_dim1 | ||
) | ||
scales_mask = k_offs[None, :] < K | ||
scales = tl.load(scales_ptr + scales_offs, mask=scales_mask, other=0.0).to( | ||
tl.float32 | ||
) | ||
|
||
# transpose data and apply scales - shape (N,K) * (1,K) = (N,K) | ||
scaled_data = input_data * scales | ||
output_data = tl.clamp(scaled_data, min=fp8_dtype_min, max=fp8_dtype_max).to( | ||
output_dtype | ||
) | ||
|
||
# store transpose and store output data - shape (N, K) | ||
output_offs = ( | ||
expert_idx * stride_output_dim0 | ||
+ n_offs[:, None] * stride_output_dim1 | ||
+ (k_offs[None, :] * stride_output_dim2) | ||
) | ||
output_mask = (n_offs[:, None] < N) & (k_offs[None, :] < K) | ||
tl.store(output_ptr + output_offs, output_data, mask=output_mask) |
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
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.
this seems expensive, can we just extract the bits?