Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | void CLLogits1DMaxKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 43 | { |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame] | 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 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 |
| 52 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 53 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | |
| 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 Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 65 | 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 Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 69 | build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()))); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 70 | } |
Moritz Pflanzer | ba9b3f5 | 2017-07-27 12:45:30 +0100 | [diff] [blame] | 71 | else if(input->info()->data_type() == DataType::F16) |
| 72 | { |
| 73 | build_opts.emplace("-DUSE_F16"); |
| 74 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
| 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 |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 86 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | _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 | |
| 103 | CLLogits1DShiftExpSumKernel::CLLogits1DShiftExpSumKernel() |
| 104 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 105 | { |
| 106 | } |
| 107 | |
Pablo Palmier | 48a60f9 | 2017-10-18 11:03:08 +0100 | [diff] [blame] | 108 | void CLLogits1DShiftExpSumKernel::configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | { |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 111 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output, max, sum); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 120 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | |
| 122 | _input = input; |
| 123 | _max = max; |
| 124 | _output = output; |
| 125 | _sum = sum; |
| 126 | |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame^] | 127 | const DataType dt = input->info()->data_type(); |
| 128 | auto beta_int = static_cast<int>(lround(beta * (1 << input->info()->fixed_point_position()))); |
| 129 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | // 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 Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame^] | 134 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | // Tell the kernel that the width is not a multiple of 16 |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame^] | 140 | 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 Palmier | 48a60f9 | 2017-10-18 11:03:08 +0100 | [diff] [blame] | 143 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | // Create kernel |
Georgios Pinitas | 388d3ec | 2017-11-02 12:17:56 +0000 | [diff] [blame^] | 145 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | |
| 147 | // Set fixed arguments |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 148 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | _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 | |
| 167 | void 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 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 172 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 173 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | |
| 175 | do |
| 176 | { |
| 177 | unsigned int idx = 0; |
| 178 | // Set inputs |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 179 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | enqueue(queue, *this, slice); |
| 184 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 185 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | CLLogits1DNormKernel::CLLogits1DNormKernel() |
| 189 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | void CLLogits1DNormKernel::configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output) |
| 194 | { |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame] | 195 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 196 | 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 Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 201 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output); |
Georgios Pinitas | d368df3 | 2017-07-04 11:06:15 +0100 | [diff] [blame] | 202 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum, output); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 203 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | |
| 205 | _input = input; |
| 206 | _sum = sum; |
| 207 | _output = output; |
| 208 | |
| 209 | // Set build options |
| 210 | std::set<std::string> build_opts; |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 211 | 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 Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 214 | build_opts.emplace(("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()))); |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 215 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | |
| 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 | |
| 236 | void 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 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 241 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 242 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | |
| 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 |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 251 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 254 | enqueue(queue, *this, slice); |
| 255 | } |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 256 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 257 | } |