// Simulate 8-bit asymmetric activation quantization with zero included in the range and integer zero point. Outputs use FP32 storage. use graph_io; pub fn fd_affine_quantize(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor) -> (tensor, tensor, tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); println!(" %v1 = stablehlo.constant dense<3.40282346000000007e+38> : tensor"); fd_line(" %v2 = stablehlo.reduce(%x init: %v1) applies stablehlo.minimum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v3 = stablehlo.broadcast_in_dim %v2, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v4 = stablehlo.minimum %v3, %v0 : tensor", rows, width); // lo = minimum(rmin(x),0) println!(" %v5 = stablehlo.constant dense<-3.40282346000000007e+38> : tensor"); fd_line(" %v6 = stablehlo.reduce(%x init: %v5) applies stablehlo.maximum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v7 = stablehlo.broadcast_in_dim %v6, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v8 = stablehlo.maximum %v7, %v0 : tensor", rows, width); // hi = maximum(rmax(x),0) fd_line(" %v9 = stablehlo.subtract %v8, %v4 : tensor", rows, width); fd_line(" %v10 = stablehlo.constant dense<2.55000000000000000e+02> : tensor", rows, width); fd_line(" %v11 = stablehlo.divide %v9, %v10 : tensor", rows, width); fd_line(" %v12 = stablehlo.constant dense<9.99999999999999945e-21> : tensor", rows, width); fd_line(" %v13 = stablehlo.maximum %v11, %v12 : tensor", rows, width); // scale = maximum((hi-lo)/255,1e-20) fd_line(" %v14 = stablehlo.negate %v4 : tensor", rows, width); fd_line(" %v15 = stablehlo.divide %v14, %v13 : tensor", rows, width); fd_line(" %v16 = stablehlo.round_nearest_even %v15 : tensor", rows, width); fd_line(" %v17 = stablehlo.maximum %v16, %v0 : tensor", rows, width); fd_line(" %v18 = stablehlo.minimum %v17, %v10 : tensor", rows, width); // zero = clip(rounded(-lo/scale),0,255) fd_line(" %v19 = stablehlo.divide %x, %v13 : tensor", rows, width); fd_line(" %v20 = stablehlo.round_nearest_even %v19 : tensor", rows, width); fd_line(" %v21 = stablehlo.add %v20, %v18 : tensor", rows, width); fd_line(" %v22 = stablehlo.maximum %v21, %v0 : tensor", rows, width); fd_line(" %v23 = stablehlo.minimum %v22, %v10 : tensor", rows, width); // code = clip(rounded(x/scale)+zero,0,255) fd_line(" %v24 = stablehlo.subtract %v23, %v18 : tensor", rows, width); fd_line(" %v25 = stablehlo.multiply %v24, %v13 : tensor", rows, width); // decoded = (code-zero)*scale fd_line(" return %v23, %v25, %v18, %v13 : tensor, tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }