Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 52 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | |
Joel Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 69 | // Tell the kernel that the width is not a multiple of 8 |
| 70 | if((input->info()->dimension(0) % 8) != 0) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 71 | { |
Joel Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 72 | build_opts.insert("#define NON_MULTIPLE_OF_8"); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Create kernel |
| 76 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_max", build_opts)); |
| 77 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 78 | // Set fixed arguments |
| 79 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 80 | _kernel.set_argument(idx++, input->info()->dimension(0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 81 | |
| 82 | // Configure kernel window |
Joel Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 83 | // 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 85 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 99 | IGCKernel::configure(win); |
| 100 | } |
| 101 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 102 | GCLogits1DShiftExpSumKernel::GCLogits1DShiftExpSumKernel() |
| 103 | : _input(nullptr), _max(nullptr), _output(nullptr), _sum(nullptr) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | void 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 Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 113 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 115 | |
| 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 Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 134 | // Tell the kernel that the width is not a multiple of 8 |
| 135 | if((input->info()->dimension(0) % 8) != 0) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 136 | { |
Joel Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 137 | build_opts.insert("#define NON_MULTIPLE_OF_8"); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Create kernel |
| 141 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("softmax_layer_shift_exp_sum", build_opts)); |
| 142 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 143 | // Set fixed arguments |
| 144 | unsigned int idx = 4 * num_arguments_per_3D_tensor(); //Skip the input and output parameters |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 145 | _kernel.set_argument(idx++, input->info()->dimension(0)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 146 | |
| 147 | // Configure window |
Joel Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 148 | // 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 150 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 168 | IGCKernel::configure(win); |
| 169 | } |
| 170 | |
| 171 | void 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 Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 183 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 190 | _kernel.update_shader_params(); |
| 191 | enqueue(*this, slice); |
| 192 | } |
| 193 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 194 | } |
| 195 | |
| 196 | GCLogits1DNormKernel::GCLogits1DNormKernel() |
| 197 | : _input(nullptr), _sum(nullptr), _output(nullptr) |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | void 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 Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 207 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 208 | |
| 209 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, sum, output); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 210 | 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 Liang | 07c37f9 | 2017-11-17 11:34:19 +0800 | [diff] [blame] | 229 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 230 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 246 | IGCKernel::configure(win); |
| 247 | } |
| 248 | |
| 249 | void 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 Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 264 | 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 270 | |
| 271 | _kernel.update_shader_params(); |
| 272 | enqueue(*this, slice); |
| 273 | } |
| 274 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 275 | } |