• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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/concat_xy.h"
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/lite/delegates/gpu/common/operations.h"
23 #include "tensorflow/lite/delegates/gpu/common/task/work_group_picking.h"
24 #include "tensorflow/lite/delegates/gpu/common/types.h"
25 
26 namespace tflite {
27 namespace gpu {
28 namespace {
GetConcatKernelCode(const OperationDef & op_def,const ConcatAttributes & attr)29 std::string GetConcatKernelCode(const OperationDef& op_def,
30                                 const ConcatAttributes& attr) {
31   std::vector<std::string> tensor_names(op_def.src_tensors.size());
32   for (int i = 0; i < op_def.src_tensors.size(); ++i) {
33     tensor_names[i] = "src_tensor_" + std::to_string(i);
34   }
35 
36   std::map<Axis, std::string> axis_to_selector = {
37       {Axis::WIDTH, "Width"}, {Axis::HEIGHT, "Height"},
38       {Axis::DEPTH, "Depth"}, {Axis::CHANNELS, "Channels"},
39       {Axis::BATCH, "Batch"},
40   };
41   std::map<Axis, std::string> axis_to_coord = {
42       {Axis::WIDTH, "X"},    {Axis::HEIGHT, "Y"}, {Axis::DEPTH, "D"},
43       {Axis::CHANNELS, "S"}, {Axis::BATCH, "B"},
44   };
45 
46   std::vector<std::string> src_coords;
47   std::vector<std::string> dst_coords;
48   for (auto axis :
49        {Axis::WIDTH, Axis::HEIGHT, Axis::DEPTH, Axis::CHANNELS, Axis::BATCH}) {
50     if (op_def.src_tensors[0].HasAxis(axis) && axis != Axis::BATCH) {
51       if (axis == attr.axis) {
52         src_coords.push_back("coord");
53       } else {
54         src_coords.push_back(axis_to_coord[axis]);
55       }
56     }
57     if (op_def.dst_tensors[0].HasAxis(axis)) {
58       dst_coords.push_back(axis_to_coord[axis]);
59     }
60   }
61   std::string src_coord = src_coords[0];
62   for (int i = 1; i < src_coords.size(); ++i) {
63     src_coord += ", " + src_coords[i];
64   }
65   std::string dst_coord = dst_coords[0];
66   for (int i = 1; i < dst_coords.size(); ++i) {
67     dst_coord += ", " + dst_coords[i];
68   }
69 
70   std::string c;
71   c += "MAIN_FUNCTION($0) {\n";
72   if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
73     c += "  int linear_id_0 = GLOBAL_ID_0;\n";
74     c += "  int X = linear_id_0 / args.dst_tensor.Batch();\n";
75     c += "  int B = linear_id_0 % args.dst_tensor.Batch();\n";
76   } else {
77     c += "  int X = GLOBAL_ID_0;\n";
78   }
79   if (op_def.dst_tensors[0].HasAxis(Axis::DEPTH)) {
80     c += "  int linear_id_1 = GLOBAL_ID_1;\n";
81     c += "  int Y = linear_id_1 / args.dst_tensor.Depth();\n";
82     c += "  int D = linear_id_1 % args.dst_tensor.Depth();\n";
83   } else {
84     c += "  int Y = GLOBAL_ID_1;\n";
85   }
86   c += "  int S = GLOBAL_ID_2;\n";
87   c += "  if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
88        "S >= args.dst_tensor.Slices()) { \n";
89   c += "    return; \n";
90   c += "  } \n";
91   c += "  FLT4 result = INIT_FLT4(0.0f);\n";
92   c += "  int coord = " + axis_to_coord[attr.axis] + ";\n";
93   for (int i = 0; i < op_def.src_tensors.size(); ++i) {
94     const std::string field =
95         "args." + tensor_names[i] + "." + axis_to_selector[attr.axis] + "()";
96     c += "  if (coord >= 0 && coord < " + field + ") { \n";
97     if (op_def.src_tensors[i].HasAxis(Axis::BATCH)) {
98       if (attr.axis == Axis::BATCH) {
99         c += "  args." + tensor_names[i] + ".SetBatchRef(coord);\n";
100       } else {
101         c += "  args." + tensor_names[i] + ".SetBatchRef(B);\n";
102       }
103     }
104     c += "    result = args." + tensor_names[i] + ".Read(" + src_coord + ");\n";
105     c += "  } \n";
106     c += "  coord -= " + field + ";\n";
107   }
108   c += "  args.dst_tensor.Write(result, " + dst_coord + ");\n";
109   c += "}\n";
110   return c;
111 }
112 }  // namespace
113 
CreateConcatXY(const OperationDef & definition,const ConcatAttributes & attr)114 GPUOperation CreateConcatXY(const OperationDef& definition,
115                             const ConcatAttributes& attr) {
116   GPUOperation op(definition);
117   for (int i = 0; i < definition.src_tensors.size(); ++i) {
118     const std::string name = "src_tensor_" + std::to_string(i);
119     op.AddSrcTensor(name, definition.src_tensors[i]);
120   }
121   op.AddDstTensor("dst_tensor", definition.dst_tensors[0]);
122   op.code_ = GetConcatKernelCode(definition, attr);
123   op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
124   return op;
125 }
126 
127 }  // namespace gpu
128 }  // namespace tflite
129