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/one_hot.h"
16
17 #include <string>
18 #include <utility>
19
20 #include "tensorflow/lite/delegates/gpu/common/operations.h"
21
22 namespace tflite {
23 namespace gpu {
24
GetOneHotCode(const OperationDef & op_def,const OneHotAttributes & attr,GPUOperation * op)25 std::string GetOneHotCode(const OperationDef& op_def,
26 const OneHotAttributes& attr, GPUOperation* op) {
27 op->AddSrcTensor("src_tensor", op_def.src_tensors[0]);
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.dst_tensor.SetBatchRef(B);\n";
36 c += " args.src_tensor.SetBatchRef(B);\n";
37 } else {
38 c += " int X = GLOBAL_ID_0;\n";
39 }
40 c += " int Y = GLOBAL_ID_1;\n";
41 c += " int Z = GLOBAL_ID_2;\n";
42 c += " if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height() || "
43 "Z >= args.dst_tensor.Slices()) { \n";
44 c += " return; \n";
45 c += " } \n";
46 c += " int idx = Z * 4;\n";
47 c += " int hot_idx = args.src_tensor.Read(0, 0, 0).x;\n";
48 c += " FLT4 res = INIT_FLT4(args.off_value);\n";
49 c += " if ((hot_idx >= idx) && (hot_idx < (idx + 4))) {\n";
50 c += " res.x = (idx + 0) == hot_idx ? args.on_value : args.off_value;\n";
51 c += " res.y = (idx + 1) == hot_idx ? args.on_value : args.off_value;\n";
52 c += " res.z = (idx + 2) == hot_idx ? args.on_value : args.off_value;\n";
53 c += " res.w = (idx + 3) == hot_idx ? args.on_value : args.off_value;\n";
54 c += " }\n";
55 c += " args.dst_tensor.Write(res, X, Y, Z);\n";
56 c += "}\n";
57 return c;
58 }
59
CreateOneHot(const OperationDef & definition,const OneHotAttributes & attr)60 GPUOperation CreateOneHot(const OperationDef& definition,
61 const OneHotAttributes& attr) {
62 GPUOperation op(definition);
63 op.code_ = GetOneHotCode(definition, attr, &op);
64 op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_SToZ;
65 if (definition.precision == CalculationsPrecision::F32) {
66 op.args_.AddFloat("on_value", attr.on_value);
67 op.args_.AddFloat("off_value", attr.off_value);
68 } else {
69 op.args_.AddHalf("on_value", half(attr.on_value));
70 op.args_.AddHalf("off_value", half(attr.off_value));
71 }
72 return op;
73 }
74
75 } // namespace gpu
76 } // namespace tflite
77