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/client/xla_builder.h" 23 #include "tensorflow/compiler/xla/literal.h" 24 #include "tensorflow/core/framework/op_kernel.h" 25 #include "tensorflow/core/framework/register_types.h" 26 #include "tensorflow/core/framework/tensor.h" 27 28 namespace tensorflow { 29 namespace { 30 31 class ReverseOp : public XlaOpKernel { 32 public: ReverseOp(OpKernelConstruction * ctx)33 explicit ReverseOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 34 Compile(XlaOpKernelContext * ctx)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); 68 69 class ReverseV2Op : public XlaOpKernel { 70 public: ReverseV2Op(OpKernelConstruction * ctx)71 explicit ReverseV2Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 72 Compile(XlaOpKernelContext * ctx)73 void Compile(XlaOpKernelContext* ctx) override { 74 // r = tf.reverse(x, axes) 75 const TensorShape x_shape = ctx->InputShape(0); 76 const TensorShape axes_shape = ctx->InputShape(1); 77 // Validate input sizes. 78 OP_REQUIRES(ctx, TensorShapeUtils::IsVector(axes_shape), 79 errors::InvalidArgument("axes must be a vector, not shape ", 80 axes_shape.DebugString())); 81 OP_REQUIRES(ctx, axes_shape.num_elements() <= x_shape.dims(), 82 errors::InvalidArgument("axes ", axes_shape.DebugString(), 83 " can not have more elements" 84 " than input tensor has dimensions ", 85 x_shape.DebugString(), ".")); 86 // Reverse is a no-op if axes argument is empty. 87 if (axes_shape.num_elements() == 0) { 88 ctx->SetOutput(0, ctx->Input(0)); 89 return; 90 } 91 // XlaBuilder::Rev() requires concrete values for dimensions arg. 92 std::vector<int64> axes; 93 OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(1, &axes)); 94 95 // witnessed_axes is used to ensure that the same axis is not marked to be 96 // reversed multiple times. 97 absl::InlinedVector<bool, 8> witnessed_axes(x_shape.dims(), false); 98 99 for (int d = 0; d < axes.size(); ++d) { 100 OP_REQUIRES( 101 ctx, (-x_shape.dims() <= axes[d]) && (axes[d] < x_shape.dims()), 102 errors::InvalidArgument(axes[d], " is out of range [-", 103 x_shape.dims(), ", ", x_shape.dims(), ").")); 104 // Axes can be negative and are shifted to the canonical index before 105 // being lowered to HLO. 106 if (axes[d] < 0) { 107 axes[d] += x_shape.dims(); 108 } 109 OP_REQUIRES(ctx, !witnessed_axes[axes[d]], 110 errors::InvalidArgument("canonicalized axis ", axes[d], 111 " was repeated.")); 112 witnessed_axes[axes[d]] = true; 113 } 114 115 ctx->SetOutput(0, xla::Rev(ctx->Input(0), axes)); 116 } 117 }; 118 119 REGISTER_XLA_OP(Name("ReverseV2").CompileTimeConstantInput("axis"), 120 ReverseV2Op); 121 122 } // namespace 123 } // namespace tensorflow 124