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/common/tasks/space_to_depth.h"
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 #include "tensorflow/lite/delegates/gpu/common/task/work_group_picking.h"
23
24 namespace tflite {
25 namespace gpu {
26
27 namespace {
GetSpaceToDepthCode(const OperationDef & op_def)28 std::string GetSpaceToDepthCode(const OperationDef& op_def) {
29 std::string c;
30 c += "MAIN_FUNCTION($0) {\n";
31 if (op_def.IsBatchSupported()) {
32 c += " int linear_id = GLOBAL_ID_0;\n";
33 c += " int X = linear_id / args.dst_tensor.Batch();\n";
34 c += " int B = linear_id % args.dst_tensor.Batch();\n";
35 c += " args.dst_tensor.SetBatchRef(B);\n";
36 c += " args.src_tensor.SetBatchRef(B);\n";
37 } else {
38 c += " int X = GLOBAL_ID_0;\n";
39 }
40 c += " int Y = GLOBAL_ID_1;\n";
41 c += " int S = GLOBAL_ID_2;\n";
42 c += " if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
43 "S >= args.dst_tensor.Slices()) { \n";
44 c += " return; \n";
45 c += " } \n";
46 c += " FLT tmp[4];\n";
47 c += " tmp[0] = INIT_FLT(0.0f);\n";
48 c += " tmp[1] = INIT_FLT(0.0f);\n";
49 c += " tmp[2] = INIT_FLT(0.0f);\n";
50 c += " tmp[3] = INIT_FLT(0.0f);\n";
51 c += " for (int i = 0; i < 4; ++i) {\n";
52 c += " int dst_c = 4 * S + i;\n";
53 c += " int block_id = dst_c / args.src_tensor.Channels();\n";
54 c += " int src_x = X * args.block_size + block_id % args.block_size;\n";
55 c += " int src_y = Y * args.block_size + block_id / args.block_size;\n";
56 c += " int src_c = dst_c % args.src_tensor.Channels();\n";
57 c += " int src_z = src_c / 4;\n";
58 c += " FLT4 t = args.src_tensor.Read(src_x, src_y, src_z);\n";
59 c += " FLT t_ar[4] = {t.x, t.y, t.z, t.w};\n";
60 c += " tmp[i] = t_ar[src_c % 4];\n";
61 c += " }\n";
62 c += " FLT4 result;\n";
63 c += " result.x = tmp[0];\n";
64 c += " result.y = tmp[1];\n";
65 c += " result.z = tmp[2];\n";
66 c += " result.w = tmp[3];\n";
67 c += " args.dst_tensor.Write(result, X, Y, S);\n";
68 c += "}\n";
69 return c;
70 }
71 } // namespace
72
CreateSpaceToDepth(const OperationDef & op_def,const SpaceToDepthAttributes & attr)73 GPUOperation CreateSpaceToDepth(const OperationDef& op_def,
74 const SpaceToDepthAttributes& attr) {
75 GPUOperation op(op_def);
76 op.AddSrcTensor("src_tensor", op_def.src_tensors[0]);
77 op.AddDstTensor("dst_tensor", op_def.dst_tensors[0]);
78 op.args_.AddInt("block_size", attr.block_size);
79 op.code_ = GetSpaceToDepthCode(op_def);
80 op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
81 return op;
82 }
83
84 } // namespace gpu
85 } // namespace tflite
86