• 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/reshapex4.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 {
26 
GetReshapeCode(const OperationDef & op_def)27 std::string GetReshapeCode(const OperationDef& op_def) {
28   std::string c;
29   c += "MAIN_FUNCTION($0) {\n";
30   if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
31     c += "  int linear_id = GLOBAL_ID_0;\n";
32     c += "  int X = linear_id / args.dst_tensor.Batch();\n";
33     c += "  int B = linear_id % args.dst_tensor.Batch();\n";
34     c += "  args.dst_tensor.SetBatchRef(B);\n";
35   } else {
36     c += "  int X = GLOBAL_ID_0;\n";
37   }
38   c += "  int Y = GLOBAL_ID_1;\n";
39   c += "  int Z = GLOBAL_ID_2;\n";
40   c += "  if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
41        "Z >= args.dst_tensor.Slices()) { \n";
42   c += "    return; \n";
43   c += "  } \n";
44   if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
45     c += "  int dst_bhwc4 = B;\n";
46   } else {
47     c += "  int dst_bhwc4 = 0;\n";
48   }
49   c += "  dst_bhwc4 = ((dst_bhwc4 * args.dst_tensor.Height() + Y) * "
50        "args.dst_tensor.Width() + X) * args.dst_tensor.Slices() + Z;\n";
51   c += "  int src_z = dst_bhwc4 % args.src_tensor.Slices();\n";
52   c += "  dst_bhwc4 = dst_bhwc4 / args.src_tensor.Slices();\n";
53   c += "  int src_x = dst_bhwc4 % args.src_tensor.Width();\n";
54   c += "  dst_bhwc4 = dst_bhwc4 / args.src_tensor.Width();\n";
55   c += "  int src_y = dst_bhwc4 % args.src_tensor.Height();\n";
56   if (op_def.src_tensors[0].HasAxis(Axis::BATCH)) {
57     c += "  int src_b = dst_bhwc4 / args.src_tensor.Height();\n";
58     c += "  args.src_tensor.SetBatchRef(src_b);\n";
59   }
60   c += "  FLT4 result = args.src_tensor.Read(src_x, src_y, src_z);\n";
61   c += "  args.dst_tensor.Write(result, X, Y, Z);\n";
62   c += "}\n";
63   return c;
64 }
65 
66 }  // namespace
67 
CreateReshapex4(const OperationDef & definition)68 GPUOperation CreateReshapex4(const OperationDef& definition) {
69   GPUOperation op(definition);
70   op.AddSrcTensor("src_tensor", definition.src_tensors[0]);
71   op.AddDstTensor("dst_tensor", definition.dst_tensors[0]);
72   op.code_ = GetReshapeCode(definition);
73   op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
74   return op;
75 }
76 
77 }  // namespace gpu
78 }  // namespace tflite
79