• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 "absl/algorithm/container.h"
17 #include "absl/strings/str_join.h"
18 #include "tensorflow/compiler/tf2xla/shape_util.h"
19 #include "tensorflow/compiler/tf2xla/xla_compiler.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/core/framework/op_kernel.h"
24 
25 namespace tensorflow {
26 namespace {
27 
28 class XlaPadOp : public XlaOpKernel {
29  public:
XlaPadOp(OpKernelConstruction * context)30   explicit XlaPadOp(OpKernelConstruction* context) : XlaOpKernel(context) {}
31 
Compile(XlaOpKernelContext * context)32   void Compile(XlaOpKernelContext* context) override {
33     const TensorShape input_shape = context->InputShape("input");
34     const TensorShape padding_value_shape =
35         context->InputShape("padding_value");
36 
37     std::vector<int64_t> padding_low;
38     std::vector<int64_t> padding_high;
39     std::vector<int64_t> padding_interior;
40     OP_REQUIRES_OK(context, context->ConstantInputAsIntVector("padding_low",
41                                                               &padding_low));
42     OP_REQUIRES_OK(context, context->ConstantInputAsIntVector("padding_high",
43                                                               &padding_high));
44     OP_REQUIRES_OK(context, context->ConstantInputAsIntVector(
45                                 "padding_interior", &padding_interior));
46 
47     OP_REQUIRES(context, TensorShapeUtils::IsScalar(padding_value_shape),
48                 errors::InvalidArgument("padding_value must be a scalar"));
49     const int rank = input_shape.dims();
50     OP_REQUIRES(context, rank == padding_low.size(),
51                 errors::InvalidArgument(
52                     "The size of padding_low must be equal to the input "
53                     "rank (",
54                     padding_low.size(), " vs. ", rank, ")"));
55     OP_REQUIRES(context, rank == padding_high.size(),
56                 errors::InvalidArgument(
57                     "The size of padding_high must be equal to the input "
58                     "rank (",
59                     padding_high.size(), " vs. ", rank, ")"));
60     OP_REQUIRES(context, rank == padding_interior.size(),
61                 errors::InvalidArgument(
62                     "The size of padding_interior must be equal to the input "
63                     "rank (",
64                     padding_interior.size(), " vs. ", rank, ")"));
65 
66     auto non_negative = [](int64_t x) { return x >= 0; };
67     OP_REQUIRES(
68         context, absl::c_all_of(padding_interior, non_negative),
69         errors::InvalidArgument("padding_interior must be non-negative, got [",
70                                 absl::StrJoin(padding_interior, ","), "]"));
71 
72     xla::PaddingConfig padding_config;
73     for (int i = 0; i < rank; ++i) {
74       auto* dim = padding_config.add_dimensions();
75       dim->set_edge_padding_low(padding_low[i]);
76       dim->set_edge_padding_high(padding_high[i]);
77       dim->set_interior_padding(padding_interior[i]);
78     }
79 
80     xla::XlaOp output =
81         xla::Pad(context->Input("input"), context->Input("padding_value"),
82                  padding_config);
83     context->SetOutput(0, output);
84   }
85 
86  private:
87   TF_DISALLOW_COPY_AND_ASSIGN(XlaPadOp);
88 };
89 
90 REGISTER_XLA_OP(Name("XlaPad")
91                     .CompileTimeConstantInput("padding_low")
92                     .CompileTimeConstantInput("padding_high")
93                     .CompileTimeConstantInput("padding_interior"),
94                 XlaPadOp);
95 
96 }  // namespace
97 }  // namespace tensorflow
98