blob: 1b89161e24f65d301124dfd9c6e23d2b2b8d98ec [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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/CL/kernels/CLSoftmaxLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.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 CLLogits1DMaxKernel::configure(const ICLTensor *input, ICLTensor *output)
43{
Georgios Pinitas09796752017-07-10 16:05:21 +010044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +010045 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
52 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
53
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +010055 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057
58 _input = input;
59 _output = output;
60
61 // The kernel loops over all elements in steps of 16
62 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16);
63
64 // Set build options
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010065 std::set<std::string> build_opts;
66 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
67 if(is_data_type_fixed_point(input->info()->data_type()))
68 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +010069 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010070 }
Moritz Pflanzerba9b3f52017-07-27 12:45:30 +010071 else if(input->info()->data_type() == DataType::F16)
72 {
73 build_opts.emplace("-DUSE_F16");
74 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075
76 // Tell the kernel that the width is not a multiple of 16
77 if((input->info()->dimension(0) % max_cl_vector_width) != 0)
78 {
79 build_opts.emplace("-DNON_MULTIPLE_OF_16");
80 }
81
82 // Create kernel
83 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("softmax_layer_max", build_opts));
84
85 // Set fixed arguments
steniu010d523cc2017-07-13 14:24:23 +010086 unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0));
88
89 // Configure kernel window
90 constexpr unsigned int num_elems_written_per_iteration = 1;
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
100 ICLKernel::configure(win);
101}
102
103CLLogits1DShiftExpSumKernel::CLLogits1DShiftExpSumKernel()
104 : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr)
105{
106}
107
Pablo Palmier48a60f92017-10-18 11:03:08 +0100108void CLLogits1DShiftExpSumKernel::configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109{
Georgios Pinitas09796752017-07-10 16:05:21 +0100110 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100111 ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output);
112
113 // Output auto initialization if not yet initialized
114 auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
115 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
116
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100118 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100119 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100120 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122 _input = input;
123 _max = max;
124 _output = output;
125 _sum = sum;
126
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000127 const DataType dt = input->info()->data_type();
128 auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position())));
129
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 // The kernel loops over all elements in steps of 16
131 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16);
132
133 // Set build options
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000134 CLBuildOptions build_opts;
135 build_opts.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(dt)));
136 build_opts.add_option_if(is_data_type_fixed_point(dt),
137 std::string("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
138 build_opts.add_option_if(dt == DataType::F16, std::string("-DUSE_F16"));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 // Tell the kernel that the width is not a multiple of 16
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000140 build_opts.add_option_if((input->info()->dimension(0) % max_cl_vector_width) != 0, std::string("-DNON_MULTIPLE_OF_16"));
141 build_opts.add_option_if(is_data_type_fixed_point(dt) && (beta != 1.0f), std::string("-DBETA=" + support::cpp11::to_string(beta_int)));
142 build_opts.add_option_if(is_data_type_float(dt) && (beta != 1.0f), std::string("-DBETA=" + float_to_string_with_full_precision(beta)));
Pablo Palmier48a60f92017-10-18 11:03:08 +0100143
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 // Create kernel
Georgios Pinitas388d3ec2017-11-02 12:17:56 +0000145 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147 // Set fixed arguments
steniu010d523cc2017-07-13 14:24:23 +0100148 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0));
150
151 // Configure window
152 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
153
154 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
155 AccessWindowHorizontal max_access(max->info(), 0, 1);
156 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
157 AccessWindowHorizontal sum_access(sum->info(), 0, 1);
158
159 update_window_and_padding(win, input_access, max_access, output_access, sum_access);
160
161 output_access.set_valid_region(win, input->info()->valid_region());
162 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
163
164 ICLKernel::configure(win);
165}
166
167void CLLogits1DShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
168{
169 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
170 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
171
steniu010d523cc2017-07-13 14:24:23 +0100172 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
173 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174
175 do
176 {
177 unsigned int idx = 0;
178 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100179 add_3D_tensor_argument(idx, _input, slice);
180 add_3D_tensor_argument(idx, _max, slice);
181 add_3D_tensor_argument(idx, _output, slice);
182 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 enqueue(queue, *this, slice);
184 }
steniu010d523cc2017-07-13 14:24:23 +0100185 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186}
187
188CLLogits1DNormKernel::CLLogits1DNormKernel()
189 : _input(nullptr), _sum(nullptr), _output(nullptr)
190{
191}
192
193void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output)
194{
Georgios Pinitas09796752017-07-10 16:05:21 +0100195 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100196 ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output);
197
198 // Output auto initialization if not yet initialized
199 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
200
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100201 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100202 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum, output);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100203 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
205 _input = input;
206 _sum = sum;
207 _output = output;
208
209 // Set build options
210 std::set<std::string> build_opts;
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100211 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
212 if(is_data_type_fixed_point(input->info()->data_type()))
213 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100214 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100215 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216
217 // Create kernel
218 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts));
219
220 // Configure window
221 constexpr unsigned int num_elems_processed_per_iteration = 16;
222
223 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
224
225 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
226 AccessWindowStatic sum_access(sum->info(), 0, 0, 1, sum->info()->dimension(1));
227 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
228
229 update_window_and_padding(win, input_access, sum_access, output_access);
230
231 output_access.set_valid_region(win, input->info()->valid_region());
232
233 ICLKernel::configure(win);
234}
235
236void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
237{
238 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
239 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
240
steniu010d523cc2017-07-13 14:24:23 +0100241 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
242 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243
244 do
245 {
246 Window sum_slice = slice;
247 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
248
249 unsigned int idx = 0;
250 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100251 add_3D_tensor_argument(idx, _input, slice);
252 add_3D_tensor_argument(idx, _sum, sum_slice);
253 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100254 enqueue(queue, *this, slice);
255 }
steniu010d523cc2017-07-13 14:24:23 +0100256 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257}