// Train a pairwise ranker on preferred and rejected scores and return both gradients. use graph_io; pub fn fd_pairwise_rank(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%winner: tensor, %loser: tensor) -> (tensor, tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.subtract %loser, %winner : tensor", rows, width); // d = loser-winner fd_line(" %v2 = stablehlo.logistic %v1 : tensor", rows, width); // p = sig(d) fd_line(" %v3 = stablehlo.maximum %v1, %v0 : tensor", rows, width); fd_line(" %v4 = stablehlo.abs %v1 : tensor", rows, width); fd_line(" %v5 = stablehlo.negate %v4 : tensor", rows, width); fd_line(" %v6 = stablehlo.exponential %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.log_plus_one %v6 : tensor", rows, width); fd_line(" %v8 = stablehlo.add %v3, %v7 : tensor", rows, width); // loss = maximum(d,0)+log1p(exp(-abs(d))) fd_line(" %v9 = stablehlo.negate %v2 : tensor", rows, width); // preferred_grad = -p // rejected_grad = p fd_line(" return %v8, %v9, %v2 : tensor, tensor, tensor", rows, width); println!(" }"); println!("}"); }