debug: fix gs.item() for multi-element tensor

This commit is contained in:
2026-05-15 18:09:41 +00:00
parent 755f9ad567
commit df916b87eb

View File

@@ -426,18 +426,18 @@ def nvfp4_mega_moe_full(
w_deq = w_signs * w_mags # (K, N) = (7168, 6144)
w_sf_exp = l1_sf[e0].to(torch.float32).repeat_interleave(16, dim=0) # (K, N)
# Full dequant: x = e2m1 * block_sf * igs, w = e2m1 * block_sf * gs
gs = l1_global_sf[e0]
gs = l1_global_sf[e0] # shape (2,) or scalar
igs = l1_alpha # already the input global scale
x_full = (x_deq * sf_exp * igs).to(torch.bfloat16) # (K,)
w_full = (w_deq * w_sf_exp).to(torch.bfloat16) # (K, N) without gs
ref_out = torch.nn.functional.linear(x_full.unsqueeze(0), w_full.T).squeeze(0) # (N,)
# Apply per-half global scale (gate_gs for first half, up_gs for second half)
gn = ref_out.shape[0] // 2
if gs.dim() == 0:
ref_out = ref_out * gs.item()
if gs.numel() == 1:
ref_out = ref_out * float(gs)
else:
ref_out[:gn] = ref_out[:gn] * gs[0].item()
ref_out[gn:] = ref_out[gn:] * gs[1].item()
ref_out[:gn] = ref_out[:gn] * float(gs[0])
ref_out[gn:] = ref_out[gn:] * float(gs[1])
nvfp4_mega_moe_full._ref_l1 = (s0, e0, ref_out)
print(f"[BF16-REF-L1] expert={e0} amax={ref_out.abs().max():.4e} mean={ref_out.mean():.4e}")
except Exception as ex: