// Use a tanh-approximate GELU gate and propagate both value and gate derivatives. use graph_io; pub fn fd_geglu_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.constant dense<7.97884560800000031e-01> : tensor", rows, width); fd_line(" %v2 = stablehlo.constant dense<4.47149999999999978e-02> : tensor", rows, width); fd_line(" %v3 = stablehlo.multiply %v2, %gate : tensor", rows, width); fd_line(" %v4 = stablehlo.multiply %v3, %gate : tensor", rows, width); fd_line(" %v5 = stablehlo.multiply %v4, %gate : tensor", rows, width); fd_line(" %v6 = stablehlo.add %gate, %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.multiply %v1, %v6 : tensor", rows, width); fd_line(" %v8 = stablehlo.tanh %v7 : tensor", rows, width); // t = tanh(0.7978845608*(gate+0.044715*gate*gate*gate)) fd_line(" %v9 = stablehlo.constant dense<5.00000000000000000e-01> : tensor", rows, width); fd_line(" %v10 = stablehlo.multiply %v9, %gate : tensor", rows, width); fd_line(" %v11 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v12 = stablehlo.add %v11, %v8 : tensor", rows, width); fd_line(" %v13 = stablehlo.multiply %v10, %v12 : tensor", rows, width); // a = 0.5*gate*(1+t) fd_line(" %v14 = stablehlo.multiply %x, %v13 : tensor", rows, width); // y = x*a fd_line(" %v15 = stablehlo.multiply %upstream, %v13 : tensor", rows, width); // dx = upstream*a fd_line(" %v16 = stablehlo.multiply %upstream, %x : tensor", rows, width); fd_line(" %v17 = stablehlo.multiply %v9, %v12 : tensor", rows, width); fd_line(" %v18 = stablehlo.multiply %v8, %v8 : tensor", rows, width); fd_line(" %v19 = stablehlo.subtract %v11, %v18 : tensor", rows, width); fd_line(" %v20 = stablehlo.multiply %v10, %v19 : tensor", rows, width); fd_line(" %v21 = stablehlo.multiply %v20, %v1 : tensor", rows, width); fd_line(" %v22 = stablehlo.constant dense<1.34144999999999986e-01> : tensor", rows, width); fd_line(" %v23 = stablehlo.multiply %v22, %gate : tensor", rows, width); fd_line(" %v24 = stablehlo.multiply %v23, %gate : tensor", rows, width); fd_line(" %v25 = stablehlo.add %v11, %v24 : tensor", rows, width); fd_line(" %v26 = stablehlo.multiply %v21, %v25 : tensor", rows, width); fd_line(" %v27 = stablehlo.add %v17, %v26 : tensor", rows, width); fd_line(" %v28 = stablehlo.multiply %v16, %v27 : tensor", rows, width); // dg = upstream*x*(0.5*(1+t)+0.5*gate*(1-t*t)*0.7978845608*(1+0.134145*gate*gate)) fd_line(" return %v14, %v15, %v28 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }