// Advance an exponential-moving-average teacher and track the mean squared gap from the current student. Decay lies in [0,1]. use graph_io; pub fn fd_teacher_ema(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%teacher: tensor, %student: tensor, %decay: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v2 = stablehlo.subtract %v1, %decay : tensor", rows, width); fd_line(" %v3 = stablehlo.subtract %student, %teacher : tensor", rows, width); fd_line(" %v4 = stablehlo.multiply %v2, %v3 : tensor", rows, width); fd_line(" %v5 = stablehlo.add %teacher, %v4 : tensor", rows, width); // updated = teacher+((1-decay)*(student-teacher)) fd_line(" %v6 = stablehlo.subtract %student, %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.multiply %v6, %v6 : tensor", rows, width); println!(" %v8 = stablehlo.constant dense<0.00000000000000000e+00> : tensor"); fd_line(" %v9 = stablehlo.reduce(%v7 init: %v8) 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.constant dense : tensor", rows, width); fd_line(" %v12 = stablehlo.divide %v10, %v11 : tensor", rows, width); // gap = mean((student-updated)*(student-updated)) fd_line(" return %v5, %v12 : tensor, tensor", rows, width); println!(" }"); println!("}"); }