| 84 | explicit SelectOpV2(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} |
| 85 | |
| 86 | void Compile(XlaOpKernelContext* ctx) override { |
| 87 | const TensorShape cond_shape = ctx->InputShape(0); |
| 88 | const TensorShape then_shape = ctx->InputShape(1); |
| 89 | const TensorShape else_shape = ctx->InputShape(2); |
| 90 | |
| 91 | // Compute the output shape from the broadcast of the two data inputs, with |
| 92 | // the broadcast of the conditional. |
| 93 | // Then Broadcast all three inputs to the output shape and emit a select. |
| 94 | |
| 95 | BCast bcast_then_else(BCast::FromShape(then_shape), |
| 96 | BCast::FromShape(else_shape), |
| 97 | /*fewer_dims_optimization=*/false); |
| 98 | if (!bcast_then_else.IsValid()) { |
| 99 | ctx->SetStatus(errors::InvalidArgument( |
| 100 | "Incompatible shapes: ", then_shape.DebugString(), " vs. ", |
| 101 | else_shape.DebugString())); |
| 102 | return; |
| 103 | } |
| 104 | BCast bcast(bcast_then_else.output_shape(), BCast::FromShape(cond_shape), |
| 105 | /*fewer_dims_optimization=*/false); |
| 106 | if (!bcast.IsValid()) { |
| 107 | ctx->SetStatus(errors::InvalidArgument( |
| 108 | "Incompatible shapes: ", |
| 109 | BCast::ToShape(bcast_then_else.output_shape()).DebugString(), " vs. ", |
| 110 | cond_shape.DebugString())); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | auto bcasted_cond = BroadcastTo(ctx->Input(0), bcast.output_shape()); |
| 115 | OP_REQUIRES_OK(ctx, bcasted_cond.status()); |
| 116 | auto cond_handle = bcasted_cond.ValueOrDie(); |
| 117 | |
| 118 | auto bcasted_then = BroadcastTo(ctx->Input(1), bcast.output_shape()); |
| 119 | OP_REQUIRES_OK(ctx, bcasted_then.status()); |
| 120 | auto then_handle = bcasted_then.ValueOrDie(); |
| 121 | |
| 122 | auto bcasted_else = BroadcastTo(ctx->Input(2), bcast.output_shape()); |
| 123 | OP_REQUIRES_OK(ctx, bcasted_else.status()); |
| 124 | auto else_handle = bcasted_else.ValueOrDie(); |
| 125 | |
| 126 | ctx->SetOutput(0, xla::Select(cond_handle, then_handle, else_handle)); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | TF_DISALLOW_COPY_AND_ASSIGN(SelectOpV2); |
nothing calls this directly
no test coverage detected