hamer-p150 / code /scripts /micro /fidelity_bias.py
changh95's picture
Optimized build (2026-10-03): model call 3.49 ms, device 3.36 ms
58ba6df verified
Raw History Blame Contribute Delete
1.77 kB
# SPDX-License-Identifier: Apache-2.0
"""Round 6: which matmul operand each math fidelity truncates (ttnn.matmul, bf16 x bf16, fp32 DST, fp32 out).
Pre-rounding an operand to b explicit mantissa bits makes the truncation exact: HiFi2 is unbiased once in0 has
<= 6 bits (it truncates the ACTIVATION to 6 bits: -0.28% magnitude bias), HiFi3 / HiFi4 are unbiased."""
import sys, torch
sys.path.insert(0,'/home/ttuser/experiments/tt-models/tools')
from open_device_12x10 import open_device, close_device
import ttnn
def rnd(x, bits): # round-to-nearest keeping `bits` explicit mantissa bits
m, e = torch.frexp(x.double())
s = 2.0 ** (bits + 1)
return torch.ldexp(torch.round(m * s) / s, e).to(torch.bfloat16)
dev=open_device(l1_small_size=0, trace_region_size=0)
try:
torch.manual_seed(0)
M,K,N=192,1024,1024
a0=torch.randn(M,K).bfloat16(); w0=(torch.randn(K,N)/32).bfloat16()
for tag,a,w in (("plain",a0,w0),("a6",rnd(a0,6),w0),("w6",a0,rnd(w0,6)),("a5",rnd(a0,5),w0),("w5",a0,rnd(w0,5)),("a4",rnd(a0,4),w0),("w4",a0,rnd(w0,4))):
refq=a.double()@w.double()
ta=ttnn.from_torch(a,dtype=ttnn.bfloat16,layout=ttnn.TILE_LAYOUT,device=dev)
tw=ttnn.from_torch(w,dtype=ttnn.bfloat16,layout=ttnn.TILE_LAYOUT,device=dev)
for fid in ("LoFi","HiFi2","HiFi3"):
cc=ttnn.init_device_compute_kernel_config(dev.arch(),math_fidelity=getattr(ttnn.MathFidelity,fid),fp32_dest_acc_en=True,packer_l1_acc=True)
o=ttnn.to_torch(ttnn.matmul(ta,tw,compute_kernel_config=cc,dtype=ttnn.float32)).double()
e=o-refq
print(f"{tag:6s} {fid}: mag-bias {float((e*refq.sign()).mean()/refq.abs().mean()):+.2e} rel|e| {float(e.abs().mean()/refq.abs().mean()):.2e}")
finally:
close_device(dev)