blob: f4ed9617fa6424ba6294ed6b41df316cd4b70738 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010037
38#include <set>
39#include <string>
40
41using namespace arm_compute;
42
43void GCLogits1DMaxKernel::configure(const IGCTensor *input, IGCTensor *output)
44{
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
46 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
47
48 // Softmax across the x dimension
49 TensorShape output_shape{ input->info()->tensor_shape() };
50 output_shape.set(0, 1);
51
52 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010053 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010054
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
57
58 _input = input;
59 _output = output;
60
61 // Set build options
62 std::set<std::string> build_opts;
63 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
64 build_opts.insert("#define " + dt_name);
65 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
66 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
67 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
68 build_opts.insert("#define SOFTMAX_LAYER_MAX");
69
Joel Liang07c37f92017-11-17 11:34:19 +080070 // Tell the kernel that the width is not a multiple of 8
71 if((input->info()->dimension(0) % 8) != 0)
Anthony Barbier7068f992017-10-26 15:23:08 +010072 {
Joel Liang07c37f92017-11-17 11:34:19 +080073 build_opts.insert("#define NON_MULTIPLE_OF_8");
Anthony Barbier7068f992017-10-26 15:23:08 +010074 }
75
76 // Create kernel
77 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_max", build_opts));
78
Anthony Barbier7068f992017-10-26 15:23:08 +010079 // Set fixed arguments
80 unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Joel Liangf1f3ebd2017-11-10 09:59:19 +080081 _kernel.set_argument(idx++, input->info()->dimension(0));
Anthony Barbier7068f992017-10-26 15:23:08 +010082
83 // Configure kernel window
Joel Liang07c37f92017-11-17 11:34:19 +080084 // The kernel loops over all elements in steps of 8
85 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 8);
Anthony Barbier7068f992017-10-26 15:23:08 +010086 unsigned int num_elems_written_per_iteration = 1;
87 if(input->info()->data_type() == DataType::F16)
88 {
89 num_elems_written_per_iteration = 2;
90 }
91
92 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
93 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
94 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
95
96 update_window_and_padding(win, input_access, output_access);
97
98 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
99
Anthony Barbier7068f992017-10-26 15:23:08 +0100100 IGCKernel::configure(win);
101}
102
Anthony Barbier7068f992017-10-26 15:23:08 +0100103GCLogits1DShiftExpSumKernel::GCLogits1DShiftExpSumKernel()
104 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
105{
106}
107
108void GCLogits1DShiftExpSumKernel::configure(const IGCTensor *input, const IGCTensor *max, IGCTensor *output, IGCTensor *sum)
109{
110 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
111 ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output);
112
113 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100114 auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type());
115 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +0100116
117 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum);
118 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
119 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum);
120
121 _input = input;
122 _max = max;
123 _output = output;
124 _sum = sum;
125
126 // Set build options
127 std::set<std::string> build_opts;
128 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
129 build_opts.insert("#define " + dt_name);
130 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
131 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
132 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
133 build_opts.insert("#define SOFTMAX_LAYER_SHIFT_EXP_SUM");
134
Joel Liang07c37f92017-11-17 11:34:19 +0800135 // Tell the kernel that the width is not a multiple of 8
136 if((input->info()->dimension(0) % 8) != 0)
Anthony Barbier7068f992017-10-26 15:23:08 +0100137 {
Joel Liang07c37f92017-11-17 11:34:19 +0800138 build_opts.insert("#define NON_MULTIPLE_OF_8");
Anthony Barbier7068f992017-10-26 15:23:08 +0100139 }
140
141 // Create kernel
142 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts));
143
Anthony Barbier7068f992017-10-26 15:23:08 +0100144 // Set fixed arguments
145 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800146 _kernel.set_argument(idx++, input->info()->dimension(0));
Anthony Barbier7068f992017-10-26 15:23:08 +0100147
148 // Configure window
Joel Liang07c37f92017-11-17 11:34:19 +0800149 // The kernel loops over all elements in steps of 8
150 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 8);
Anthony Barbier7068f992017-10-26 15:23:08 +0100151 unsigned int num_elems_written_per_iteration = 1;
152 if(input->info()->data_type() == DataType::F16)
153 {
154 num_elems_written_per_iteration = 2;
155 }
156
157 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
158
159 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
160 AccessWindowHorizontal max_access(max->info(), 0, num_elems_written_per_iteration);
161 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
162 AccessWindowHorizontal sum_access(sum->info(), 0, num_elems_written_per_iteration);
163
164 update_window_and_padding(win, input_access, max_access, output_access, sum_access);
165
166 output_access.set_valid_region(win, input->info()->valid_region());
167 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
168
Anthony Barbier7068f992017-10-26 15:23:08 +0100169 IGCKernel::configure(win);
170}
171
172void GCLogits1DShiftExpSumKernel::run(const Window &window)
173{
174 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
175 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
176
177 Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
178 Window slice = window_collapsed.first_slice_window_3D();
179
180 _kernel.use();
181
182 do
183 {
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800184 unsigned int idx = 0;
185 unsigned int binding = 1; // SSBO binding starts from 1.
186 // Set inputs
187 add_3D_tensor_argument(idx, _input, binding++, slice);
188 add_3D_tensor_argument(idx, _max, binding++, slice);
189 add_3D_tensor_argument(idx, _output, binding++, slice);
190 add_3D_tensor_argument(idx, _sum, binding++, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100191 _kernel.update_shader_params();
192 enqueue(*this, slice);
193 }
194 while(window_collapsed.slide_window_slice_3D(slice));
195}
196
197GCLogits1DNormKernel::GCLogits1DNormKernel()
198 : _input(nullptr), _sum(nullptr), _output(nullptr)
199{
200}
201
202void GCLogits1DNormKernel::configure(const IGCTensor *input, const IGCTensor *sum, IGCTensor *output)
203{
204 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
205 ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output);
206
207 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100208 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +0100209
210 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output);
Anthony Barbier7068f992017-10-26 15:23:08 +0100211 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
212
213 _input = input;
214 _sum = sum;
215 _output = output;
216
217 // Set build options
218 std::set<std::string> build_opts;
219 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
220 build_opts.insert("#define " + dt_name);
221 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
222 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
223 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
224 build_opts.insert("#define SOFTMAX_LAYER_NORM");
225
226 // Create kernel
227 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts));
228
229 // Configure window
Joel Liang07c37f92017-11-17 11:34:19 +0800230 constexpr unsigned int num_elems_processed_per_iteration = 8;
Anthony Barbier7068f992017-10-26 15:23:08 +0100231 unsigned int num_elems_written_per_iteration = 1;
232 if(input->info()->data_type() == DataType::F16)
233 {
234 num_elems_written_per_iteration = 2;
235 }
236
237 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
238
239 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
240 AccessWindowStatic sum_access(sum->info(), 0, 0, num_elems_written_per_iteration, sum->info()->dimension(1));
241 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
242
243 update_window_and_padding(win, input_access, sum_access, output_access);
244
245 output_access.set_valid_region(win, input->info()->valid_region());
246
Anthony Barbier7068f992017-10-26 15:23:08 +0100247 IGCKernel::configure(win);
248}
249
250void GCLogits1DNormKernel::run(const Window &window)
251{
252 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
253 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
254
255 Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
256 Window slice = window_collapsed.first_slice_window_3D();
257
258 _kernel.use();
259
260 do
261 {
262 Window sum_slice = slice;
263 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
264
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800265 unsigned int idx = 0;
266 unsigned int binding = 1; // SSBO binding starts from 1.
267 // Set inputs
268 add_3D_tensor_argument(idx, _input, binding++, slice);
269 add_3D_tensor_argument(idx, _sum, binding++, slice);
270 add_3D_tensor_argument(idx, _output, binding++, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100271
272 _kernel.update_shader_params();
273 enqueue(*this, slice);
274 }
275 while(window_collapsed.slide_window_slice_3D(slice));
276}