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 Tile Op.
17 
18 #include <vector>
19 #include "absl/algorithm/container.h"
20 #include "absl/types/span.h"
21 #include "tensorflow/compiler/tf2xla/lib/broadcast.h"
22 #include "tensorflow/compiler/tf2xla/type_util.h"
23 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
24 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
25 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
26 #include "tensorflow/compiler/xla/client/xla_builder.h"
27 #include "tensorflow/core/framework/numeric_op.h"
28 #include "tensorflow/core/framework/op_kernel.h"
29 #include "tensorflow/core/framework/tensor.h"
30 #include "tensorflow/core/framework/tensor_shape.h"
31 #include "tensorflow/core/framework/type_index.h"
32 #include "tensorflow/core/lib/core/errors.h"
33 #include "tensorflow/core/platform/macros.h"
34 
35 namespace tensorflow {
36 namespace {
37 
38 // --------------------------------------------------------------------------
39 class TileOp : public XlaOpKernel {
40  public:
TileOp(OpKernelConstruction * ctx)41   explicit TileOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
42 
Compile(XlaOpKernelContext * ctx)43   void Compile(XlaOpKernelContext* ctx) override {
44     const TensorShape input_shape = ctx->InputShape("input");
45     const TensorShape multiples_shape = ctx->InputShape("multiples");
46 
47     OP_REQUIRES(
48         ctx, TensorShapeUtils::IsVector(multiples_shape),
49         errors::InvalidArgument("Expected multiples to be 1-D, but got shape ",
50                                 multiples_shape.DebugString()));
51     OP_REQUIRES(ctx, input_shape.dims() == multiples_shape.num_elements(),
52                 errors::InvalidArgument(
53                     "Expected multiples argument to be a vector of length ",
54                     input_shape.dims(), " but got length ",
55                     multiples_shape.dim_size(0)));
56     const int input_dims = input_shape.dims();
57     auto input = ctx->Input(0);
58     // If input is a scalar then multiples has 0 elements and this is
59     // a NoOp.
60     if (input_dims == 0) {
61       ctx->SetOutput(0, input);
62       return;
63     }
64 
65     std::vector<int64> multiples;
66     OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector("multiples", &multiples));
67     std::vector<int64> output_dims(input_shape.dims());
68     for (int64 i = 0; i < input_shape.dims(); ++i) {
69       OP_REQUIRES(ctx, multiples[i] >= 0,
70                   errors::InvalidArgument("Expected multiples[", i,
71                                           "] >= 0, but got ", output_dims[i]));
72       output_dims[i] = input_shape.dim_size(i) * multiples[i];
73     }
74 
75     // If all multiples are 1, than the input is the same as the output.
76     if (absl::c_all_of(multiples,
77                        [](int64 multiple) { return multiple == 1; })) {
78       ctx->SetOutput(0, input);
79       return;
80     }
81 
82     bool can_tile_with_implicit_broadcast = true;
83     for (int i = 0; i < input_dims; ++i) {
84       int64 multiple = multiples[i];
85       // If the multiple and input dimension are not 1, then tile cannot be
86       // implemented with a single hlo broadcast.
87       if (multiple != 1 && input_shape.dim_size(i) != 1) {
88         can_tile_with_implicit_broadcast = false;
89       }
90     }
91 
92     if (can_tile_with_implicit_broadcast) {
93       // Create a constant Zero the size of the output shape to leverage binary
94       // operation broadcast semantics.
95       auto broadcasted_zero = xla::Broadcast(
96           XlaHelpers::Zero(ctx->builder(), ctx->input_type(0)), output_dims);
97       if (ctx->input_type(0) == DT_BOOL) {
98         ctx->SetOutput(0, xla::Or(broadcasted_zero, input));
99       } else {
100         ctx->SetOutput(0, xla::Add(broadcasted_zero, input));
101       }
102       return;
103     }
104 
105     auto result = BroadcastTo(ctx->Input("input"), output_dims);
106     OP_REQUIRES_OK(ctx, result.status());
107     ctx->SetOutput(0, result.ValueOrDie());
108   }
109 
110  private:
111   TF_DISALLOW_COPY_AND_ASSIGN(TileOp);
112 };
113 
114 REGISTER_XLA_OP(Name("Tile").CompileTimeConstantInput("multiples"), TileOp);
115 
116 }  // namespace
117 }  // namespace tensorflow
118