// Normalize each embedding row, add affine gain and bias, and propagate an upstream derivative. Sum gain contributions across rows externally. use graph_io; pub fn fd_layernorm_backward(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor, %gain: tensor, %bias: tensor, %upstream: tensor) -> (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(%x 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 %x, %v5 : tensor", rows, width); // c = x-mean(x) fd_line(" %v7 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v8 = stablehlo.multiply %v6, %v6 : tensor", rows, width); fd_line(" %v9 = stablehlo.reduce(%v8 init: %v1) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v10 = stablehlo.broadcast_in_dim %v9, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v11 = stablehlo.divide %v10, %v4 : tensor", rows, width); fd_line(" %v12 = stablehlo.constant dense<1.00000000000000008e-05> : tensor", rows, width); fd_line(" %v13 = stablehlo.add %v11, %v12 : tensor", rows, width); fd_line(" %v14 = stablehlo.sqrt %v13 : tensor", rows, width); fd_line(" %v15 = stablehlo.divide %v7, %v14 : tensor", rows, width); // inv = 1/sqrt(mean(c*c)+1e-5) fd_line(" %v16 = stablehlo.multiply %v6, %v15 : tensor", rows, width); // u = c*inv fd_line(" %v17 = stablehlo.multiply %v16, %gain : tensor", rows, width); fd_line(" %v18 = stablehlo.add %v17, %bias : tensor", rows, width); // y = u*gain+bias fd_line(" %v19 = stablehlo.multiply %upstream, %gain : tensor", rows, width); // g = upstream*gain fd_line(" %v20 = stablehlo.reduce(%v19 init: %v1) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v21 = stablehlo.broadcast_in_dim %v20, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v22 = stablehlo.divide %v21, %v4 : tensor", rows, width); fd_line(" %v23 = stablehlo.subtract %v19, %v22 : tensor", rows, width); fd_line(" %v24 = stablehlo.multiply %v19, %v16 : tensor", rows, width); fd_line(" %v25 = stablehlo.reduce(%v24 init: %v1) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v26 = stablehlo.broadcast_in_dim %v25, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v27 = stablehlo.divide %v26, %v4 : tensor", rows, width); fd_line(" %v28 = stablehlo.multiply %v16, %v27 : tensor", rows, width); fd_line(" %v29 = stablehlo.subtract %v23, %v28 : tensor", rows, width); fd_line(" %v30 = stablehlo.multiply %v15, %v29 : tensor", rows, width); // dx = inv*(g-mean(g)-u*mean(g*u)) fd_line(" %v31 = stablehlo.multiply %upstream, %v16 : tensor", rows, width); // dg = upstream*u fd_line(" return %v18, %v30, %v31 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }