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