// Run a SwiGLU gate and return gradients to both halves of a transformer feed-forward projection. use graph_io; pub fn fd_swiglu_backward(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor, %gate: tensor, %upstream: tensor) -> (tensor, tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.logistic %gate : tensor", rows, width); // s = sig(gate) fd_line(" %v2 = stablehlo.multiply %gate, %v1 : tensor", rows, width); // a = gate*s fd_line(" %v3 = stablehlo.multiply %x, %v2 : tensor", rows, width); // y = x*a fd_line(" %v4 = stablehlo.multiply %upstream, %v2 : tensor", rows, width); // dx = upstream*a fd_line(" %v5 = stablehlo.multiply %upstream, %x : tensor", rows, width); fd_line(" %v6 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v7 = stablehlo.subtract %v6, %v1 : tensor", rows, width); fd_line(" %v8 = stablehlo.multiply %v2, %v7 : tensor", rows, width); fd_line(" %v9 = stablehlo.add %v1, %v8 : tensor", rows, width); fd_line(" %v10 = stablehlo.multiply %v5, %v9 : tensor", rows, width); // dg = upstream*x*(s+gate*s*(1-s)) fd_line(" return %v3, %v4, %v10 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }