// Simulate symmetric per-row INT8 weight quantization with nearest-even rounding. Return FP32 codes, reconstruction and scale for error analysis. use graph_io; pub fn fd_int8_roundtrip(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor) -> (tensor, tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.abs %x : tensor", rows, width); println!(" %v2 = stablehlo.constant dense<-3.40282346000000007e+38> : tensor"); fd_line(" %v3 = stablehlo.reduce(%v1 init: %v2) applies stablehlo.maximum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v4 = stablehlo.broadcast_in_dim %v3, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v5 = stablehlo.constant dense<1.27000000000000000e+02> : tensor", rows, width); fd_line(" %v6 = stablehlo.divide %v4, %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.constant dense<9.99999999999999945e-21> : tensor", rows, width); fd_line(" %v8 = stablehlo.maximum %v6, %v7 : tensor", rows, width); // scale = maximum(rmax(abs(x))/127,1e-20) fd_line(" %v9 = stablehlo.divide %x, %v8 : tensor", rows, width); fd_line(" %v10 = stablehlo.round_nearest_even %v9 : tensor", rows, width); fd_line(" %v11 = stablehlo.negate %v5 : tensor", rows, width); fd_line(" %v12 = stablehlo.maximum %v10, %v11 : tensor", rows, width); fd_line(" %v13 = stablehlo.minimum %v12, %v5 : tensor", rows, width); // code = clip(rounded(x/scale),-127,127) fd_line(" %v14 = stablehlo.multiply %v13, %v8 : tensor", rows, width); // decoded = code*scale fd_line(" return %v13, %v14, %v8 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }