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 #include "tensorflow/compiler/tf2xla/xla_helpers.h" 17 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" 18 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 19 #include "tensorflow/compiler/xla/client/xla_builder.h" 20 #include "tensorflow/core/util/mirror_pad_mode.h" 21 22 namespace tensorflow { 23 namespace { 24 25 class MirrorPadOp : public XlaOpKernel { 26 public: MirrorPadOp(OpKernelConstruction * context)27 explicit MirrorPadOp(OpKernelConstruction* context) : XlaOpKernel(context) {} 28 DoMirrorPad(const xla::XlaOp & t,const xla::Shape & original_shape,const xla::LiteralSlice & pad_literal,const MirrorPadMode mode,xla::XlaBuilder * b)29 xla::StatusOr<xla::XlaOp> DoMirrorPad(const xla::XlaOp& t, 30 const xla::Shape& original_shape, 31 const xla::LiteralSlice& pad_literal, 32 const MirrorPadMode mode, 33 xla::XlaBuilder* b) { 34 // The difference in the semantics of REFLECT and SYMMETRIC is that REFLECT 35 // will not mirror the border values while symmetric does. 36 // e.g. input is [1, 2, 3] and paddings is [0, 2], then the output is: 37 // - [1, 2, 3, 2, 1] in reflect mode 38 // - [1, 2, 3, 3, 2] in symmetric mode. 39 int64 excluded_edges = mode == MirrorPadMode::REFLECT ? 1 : 0; 40 xla::XlaOp accum = t; 41 for (int64 dimno = original_shape.rank() - 1; dimno >= 0; --dimno) { 42 auto t_rev = xla::Rev(accum, {dimno}); 43 int64 lhs_padding = pad_literal.Get<int64>({dimno, 0}); 44 int64 rhs_padding = pad_literal.Get<int64>({dimno, 1}); 45 int64 dim_size = original_shape.dimensions(dimno); 46 47 // Padding amounts on each side must be no more than the size of the 48 // original shape. 49 TF_RET_CHECK(lhs_padding >= 0 && 50 lhs_padding <= dim_size - excluded_edges); 51 TF_RET_CHECK(rhs_padding >= 0 && 52 rhs_padding <= dim_size - excluded_edges); 53 54 auto lhs_pad = 55 xla::SliceInDim(t_rev, dim_size - excluded_edges - lhs_padding, 56 dim_size - excluded_edges, 1, dimno); 57 auto rhs_pad = xla::SliceInDim(t_rev, excluded_edges, 58 excluded_edges + rhs_padding, 1, dimno); 59 accum = xla::ConcatInDim(b, {lhs_pad, accum, rhs_pad}, dimno); 60 } 61 return accum; 62 } 63 Compile(XlaOpKernelContext * ctx)64 void Compile(XlaOpKernelContext* ctx) override { 65 const TensorShape input_shape = ctx->InputShape("input"); 66 const TensorShape pad_shape = ctx->InputShape("paddings"); 67 68 MirrorPadMode mode; 69 OP_REQUIRES_OK(ctx, GetNodeAttr(def(), "mode", &mode)); 70 OP_REQUIRES( 71 ctx, mode == MirrorPadMode::REFLECT || mode == MirrorPadMode::SYMMETRIC, 72 xla::Unimplemented("Unsupported MirrorPad mode. Only SYMMETRIC and " 73 "REFLECT modes are currently supported")); 74 75 const int dims = input_shape.dims(); 76 OP_REQUIRES( 77 ctx, 78 TensorShapeUtils::IsMatrix(pad_shape) && pad_shape.dim_size(1) == 2, 79 errors::InvalidArgument("paddings must be a matrix with 2 columns: ", 80 pad_shape.DebugString())); 81 OP_REQUIRES( 82 ctx, dims == pad_shape.dim_size(0), 83 errors::InvalidArgument( 84 "The first dimension of paddings must be the rank of inputs", 85 pad_shape.DebugString(), " ", input_shape.DebugString())); 86 87 // Evaluate the 'padding' constant input, reshaping to a matrix. 88 xla::Literal pad_literal; 89 OP_REQUIRES_OK(ctx, 90 ctx->ConstantInputAsInt64Literal("paddings", &pad_literal)); 91 92 xla::XlaBuilder* b = ctx->builder(); 93 auto in0 = ctx->Input("input"); 94 xla::StatusOr<xla::Shape> in0_shape = b->GetShape(in0); 95 OP_REQUIRES(ctx, in0_shape.ok(), in0_shape.status()); 96 xla::StatusOr<xla::XlaOp> accum_status = 97 DoMirrorPad(in0, in0_shape.ValueOrDie(), pad_literal, mode, b); 98 99 OP_REQUIRES_OK(ctx, accum_status.status()); 100 101 ctx->SetOutput(0, accum_status.ValueOrDie()); 102 } 103 104 private: 105 TF_DISALLOW_COPY_AND_ASSIGN(MirrorPadOp); 106 }; 107 108 REGISTER_XLA_OP(Name("MirrorPad").CompileTimeConstantInput("paddings"), 109 MirrorPadOp); 110 111 } // namespace 112 } // namespace tensorflow 113