fix(trainer): use UE8M0 FP8 scales only on SM100 (#3112)

#3022 flipped use_ue8m0 to True unconditionally to support DeepGEMM on
SM100, which also switched SM90 to power-of-2-rounded scale factors —
a pure precision loss on Hopper. On a GLM-5.2 RL prod run this doubled
trainer/inference mismatch KL (0.0038 -> 0.0059 at step 1, verified
A/B on an identical batch: same tokens and inference logprobs read
0.0059 with UE8M0 and 0.0038 with float scales).

Derive use_ue8m0 from the device (compute capability >= 10) so SM100
keeps the required UE8M0 recipe and SM90 returns to exact float scales.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
samsja
2026-07-22 23:06:35 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent f5946b098e
commit 440b3afaa8
3 changed files with 25 additions and 12 deletions
@@ -15,6 +15,13 @@ def ceil_div(x: int, y: int) -> int:
return (x + y - 1) // y
def ue8m0_for_device(device: torch.device | None = None) -> bool:
"""DeepGEMM requires UE8M0 (power-of-2) scales on SM100; SM90 supports exact float
scales, and UE8M0 there is a pure precision loss (it measurably raises the
trainer/inference mismatch KL)."""
return torch.cuda.get_device_capability(device)[0] >= 10
# ---------------------------------------------------------------------------
# Layout building
# ---------------------------------------------------------------------------
@@ -16,6 +16,7 @@ from prime_rl.trainer.models.kernels.fp8_utils import (
grouped_per_channel_cast_to_fp8_rowmajor_triton,
grouped_per_channel_cast_to_fp8_sm90_kmajor_triton,
grouped_per_token_cast_to_fp8_triton,
ue8m0_for_device,
unpack_rows_triton,
)
@@ -66,7 +67,7 @@ def _compute_grad_weight(
actual_ms_tensor,
ks_tensor,
block_starts_tensor,
True,
False,
GROUP_ALIGNMENT,
)
dy_fp8 = grouped_per_channel_cast_to_fp8_sm90_kmajor_triton(
@@ -77,7 +78,7 @@ def _compute_grad_weight(
actual_ms_tensor,
ks_tensor,
block_starts_tensor,
True,
False,
GROUP_ALIGNMENT,
)
grouped_weight_grad = deep_gemm.k_grouped_fp8_gemm_nt_contiguous
@@ -113,6 +114,7 @@ class _GroupedFP8Gemm(torch.autograd.Function):
block_starts_tensor,
) = build_grouped_layout(offs, total_m=x.size(0))
use_ue8m0 = ue8m0_for_device(x.device)
x_fp8 = grouped_per_token_cast_to_fp8_triton(
x,
padded_total_m,
@@ -120,12 +122,12 @@ class _GroupedFP8Gemm(torch.autograd.Function):
starts_tensor,
actual_ms_tensor,
block_starts_tensor,
True,
use_ue8m0,
GROUP_ALIGNMENT,
)
weight_fp8 = grouped_per_block_cast_to_fp8_triton(
weight.transpose(1, 2),
True,
use_ue8m0,
GROUP_ALIGNMENT,
)
@@ -197,6 +199,7 @@ class _GroupedFP8Gemm(torch.autograd.Function):
)
if ctx.needs_input_grad[0]:
use_ue8m0 = ue8m0_for_device(grad_output.device)
dy_fp8 = grouped_per_token_cast_to_fp8_triton(
grad_output,
padded_total_m,
@@ -204,12 +207,12 @@ class _GroupedFP8Gemm(torch.autograd.Function):
starts_tensor,
actual_ms_tensor,
block_starts_tensor,
True,
use_ue8m0,
GROUP_ALIGNMENT,
)
weight_dx_fp8 = grouped_per_block_cast_to_fp8_triton(
weight,
True,
use_ue8m0,
GROUP_ALIGNMENT,
)
grad_x_padded = torch.empty(
@@ -17,6 +17,7 @@ from prime_rl.trainer.models.kernels.fp8_utils import (
per_block_cast_to_fp8_triton,
per_token_cast_to_fp8_tp_triton,
per_token_cast_to_fp8_triton,
ue8m0_for_device,
)
from prime_rl.utils.logger import get_logger
@@ -26,8 +27,9 @@ class _FP8BlockwiseMM(torch.autograd.Function):
def forward(ctx, x, weight, block_size, out_dtype=torch.bfloat16):
x_shape = x.shape
x_2d = x.reshape(-1, x_shape[-1]).contiguous()
x_fp8 = per_token_cast_to_fp8_triton(x_2d, True, block_size)
weight_fp8 = per_block_cast_to_fp8_triton(weight, True, block_size)
use_ue8m0 = ue8m0_for_device(x.device)
x_fp8 = per_token_cast_to_fp8_triton(x_2d, use_ue8m0, block_size)
weight_fp8 = per_block_cast_to_fp8_triton(weight, use_ue8m0, block_size)
out = torch.empty((x_2d.size(0), weight.size(0)), device=x.device, dtype=out_dtype)
deep_gemm.fp8_gemm_nt(x_fp8, weight_fp8, out)
@@ -42,11 +44,12 @@ class _FP8BlockwiseMM(torch.autograd.Function):
x_2d, weight = ctx.saved_tensors
block_size = ctx.block_size
grad_output_2d = grad_output.reshape(-1, grad_output.shape[-1]).contiguous()
use_ue8m0 = ue8m0_for_device(grad_output.device)
grad_x = grad_weight = None
if ctx.needs_input_grad[0]:
grad_output_fp8 = per_token_cast_to_fp8_triton(grad_output_2d, True, block_size)
weight_dx_fp8 = per_block_cast_to_fp8_tp_triton(weight, True, block_size)
grad_output_fp8 = per_token_cast_to_fp8_triton(grad_output_2d, use_ue8m0, block_size)
weight_dx_fp8 = per_block_cast_to_fp8_tp_triton(weight, use_ue8m0, block_size)
grad_x_2d = torch.empty_like(x_2d)
deep_gemm.fp8_gemm_nt(grad_output_fp8, weight_dx_fp8, grad_x_2d)
grad_x = grad_x_2d.reshape(ctx.x_shape)
@@ -64,8 +67,8 @@ class _FP8BlockwiseMM(torch.autograd.Function):
else:
grad_output_2d_padded = grad_output_2d
x_2d_padded = x_2d
grad_output_t_fp8 = per_token_cast_to_fp8_tp_triton(grad_output_2d_padded, True, block_size)
x_t_fp8 = per_token_cast_to_fp8_tp_triton(x_2d_padded, True, block_size)
grad_output_t_fp8 = per_token_cast_to_fp8_tp_triton(grad_output_2d_padded, use_ue8m0, block_size)
x_t_fp8 = per_token_cast_to_fp8_tp_triton(x_2d_padded, use_ue8m0, block_size)
grad_weight_fp32 = torch.zeros_like(weight, dtype=torch.float32)
deep_gemm.fp8_gemm_nt(
grad_output_t_fp8,