xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tasks/softmax1x1.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/softmax1x1.h"
17 
18 #include <string>
19 #include <utility>
20 
21 #include "tensorflow/lite/delegates/gpu/common/status.h"
22 #include "tensorflow/lite/delegates/gpu/common/task/util.h"
23 
24 namespace tflite {
25 namespace gpu {
26 
Softmax1x1(const OperationDef & definition)27 Softmax1x1::Softmax1x1(const OperationDef& definition)
28     : GPUOperation(definition) {
29   work_group_size_ = int3(32, 1, 1);
30   code_ = GetSoftmaxKernelCode(definition_);
31 }
32 
Softmax1x1(Softmax1x1 && kernel)33 Softmax1x1::Softmax1x1(Softmax1x1&& kernel) : GPUOperation(std::move(kernel)) {}
34 
operator =(Softmax1x1 && kernel)35 Softmax1x1& Softmax1x1::operator=(Softmax1x1&& kernel) {
36   if (this != &kernel) {
37     GPUOperation::operator=(std::move(kernel));
38   }
39   return *this;
40 }
41 
GetSoftmaxKernelCode(const OperationDef & op_def)42 std::string Softmax1x1::GetSoftmaxKernelCode(const OperationDef& op_def) {
43   AddSrcTensor("src_tensor", op_def.src_tensors[0]);
44   AddDstTensor("dst_tensor", op_def.dst_tensors[0]);
45   args_.AddFloat("mask_x");
46   args_.AddFloat("mask_y");
47   args_.AddFloat("mask_z");
48   args_.AddFloat("mask_w");
49 
50   std::string c;
51   c += "MAIN_FUNCTION($0) {\n";
52   if (op_def.IsBatchSupported()) {
53     c += "  int batch_id = GLOBAL_ID_1;\n";
54     c += "  if (batch_id >= args.dst_tensor.Batch()) return;\n";
55     c += "  args.dst_tensor.SetBatchRef(batch_id);\n";
56     c += "  args.src_tensor.SetBatchRef(batch_id);\n";
57   }
58   c += "  float4 mask = INIT_FLOAT4v4(args.mask_x, args.mask_y, args.mask_z, "
59        "args.mask_w);\n";
60   c +=
61       "  float4 maxx4 = INIT_FLOAT4(args.src_tensor.Read<float>(0, 0, 0).x);\n";
62   c += "  int tid = LOCAL_ID_0;\n";
63   c += "  for (int s = tid; s < args.src_tensor.Slices(); s += 32) {\n";
64   c += "    float4 mask_a = s == args.src_tensor.Slices() - 1 ? mask : "
65        "INIT_FLOAT4(1.0f);\n";
66   c += "    float4 mask_b = INIT_FLOAT4(1.0f) - mask_a;\n";
67   c += "    float4 src = args.src_tensor.Read<float>(0, 0, s);\n";
68   c += "    src = src * mask_a + mask_b * src.x;\n";
69   c += "    maxx4 = max(maxx4, src);\n";
70   c += "  }\n";
71   c += "  float maximum = max(maxx4.x, maxx4.y);\n";
72   c += "  maximum = max(maximum, maxx4.z);\n";
73   c += "  maximum = max(maximum, maxx4.w);\n";
74   c += "  __local float loc_mem[32];\n";
75   c += "  loc_mem[tid] = maximum;\n";
76   c += "  LOCAL_MEM_BARRIER;\n";
77   c += "  if (tid % 8 == 0) {\n";
78   c += "    maximum = max(loc_mem[tid], loc_mem[tid + 1]);\n";
79   c += "    maximum = max(maximum, loc_mem[tid + 2]);\n";
80   c += "    maximum = max(maximum, loc_mem[tid + 3]);\n";
81   c += "    maximum = max(maximum, loc_mem[tid + 4]);\n";
82   c += "    maximum = max(maximum, loc_mem[tid + 5]);\n";
83   c += "    maximum = max(maximum, loc_mem[tid + 6]);\n";
84   c += "    maximum = max(maximum, loc_mem[tid + 7]);\n";
85   c += "    loc_mem[tid] = maximum;\n";
86   c += "  }\n";
87   c += "  LOCAL_MEM_BARRIER;\n";
88   c += "  if (tid == 0) {\n";
89   c += "    maximum = max(loc_mem[0], loc_mem[8]);\n";
90   c += "    maximum = max(maximum, loc_mem[16]);\n";
91   c += "    maximum = max(maximum, loc_mem[24]);\n";
92   c += "    loc_mem[0] = maximum;\n";
93   c += "  }\n";
94   c += "  LOCAL_MEM_BARRIER;\n";
95   c += "  maximum = loc_mem[0];\n";
96   c += "  float sum = 0.0f;\n";
97   c += "  for (int s = tid; s < args.src_tensor.Slices(); s += 32) {\n";
98   c += "    float4 mask_temp = s == args.src_tensor.Slices() - 1 ? mask : "
99        "INIT_FLOAT4(1.0f);\n";
100   c += "    float4 src = args.src_tensor.Read<float>(0, 0, s) - "
101        "INIT_FLOAT4(maximum);\n";
102   c += "    sum += dot(mask_temp, exp(src));\n";
103   c += "  }\n";
104   c += "  LOCAL_MEM_BARRIER;\n";
105   c += "  loc_mem[tid] = sum;\n";
106   c += "  LOCAL_MEM_BARRIER;\n";
107   c += "  if (tid % 8 == 0) {\n";
108   c += "    sum = loc_mem[tid] + loc_mem[tid + 1];\n";
109   c += "    sum += loc_mem[tid + 2];\n";
110   c += "    sum += loc_mem[tid + 3];\n";
111   c += "    sum += loc_mem[tid + 4];\n";
112   c += "    sum += loc_mem[tid + 5];\n";
113   c += "    sum += loc_mem[tid + 6];\n";
114   c += "    sum += loc_mem[tid + 7];\n";
115   c += "    loc_mem[tid] = sum;\n";
116   c += "  }\n";
117   c += "  LOCAL_MEM_BARRIER;\n";
118   c += "  if (tid == 0) {\n";
119   c += "    sum = loc_mem[0] + loc_mem[8] + loc_mem[16] + loc_mem[24];\n";
120   c += "    loc_mem[0] = 1.0f / sum;\n";
121   c += "  }\n";
122   c += "  LOCAL_MEM_BARRIER;\n";
123   c += "  sum = loc_mem[0];\n";
124   c += "\n";
125   c += "  int dst_s = GLOBAL_ID_0;\n";
126   c += "  if (dst_s < args.dst_tensor.Slices()) {\n";
127   c += "    float4 src = args.src_tensor.Read<float>(0, 0, dst_s) - "
128        "INIT_FLOAT4(maximum);\n";
129   c += "    FLT4 res = TO_FLT4(exp(src) * sum);\n";
130   c += "    args.dst_tensor.Write(res, 0, 0, dst_s);\n";
131   c += "  }\n";
132   c += "}\n";
133   return c;
134 }
135 
BindArguments(ArgumentsBinder * args)136 absl::Status Softmax1x1::BindArguments(ArgumentsBinder* args) {
137   float4 mask = GetMaskForLastPlane(src_[0]->Channels());
138   RETURN_IF_ERROR(args->SetFloat("mask_x", mask.x));
139   RETURN_IF_ERROR(args->SetFloat("mask_y", mask.y));
140   RETURN_IF_ERROR(args->SetFloat("mask_z", mask.z));
141   RETURN_IF_ERROR(args->SetFloat("mask_w", mask.w));
142   return absl::OkStatus();
143 }
144 
GetGridSize() const145 int3 Softmax1x1::GetGridSize() const {
146   return int3(dst_[0]->Slices(), dst_[0]->Batch(), 1);
147 }
148 
CreateSoftmax1x1(const OperationDef & definition)149 Softmax1x1 CreateSoftmax1x1(const OperationDef& definition) {
150   return Softmax1x1(definition);
151 }
152 
153 }  // namespace gpu
154 }  // namespace tflite
155