aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/tf2xla/kernels/xla_pad_op.cc
blob: 59502d83c7338bd1b05b3323a97761fff2da186a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "absl/algorithm/container.h"
#include "absl/strings/str_join.h"
#include "tensorflow/compiler/tf2xla/shape_util.h"
#include "tensorflow/compiler/tf2xla/xla_compiler.h"
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/client/xla_builder.h"
#include "tensorflow/core/framework/op_kernel.h"

namespace tensorflow {
namespace {

class XlaPadOp : public XlaOpKernel {
 public:
  explicit XlaPadOp(OpKernelConstruction* context) : XlaOpKernel(context) {}

  void Compile(XlaOpKernelContext* context) override {
    const TensorShape input_shape = context->InputShape("input");
    const TensorShape padding_value_shape =
        context->InputShape("padding_value");

    std::vector<int64> padding_low;
    std::vector<int64> padding_high;
    std::vector<int64> padding_interior;
    OP_REQUIRES_OK(context, context->ConstantInputAsIntVector("padding_low",
                                                              &padding_low));
    OP_REQUIRES_OK(context, context->ConstantInputAsIntVector("padding_high",
                                                              &padding_high));
    OP_REQUIRES_OK(context, context->ConstantInputAsIntVector(
                                "padding_interior", &padding_interior));

    OP_REQUIRES(context, TensorShapeUtils::IsScalar(padding_value_shape),
                errors::InvalidArgument("padding_value must be a scalar"));
    const int rank = input_shape.dims();
    OP_REQUIRES(context, rank == padding_low.size(),
                errors::InvalidArgument(
                    "The size of padding_low must be equal to the input "
                    "rank (",
                    padding_low.size(), " vs. ", rank, ")"));
    OP_REQUIRES(context, rank == padding_high.size(),
                errors::InvalidArgument(
                    "The size of padding_high must be equal to the input "
                    "rank (",
                    padding_high.size(), " vs. ", rank, ")"));
    OP_REQUIRES(context, rank == padding_interior.size(),
                errors::InvalidArgument(
                    "The size of padding_interior must be equal to the input "
                    "rank (",
                    padding_interior.size(), " vs. ", rank, ")"));

    auto non_negative = [](int64 x) { return x >= 0; };
    OP_REQUIRES(
        context, absl::c_all_of(padding_low, non_negative),
        errors::InvalidArgument("padding_low must be non-negative, got [",
                                absl::StrJoin(padding_low, ","), "]"));
    OP_REQUIRES(
        context, absl::c_all_of(padding_high, non_negative),
        errors::InvalidArgument("padding_high must be non-negative, got [",
                                absl::StrJoin(padding_high, ","), "]"));
    OP_REQUIRES(
        context, absl::c_all_of(padding_interior, non_negative),
        errors::InvalidArgument("padding_interior must be non-negative, got [",
                                absl::StrJoin(padding_interior, ","), "]"));

    xla::PaddingConfig padding_config;
    for (int i = 0; i < rank; ++i) {
      auto* dim = padding_config.add_dimensions();
      dim->set_edge_padding_low(padding_low[i]);
      dim->set_edge_padding_high(padding_high[i]);
      dim->set_interior_padding(padding_interior[i]);
    }

    xla::XlaOp output =
        xla::Pad(context->Input("input"), context->Input("padding_value"),
                 padding_config);
    context->SetOutput(0, output);
  }

 private:
  TF_DISALLOW_COPY_AND_ASSIGN(XlaPadOp);
};

REGISTER_XLA_OP(Name("XlaPad")
                    .CompileTimeConstInput("padding_low")
                    .CompileTimeConstInput("padding_high")
                    .CompileTimeConstInput("padding_interior"),
                XlaPadOp);

}  // namespace
}  // namespace tensorflow