Skip to content

Support specify output type for FP8 Triton rowwise scaling kernel #4473

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 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions fbgemm_gpu/experimental/gemm/triton_gemm/fp8_gemm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ def matmul_fp8_row(
tma_persistent: bool = True,
no_use_persistent: Optional[bool] = None,
use_warp_specialization: bool = False,
out_dtype: Optional[torch.dtype] = None,
) -> torch.Tensor:
"""
Performs matmul on [M, K] and [N, K] fp8 matrices with row-wise scalings [M], [N].
Expand All @@ -1195,6 +1196,7 @@ def matmul_fp8_row(
allow_tf32 (bool): Whether to use TF32 for tensor core.
fp8_fast_accum (bool): Whether to use fast accumulation for tensor core.
tma_persistent (bool): Whether to use TMA persistent kernel impl.
out_dtype (torch.dtype): Output type of the function.

Returns:
torch.Tensor: [M, N] Output tensor a @ b / (a_scale[:, None] * b_scale[None, :])
Expand All @@ -1217,7 +1219,7 @@ def matmul_fp8_row(
assert a.dtype in (torch.float8_e4m3fnuz, torch.float8_e5m2fnuz)
assert b.dtype == pt_fp8_dtype
M, N, K, m_key, n_key, k_key, c, c_dtype_triton, dot_out_dtype_triton, device = (
prep_matmul(a, b, dot_out_dtype)
prep_matmul(a, b, dot_out_dtype, out_dtype)
)

# Skip scaling (a_scale is None) can only be applied in certain cases.
Expand Down Expand Up @@ -1622,6 +1624,7 @@ def matmul_fp8_row_meta(
tma_persistent: bool = True,
no_use_persistent: Optional[bool] = None,
use_warp_specialization: bool = False,
out_dtype: Optional[torch.dtype] = None,
) -> torch.Tensor:
"""Shape function for torch compile."""
M, K = a.shape
Expand Down Expand Up @@ -2185,6 +2188,7 @@ def prep_matmul(
a: Union[TensorWrapper, torch.Tensor],
b: Union[TensorWrapper, torch.Tensor],
dot_out_dtype: Optional[torch.dtype],
out_dtype: Optional[torch.dtype] = None,
) -> Tuple[
int, int, int, int, int, int, torch.Tensor, tl.dtype, tl.dtype, torch.device
]:
Expand Down Expand Up @@ -2239,10 +2243,18 @@ def prep_matmul(
tl.float8e5,
tl.float8e4b8,
]
c_dtype = torch.bfloat16
c_dtype_triton = tl.bfloat16

c = torch.empty((M, N), device=device, dtype=c_dtype)
if out_dtype is None:
# Default value torch.bfloat16 is to accommodate
# legacy usage of the function.
out_dtype = torch.bfloat16
assert isinstance(
out_dtype, torch.dtype
), f"out_dtype type {type(out_dtype)} must be a torch.dtype"
c_dtype_triton = map_dtype_to_triton(out_dtype)

c = torch.empty((M, N), device=device, dtype=out_dtype)

if dot_out_dtype is None:
dot_out_dtype_triton = tl.float32
else:
Expand Down
Loading