1 /* Copyright 2020 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/space_to_depth.h"
17
18 #include <string>
19 #include <utility>
20
21 #include "absl/memory/memory.h"
22 #include "absl/types/any.h"
23 #include "tensorflow/lite/delegates/gpu/common/operations.h"
24 #include "tensorflow/lite/delegates/gpu/common/status.h"
25 #include "tensorflow/lite/delegates/gpu/gl/node_shader.h"
26
27 namespace tflite {
28 namespace gpu {
29 namespace gl {
30 namespace {
31
32 class SpaceToDepth : public NodeShader {
33 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const34 absl::Status GenerateCode(const GenerationContext& ctx,
35 GeneratedCode* generated_code) const final {
36 const auto& attr =
37 absl::any_cast<const SpaceToDepthAttributes&>(ctx.op_attr);
38 std::string code = R"(
39 for (int i = 0; i < 4; ++i) {
40 int dst_c = 4 * gid.z + i;
41 int block_id = dst_c / $input_data_0_c$;
42 int src_x = gid.x * $block_size$ + block_id % $block_size$;
43 int src_y = gid.y * $block_size$ + block_id / $block_size$;
44 int src_c = dst_c % $input_data_0_c$;
45 value_0[i] = $input_data_0[src_x, src_y, src_c / 4]$[src_c % 4];
46 }
47 )";
48
49 *generated_code = {
50 /*parameters=*/{
51 {"block_size", attr.block_size},
52 {"input_data_0_c", static_cast<int>(ctx.input_shapes[0][3])},
53 },
54 /*objects=*/{},
55 /*shared_variables=*/{},
56 /*workload=*/uint3(),
57 /*workgroup=*/uint3(),
58 /*source_code=*/std::move(code),
59 /*input=*/IOStructure::ONLY_DEFINITIONS,
60 /*output=*/IOStructure::AUTO,
61 };
62 return absl::OkStatus();
63 }
64 };
65
66 class DepthToSpace : public NodeShader {
67 public:
GenerateCode(const GenerationContext & ctx,GeneratedCode * generated_code) const68 absl::Status GenerateCode(const GenerationContext& ctx,
69 GeneratedCode* generated_code) const final {
70 const auto& attr =
71 absl::any_cast<const SpaceToDepthAttributes&>(ctx.op_attr);
72 std::string code = R"(
73 for (int i = 0; i < 4; ++i) {
74 int dst_c = 4 * gid.z + i;
75 int block_x = gid.x % $block_size$;
76 int src_x = gid.x / $block_size$;
77 int block_y = gid.y % $block_size$;
78 int src_y = gid.y / $block_size$;
79 int block_id = block_y * $block_size$ + block_x;
80 int src_c = block_id * $output_channels$ + dst_c;
81 value_0[i] = $input_data_0[src_x, src_y, src_c / 4]$[src_c % 4];
82 }
83 )";
84
85 *generated_code = {
86 /*parameters=*/{
87 {"block_size", attr.block_size},
88 {"output_channels", static_cast<int>(ctx.output_shapes[0][3])},
89 },
90 /*objects=*/{},
91 /*shared_variables=*/{},
92 /*workload=*/uint3(),
93 /*workgroup=*/uint3(),
94 /*source_code=*/std::move(code),
95 /*input=*/IOStructure::ONLY_DEFINITIONS,
96 /*output=*/IOStructure::AUTO,
97 };
98 return absl::OkStatus();
99 }
100 };
101 } // namespace
102
NewSpaceToDepthNodeShader()103 std::unique_ptr<NodeShader> NewSpaceToDepthNodeShader() {
104 return absl::make_unique<SpaceToDepth>();
105 }
106
NewDepthToSpaceNodeShader()107 std::unique_ptr<NodeShader> NewDepthToSpaceNodeShader() {
108 return absl::make_unique<DepthToSpace>();
109 }
110
111 } // namespace gl
112 } // namespace gpu
113 } // namespace tflite
114