• 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/reshape.h"
17 
18 #include <string>
19 
20 #include "tensorflow/lite/delegates/gpu/common/task/work_group_picking.h"
21 
22 namespace tflite {
23 namespace gpu {
24 
25 namespace {
GetReshapeCode(const OperationDef & op_def)26 std::string GetReshapeCode(const OperationDef& op_def) {
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     c += "  args.dst_tensor.SetBatchRef(B);\n";
34   } else {
35     c += "  int X = GLOBAL_ID_0;\n";
36   }
37   c += "  int Y = GLOBAL_ID_1;\n";
38   c += "  int Z = GLOBAL_ID_2;\n";
39   c += "  if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
40        "Z >= args.dst_tensor.Slices()) { \n";
41   c += "    return; \n";
42   c += "  } \n";
43   c += "  args.src_tensor::scalar_type temps[4];\n";
44   c += "  temps[0] = args.src_tensor::scalar_zero_value;\n";
45   c += "  temps[1] = args.src_tensor::scalar_zero_value;\n";
46   c += "  temps[2] = args.src_tensor::scalar_zero_value;\n";
47   c += "  temps[3] = args.src_tensor::scalar_zero_value;\n";
48   if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
49     c += "  int base = B;\n";
50   } else {
51     c += "  int base = 0;\n";
52   }
53   c += "  base = ((base * args.dst_tensor.Height() + Y) * "
54        "args.dst_tensor.Width() + X) * args.dst_tensor.Channels() + Z * 4;\n";
55   c += "  for (int i = 0; i < 4; ++i) {\n";
56   c += "    int dst_channel = Z * 4 + i;\n";
57   c += "    if (dst_channel < args.dst_tensor.Channels()) {;\n";
58   c += "      int p = base + i;\n";
59   c += "      int src_c = p % args.src_tensor.Channels();\n";
60   c += "      p = p / args.src_tensor.Channels();\n";
61   c += "      int src_x = p % args.src_tensor.Width();\n";
62   c += "      p = p / args.src_tensor.Width();\n";
63   c += "      int src_y = p % args.src_tensor.Height();\n";
64   if (op_def.src_tensors[0].HasAxis(Axis::BATCH)) {
65     c += "  int src_b = p / args.src_tensor.Height();\n";
66     c += "  args.src_tensor.SetBatchRef(src_b);\n";
67   }
68   c += "      args.src_tensor.ReadPerChannel(temps[i], src_x, src_y, src_c);\n";
69   c += "    }\n";
70   c += "  }\n";
71   c += "  args.src_tensor::type result;\n";
72   c += "  result.x = temps[0];\n";
73   c += "  result.y = temps[1];\n";
74   c += "  result.z = temps[2];\n";
75   c += "  result.w = temps[3];\n";
76   c += "  args.dst_tensor.Write(result, X, Y, Z);\n";
77   c += "}\n";
78   return c;
79 }
80 
81 }  // namespace
82 
CreateReshape(const OperationDef & definition)83 GPUOperation CreateReshape(const OperationDef& definition) {
84   GPUOperation op(definition);
85   op.AddSrcTensor("src_tensor", definition.src_tensors[0]);
86   op.AddDstTensor("dst_tensor", definition.dst_tensors[0]);
87   op.code_ = GetReshapeCode(definition);
88   op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
89   return op;
90 }
91 
92 }  // namespace gpu
93 }  // namespace tflite
94