// Apply AdamW with decoupled decay, beta1=0.9 and beta2=0.999. Pass bias denominators 1-beta^step and nonnegative second moments. use graph_io; pub fn fd_adamw_step(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%weight: tensor, %grad: tensor, %moment: tensor, %variance: tensor, %lr: tensor, %decay: tensor, %correction1: tensor, %correction2: tensor) -> (tensor, tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.constant dense<9.00000000000000022e-01> : tensor", rows, width); fd_line(" %v2 = stablehlo.multiply %v1, %moment : tensor", rows, width); fd_line(" %v3 = stablehlo.constant dense<1.00000000000000006e-01> : tensor", rows, width); fd_line(" %v4 = stablehlo.multiply %v3, %grad : tensor", rows, width); fd_line(" %v5 = stablehlo.add %v2, %v4 : tensor", rows, width); // m = 0.9*moment+0.1*grad fd_line(" %v6 = stablehlo.constant dense<9.98999999999999999e-01> : tensor", rows, width); fd_line(" %v7 = stablehlo.multiply %v6, %variance : tensor", rows, width); fd_line(" %v8 = stablehlo.constant dense<1.00000000000000002e-03> : tensor", rows, width); fd_line(" %v9 = stablehlo.multiply %v8, %grad : tensor", rows, width); fd_line(" %v10 = stablehlo.multiply %v9, %grad : tensor", rows, width); fd_line(" %v11 = stablehlo.add %v7, %v10 : tensor", rows, width); // v = 0.999*variance+0.001*grad*grad fd_line(" %v12 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v13 = stablehlo.multiply %lr, %decay : tensor", rows, width); fd_line(" %v14 = stablehlo.subtract %v12, %v13 : tensor", rows, width); fd_line(" %v15 = stablehlo.multiply %weight, %v14 : tensor", rows, width); fd_line(" %v16 = stablehlo.divide %v5, %correction1 : tensor", rows, width); fd_line(" %v17 = stablehlo.multiply %lr, %v16 : tensor", rows, width); fd_line(" %v18 = stablehlo.divide %v11, %correction2 : tensor", rows, width); fd_line(" %v19 = stablehlo.sqrt %v18 : tensor", rows, width); fd_line(" %v20 = stablehlo.constant dense<1.00000000000000002e-08> : tensor", rows, width); fd_line(" %v21 = stablehlo.add %v19, %v20 : tensor", rows, width); fd_line(" %v22 = stablehlo.divide %v17, %v21 : tensor", rows, width); fd_line(" %v23 = stablehlo.subtract %v15, %v22 : tensor", rows, width); // updated = weight*(1-lr*decay)-lr*(m/correction1)/(sqrt(v/correction2)+1e-8) fd_line(" return %v23, %v5, %v11 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }