• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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/lite/delegates/gpu/gl/kernels/pad.h"
17 
18 #include <algorithm>
19 #include <any>
20 #include <cstdint>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include "absl/memory/memory.h"
28 #include "tensorflow/lite/delegates/gpu/common/operations.h"
29 #include "tensorflow/lite/delegates/gpu/common/status.h"
30 #include "tensorflow/lite/delegates/gpu/common/types.h"
31 #include "tensorflow/lite/delegates/gpu/gl/variable.h"
32 
33 namespace tflite {
34 namespace gpu {
35 namespace gl {
36 namespace {
37 
38 class Pad : public NodeShader {
39  public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const40   absl::Status GenerateCode(const GenerationContext& ctx,
41                             GeneratedCode* generated_code) const final {
42     const auto& attr = std::any_cast<const PadAttributes&>(ctx.op_attr);
43 
44     if (attr.type != PaddingContentType::ZEROS &&
45         attr.type != PaddingContentType::REFLECT) {
46       return absl::UnimplementedError(
47           "Only ZERO and REFLECT padding types are supported.");
48     }
49     if (attr.appended.h < 0 || attr.appended.w < 0 || attr.appended.c < 0 ||
50         attr.prepended.h < 0 || attr.prepended.w < 0 || attr.prepended.c < 0) {
51       return absl::UnimplementedError("Negative padding is not supported.");
52     }
53     if (attr.appended.b != 0 || attr.prepended.b != 0) {
54       return absl::UnimplementedError("Padding for BATCH is not supported.");
55     }
56     std::vector<Variable> parameters = {
57         {"input_data_0_h", static_cast<int>(ctx.input_shapes[0][1])},
58         {"input_data_0_w", static_cast<int>(ctx.input_shapes[0][2])},
59         {"input_data_0_c", static_cast<int>(ctx.input_shapes[0][3])},
60         {"prepended",
61          int4(attr.prepended.w, attr.prepended.h, attr.prepended.c, 0)},
62     };
63     std::string source;
64     if (attr.type == PaddingContentType::REFLECT) {
65       source = R"(
66   int src_x = gid.x - $prepended.x$;
67   src_x = abs(src_x);
68   src_x = $input_data_0_w$ - 1 - abs(src_x - $input_data_0_w$ + 1);
69 
70   int src_y = gid.y - $prepended.y$;
71   src_y = abs(src_y);
72   src_y = $input_data_0_h$ - 1 - abs(src_y - $input_data_0_h$ + 1);
73 )";
74       if (attr.prepended.c == 0 && attr.appended.c == 0) {
75         // optimized case
76         source += "  value_0 = $input_data_0[src_x, src_y, gid.z]$;\n";
77       } else {
78         source += R"(
79   int start_channel = gid.z * 4;
80   for (int i = 0; i < 4; ++i) {
81     int channel = start_channel + i;
82     int src_z = channel - $prepended.z$;
83     src_z = abs(src_z);
84     src_z = $input_data_0_c$ - 1 - abs(src_z - $input_data_0_c$ + 1);
85     // We need additional clamp for z, so that we use alignment for channels
86     // and can proceed extra channels that can lead to reading out of
87     // resource.
88     src_z = clamp(src_z, 0, $input_data_0_c$ - 1);
89     value_0[i] = $input_data_0[src_x, src_y, src_z / 4]$[src_z % 4];
90   }
91 )";
92       }
93     } else {
94       source = R"(
95   int src_x = gid.x - $prepended.x$;
96   int src_y = gid.y - $prepended.y$;
97   if (src_x >= 0 && src_x < $input_data_0_w$ && src_y >= 0 && src_y < $input_data_0_h$) {
98 )";
99       if (attr.prepended.c == 0 && attr.appended.c == 0) {
100         // optimized case
101         source += "    value_0 = $input_data_0[src_x, src_y, gid.z]$;\n";
102       } else if (attr.prepended.c % 4 == 0) {
103         parameters.push_back(
104             {"src_slices",
105              DivideRoundUp(static_cast<int>(ctx.input_shapes[0][3]), 4)});
106         source += R"(
107     int src_z = gid.z - $prepended.z$ / 4;
108     if (src_z >= 0 && src_z < $src_slices$) {
109       value_0 = $input_data_0[src_x, src_y, src_z]$;
110     }
111 )";
112       } else {
113         source += R"(
114     int start_channel = gid.z * 4;
115     for (int i = 0; i < 4; ++i) {
116       int channel = start_channel + i;
117       int src_z = channel - $prepended.z$;
118       if (src_z >= 0 && src_z < $input_data_0_c$) {
119         value_0[i] = $input_data_0[src_x, src_y, src_z / 4]$[src_z % 4];
120       }
121     }
122 )";
123       }
124       source += "  }\n";
125     }
126     *generated_code = {
127         /*parameters=*/std::move(parameters),
128         /*objects=*/{},
129         /*shared_variables=*/{},
130         /*workload=*/uint3(),
131         /*workgroup=*/uint3(),
132         /*source_code=*/std::move(source),
133         /*input=*/IOStructure::ONLY_DEFINITIONS,
134         /*output=*/IOStructure::AUTO,
135     };
136     return absl::OkStatus();
137   }
138 };
139 
140 }  // namespace
141 
NewPadNodeShader()142 std::unique_ptr<NodeShader> NewPadNodeShader() {
143   return std::make_unique<Pad>();
144 }
145 
146 }  // namespace gl
147 }  // namespace gpu
148 }  // namespace tflite
149