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 += " args.src_tensor::type result = args.src_tensor.Read(" + src_coords +
75 ");\n";
76 } else {
77 c += " args.src_tensor::scalar_type tmp[4];\n";
78 c += " tmp[0] = args.src_tensor::scalar_zero_value;\n";
79 c += " tmp[1] = args.src_tensor::scalar_zero_value;\n";
80 c += " tmp[2] = args.src_tensor::scalar_zero_value;\n";
81 c += " tmp[3] = args.src_tensor::scalar_zero_value;\n";
82 c += " for (int i = 0; i < 4; ++i) {\n";
83 c += " int dst_c = 4 * S + i;\n";
84 c += " int src_s = dst_c % args.src_tensor.Channels();\n";
85 c += " args.src_tensor.ReadPerChannel(tmp[i], " + src_coords + ");\n";
86 c += " }\n";
87 c += " args.src_tensor::type result;\n";
88 c += " result.x = tmp[0];\n";
89 c += " result.y = tmp[1];\n";
90 c += " result.z = tmp[2];\n";
91 c += " result.w = tmp[3];\n";
92 }
93 c += " args.dst_tensor.Write(result, " + dst_coords + ");\n";
94 c += "}\n";
95 return c;
96 }
97 } // namespace
98
CreateTile(const OperationDef & op_def,int src_channels)99 GPUOperation CreateTile(const OperationDef& op_def, int src_channels) {
100 GPUOperation op(op_def);
101 op.AddSrcTensor("src_tensor", op_def.src_tensors[0]);
102 op.AddDstTensor("dst_tensor", op_def.dst_tensors[0]);
103 op.code_ = GetTileCode(op_def, src_channels % 4 == 0);
104 op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
105 return op;
106 }
107
108 } // namespace gpu
109 } // namespace tflite
110