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/transpose_conv.h"
17
18 #include <any>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23
24 #include "absl/memory/memory.h"
25 #include "tensorflow/lite/delegates/gpu/common/convert.h"
26 #include "tensorflow/lite/delegates/gpu/common/operations.h"
27 #include "tensorflow/lite/delegates/gpu/common/shape.h"
28 #include "tensorflow/lite/delegates/gpu/common/status.h"
29 #include "tensorflow/lite/delegates/gpu/common/types.h"
30 #include "tensorflow/lite/delegates/gpu/common/util.h"
31 #include "tensorflow/lite/delegates/gpu/gl/node_shader.h"
32 #include "tensorflow/lite/delegates/gpu/gl/variable.h"
33
34 namespace tflite {
35 namespace gpu {
36 namespace gl {
37 namespace {
38
39 class ConvolutionTransposedBuffers : public NodeShader {
40 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const41 absl::Status GenerateCode(const GenerationContext& ctx,
42 GeneratedCode* generated_code) const final {
43 if (ctx.input_shapes.size() != 1) {
44 return absl::UnimplementedError(
45 "Convolution Transposed does not support more than 1 runtime tensor");
46 }
47 const auto& attr =
48 std::any_cast<const ConvolutionTransposedAttributes&>(ctx.op_attr);
49 auto weights = attr.weights.shape;
50
51 std::vector<Variable> parameters = {
52 {"input_data_0_h", static_cast<int>(ctx.input_shapes[0][1])},
53 {"input_data_0_w", static_cast<int>(ctx.input_shapes[0][2])},
54 {"src_depth", DivideRoundUp(weights.i, 4)},
55 {"kernel_size", int2(weights.w, weights.h)},
56 {"stride", int2(attr.stride.w, attr.stride.h)},
57 {"padding", int2(weights.w - 1 - attr.padding.prepended.w,
58 weights.h - 1 - attr.padding.prepended.h)},
59 };
60
61 std::vector<std::pair<std::string, Object>> objects = {
62 {"weights",
63 MakeReadonlyObject(Get3DSizeForPHWO4I4(attr.weights.shape),
64 ConvertToPHWO4I4Transposed(attr.weights))}};
65
66 std::string source = R"(
67 #define IN_BOUNDS(p, p0, p1) (all(greaterThanEqual(p, p0)) && all(lessThan(p, p1)))
68
69 ivec2 p0 = ($padding$ + $stride$ - gid.xy % $stride$) % $stride$;
70 for (int y = p0.y; y < $kernel_size.y$; y += $stride.y$) {
71 for (int x = p0.x; x < $kernel_size.x$; x += $stride.x$) {
72
73 int i = int(float(y * $kernel_size.x$) + float(x));
74 ivec2 idx = ivec2(vec2(gid.xy + ivec2(x, y)) - vec2($padding$));
75
76 if (IN_BOUNDS(idx, ivec2(0), ivec2($input_data_0_w$, $input_data_0_h$) * $stride$)) {
77 ivec2 coord = idx / $stride$;
78 for (int l = 0; l < $src_depth$; ++l) {
79 vec4 src_color = $input_data_0[coord.x, coord.y, l]$;
80 value_0.x += dot(src_color, $weights[l * 4 + 0, i, gid.z]$);
81 value_0.y += dot(src_color, $weights[l * 4 + 1, i, gid.z]$);
82 value_0.z += dot(src_color, $weights[l * 4 + 2, i, gid.z]$);
83 value_0.w += dot(src_color, $weights[l * 4 + 3, i, gid.z]$);
84 }
85 }
86 }
87 }
88 )";
89 if (!attr.bias.data.empty()) {
90 source += "value_0 += $bias[gid.z]$;\n";
91 objects.push_back({"bias", MakeReadonlyObject(attr.bias.data)});
92 }
93 *generated_code = {
94 /*parameters=*/std::move(parameters),
95 /*objects=*/std::move(objects),
96 /*shared_variables=*/{},
97 /*workload=*/uint3(),
98 /*workgroup=*/uint3(),
99 /*source_code=*/source,
100 /*input=*/IOStructure::ONLY_DEFINITIONS,
101 /*output=*/IOStructure::AUTO,
102 };
103 return absl::OkStatus();
104 }
105 };
106
107 } // namespace
108
NewConvolutionTransposedNodeShader()109 std::unique_ptr<NodeShader> NewConvolutionTransposedNodeShader() {
110 return std::make_unique<ConvolutionTransposedBuffers>();
111 }
112
113 } // namespace gl
114 } // namespace gpu
115 } // namespace tflite
116