// Ensemble two model distributions by a probability-space mixture. Supply a row-constant mixing weight in [0,1]. use graph_io; pub fn fd_logit_ensemble(rows: i64, width: i64) { println!("module {"); fd_line(" func.func @main(%first: tensor, %second: tensor, %alpha: tensor) -> (tensor, tensor) {", rows, width); fd_line(" %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor", rows, width); println!(" %v1 = stablehlo.constant dense<-3.40282346000000007e+38> : tensor"); fd_line(" %v2 = stablehlo.reduce(%first init: %v1) applies stablehlo.maximum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v3 = stablehlo.broadcast_in_dim %v2, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v4 = stablehlo.subtract %first, %v3 : tensor", rows, width); fd_line(" %v5 = stablehlo.exponential %v4 : tensor", rows, width); println!(" %v6 = stablehlo.constant dense<0.00000000000000000e+00> : tensor"); fd_line(" %v7 = stablehlo.reduce(%v5 init: %v6) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v8 = stablehlo.broadcast_in_dim %v7, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v9 = stablehlo.log %v8 : tensor", rows, width); fd_line(" %v10 = stablehlo.subtract %v4, %v9 : tensor", rows, width); fd_line(" %v11 = stablehlo.exponential %v10 : tensor", rows, width); // a = exp(logsoft(first)) fd_line(" %v12 = stablehlo.reduce(%second init: %v1) applies stablehlo.maximum across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v13 = stablehlo.broadcast_in_dim %v12, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v14 = stablehlo.subtract %second, %v13 : tensor", rows, width); fd_line(" %v15 = stablehlo.exponential %v14 : tensor", rows, width); fd_line(" %v16 = stablehlo.reduce(%v15 init: %v6) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v17 = stablehlo.broadcast_in_dim %v16, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v18 = stablehlo.log %v17 : tensor", rows, width); fd_line(" %v19 = stablehlo.subtract %v14, %v18 : tensor", rows, width); fd_line(" %v20 = stablehlo.exponential %v19 : tensor", rows, width); // b = exp(logsoft(second)) fd_line(" %v21 = stablehlo.multiply %alpha, %v11 : tensor", rows, width); fd_line(" %v22 = stablehlo.constant dense<1.00000000000000000e+00> : tensor", rows, width); fd_line(" %v23 = stablehlo.subtract %v22, %alpha : tensor", rows, width); fd_line(" %v24 = stablehlo.multiply %v23, %v20 : tensor", rows, width); fd_line(" %v25 = stablehlo.add %v21, %v24 : tensor", rows, width); // mixture = alpha*a+(1-alpha)*b fd_line(" %v26 = stablehlo.constant dense<1.00000000000000008e-30> : tensor", rows, width); fd_line(" %v27 = stablehlo.maximum %v25, %v26 : tensor", rows, width); fd_line(" %v28 = stablehlo.log %v27 : tensor", rows, width); fd_line(" %v29 = stablehlo.multiply %v25, %v28 : tensor", rows, width); fd_line(" %v30 = stablehlo.reduce(%v29 init: %v6) applies stablehlo.add across dimensions = [1] : (tensor, tensor) -> tensor", rows, width); fd_line(" %v31 = stablehlo.broadcast_in_dim %v30, dims = [0] : (tensor) -> tensor", rows, width); fd_line(" %v32 = stablehlo.negate %v31 : tensor", rows, width); // entropy = -rsum(mixture*log(maximum(mixture,1e-30))) fd_line(" return %v25, %v32 : tensor, tensor", rows, width); println!(" }"); println!("}"); }