// Merge two per-example mean gradients using their counts, then emit the merged mean and new count. Nonnegative counts are row constants. use graph_io; pub fn fd_weighted_accumulation(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%first: tensor, %second: tensor, %count1: tensor, %count2: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.add %count1, %count2 : tensor", rows, width); // count = count1+count2 fd_line(" %v2 = stablehlo.multiply %first, %count1 : tensor", rows, width); fd_line(" %v3 = stablehlo.multiply %second, %count2 : tensor", rows, width); fd_line(" %v4 = stablehlo.add %v2, %v3 : tensor", rows, width); fd_line(" %v5 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v6 = stablehlo.maximum %v1, %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.divide %v4, %v6 : tensor", rows, width); // merged = (first*count1+second*count2)/maximum(count,1) fd_line(" return %v7, %v1 : tensor, tensor", rows, width); println!(" }"); println!("}"); }