// Subtract each filter row’s mean gradient and measure its remaining RMS magnitude. use graph_io; pub fn fd_centralize_grad(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%grad: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); println!(" %v1 = stablehlo.constant dense<0.00000000000000000e+00> : tensor"); fd_line(" %v2 = stablehlo.reduce(%grad init: %v1) applies stablehlo.add 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.constant dense : tensor", rows, width); fd_line(" %v5 = stablehlo.divide %v3, %v4 : tensor", rows, width); fd_line(" %v6 = stablehlo.subtract %grad, %v5 : tensor", rows, width); // centered = grad-mean(grad) fd_line(" %v7 = stablehlo.multiply %v6, %v6 : tensor", rows, width); fd_line(" %v8 = stablehlo.reduce(%v7 init: %v1) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v9 = stablehlo.broadcast_in_dim %v8, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v10 = stablehlo.divide %v9, %v4 : tensor", rows, width); fd_line(" %v11 = stablehlo.sqrt %v10 : tensor", rows, width); // rms = sqrt(mean(centered*centered)) fd_line(" return %v6, %v11 : tensor, tensor", rows, width); println!(" }"); println!("}"); }