// Compress a gradient to one sign and one row scale and carry the untransmitted residual to the next step. use graph_io; pub fn fd_error_feedback(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%grad: tensor, %residual: tensor) -> (tensor, tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.add %grad, %residual : tensor", rows, width); // corrected = grad+residual fd_line(" %v2 = stablehlo.abs %v1 : tensor", rows, width); println!(" %v3 = stablehlo.constant dense<0.00000000000000000e+00> : tensor"); fd_line(" %v4 = stablehlo.reduce(%v2 init: %v3) applies stablehlo.add 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.constant dense : tensor", rows, width); fd_line(" %v7 = stablehlo.divide %v5, %v6 : tensor", rows, width); // scale = mean(abs(corrected)) fd_line(" %v8 = stablehlo.sign %v1 : tensor", rows, width); fd_line(" %v9 = stablehlo.multiply %v7, %v8 : tensor", rows, width); // sent = scale*sign(corrected) fd_line(" %v10 = stablehlo.subtract %v1, %v9 : tensor", rows, width); // carry = corrected-sent fd_line(" return %v9, %v10, %v7 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }