// Fuse a residual addition with pre-normalization and return the combined residual stream and scaled output. use graph_io; pub fn fd_residual_rms(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor, %residual: tensor, %gain: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.add %x, %residual : tensor", rows, width); // stream = x+residual fd_line(" %v2 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v3 = stablehlo.multiply %v1, %v1 : tensor", rows, width); println!(" %v4 = stablehlo.constant dense<0.00000000000000000e+00> : tensor"); fd_line(" %v5 = stablehlo.reduce(%v3 init: %v4) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v6 = stablehlo.broadcast_in_dim %v5, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v7 = stablehlo.constant dense : tensor", rows, width); fd_line(" %v8 = stablehlo.divide %v6, %v7 : tensor", rows, width); fd_line(" %v9 = stablehlo.constant dense<1.00000000000000008e-05> : tensor", rows, width); fd_line(" %v10 = stablehlo.add %v8, %v9 : tensor", rows, width); fd_line(" %v11 = stablehlo.sqrt %v10 : tensor", rows, width); fd_line(" %v12 = stablehlo.divide %v2, %v11 : tensor", rows, width); // inv = 1/sqrt(mean(stream*stream)+1e-5) fd_line(" %v13 = stablehlo.multiply %v1, %v12 : tensor", rows, width); fd_line(" %v14 = stablehlo.multiply %v13, %gain : tensor", rows, width); // normalized = stream*inv*gain fd_line(" return %v1, %v14 : tensor, tensor", rows, width); println!(" }"); println!("}"); }