// Distil a teacher into student logits with temperature two and the temperature-squared loss convention. use graph_io; pub fn fd_teacher_fit(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%student: tensor, %teacher: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.constant dense<2.00000000000000000e+00> : tensor", rows, width); fd_line(" %v2 = stablehlo.divide %student, %v1 : tensor", rows, width); println!(" %v3 = stablehlo.constant dense<-3.40282346000000007e+38> : tensor"); fd_line(" %v4 = stablehlo.reduce(%v2 init: %v3) applies stablehlo.maximum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v5 = stablehlo.broadcast_in_dim %v4, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v6 = stablehlo.subtract %v2, %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.exponential %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.log %v10 : tensor", rows, width); fd_line(" %v12 = stablehlo.subtract %v6, %v11 : tensor", rows, width); // p = logsoft(student/2) fd_line(" %v13 = stablehlo.divide %teacher, %v1 : tensor", rows, width); fd_line(" %v14 = stablehlo.reduce(%v13 init: %v3) applies stablehlo.maximum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v15 = stablehlo.broadcast_in_dim %v14, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v16 = stablehlo.subtract %v13, %v15 : tensor", rows, width); fd_line(" %v17 = stablehlo.exponential %v16 : tensor", rows, width); fd_line(" %v18 = stablehlo.reduce(%v17 init: %v8) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v19 = stablehlo.broadcast_in_dim %v18, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v20 = stablehlo.log %v19 : tensor", rows, width); fd_line(" %v21 = stablehlo.subtract %v16, %v20 : tensor", rows, width); // t = logsoft(teacher/2) fd_line(" %v22 = stablehlo.constant dense<4.00000000000000000e+00> : tensor", rows, width); fd_line(" %v23 = stablehlo.exponential %v21 : tensor", rows, width); fd_line(" %v24 = stablehlo.subtract %v21, %v12 : tensor", rows, width); fd_line(" %v25 = stablehlo.multiply %v23, %v24 : tensor", rows, width); fd_line(" %v26 = stablehlo.reduce(%v25 init: %v8) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v27 = stablehlo.broadcast_in_dim %v26, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v28 = stablehlo.multiply %v22, %v27 : tensor", rows, width); // loss = 4*rsum(exp(t)*(t-p)) fd_line(" %v29 = stablehlo.exponential %v12 : tensor", rows, width); fd_line(" %v30 = stablehlo.subtract %v29, %v23 : tensor", rows, width); fd_line(" %v31 = stablehlo.multiply %v1, %v30 : tensor", rows, width); // grad = 2*(exp(p)-exp(t)) fd_line(" return %v28, %v31 : tensor, tensor", rows, width); println!(" }"); println!("}"); }