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 std::vector<int64> dynamic_multiples; 82 ctx->set_dynamic_dimension_is_minus_one(true); 83 // The multiplier can be a dynamic value. 84 OP_REQUIRES_OK( 85 ctx, ctx->ConstantInputAsIntVector("multiples", &dynamic_multiples)); 86 87 auto result_or = BroadcastTo(ctx->Input("input"), output_dims); 88 89 OP_REQUIRES_OK(ctx, result_or.status()); 90 auto result = result_or.ValueOrDie(); 91 for (int64 i = 0; i < dynamic_multiples.size(); ++i) { 92 // If a dimension is dynamic, call set-dimension-size on the output. 93 if (dynamic_multiples[i] == -1) { 94 auto dynamic_dim_size = 95 xla::Slice(ctx->Input("multiples"), {i}, {i + 1}, {1}); 96 dynamic_dim_size = xla::Reshape(dynamic_dim_size, {}); 97 dynamic_dim_size = xla::ConvertElementType(dynamic_dim_size, xla::S32); 98 result = xla::SetDimensionSize(result, dynamic_dim_size, i); 99 } 100 } 101 102 ctx->SetOutput(0, result); 103 } 104 105 private: 106 TF_DISALLOW_COPY_AND_ASSIGN(TileOp); 107 }; 108 109 REGISTER_XLA_OP(Name("Tile").CompileTimeConstantInput("multiples"), TileOp); 110 111 } // namespace 112 } // namespace tensorflow 113