// Compute stable binary cross entropy from logits and its derivative. Supply binary or soft targets in [0,1]. use graph_io; pub fn fd_logistic_loss(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%x: tensor, %target: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); fd_line(" %v1 = stablehlo.logistic %x : tensor", rows, width); // prob = sig(x) fd_line(" %v2 = stablehlo.maximum %x, %v0 : tensor", rows, width); fd_line(" %v3 = stablehlo.multiply %x, %target : tensor", rows, width); fd_line(" %v4 = stablehlo.subtract %v2, %v3 : tensor", rows, width); fd_line(" %v5 = stablehlo.abs %x : tensor", rows, width); fd_line(" %v6 = stablehlo.negate %v5 : tensor", rows, width); fd_line(" %v7 = stablehlo.exponential %v6 : tensor", rows, width); fd_line(" %v8 = stablehlo.log_plus_one %v7 : tensor", rows, width); fd_line(" %v9 = stablehlo.add %v4, %v8 : tensor", rows, width); // loss = maximum(x,0)-x*target+log1p(exp(-abs(x))) fd_line(" %v10 = stablehlo.subtract %v1, %target : tensor", rows, width); // grad = prob-target fd_line(" return %v9, %v10 : tensor, tensor", rows, width); println!(" }"); println!("}"); }