Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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/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 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | void 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 |
| 52 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 53 | |
| 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 | |
| 69 | // Tell the kernel that the width is not a multiple of 4 |
| 70 | if((input->info()->dimension(0) % 4) != 0) |
| 71 | { |
| 72 | build_opts.insert("#define NON_MULTIPLE_OF_4"); |
| 73 | } |
| 74 | |
| 75 | // Create kernel |
| 76 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_max", build_opts)); |
| 77 | |
| 78 | _kernel.clear_params(); |
| 79 | |
| 80 | // Set fixed arguments |
| 81 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
| 82 | _kernel.set_params(idx++, input->info()->dimension(0)); |
| 83 | |
| 84 | // Configure kernel window |
| 85 | // The kernel loops over all elements in steps of 4 |
| 86 | const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 4); |
| 87 | unsigned int num_elems_written_per_iteration = 1; |
| 88 | if(input->info()->data_type() == DataType::F16) |
| 89 | { |
| 90 | num_elems_written_per_iteration = 2; |
| 91 | } |
| 92 | |
| 93 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 94 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 95 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 96 | |
| 97 | update_window_and_padding(win, input_access, output_access); |
| 98 | |
| 99 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 100 | |
| 101 | // set shader params binding point |
| 102 | _kernel.set_shader_params_binding_point(0); |
| 103 | |
| 104 | IGCKernel::configure(win); |
| 105 | } |
| 106 | |
| 107 | void GCLogits1DMaxKernel::run(const Window &window) |
| 108 | { |
| 109 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 110 | |
| 111 | Window slice = window.first_slice_window_3D(); |
| 112 | |
| 113 | _kernel.use(); |
| 114 | |
| 115 | do |
| 116 | { |
| 117 | unsigned int idx1 = 0; |
| 118 | switch(_input->info()->data_type()) |
| 119 | { |
| 120 | case DataType::F16: |
| 121 | add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); |
| 122 | add_3D_tensor_argument(idx1, _output, BufferParam(2, 2), slice); |
| 123 | break; |
| 124 | |
| 125 | case DataType::F32: |
| 126 | add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); |
| 127 | add_3D_tensor_argument(idx1, _output, BufferParam(2, 2), slice); |
| 128 | break; |
| 129 | |
| 130 | default: |
| 131 | ARM_COMPUTE_ERROR("Current data type is mot supported"); |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | _kernel.update_shader_params(); |
| 136 | enqueue(*this, slice); |
| 137 | } |
| 138 | while(window.slide_window_slice_3D(slice)); |
| 139 | } |
| 140 | |
| 141 | GCLogits1DShiftExpSumKernel::GCLogits1DShiftExpSumKernel() |
| 142 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | void GCLogits1DShiftExpSumKernel::configure(const IGCTensor *input, const IGCTensor *max, IGCTensor *output, IGCTensor *sum) |
| 147 | { |
| 148 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 149 | ARM_COMPUTE_ERROR_ON_NULLPTR(max, sum, output); |
| 150 | |
| 151 | // Output auto initialization if not yet initialized |
| 152 | auto_init_if_empty(*sum->info(), max->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 153 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 154 | |
| 155 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, max, sum); |
| 156 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 157 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(max, sum); |
| 158 | |
| 159 | _input = input; |
| 160 | _max = max; |
| 161 | _output = output; |
| 162 | _sum = sum; |
| 163 | |
| 164 | // Set build options |
| 165 | std::set<std::string> build_opts; |
| 166 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 167 | build_opts.insert("#define " + dt_name); |
| 168 | build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); |
| 169 | build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); |
| 170 | build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); |
| 171 | build_opts.insert("#define SOFTMAX_LAYER_SHIFT_EXP_SUM"); |
| 172 | |
| 173 | // Tell the kernel that the width is not a multiple of 4 |
| 174 | if((input->info()->dimension(0) % 4) != 0) |
| 175 | { |
| 176 | build_opts.insert("#define NON_MULTIPLE_OF_4"); |
| 177 | } |
| 178 | |
| 179 | // Create kernel |
| 180 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts)); |
| 181 | |
| 182 | _kernel.clear_params(); |
| 183 | |
| 184 | // Set fixed arguments |
| 185 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
| 186 | _kernel.set_params(idx++, input->info()->dimension(0)); |
| 187 | |
| 188 | // Configure window |
| 189 | // The kernel loops over all elements in steps of 4 |
| 190 | const unsigned int num_elems_processed_per_iteration = ceil_to_multiple(input->info()->dimension(0), 4); |
| 191 | unsigned int num_elems_written_per_iteration = 1; |
| 192 | if(input->info()->data_type() == DataType::F16) |
| 193 | { |
| 194 | num_elems_written_per_iteration = 2; |
| 195 | } |
| 196 | |
| 197 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 198 | |
| 199 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 200 | AccessWindowHorizontal max_access(max->info(), 0, num_elems_written_per_iteration); |
| 201 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 202 | AccessWindowHorizontal sum_access(sum->info(), 0, num_elems_written_per_iteration); |
| 203 | |
| 204 | update_window_and_padding(win, input_access, max_access, output_access, sum_access); |
| 205 | |
| 206 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 207 | sum_access.set_valid_region(win, ValidRegion(Coordinates(), sum->info()->tensor_shape())); |
| 208 | |
| 209 | // set shader params binding point |
| 210 | _kernel.set_shader_params_binding_point(0); |
| 211 | |
| 212 | IGCKernel::configure(win); |
| 213 | } |
| 214 | |
| 215 | void GCLogits1DShiftExpSumKernel::run(const Window &window) |
| 216 | { |
| 217 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 218 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 219 | |
| 220 | Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); |
| 221 | Window slice = window_collapsed.first_slice_window_3D(); |
| 222 | |
| 223 | _kernel.use(); |
| 224 | |
| 225 | do |
| 226 | { |
| 227 | unsigned int idx = 0; |
| 228 | switch(_input->info()->data_type()) |
| 229 | { |
| 230 | case DataType::F16: |
| 231 | add_3D_tensor_argument(idx, _input, BufferParam(1, 2), slice); |
| 232 | add_3D_tensor_argument(idx, _max, BufferParam(2, 2), slice); |
| 233 | add_3D_tensor_argument(idx, _output, BufferParam(3, 2), slice); |
| 234 | add_3D_tensor_argument(idx, _sum, BufferParam(4, 2), slice); |
| 235 | break; |
| 236 | |
| 237 | case DataType::F32: |
| 238 | add_3D_tensor_argument(idx, _input, BufferParam(1, 2), slice); |
| 239 | add_3D_tensor_argument(idx, _max, BufferParam(2, 2), slice); |
| 240 | add_3D_tensor_argument(idx, _output, BufferParam(3, 2), slice); |
| 241 | add_3D_tensor_argument(idx, _sum, BufferParam(4, 2), slice); |
| 242 | break; |
| 243 | |
| 244 | default: |
| 245 | ARM_COMPUTE_ERROR("Current data type is mot supported"); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | _kernel.update_shader_params(); |
| 250 | enqueue(*this, slice); |
| 251 | } |
| 252 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 253 | } |
| 254 | |
| 255 | GCLogits1DNormKernel::GCLogits1DNormKernel() |
| 256 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 257 | { |
| 258 | } |
| 259 | |
| 260 | void GCLogits1DNormKernel::configure(const IGCTensor *input, const IGCTensor *sum, IGCTensor *output) |
| 261 | { |
| 262 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 263 | ARM_COMPUTE_ERROR_ON_NULLPTR(sum, output); |
| 264 | |
| 265 | // Output auto initialization if not yet initialized |
| 266 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 267 | |
| 268 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output); |
| 269 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, sum, output); |
| 270 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 271 | |
| 272 | _input = input; |
| 273 | _sum = sum; |
| 274 | _output = output; |
| 275 | |
| 276 | // Set build options |
| 277 | std::set<std::string> build_opts; |
| 278 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 279 | build_opts.insert("#define " + dt_name); |
| 280 | build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); |
| 281 | build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); |
| 282 | build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); |
| 283 | build_opts.insert("#define SOFTMAX_LAYER_NORM"); |
| 284 | |
| 285 | // Create kernel |
| 286 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_norm", build_opts)); |
| 287 | |
| 288 | // Configure window |
| 289 | constexpr unsigned int num_elems_processed_per_iteration = 4; |
| 290 | unsigned int num_elems_written_per_iteration = 1; |
| 291 | if(input->info()->data_type() == DataType::F16) |
| 292 | { |
| 293 | num_elems_written_per_iteration = 2; |
| 294 | } |
| 295 | |
| 296 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 297 | |
| 298 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 299 | AccessWindowStatic sum_access(sum->info(), 0, 0, num_elems_written_per_iteration, sum->info()->dimension(1)); |
| 300 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 301 | |
| 302 | update_window_and_padding(win, input_access, sum_access, output_access); |
| 303 | |
| 304 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 305 | |
| 306 | _kernel.clear_params(); |
| 307 | |
| 308 | // set shader params binding point |
| 309 | _kernel.set_shader_params_binding_point(0); |
| 310 | |
| 311 | IGCKernel::configure(win); |
| 312 | } |
| 313 | |
| 314 | void GCLogits1DNormKernel::run(const Window &window) |
| 315 | { |
| 316 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 317 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 318 | |
| 319 | Window window_collapsed = window.collapse_if_possible(IGCKernel::window(), Window::DimZ); |
| 320 | Window slice = window_collapsed.first_slice_window_3D(); |
| 321 | |
| 322 | _kernel.use(); |
| 323 | |
| 324 | do |
| 325 | { |
| 326 | Window sum_slice = slice; |
| 327 | sum_slice.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 328 | |
| 329 | unsigned int idx1 = 0; |
| 330 | switch(_input->info()->data_type()) |
| 331 | { |
| 332 | case DataType::F16: |
| 333 | add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); |
| 334 | add_3D_tensor_argument(idx1, _sum, BufferParam(2, 2), slice); |
| 335 | add_3D_tensor_argument(idx1, _output, BufferParam(3, 2), slice); |
| 336 | break; |
| 337 | |
| 338 | case DataType::F32: |
| 339 | add_3D_tensor_argument(idx1, _input, BufferParam(1, 2), slice); |
| 340 | add_3D_tensor_argument(idx1, _sum, BufferParam(2, 2), slice); |
| 341 | add_3D_tensor_argument(idx1, _output, BufferParam(3, 2), slice); |
| 342 | break; |
| 343 | |
| 344 | default: |
| 345 | ARM_COMPUTE_ERROR("Current data type is mot supported"); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | _kernel.update_shader_params(); |
| 350 | enqueue(*this, slice); |
| 351 | } |
| 352 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 353 | } |