| 33 | explicit SelectOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} |
| 34 | |
| 35 | void Compile(XlaOpKernelContext* ctx) override { |
| 36 | const TensorShape cond_shape = ctx->InputShape(0); |
| 37 | const TensorShape then_shape = ctx->InputShape(1); |
| 38 | const TensorShape else_shape = ctx->InputShape(2); |
| 39 | |
| 40 | OP_REQUIRES( |
| 41 | ctx, then_shape.IsSameSize(else_shape), |
| 42 | errors::InvalidArgument( |
| 43 | "'then' and 'else' must have the same size. but received: ", |
| 44 | then_shape.DebugString(), " vs. ", else_shape.DebugString())); |
| 45 | |
| 46 | auto cond_handle = ctx->Input(0); |
| 47 | auto then_handle = ctx->Input(1); |
| 48 | auto else_handle = ctx->Input(2); |
| 49 | |
| 50 | bool broadcasting = !cond_shape.IsSameSize(then_shape); |
| 51 | bool cond_is_scalar = TensorShapeUtils::IsScalar(cond_shape); |
| 52 | if (broadcasting && !cond_is_scalar) { |
| 53 | OP_REQUIRES(ctx, TensorShapeUtils::IsVector(cond_shape), |
| 54 | errors::InvalidArgument( |
| 55 | "'cond' must be a scalar or a vector, but saw shape: ", |
| 56 | cond_shape.DebugString())); |
| 57 | OP_REQUIRES(ctx, TensorShapeUtils::IsVectorOrHigher(then_shape), |
| 58 | errors::InvalidArgument( |
| 59 | "'then' must be at least a vector, but saw shape: ", |
| 60 | then_shape.DebugString())); |
| 61 | OP_REQUIRES(ctx, then_shape.dim_size(0) == cond_shape.num_elements(), |
| 62 | errors::InvalidArgument("Number of batches of 'then' must " |
| 63 | "match size of 'cond', but saw: ", |
| 64 | then_shape.dim_size(0), " vs. ", |
| 65 | cond_shape.num_elements())); |
| 66 | |
| 67 | // Broadcast into the dimensions on the right. |
| 68 | std::vector<int64> broadcast_dimensions(cond_shape.dims()); |
| 69 | absl::c_iota(broadcast_dimensions, 0); |
| 70 | cond_handle = xla::BroadcastInDim(cond_handle, then_shape.dim_sizes(), |
| 71 | broadcast_dimensions); |
| 72 | } |
| 73 | ctx->SetOutput(0, xla::Select(cond_handle, then_handle, else_handle)); |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | TF_DISALLOW_COPY_AND_ASSIGN(SelectOp); |
nothing calls this directly
no test coverage detected