xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tasks/select_v2.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 #include "tensorflow/lite/delegates/gpu/common/tasks/select_v2.h"
16 
17 #include <string>
18 #include <utility>
19 
20 namespace tflite {
21 namespace gpu {
22 
GetSelectV2Code(const OperationDef & op_def,const SelectV2Attributes & attr,GPUOperation * op)23 std::string GetSelectV2Code(const OperationDef& op_def,
24                             const SelectV2Attributes& attr, GPUOperation* op) {
25   op->AddSrcTensor("cond_tensor", op_def.src_tensors[0]);
26   op->AddSrcTensor("true_tensor", op_def.src_tensors[1]);
27   op->AddSrcTensor("else_tensor", op_def.src_tensors[2]);
28   op->AddDstTensor("dst_tensor", op_def.dst_tensors[0]);
29   std::string c;
30   c += "MAIN_FUNCTION($0) {\n";
31   if (op_def.dst_tensors[0].HasAxis(Axis::BATCH)) {
32     c += "  int linear_id = GLOBAL_ID_0;\n";
33     c += "  int X = linear_id / args.dst_tensor.Batch();\n";
34     c += "  int B = linear_id % args.dst_tensor.Batch();\n";
35     c += "  args.cond_tensor.SetBatchRef(B);\n";
36     c += "  args.dst_tensor.SetBatchRef(B);\n";
37     c += attr.broadcast_true ? "" : "  args.true_tensor.SetBatchRef(B);\n";
38     c += attr.broadcast_false ? "" : "  args.else_tensor.SetBatchRef(B);\n";
39   } else {
40     c += "  int X = GLOBAL_ID_0;\n";
41   }
42   c += "  int Y = GLOBAL_ID_1;\n";
43   c += "  int Z = GLOBAL_ID_2;\n";
44   c += "  if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
45        "Z >= args.dst_tensor.Slices()) { \n";
46   c += "    return; \n";
47   c += "  } \n";
48   c += "  FLT4 true_val, else_val;\n";
49   if (!attr.broadcast_true) {
50     c += "  true_val = args.true_tensor.Read(X, Y, Z);\n";
51   } else {
52     c += "  true_val = INIT_FLT4(args.true_tensor.Read(0, 0, 0, 0).x);\n";
53   }
54   if (!attr.broadcast_false) {
55     c += "  else_val = args.else_tensor.Read(X, Y, Z);\n";
56   } else {
57     c += "  else_val = INIT_FLT4(args.else_tensor.Read(0, 0, 0, 0).x);\n";
58   }
59   c += "  bool should_gather_rows = \n";
60   if (attr.broadcast_true && attr.broadcast_false) {
61     c += "      true;\n";
62   } else {
63     c += "      args.dst_tensor.Slices() != args.cond_tensor.Slices();\n";
64   }
65   c += "  FLT4 res;\n";
66   c += "  if (should_gather_rows) {\n";
67   c += "    bool cond = args.cond_tensor.Read<bool>(X, 0, 0).x;\n";
68   c += "    res = cond ? true_val : else_val;\n";
69   c += "  } else {\n";
70   c += "    bool4 cond = args.cond_tensor.Read<bool>(0, Y, Z);\n";
71   c += "    res = true_val;\n";
72   c += "    res.x = cond.x ? true_val.x : else_val.x;\n";
73   c += "    res.y = cond.y ? true_val.y : else_val.y;\n";
74   c += "    res.z = cond.z ? true_val.z : else_val.z;\n";
75   c += "    res.w = cond.w ? true_val.w : else_val.w;\n";
76   c += "  }\n;";
77   c += "  args.dst_tensor.Write(res, X, Y, Z);\n";
78   c += "}\n";
79   return c;
80 }
81 
CreateSelectV2(const OperationDef & definition,const SelectV2Attributes & attr)82 GPUOperation CreateSelectV2(const OperationDef& definition,
83                             const SelectV2Attributes& attr) {
84   GPUOperation op(definition);
85   op.code_ = GetSelectV2Code(definition, attr, &op);
86   op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
87   op.args_.AddInt("broadcast_true", attr.broadcast_true);
88   op.args_.AddInt("broadcast_else", attr.broadcast_false);
89   return op;
90 }
91 
92 }  // namespace gpu
93 }  // namespace tflite
94