// Measure robust regression error in log space and compute its bounded tanh derivative. use graph_io; pub fn fd_log_cosh(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.subtract %x, %target : tensor", rows, width); // r = x-target fd_line(" %v2 = stablehlo.abs %v1 : tensor", rows, width); // a = abs(r) fd_line(" %v3 = stablehlo.constant dense<2.00000000000000000e+00> : tensor", rows, width); fd_line(" %v4 = stablehlo.negate %v3 : tensor", rows, width); fd_line(" %v5 = stablehlo.multiply %v4, %v2 : 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 %v2, %v7 : tensor", rows, width); fd_line(" %v9 = stablehlo.constant dense<6.93147180559945286e-01> : tensor", rows, width); fd_line(" %v10 = stablehlo.subtract %v8, %v9 : tensor", rows, width); // loss = a+log1p(exp(-2*a))-0.6931471805599453 fd_line(" %v11 = stablehlo.tanh %v1 : tensor", rows, width); // grad = tanh(r) fd_line(" return %v10, %v11 : tensor, tensor", rows, width); println!(" }"); println!("}"); }