| 33 | explicit ReverseOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} |
| 34 | |
| 35 | void Compile(XlaOpKernelContext* ctx) override { |
| 36 | // r = tf.reverse(x, revdims) |
| 37 | const TensorShape x_shape = ctx->InputShape(0); |
| 38 | const TensorShape revd_shape = ctx->InputShape(1); |
| 39 | // Validate input sizes. |
| 40 | OP_REQUIRES(ctx, TensorShapeUtils::IsVector(revd_shape), |
| 41 | errors::InvalidArgument("axes must be a vector, not shape ", |
| 42 | revd_shape.DebugString())); |
| 43 | OP_REQUIRES(ctx, revd_shape.num_elements() == x_shape.dims(), |
| 44 | errors::InvalidArgument("axes ", revd_shape.DebugString(), |
| 45 | " must have same number of elements as" |
| 46 | " than input tensor has dimensions ", |
| 47 | x_shape.DebugString(), ".")); |
| 48 | if (revd_shape.num_elements() == 0) { |
| 49 | ctx->SetOutput(0, ctx->Input(0)); |
| 50 | return; |
| 51 | } |
| 52 | // XlaBuilder::Rev() requires concrete values for dimensions arg. |
| 53 | xla::Literal lax; |
| 54 | OP_REQUIRES_OK(ctx, ctx->ConstantInput(1, &lax)); |
| 55 | |
| 56 | std::vector<int64> dimensions; |
| 57 | for (int d = 0; d < x_shape.dims(); ++d) { |
| 58 | if (lax.Get<bool>({d})) { |
| 59 | dimensions.push_back(d); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | ctx->SetOutput(0, xla::Rev(ctx->Input(0), dimensions)); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | REGISTER_XLA_OP(Name("Reverse").CompileTimeConstantInput("dims"), ReverseOp); |
nothing calls this directly
no test coverage detected