xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2xla/kernels/tile_ops.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
20 #include "absl/algorithm/container.h"
21 #include "absl/types/span.h"
22 #include "tensorflow/compiler/tf2xla/lib/broadcast.h"
23 #include "tensorflow/compiler/tf2xla/type_util.h"
24 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
25 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
26 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
27 #include "tensorflow/compiler/xla/client/value_inference.h"
28 #include "tensorflow/compiler/xla/client/xla_builder.h"
29 #include "tensorflow/core/framework/numeric_op.h"
30 #include "tensorflow/core/framework/op_kernel.h"
31 #include "tensorflow/core/framework/tensor.h"
32 #include "tensorflow/core/framework/tensor_shape.h"
33 #include "tensorflow/core/framework/type_index.h"
34 #include "tensorflow/core/lib/core/errors.h"
35 #include "tensorflow/core/platform/macros.h"
36 
37 namespace tensorflow {
38 namespace {
39 
40 // --------------------------------------------------------------------------
41 class TileOp : public XlaOpKernel {
42  public:
TileOp(OpKernelConstruction * ctx)43   explicit TileOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
44 
Compile(XlaOpKernelContext * ctx)45   void Compile(XlaOpKernelContext* ctx) override {
46     const TensorShape input_shape = ctx->InputShape("input");
47     const TensorShape multiples_shape = ctx->InputShape("multiples");
48 
49     OP_REQUIRES(
50         ctx, TensorShapeUtils::IsVector(multiples_shape),
51         errors::InvalidArgument("Expected multiples to be 1-D, but got shape ",
52                                 multiples_shape.DebugString()));
53     OP_REQUIRES(ctx, input_shape.dims() == multiples_shape.num_elements(),
54                 errors::InvalidArgument(
55                     "Expected multiples argument to be a vector of length ",
56                     input_shape.dims(), " but got length ",
57                     multiples_shape.dim_size(0)));
58     const int input_dims = input_shape.dims();
59     auto input = ctx->Input(0);
60     // If input is a scalar then multiples has 0 elements and this is
61     // a NoOp.
62     if (input_dims == 0) {
63       ctx->SetOutput(0, input);
64       return;
65     }
66 
67     std::vector<int64_t> multiples_bounds;
68     OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(
69                             "multiples", &multiples_bounds,
70                             xla::ValueInferenceMode::kUpperBound));
71 
72     std::vector<int64_t> output_dims(input_shape.dims());
73     for (int64_t i = 0; i < input_shape.dims(); ++i) {
74       OP_REQUIRES(ctx, multiples_bounds[i] >= 0,
75                   errors::InvalidArgument("Expected multiples[", i,
76                                           "] >= 0, but got ", output_dims[i]));
77       output_dims[i] = input_shape.dim_size(i) * multiples_bounds[i];
78     }
79 
80     std::vector<bool> multiples_are_dynamic;
81 
82     OP_REQUIRES_OK(ctx, ctx->ResolveInputDynamismIntoPredVector(
83                             1, &multiples_are_dynamic));
84 
85     bool all_multiples_are_static = absl::c_all_of(
86         multiples_are_dynamic, [](bool dynamic) { return !dynamic; });
87     // If a value is static, it means the upper bound is the value itself:
88     // constant_value = constant_upper_boudn = counstant_lower_bound
89     if (all_multiples_are_static) {
90       // If all multiples are 1, than the input is the same as the output.
91       if (absl::c_all_of(multiples_bounds,
92                          [](int64_t multiple) { return multiple == 1; })) {
93         ctx->SetOutput(0, input);
94         return;
95       }
96     }
97 
98     auto result_or = BroadcastTo(ctx->Input("input"), output_dims);
99 
100     OP_REQUIRES_OK(ctx, result_or.status());
101     auto result = result_or.ValueOrDie();
102     if (!all_multiples_are_static) {
103       // Some values of multiples are unknown at compile time, this is a dynamic
104       // tile op. We need to call set dimension size.
105       for (int64_t i = 0; i < multiples_are_dynamic.size(); ++i) {
106         if (!multiples_are_dynamic[i]) {
107           continue;
108         }
109         // If a dimension is dynamic, call set-dimension-size on the output.
110         auto dynamic_dim_size =
111             xla::Slice(ctx->Input("multiples"), {i}, {i + 1}, {1});
112         dynamic_dim_size = xla::Reshape(dynamic_dim_size, {});
113         dynamic_dim_size = xla::ConvertElementType(dynamic_dim_size, xla::S32);
114         result = xla::SetDimensionSize(result, dynamic_dim_size, i);
115       }
116     }
117 
118     ctx->SetOutput(0, result);
119   }
120 
121  private:
122   TF_DISALLOW_COPY_AND_ASSIGN(TileOp);
123 };
124 
125 REGISTER_XLA_OP(Name("Tile").CompileTimeConstantInput("multiples"), TileOp);
126 
127 }  // namespace
128 }  // namespace tensorflow
129