// Fit continuous targets with unit-transition Huber loss. Clip the residual influence of large errors. use graph_io; pub fn fd_huber_fit(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<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v4 = stablehlo.minimum %v2, %v3 : tensor", rows, width); // q = minimum(a,1) fd_line(" %v5 = stablehlo.constant dense<5.00000000000000000e-01> : tensor", rows, width); fd_line(" %v6 = stablehlo.multiply %v5, %v4 : tensor", rows, width); fd_line(" %v7 = stablehlo.multiply %v6, %v4 : tensor", rows, width); fd_line(" %v8 = stablehlo.add %v7, %v2 : tensor", rows, width); fd_line(" %v9 = stablehlo.subtract %v8, %v4 : tensor", rows, width); // loss = 0.5*q*q+a-q fd_line(" %v10 = stablehlo.negate %v3 : tensor", rows, width); fd_line(" %v11 = stablehlo.maximum %v1, %v10 : tensor", rows, width); fd_line(" %v12 = stablehlo.minimum %v11, %v3 : tensor", rows, width); // grad = clip(r,-1,1) fd_line(" return %v9, %v12 : tensor, tensor", rows, width); println!(" }"); println!("}"); }