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