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 Slice Op. 17 18 #include "tensorflow/compiler/tf2xla/type_util.h" 19 #include "tensorflow/compiler/tf2xla/xla_helpers.h" 20 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" 21 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/register_types.h" 24 #include "tensorflow/core/framework/tensor.h" 25 #include "tensorflow/core/kernels/ops_util.h" 26 #include "tensorflow/core/lib/core/status.h" 27 #include "tensorflow/core/lib/gtl/array_slice.h" 28 #include "tensorflow/core/platform/mem.h" 29 30 namespace tensorflow { 31 namespace { 32 33 class SliceOp : public XlaOpKernel { 34 public: SliceOp(OpKernelConstruction * ctx)35 explicit SliceOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 36 Compile(XlaOpKernelContext * ctx)37 void Compile(XlaOpKernelContext* ctx) override { 38 const TensorShape input_shape = ctx->InputShape(0); 39 const TensorShape begin_tensor_shape = ctx->InputShape(1); 40 const TensorShape size_tensor_shape = ctx->InputShape(2); 41 42 OP_REQUIRES( 43 ctx, 44 IsLegacyVector(begin_tensor_shape) && 45 IsLegacyVector(size_tensor_shape) && 46 begin_tensor_shape.num_elements() == input_shape.dims() && 47 size_tensor_shape.num_elements() == input_shape.dims(), 48 errors::InvalidArgument( 49 "Expected begin and size arguments to be 1-D tensors of size ", 50 input_shape.dims(), ", but got shapes ", 51 begin_tensor_shape.DebugString(), " and ", 52 size_tensor_shape.DebugString(), " instead.")); 53 54 const int input_dims = input_shape.dims(); 55 56 std::vector<int64> begin; 57 std::vector<int64> size; 58 OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(2, &size)); 59 if (ctx->ConstantInputAsIntVector(1, &begin).ok()) { 60 // `begin` is a compile-time constant. 61 for (int i = 0; i < input_dims; ++i) { 62 if (size[i] == -1) { 63 // A size[i] of -1 means "all elements from begin[i] to dim_size(i)". 64 size[i] = input_shape.dim_size(i) - begin[i]; 65 } 66 } 67 68 for (int i = 0; i < input_dims; ++i) { 69 int64 b = begin[i]; 70 int64 s = size[i]; 71 if (input_shape.dim_size(i) == 0) { 72 OP_REQUIRES(ctx, b == 0 && s == 0, 73 errors::InvalidArgument( 74 "Expected begin[", i, "] == 0 (got ", b, 75 ") and size[", i, "] == 0 ", "(got ", s, ") when ", 76 "input_shape.dim_size(", i, ") == 0")); 77 } else { 78 OP_REQUIRES(ctx, 0 <= b && b <= input_shape.dim_size(i), 79 errors::InvalidArgument("Expected begin[", i, "] in [0, ", 80 input_shape.dim_size(i), 81 "], but got ", b)); 82 OP_REQUIRES(ctx, 0 <= s && b + s <= input_shape.dim_size(i), 83 errors::InvalidArgument("Expected size[", i, "] in [0, ", 84 input_shape.dim_size(i) - b, 85 "], but ", "got ", s)); 86 } 87 } 88 89 std::vector<int64> limits; 90 limits.reserve(begin.size()); 91 for (int i = 0; i < begin.size(); ++i) { 92 limits.push_back(begin[i] + size[i]); 93 } 94 std::vector<int64> strides(begin.size(), 1); 95 ctx->SetOutput( 96 0, ctx->builder()->Slice(ctx->Input(0), begin, limits, strides)); 97 } else { 98 // `begin` is not a compile-time constant. 99 for (int i = 0; i < input_dims; ++i) { 100 OP_REQUIRES(ctx, 0 <= size[i], 101 errors::InvalidArgument( 102 "XLA compilation of Slice operator with negative sizes " 103 "requires that 'begin' is a compile-time constant.")); 104 OP_REQUIRES(ctx, size[i] <= input_shape.dim_size(i), 105 errors::InvalidArgument("Expected size[", i, "] in [0, ", 106 input_shape.dim_size(i), "], but ", 107 "got ", size[i])); 108 } 109 ctx->SetOutput( 110 0, ctx->builder()->DynamicSlice(ctx->Input(0), ctx->Input(1), size)); 111 } 112 } 113 }; 114 115 REGISTER_XLA_OP( 116 Name("Slice").CompileTimeConstInput("begin").CompileTimeConstInput("size"), 117 SliceOp); 118 119 } // namespace 120 } // namespace tensorflow 121