2025-07-18 11:32:22 +08:00
|
|
|
import torch
|
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ceil_div(x: int, y: int) -> int:
|
|
|
|
|
return (x + y - 1) // y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def align(x: int, y: int) -> int:
|
|
|
|
|
return ceil_div(x, y) * y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ceil_to_ue8m0(x: torch.Tensor):
|
|
|
|
|
assert x.view(-1).amax().item() > 0
|
|
|
|
|
return torch.pow(2.0, torch.ceil(torch.log2(x.abs())))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def per_token_cast_to_fp8(x: torch.Tensor, use_ue8m0: bool) -> Tuple[torch.Tensor, torch.Tensor]:
|
2025-09-25 16:19:07 +08:00
|
|
|
assert x.dim() == 2
|
2025-07-18 11:32:22 +08:00
|
|
|
m, n = x.shape
|
2025-09-25 16:19:07 +08:00
|
|
|
padded_n = align(n, 128)
|
|
|
|
|
x_padded = torch.empty((m, padded_n), dtype=x.dtype, device=x.device).fill_(0)
|
|
|
|
|
x_padded[:, :n] = x
|
|
|
|
|
x_view = x_padded.view(m, -1, 128)
|
2025-07-18 11:32:22 +08:00
|
|
|
x_amax = x_view.abs().float().amax(dim=2).view(m, -1).clamp(1e-4)
|
|
|
|
|
sf = x_amax / 448.0
|
|
|
|
|
sf = ceil_to_ue8m0(sf) if use_ue8m0 else sf
|
2025-09-25 16:19:07 +08:00
|
|
|
return (x_view * (1.0 / sf.unsqueeze(2))).to(torch.float8_e4m3fn).view(m, padded_n)[:, :n].contiguous(), sf
|
2025-07-18 11:32:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def per_channel_cast_to_fp8(x: torch.Tensor, use_ue8m0: bool) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
|
|
|
assert x.dim() == 2 and x.size(0) % 128 == 0
|
|
|
|
|
m, n = x.shape
|
|
|
|
|
x_view = x.view(-1, 128, n)
|
|
|
|
|
x_amax = x_view.abs().float().amax(dim=1).view(-1, n).clamp(1e-4)
|
|
|
|
|
sf = x_amax / 448.0
|
|
|
|
|
sf = ceil_to_ue8m0(sf) if use_ue8m0 else sf
|
|
|
|
|
return (x_view * (1.0 / sf.unsqueeze(1))).to(torch.float8_e4m3fn).view(m, n), sf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def per_block_cast_to_fp8(x: torch.Tensor, use_ue8m0: bool) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
|
|
|
assert x.dim() == 2
|
|
|
|
|
m, n = x.shape
|
|
|
|
|
x_padded = torch.zeros((align(m, 128), align(n, 128)), dtype=x.dtype, device=x.device)
|
|
|
|
|
x_padded[:m, :n] = x
|
|
|
|
|
x_view = x_padded.view(-1, 128, x_padded.size(1) // 128, 128)
|
|
|
|
|
x_amax = x_view.abs().float().amax(dim=(1, 3), keepdim=True).clamp(1e-4)
|
|
|
|
|
sf = x_amax / 448.0
|
|
|
|
|
sf = ceil_to_ue8m0(sf) if use_ue8m0 else sf
|
|
|
|
|
x_scaled = (x_view * (1.0 / sf)).to(torch.float8_e4m3fn)
|
|
|
|
|
return x_scaled.view_as(x_padded)[:m, :n].contiguous(), sf.view(x_view.size(0), x_view.size(2))
|
2025-08-02 19:52:22 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def per_custom_dims_cast_to_fp8(x: torch.Tensor, dims: Tuple, use_ue8m0: bool) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
|
|
|
excluded_dims = tuple([i for i in range(x.dim()) if i not in set(dims)])
|
|
|
|
|
x_amax = x.abs().float().amax(dim=excluded_dims, keepdim=True).clamp(1e-4)
|
|
|
|
|
sf = x_amax / 448.0
|
|
|
|
|
sf = ceil_to_ue8m0(sf) if use_ue8m0 else sf
|
|
|
|
|
x_scaled = (x * (1.0 / sf)).to(torch.float8_e4m3fn)
|
2025-09-25 16:19:07 +08:00
|
|
|
return x_scaled, sf.squeeze()
|