// Advance a momentum buffer and apply the look-ahead gradient used by Nesterov SGD. Momentum is 0.9. use graph_io; pub fn fd_nesterov_step(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%weight: tensor, %grad: tensor, %velocity: tensor, %lr: 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, %velocity : tensor", rows, width); fd_line(" %v3 = stablehlo.add %v2, %grad : tensor", rows, width); // next_velocity = 0.9*velocity+grad fd_line(" %v4 = stablehlo.multiply %v1, %v3 : tensor", rows, width); fd_line(" %v5 = stablehlo.add %grad, %v4 : tensor", rows, width); // direction = grad+0.9*next_velocity fd_line(" %v6 = stablehlo.multiply %lr, %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.subtract %weight, %v6 : tensor", rows, width); // updated = weight-lr*direction fd_line(" return %v7, %v3 : tensor, tensor", rows, width); println!(" }"); println!("}"); }