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 #include "tensorflow/compiler/tf2xla/type_util.h" 17 #include "tensorflow/compiler/tf2xla/xla_helpers.h" 18 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" 19 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 20 #include "tensorflow/compiler/xla/client/value_inference.h" 21 #include "tensorflow/compiler/xla/client/xla_builder.h" 22 #include "tensorflow/compiler/xla/xla_data.pb.h" 23 #include "tensorflow/core/framework/kernel_def_builder.h" 24 #include "tensorflow/core/framework/register_types.h" 25 #include "tensorflow/core/framework/tensor_shape.h" 26 27 namespace tensorflow { 28 namespace { 29 30 class PadOp : public XlaOpKernel { 31 public: PadOp(OpKernelConstruction * ctx)32 explicit PadOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {} 33 Compile(XlaOpKernelContext * ctx)34 void Compile(XlaOpKernelContext* ctx) override { 35 const TensorShape input_shape = ctx->InputShape("input"); 36 const TensorShape pad_shape = ctx->InputShape("paddings"); 37 const int dims = input_shape.dims(); 38 OP_REQUIRES( 39 ctx, 40 TensorShapeUtils::IsMatrix(pad_shape) && pad_shape.dim_size(1) == 2, 41 errors::InvalidArgument("paddings must be a matrix with 2 columns: ", 42 pad_shape.DebugString())); 43 OP_REQUIRES( 44 ctx, dims == pad_shape.dim_size(0), 45 errors::InvalidArgument( 46 "The first dimension of paddings must be the rank of inputs", 47 pad_shape.DebugString(), " ", input_shape.DebugString())); 48 49 xla::XlaOp input = ctx->Input("input"); 50 if (dims == 0) { 51 // Tensor is rank 0. Return it unchanged. 52 ctx->SetOutput(0, input); 53 return; 54 } 55 56 xla::Literal pad_literal; 57 OP_REQUIRES_OK(ctx, ctx->ConstantInputAsInt64Literal( 58 "paddings", &pad_literal, 59 xla::ValueInferenceMode::kUpperBound)); 60 61 xla::Literal padding_dynamism_literal; 62 OP_REQUIRES_OK( 63 ctx, ctx->ResolveInputDynamism("paddings", &padding_dynamism_literal)); 64 65 xla::PaddingConfig config; 66 for (int i = 0; i < dims; ++i) { 67 auto* dim = config.add_dimensions(); 68 int before = pad_literal.Get<int64>({i, 0}); 69 int after = pad_literal.Get<int64>({i, 1}); 70 OP_REQUIRES(ctx, before >= 0 && after >= 0, 71 errors::InvalidArgument( 72 "Paddings must be non-negative: ", before, " ", after)); 73 dim->set_edge_padding_low(before); 74 dim->set_edge_padding_high(after); 75 } 76 77 // PadV2 added a "constant_values" input that indicates the pad value. 78 xla::XlaOp constant_values; 79 xla::XlaOp pad; 80 if (ctx->num_inputs() == 3) { 81 OP_REQUIRES( 82 ctx, TensorShapeUtils::IsScalar(ctx->InputShape("constant_values")), 83 errors::InvalidArgument("constant_values must be a scalar.")); 84 pad = xla::Pad(input, ctx->Input("constant_values"), config); 85 } else { 86 auto zero = XlaHelpers::Zero(ctx->builder(), input_type(0)); 87 pad = xla::Pad(input, zero, config); 88 } 89 90 for (int i = 0; i < dims; ++i) { 91 bool low_pad_is_dynamic = padding_dynamism_literal.Get<bool>({i, 0}); 92 93 OP_REQUIRES( 94 ctx, !low_pad_is_dynamic, 95 errors::InvalidArgument("low_pad in Pad op has to be static.")); 96 bool high_pad_is_dynamic = padding_dynamism_literal.Get<bool>({i, 1}); 97 if (high_pad_is_dynamic) { 98 // When we have 99 // pad_width = MAX_WIDTH - size(t) 100 // op = pad(t, /*high_pad=*/pad_width) 101 // The bound of the result size should be MAX_WIDTH, instead of 102 // `bound(t) + bound(pad_width)` 103 // 104 // We do this by analyzing the expression 105 // size(op) = size(t) + MAX_WIDTH - size(t) 106 // and leave value inference to analyze it. 107 xla::XlaOp high_pad_size = 108 xla::Slice(ctx->Input("paddings"), {i, 1}, {i + 1, 2}, {1, 1}); 109 high_pad_size = xla::Reshape(high_pad_size, {}); 110 high_pad_size = xla::ConvertElementType(high_pad_size, xla::S32); 111 // Low pad has to be static. 112 xla::XlaOp low_pad_size = xla::ConstantR0<int32>( 113 ctx->builder(), pad_literal.Get<int64>({i, 0})); 114 xla::XlaOp input_size = xla::GetDimensionSize(input, i); 115 xla::XlaOp total_size = low_pad_size + input_size + high_pad_size; 116 auto size_upper_bound_status_or = 117 ctx->value_inference().AnalyzeConstant( 118 total_size, xla::ValueInferenceMode::kUpperBound); 119 OP_REQUIRES_OK(ctx, size_upper_bound_status_or.status()); 120 auto size_upper_bound = 121 size_upper_bound_status_or.ValueOrDie().Get<int32>({}); 122 OP_REQUIRES( 123 ctx, size_upper_bound.has_value(), 124 errors::InvalidArgument( 125 "Failed to infer upperbound of total size after padding.")); 126 // If we know a tighter upperbound, trim the output with the new 127 // upperbound. 128 pad = xla::SliceInDim(pad, 0, size_upper_bound.value(), 1, i); 129 pad = xla::SetDimensionSize(pad, total_size, i); 130 } 131 } 132 ctx->SetOutput(0, pad); 133 } 134 }; 135 136 REGISTER_XLA_OP(Name("Pad").CompileTimeConstantInput("paddings"), PadOp); 137 REGISTER_XLA_OP(Name("PadV2").CompileTimeConstantInput("paddings"), PadOp); 138 139 } // namespace 140 } // namespace tensorflow 141