2024-08-17 18:07:19 +00:00
|
|
|
import torch
|
2024-08-26 16:33:57 +00:00
|
|
|
|
2024-08-26 19:12:06 +00:00
|
|
|
def calc_mantissa(abs_x, exponent, normal_mask, MANTISSA_BITS, EXPONENT_BIAS, generator=None):
|
2024-08-26 16:33:57 +00:00
|
|
|
mantissa_scaled = torch.where(
|
|
|
|
normal_mask,
|
|
|
|
(abs_x / (2.0 ** (exponent - EXPONENT_BIAS)) - 1.0) * (2**MANTISSA_BITS),
|
|
|
|
(abs_x / (2.0 ** (-EXPONENT_BIAS + 1 - MANTISSA_BITS)))
|
|
|
|
)
|
|
|
|
|
2024-08-26 19:12:06 +00:00
|
|
|
mantissa_scaled += torch.rand(mantissa_scaled.size(), dtype=mantissa_scaled.dtype, layout=mantissa_scaled.layout, device=mantissa_scaled.device, generator=generator)
|
2024-08-26 16:33:57 +00:00
|
|
|
return mantissa_scaled.floor() / (2**MANTISSA_BITS)
|
2024-08-17 18:07:19 +00:00
|
|
|
|
|
|
|
#Not 100% sure about this
|
2024-08-26 19:12:06 +00:00
|
|
|
def manual_stochastic_round_to_float8(x, dtype, generator=None):
|
2024-08-17 18:07:19 +00:00
|
|
|
if dtype == torch.float8_e4m3fn:
|
|
|
|
EXPONENT_BITS, MANTISSA_BITS, EXPONENT_BIAS = 4, 3, 7
|
|
|
|
elif dtype == torch.float8_e5m2:
|
|
|
|
EXPONENT_BITS, MANTISSA_BITS, EXPONENT_BIAS = 5, 2, 15
|
|
|
|
else:
|
|
|
|
raise ValueError("Unsupported dtype")
|
|
|
|
|
2024-08-26 16:33:57 +00:00
|
|
|
x = x.half()
|
2024-08-17 18:07:19 +00:00
|
|
|
sign = torch.sign(x)
|
|
|
|
abs_x = x.abs()
|
2024-08-26 16:33:57 +00:00
|
|
|
sign = torch.where(abs_x == 0, 0, sign)
|
2024-08-17 18:07:19 +00:00
|
|
|
|
|
|
|
# Combine exponent calculation and clamping
|
|
|
|
exponent = torch.clamp(
|
2024-08-26 16:33:57 +00:00
|
|
|
torch.floor(torch.log2(abs_x)) + EXPONENT_BIAS,
|
2024-08-17 18:07:19 +00:00
|
|
|
0, 2**EXPONENT_BITS - 1
|
|
|
|
)
|
|
|
|
|
|
|
|
# Combine mantissa calculation and rounding
|
2024-08-19 17:38:03 +00:00
|
|
|
normal_mask = ~(exponent == 0)
|
|
|
|
|
2024-08-26 19:12:06 +00:00
|
|
|
abs_x[:] = calc_mantissa(abs_x, exponent, normal_mask, MANTISSA_BITS, EXPONENT_BIAS, generator=generator)
|
2024-08-26 16:33:57 +00:00
|
|
|
|
|
|
|
sign *= torch.where(
|
2024-08-19 17:38:03 +00:00
|
|
|
normal_mask,
|
2024-08-26 16:33:57 +00:00
|
|
|
(2.0 ** (exponent - EXPONENT_BIAS)) * (1.0 + abs_x),
|
|
|
|
(2.0 ** (-EXPONENT_BIAS + 1)) * abs_x
|
2024-08-19 17:38:03 +00:00
|
|
|
)
|
2024-08-17 18:07:19 +00:00
|
|
|
|
2024-09-03 05:25:05 +00:00
|
|
|
return sign
|
2024-08-17 18:07:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-26 19:12:06 +00:00
|
|
|
def stochastic_rounding(value, dtype, seed=0):
|
2024-08-17 18:07:19 +00:00
|
|
|
if dtype == torch.float32:
|
|
|
|
return value.to(dtype=torch.float32)
|
|
|
|
if dtype == torch.float16:
|
|
|
|
return value.to(dtype=torch.float16)
|
|
|
|
if dtype == torch.bfloat16:
|
|
|
|
return value.to(dtype=torch.bfloat16)
|
|
|
|
if dtype == torch.float8_e4m3fn or dtype == torch.float8_e5m2:
|
2024-08-26 19:12:06 +00:00
|
|
|
generator = torch.Generator(device=value.device)
|
|
|
|
generator.manual_seed(seed)
|
2024-09-03 05:25:05 +00:00
|
|
|
output = torch.empty_like(value, dtype=dtype)
|
|
|
|
num_slices = max(1, (value.numel() / (4096 * 4096)))
|
|
|
|
slice_size = max(1, round(value.shape[0] / num_slices))
|
|
|
|
for i in range(0, value.shape[0], slice_size):
|
|
|
|
output[i:i+slice_size].copy_(manual_stochastic_round_to_float8(value[i:i+slice_size], dtype, generator=generator))
|
|
|
|
return output
|
2024-08-17 18:07:19 +00:00
|
|
|
|
|
|
|
return value.to(dtype=dtype)
|