1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // XLA-specific reverse Op. 17 18 #include "tensorflow/compiler/tf2xla/type_util.h" 19 #include "tensorflow/compiler/tf2xla/xla_helpers.h" 20 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" 21 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 22 #include "tensorflow/compiler/xla/literal_util.h" 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/register_types.h" 25 #include "tensorflow/core/framework/tensor.h" 26 27 namespace tensorflow { 28 namespace { 29 30 class ReverseOp : public XlaOpKernel { 31 public: ReverseOp(OpKernelConstruction * ctx)32 explicit ReverseOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 33 Compile(XlaOpKernelContext * ctx)34 void Compile(XlaOpKernelContext* ctx) override { 35 // r = tf.reverse(x, revdims) 36 const TensorShape x_shape = ctx->InputShape(0); 37 const TensorShape revd_shape = ctx->InputShape(1); 38 // Validate input sizes. 39 OP_REQUIRES(ctx, TensorShapeUtils::IsVector(revd_shape), 40 errors::InvalidArgument("axes must be a vector, not shape ", 41 revd_shape.DebugString())); 42 OP_REQUIRES(ctx, revd_shape.num_elements() == x_shape.dims(), 43 errors::InvalidArgument("axes ", revd_shape.DebugString(), 44 " must have same number of elements as" 45 " than input tensor has dimensions ", 46 x_shape.DebugString(), ".")); 47 if (revd_shape.num_elements() == 0) { 48 ctx->SetOutput(0, ctx->Input(0)); 49 return; 50 } 51 // ComputationBuilder::Rev() requires concrete values for dimensions arg. 52 xla::Literal lax; 53 OP_REQUIRES_OK(ctx, ctx->ConstantInputReshaped(1, {x_shape.dims()}, &lax)); 54 std::vector<bool> revdims(x_shape.dims()); 55 std::copy(lax.data<bool>().begin(), lax.data<bool>().end(), 56 revdims.begin()); 57 std::vector<int64> dimensions; 58 59 for (int d = 0; d < x_shape.dims(); ++d) { 60 if (revdims[d]) { 61 dimensions.push_back(d); 62 } 63 } 64 65 ctx->SetOutput(0, ctx->builder()->Rev(ctx->Input(0), dimensions)); 66 } 67 }; 68 69 REGISTER_XLA_OP(Name("Reverse").CompileTimeConstInput("dims"), ReverseOp); 70 71 class ReverseV2Op : public XlaOpKernel { 72 public: ReverseV2Op(OpKernelConstruction * ctx)73 explicit ReverseV2Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 74 Compile(XlaOpKernelContext * ctx)75 void Compile(XlaOpKernelContext* ctx) override { 76 // r = tf.reverse(x, axes) 77 const TensorShape x_shape = ctx->InputShape(0); 78 const TensorShape axes_shape = ctx->InputShape(1); 79 // Validate input sizes. 80 OP_REQUIRES(ctx, TensorShapeUtils::IsVector(axes_shape), 81 errors::InvalidArgument("axes must be a vector, not shape ", 82 axes_shape.DebugString())); 83 OP_REQUIRES(ctx, axes_shape.num_elements() <= x_shape.dims(), 84 errors::InvalidArgument("axes ", axes_shape.DebugString(), 85 " can not have more elements" 86 " than input tensor has dimensions ", 87 x_shape.DebugString(), ".")); 88 // Reverse is a no-op if axes argument is empty. 89 if (axes_shape.num_elements() == 0) { 90 ctx->SetOutput(0, ctx->Input(0)); 91 return; 92 } 93 // ComputationBuilder::Rev() requires concrete values for dimensions arg. 94 std::vector<int64> axes; 95 OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(1, &axes)); 96 97 for (int d = 0; d < axes.size(); ++d) { 98 OP_REQUIRES(ctx, (0 <= axes[d]) && (axes[d] < x_shape.dims()), 99 errors::InvalidArgument(axes[d], " is out of range [0, ", 100 x_shape.dims(), ").")); 101 } 102 103 ctx->SetOutput(0, ctx->builder()->Rev(ctx->Input(0), axes)); 104 } 105 }; 106 107 REGISTER_XLA_OP(Name("ReverseV2").CompileTimeConstInput("axis"), ReverseV2Op); 108 109 } // namespace 110 } // namespace tensorflow 111