Home | History | Annotate | Download | only in kernels
      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/core/util/mirror_pad_mode.h"
     20 
     21 namespace tensorflow {
     22 namespace {
     23 
     24 class MirrorPadOp : public XlaOpKernel {
     25  public:
     26   explicit MirrorPadOp(OpKernelConstruction* context) : XlaOpKernel(context) {}
     27 
     28   xla::StatusOr<xla::ComputationDataHandle> DoMirrorPad(
     29       const xla::ComputationDataHandle& t, const xla::Shape& original_shape,
     30       const xla::Literal& pad_literal, xla::ComputationBuilder* b) {
     31     xla::ComputationDataHandle accum = t;
     32     for (int64 dimno = xla::ShapeUtil::Rank(original_shape) - 1; dimno >= 0;
     33          --dimno) {
     34       auto t_rev = b->Rev(accum, {dimno});
     35       TF_ASSIGN_OR_RETURN(int64 lhs_padding,
     36                           pad_literal.GetIntegralAsS64({dimno, 0}));
     37       TF_ASSIGN_OR_RETURN(int64 rhs_padding,
     38                           pad_literal.GetIntegralAsS64({dimno, 1}));
     39       int64 dim_size = original_shape.dimensions(dimno);
     40       auto lhs_pad = b->SliceInDim(t_rev, dim_size - 1 - lhs_padding,
     41                                    dim_size - 1, 1, dimno);
     42       auto rhs_pad = b->SliceInDim(t_rev, 1, 1 + rhs_padding, 1, dimno);
     43       accum = b->ConcatInDim({lhs_pad, accum, rhs_pad}, dimno);
     44     }
     45     return accum;
     46   }
     47 
     48   void Compile(XlaOpKernelContext* ctx) override {
     49     const TensorShape input_shape = ctx->InputShape(0);
     50     const TensorShape pad_shape = ctx->InputShape(1);
     51 
     52     MirrorPadMode mode;
     53     OP_REQUIRES_OK(ctx, GetNodeAttr(def(), "mode", &mode));
     54     OP_REQUIRES(ctx, mode == MirrorPadMode::REFLECT,
     55                 xla::Unimplemented(
     56                     "Only REFLECT MirrorPad mode is currently supported"));
     57 
     58     const int dims = input_shape.dims();
     59     OP_REQUIRES(
     60         ctx,
     61         TensorShapeUtils::IsMatrix(pad_shape) && pad_shape.dim_size(1) == 2,
     62         errors::InvalidArgument("paddings must be a matrix with 2 columns: ",
     63                                 pad_shape.DebugString()));
     64     const int fixed_dims =
     65         (allow_legacy_scalars() && dims == 0 && pad_shape.dim_size(0) == 1)
     66             ? 1
     67             : dims;
     68     OP_REQUIRES(
     69         ctx, fixed_dims == pad_shape.dim_size(0),
     70         errors::InvalidArgument(
     71             "The first dimension of paddings must be the rank of inputs",
     72             pad_shape.DebugString(), " ", input_shape.DebugString()));
     73 
     74     // Evaluate the 'padding' constant input, reshaping to a matrix.
     75     xla::Literal pad_literal;
     76     OP_REQUIRES_OK(
     77         ctx, ctx->ConstantInputReshaped(1, {fixed_dims, 2}, &pad_literal));
     78 
     79     xla::ComputationBuilder* b = ctx->builder();
     80     auto in0 = ctx->Input(0);
     81     xla::StatusOr<std::unique_ptr<xla::Shape>> in0_shape = b->GetShape(in0);
     82     OP_REQUIRES(ctx, in0_shape.ok(), in0_shape.status());
     83     xla::StatusOr<xla::ComputationDataHandle> accum_status =
     84         DoMirrorPad(in0, *in0_shape.ValueOrDie(), pad_literal, b);
     85 
     86     OP_REQUIRES_OK(ctx, accum_status.status());
     87 
     88     ctx->SetOutput(0, accum_status.ValueOrDie());
     89   }
     90 
     91  private:
     92   TF_DISALLOW_COPY_AND_ASSIGN(MirrorPadOp);
     93 };
     94 
     95 REGISTER_XLA_OP(Name("MirrorPad").CompileTimeConstInput("paddings"),
     96                 MirrorPadOp);
     97 
     98 }  // namespace
     99 }  // namespace tensorflow
    100