blob: fb066bc645c9b056548bede353f78f70e393cefd [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);
Pablo Palmier48a60f92017-10-18 11:03:08 +0100112 ARM_COMPUTE_ERROR_ON(beta != 1.0f && input->info()->data_type() != DataType::F32);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100113
114 // Output auto initialization if not yet initialized
115 auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
116 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
117
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100119 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100120 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100121 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 _input = input;
124 _max = max;
125 _output = output;
126 _sum = sum;
127
128 // The kernel loops over all elements in steps of 16
129 const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 16);
130
131 // Set build options
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100132 std::set<std::string> build_opts;
133 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
134 if(is_data_type_fixed_point(input->info()->data_type()))
135 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100136 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100137 }
Moritz Pflanzerba9b3f52017-07-27 12:45:30 +0100138 else if(input->info()->data_type() == DataType::F16)
139 {
140 build_opts.emplace("-DUSE_F16");
141 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
143 // Tell the kernel that the width is not a multiple of 16
144 if((input->info()->dimension(0) % max_cl_vector_width) != 0)
145 {
146 build_opts.emplace("-DNON_MULTIPLE_OF_16");
147 }
148
Pablo Palmier48a60f92017-10-18 11:03:08 +0100149 if(beta != 1.0f)
150 {
151 build_opts.emplace(("-DBETA=" + float_to_string_with_full_precision(beta)));
152 }
153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 // Create kernel
155 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts));
156
157 // Set fixed arguments
steniu010d523cc2017-07-13 14:24:23 +0100158 unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0));
160
161 // Configure window
162 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
163
164 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
165 AccessWindowHorizontal max_access(max->info(), 0, 1);
166 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
167 AccessWindowHorizontal sum_access(sum->info(), 0, 1);
168
169 update_window_and_padding(win, input_access, max_access, output_access, sum_access);
170
171 output_access.set_valid_region(win, input->info()->valid_region());
172 sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape()));
173
174 ICLKernel::configure(win);
175}
176
177void CLLogits1DShiftExpSumKernel::run(const Window &window, cl::CommandQueue &queue)
178{
179 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
180 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
181
steniu010d523cc2017-07-13 14:24:23 +0100182 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
183 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
185 do
186 {
187 unsigned int idx = 0;
188 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100189 add_3D_tensor_argument(idx, _input, slice);
190 add_3D_tensor_argument(idx, _max, slice);
191 add_3D_tensor_argument(idx, _output, slice);
192 add_3D_tensor_argument(idx, _sum, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 enqueue(queue, *this, slice);
194 }
steniu010d523cc2017-07-13 14:24:23 +0100195 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196}
197
198CLLogits1DNormKernel::CLLogits1DNormKernel()
199 : _input(nullptr), _sum(nullptr), _output(nullptr)
200{
201}
202
203void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output)
204{
Georgios Pinitas09796752017-07-10 16:05:21 +0100205 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 +0100206 ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output);
207
208 // Output auto initialization if not yet initialized
209 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position());
210
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100211 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output);
Georgios Pinitasd368df32017-07-04 11:06:15 +0100212 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum, output);
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100213 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214
215 _input = input;
216 _sum = sum;
217 _output = output;
218
219 // Set build options
220 std::set<std::string> build_opts;
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100221 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
222 if(is_data_type_fixed_point(input->info()->data_type()))
223 {
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +0100224 build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())));
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100225 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226
227 // Create kernel
228 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts));
229
230 // Configure window
231 constexpr unsigned int num_elems_processed_per_iteration = 16;
232
233 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
234
235 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
236 AccessWindowStatic sum_access(sum->info(), 0, 0, 1, sum->info()->dimension(1));
237 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
238
239 update_window_and_padding(win, input_access, sum_access, output_access);
240
241 output_access.set_valid_region(win, input->info()->valid_region());
242
243 ICLKernel::configure(win);
244}
245
246void CLLogits1DNormKernel::run(const Window &window, cl::CommandQueue &queue)
247{
248 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
249 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
250
steniu010d523cc2017-07-13 14:24:23 +0100251 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
252 Window slice = window_collapsed.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253
254 do
255 {
256 Window sum_slice = slice;
257 sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1));
258
259 unsigned int idx = 0;
260 // Set inputs
steniu010d523cc2017-07-13 14:24:23 +0100261 add_3D_tensor_argument(idx, _input, slice);
262 add_3D_tensor_argument(idx, _sum, sum_slice);
263 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264 enqueue(queue, *this, slice);
265 }
steniu010d523cc2017-07-13 14:24:23 +0100266 while(window_collapsed.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267}