1 /* Copyright 2021 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/tile.h"
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 namespace tflite {
23 namespace gpu {
24
25 namespace {
GetTileCode(const OperationDef & op_def,bool src_channels_x4)26 std::string GetTileCode(const OperationDef& op_def, bool src_channels_x4) {
27 std::string c;
28 c += "MAIN_FUNCTION($0) {\n";
29 if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
30 c += " int linear_id = GLOBAL_ID_0;\n";
31 c += " int X = linear_id / args.dst_tensor.Batch();\n";
32 c += " int B = linear_id % args.dst_tensor.Batch();\n";
33 } else {
34 c += " int X = GLOBAL_ID_0;\n";
35 }
36 if (op_def.dst_tensors[0].HasAxis(Axis::DEPTH)) {
37 c += " int linear_id = GLOBAL_ID_1;\n";
38 c += " int Y = linear_id / args.dst_tensor.Depth();\n";
39 c += " int Z = linear_id % args.dst_tensor.Depth();\n";
40 } else {
41 c += " int Y = GLOBAL_ID_1;\n";
42 }
43 c += " int S = GLOBAL_ID_2;\n";
44 c += " if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
45 "S >= args.dst_tensor.Slices()) { \n";
46 c += " return; \n";
47 c += " } \n";
48 std::string dst_coords = "X, Y";
49 if (op_def.dst_tensors[0].HasAxis(Axis::DEPTH)) {
50 dst_coords += ", Z";
51 }
52 dst_coords += ", S";
53 if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
54 dst_coords += ", B";
55 }
56 std::string src_coords = "src_x, src_y";
57 if (op_def.src_tensors[0].HasAxis(Axis::DEPTH)) {
58 src_coords += ", src_z";
59 }
60 src_coords += ", src_s";
61 if (op_def.src_tensors[0].HasAxis(Axis::BATCH)) {
62 src_coords += ", src_b";
63 }
64 c += " int src_x = X % args.src_tensor.Width();\n";
65 c += " int src_y = Y % args.src_tensor.Height();\n";
66 if (op_def.src_tensors[0].HasAxis(Axis::DEPTH)) {
67 c += " int src_z = Z % args.src_tensor.Depth();\n";
68 }
69 if (op_def.src_tensors[0].HasAxis(Axis::BATCH)) {
70 c += " int src_b = B % args.src_tensor.Batch();\n";
71 }
72 if (src_channels_x4) {
73 c += " int src_s = S % args.src_tensor.Slices();\n";
74 c += " FLT4 result = args.src_tensor.Read(" + src_coords + ");\n";
75 } else {
76 c += " FLT tmp[4];\n";
77 c += " tmp[0] = INIT_FLT(0.0f);\n";
78 c += " tmp[1] = INIT_FLT(0.0f);\n";
79 c += " tmp[2] = INIT_FLT(0.0f);\n";
80 c += " tmp[3] = INIT_FLT(0.0f);\n";
81 c += " for (int i = 0; i < 4; ++i) {\n";
82 c += " int dst_c = 4 * S + i;\n";
83 c += " int src_c = dst_c % args.src_tensor.Channels();\n";
84 c += " int src_s = src_c / 4;\n";
85 c += " FLT4 t = args.src_tensor.Read(" + src_coords + ");\n";
86 c += " tmp[i] = SELECT_BY_INDEX_FROM_FLT4(t, src_c % 4);\n";
87 c += " }\n";
88 c += " FLT4 result;\n";
89 c += " result.x = tmp[0];\n";
90 c += " result.y = tmp[1];\n";
91 c += " result.z = tmp[2];\n";
92 c += " result.w = tmp[3];\n";
93 }
94 c += " args.dst_tensor.Write(result, " + dst_coords + ");\n";
95 c += "}\n";
96 return c;
97 }
98 } // namespace
99
CreateTile(const OperationDef & op_def,int src_channels)100 GPUOperation CreateTile(const OperationDef& op_def, int src_channels) {
101 GPUOperation op(op_def);
102 op.AddSrcTensor("src_tensor", op_def.src_tensors[0]);
103 op.AddDstTensor("dst_tensor", op_def.dst_tensors[0]);
104 op.code_ = GetTileCode(op_def, src_channels % 4 == 0);
105 op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
106 return op;
107 }
108
109 } // namespace gpu
110 } // namespace tflite
111