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/softmax.h"
17
18 #include <string>
19
20 #include "tensorflow/lite/delegates/gpu/common/status.h"
21 #include "tensorflow/lite/delegates/gpu/common/task/work_group_picking.h"
22
23 namespace tflite {
24 namespace gpu {
25
26 namespace {
GetSoftmaxKernelCode(const OperationDef & op_def)27 std::string GetSoftmaxKernelCode(const OperationDef& op_def) {
28 std::string c;
29 c += "MAIN_FUNCTION($0) {\n";
30 c += " int X = GLOBAL_ID_0;\n";
31 c += " int Y = GLOBAL_ID_1;\n";
32 c += " if (X >= args.dst_tensor.Width() || Y >= args.dst_tensor.Height()) "
33 "return; \n";
34 c += " float sum = 0.0f;\n";
35 c += " float maximum = args.src_tensor.Read<float>(X, Y, 0).x;\n";
36 c += " for (int d = 0; d < args.dst_tensor.Slices(); ++d) {\n";
37 c += " float4 t = args.src_tensor.Read<float>(X, Y, d);\n";
38 c += " maximum = max(maximum, t.x);\n";
39 c += " if (d * 4 + 1 < args.dst_tensor.Channels()) maximum = max(maximum, "
40 "t.y);\n";
41 c += " if (d * 4 + 2 < args.dst_tensor.Channels()) maximum = max(maximum, "
42 "t.z);\n";
43 c += " if (d * 4 + 3 < args.dst_tensor.Channels()) maximum = max(maximum, "
44 "t.w);\n";
45 c += " }\n";
46 c += " for (int d = 0; d < args.dst_tensor.Slices(); ++d) {\n";
47 c += " float4 t = args.src_tensor.Read<float>(X, Y, d) - "
48 "INIT_FLOAT4(maximum);\n";
49 c += " sum += exp(t.x);\n";
50 c += " if (d * 4 + 1 < args.dst_tensor.Channels()) sum += exp(t.y);\n";
51 c += " if (d * 4 + 2 < args.dst_tensor.Channels()) sum += exp(t.z);\n";
52 c += " if (d * 4 + 3 < args.dst_tensor.Channels()) sum += exp(t.w);\n";
53 c += " }\n";
54 c += " for (int d = 0; d < args.dst_tensor.Slices(); ++d) {\n";
55 c += " float4 t = args.src_tensor.Read<float>(X, Y, d) - "
56 "INIT_FLOAT4(maximum);\n";
57 c += " t = exp(t) / sum;\n";
58 c += " FLT4 result = TO_FLT4(t);\n";
59 c += " args.dst_tensor.Write(result, X, Y, d);\n";
60 c += " }\n";
61 c += "}\n";
62 return c;
63 }
64 } // namespace
65
CreateSoftmax(const OperationDef & definition)66 GPUOperation CreateSoftmax(const OperationDef& definition) {
67 GPUOperation op(definition);
68 auto src_desc = definition.src_tensors[0];
69 if (definition.IsBatchSupported()) {
70 src_desc.SetStateVar("BatchedWidth", "true");
71 }
72 op.AddSrcTensor("src_tensor", src_desc);
73 auto dst_desc = definition.dst_tensors[0];
74 if (definition.IsBatchSupported()) {
75 dst_desc.SetStateVar("BatchedWidth", "true");
76 }
77 op.AddDstTensor("dst_tensor", dst_desc);
78 op.code_ = GetSoftmaxKernelCode(definition);
79 op.tensor_to_grid_ = TensorToGrid::kWBToX_HDToY_ZIs1;
80 return op;
81 }
82
83 } // namespace gpu
84 } // namespace tflite
85