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