| 108 | explicit SizeOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} |
| 109 | |
| 110 | void Compile(XlaOpKernelContext* ctx) override { |
| 111 | const TensorShape input_shape = ctx->InputShape(0); |
| 112 | OP_REQUIRES(ctx, |
| 113 | FastBoundsCheck(input_shape.num_elements(), |
| 114 | std::numeric_limits<int32>::max()), |
| 115 | errors::InvalidArgument("Size does not work for tensors > " |
| 116 | "int32 max.")); |
| 117 | Tensor size_constant(DT_INT32, TensorShape({})); |
| 118 | const int rank = input_shape.dims(); |
| 119 | xla::XlaBuilder* builder = ctx->builder(); |
| 120 | auto size = xla::One(builder, xla::U32); |
| 121 | for (int64 i = 0; i < rank; ++i) { |
| 122 | size = xla::Mul( |
| 123 | size, xla::ConvertElementType(xla::GetDimensionSize(ctx->Input(0), i), |
| 124 | xla::U32)); |
| 125 | } |
| 126 | size = xla::ConvertElementType(size, ctx->output_xla_type(0)); |
| 127 | ctx->SetOutput(0, size); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | REGISTER_XLA_OP(Name("Size").CompilationOnly().IsMetadataOp(), SizeOp); |
nothing calls this directly
no test coverage detected