blob: 7ae2fc9fa5adec784da93b048a050466e1658a37 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2017-2018 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"
36
37#include <set>
38#include <string>
39
40using namespace arm_compute;
41
42void GCLogits1DMaxKernel::configure(const IGCTensor *input, IGCTensor *output)
43{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
46
47 // Softmax across the x dimension
48 TensorShape output_shape{ input->info()->tensor_shape() };
49 output_shape.set(0, 1);
50
51 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010052 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010053
54 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
56
57 _input = input;
58 _output = output;
59
60 // Set build options
61 std::set<std::string> build_opts;
62 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
63 build_opts.insert("#define " + dt_name);
64 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
65 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
66 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
67 build_opts.insert("#define SOFTMAX_LAYER_MAX");
68
Joel Liang07c37f92017-11-17 11:34:19 +080069 // Tell the kernel that the width is not a multiple of 8
70 if((input->info()->dimension(0) % 8) != 0)
Anthony Barbier7068f992017-10-26 15:23:08 +010071 {
Joel Liang07c37f92017-11-17 11:34:19 +080072 build_opts.insert("#define NON_MULTIPLE_OF_8");
Anthony Barbier7068f992017-10-26 15:23:08 +010073 }
74
75 // Create kernel
76 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_max", build_opts));
77
Anthony Barbier7068f992017-10-26 15:23:08 +010078 // Set fixed arguments
79 unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Joel Liangf1f3ebd2017-11-10 09:59:19 +080080 _kernel.set_argument(idx++, input->info()->dimension(0));
Anthony Barbier7068f992017-10-26 15:23:08 +010081
82 // Configure kernel window
Joel Liang07c37f92017-11-17 11:34:19 +080083 // The kernel loops over all elements in steps of 8
84 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 8);
Anthony Barbier7068f992017-10-26 15:23:08 +010085 unsigned int num_elems_written_per_iteration = 1;
86 if(input->info()->data_type() == DataType::F16)
87 {
88 num_elems_written_per_iteration = 2;
89 }
90
91 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
92 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
93 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
94
95 update_window_and_padding(win, input_access, output_access);
96
97 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
98
Anthony Barbier7068f992017-10-26 15:23:08 +010099 IGCKernel::configure(win);
100}
101
Anthony Barbier7068f992017-10-26 15:23:08 +0100102GCLogits1DShiftExpSumKernel::GCLogits1DShiftExpSumKernel()
103 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
104{
105}
106
107void GCLogits1DShiftExpSumKernel::configure(const IGCTensor *input, const IGCTensor *max, IGCTensor *output, IGCTensor *sum)
108{
109 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
110 ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output);
111
112 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100113 auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type());
114 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +0100115
116 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum);
117 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
118 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum);
119
120 _input = input;
121 _max = max;
122 _output = output;
123 _sum = sum;
124
125 // Set build options
126 std::set<std::string> build_opts;
127 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
128 build_opts.insert("#define " + dt_name);
129 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
130 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
131 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
132 build_opts.insert("#define SOFTMAX_LAYER_SHIFT_EXP_SUM");
133
Joel Liang07c37f92017-11-17 11:34:19 +0800134 // Tell the kernel that the width is not a multiple of 8
135 if((input->info()->dimension(0) % 8) != 0)
Anthony Barbier7068f992017-10-26 15:23:08 +0100136 {
Joel Liang07c37f92017-11-17 11:34:19 +0800137 build_opts.insert("#define NON_MULTIPLE_OF_8");
Anthony Barbier7068f992017-10-26 15:23:08 +0100138 }
139
140 // Create kernel
141 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts));
142
Anthony Barbier7068f992017-10-26 15:23:08 +0100143 // Set fixed arguments
144 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800145 _kernel.set_argument(idx++, input->info()->dimension(0));
Anthony Barbier7068f992017-10-26 15:23:08 +0100146
147 // Configure window
Joel Liang07c37f92017-11-17 11:34:19 +0800148 // The kernel loops over all elements in steps of 8
149 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 8);
Anthony Barbier7068f992017-10-26 15:23:08 +0100150 unsigned int num_elems_written_per_iteration = 1;
151 if(input->info()->data_type() == DataType::F16)
152 {
153 num_elems_written_per_iteration = 2;
154 }
155
156 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
157
158 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
159 AccessWindowHorizontal max_access(max->info(), 0, num_elems_written_per_iteration);
160 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
161 AccessWindowHorizontal sum_access(sum->info(), 0, num_elems_written_per_iteration);
162
163 update_window_and_padding(win, input_access, max_access, output_access, sum_access);
164
165 output_access.set_valid_region(win, input->info()->valid_region());
166 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
167
Anthony Barbier7068f992017-10-26 15:23:08 +0100168 IGCKernel::configure(win);
169}
170
171void GCLogits1DShiftExpSumKernel::run(const Window &window)
172{
173 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
174 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
175
176 Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
177 Window slice = window_collapsed.first_slice_window_3D();
178
179 _kernel.use();
180
181 do
182 {
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800183 unsigned int idx = 0;
184 unsigned int binding = 1; // SSBO binding starts from 1.
185 // Set inputs
186 add_3D_tensor_argument(idx, _input, binding++, slice);
187 add_3D_tensor_argument(idx, _max, binding++, slice);
188 add_3D_tensor_argument(idx, _output, binding++, slice);
189 add_3D_tensor_argument(idx, _sum, binding++, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100190 _kernel.update_shader_params();
191 enqueue(*this, slice);
192 }
193 while(window_collapsed.slide_window_slice_3D(slice));
194}
195
196GCLogits1DNormKernel::GCLogits1DNormKernel()
197 : _input(nullptr), _sum(nullptr), _output(nullptr)
198{
199}
200
201void GCLogits1DNormKernel::configure(const IGCTensor *input, const IGCTensor *sum, IGCTensor *output)
202{
203 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
204 ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output);
205
206 // Output auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100207 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +0100208
209 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output);
Anthony Barbier7068f992017-10-26 15:23:08 +0100210 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
211
212 _input = input;
213 _sum = sum;
214 _output = output;
215
216 // Set build options
217 std::set<std::string> build_opts;
218 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
219 build_opts.insert("#define " + dt_name);
220 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
221 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
222 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
223 build_opts.insert("#define SOFTMAX_LAYER_NORM");
224
225 // Create kernel
226 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts));
227
228 // Configure window
Joel Liang07c37f92017-11-17 11:34:19 +0800229 constexpr unsigned int num_elems_processed_per_iteration = 8;
Anthony Barbier7068f992017-10-26 15:23:08 +0100230 unsigned int num_elems_written_per_iteration = 1;
231 if(input->info()->data_type() == DataType::F16)
232 {
233 num_elems_written_per_iteration = 2;
234 }
235
236 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
237
238 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
239 AccessWindowStatic sum_access(sum->info(), 0, 0, num_elems_written_per_iteration, sum->info()->dimension(1));
240 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
241
242 update_window_and_padding(win, input_access, sum_access, output_access);
243
244 output_access.set_valid_region(win, input->info()->valid_region());
245
Anthony Barbier7068f992017-10-26 15:23:08 +0100246 IGCKernel::configure(win);
247}
248
249void GCLogits1DNormKernel::run(const Window &window)
250{
251 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
252 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
253
254 Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ);
255 Window slice = window_collapsed.first_slice_window_3D();
256
257 _kernel.use();
258
259 do
260 {
261 Window sum_slice = slice;
262 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
263
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800264 unsigned int idx = 0;
265 unsigned int binding = 1; // SSBO binding starts from 1.
266 // Set inputs
267 add_3D_tensor_argument(idx, _input, binding++, slice);
268 add_3D_tensor_argument(idx, _sum, binding++, slice);
269 add_3D_tensor_argument(idx, _output, binding++, slice);
Anthony Barbier7068f992017-10-26 15:23:08 +0100270
271 _kernel.update_shader_params();
272 enqueue(*this, slice);
273 }
274 while(window_collapsed.slide_window_slice_3D(slice));
275}