Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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/GCBatchNormalizationLayerKernel.h" |
| 25 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 27 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 28 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/AccessWindowStatic.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 36 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
| 43 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, |
| 44 | const ITensorInfo *mean, const ITensorInfo *var, |
| 45 | const ITensorInfo *beta, const ITensorInfo *gamma, |
| 46 | float epsilon, ActivationLayerInfo act_info) |
| 47 | { |
| 48 | ARM_COMPUTE_UNUSED(epsilon); |
| 49 | ARM_COMPUTE_UNUSED(var); |
| 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 51 | |
| 52 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(mean, var); |
| 54 | |
| 55 | if(output->total_size() != 0) |
| 56 | { |
| 57 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 58 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if(beta != nullptr) |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, beta); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, beta); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 65 | } |
| 66 | if(gamma != nullptr) |
| 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, gamma); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, gamma); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 70 | } |
| 71 | if(act_info.enabled()) |
| 72 | { |
| 73 | ARM_COMPUTE_ERROR_ON(input->data_type() != DataType::F32 && input->data_type() != DataType::F16); |
| 74 | ARM_COMPUTE_ERROR_ON(act_info.activation() != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU |
| 75 | && act_info.activation() != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU |
| 76 | && act_info.activation() != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU); |
| 77 | ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a()); |
| 78 | } |
| 79 | return Status{}; |
| 80 | } |
| 81 | |
| 82 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, |
| 83 | ITensorInfo *mean, ITensorInfo *var, |
| 84 | ITensorInfo *beta, ITensorInfo *gamma) |
| 85 | { |
| 86 | // Output tensor auto initialization if not yet initialized |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 87 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 88 | |
| 89 | unsigned int num_elems_processed_per_iteration = 1; |
| 90 | if(input->data_type() == DataType::F16) |
| 91 | { |
| 92 | num_elems_processed_per_iteration = 4; |
| 93 | } |
| 94 | |
| 95 | // Configure kernel window |
| 96 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 97 | |
| 98 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 99 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 100 | AccessWindowStatic mean_access(mean, 0, 0, mean->dimension(0) + 3, mean->dimension(1)); |
| 101 | AccessWindowStatic var_access(var, 0, 0, var->dimension(0) + 3, var->dimension(1)); |
| 102 | |
| 103 | bool window_changed = false; |
| 104 | if(beta != nullptr) |
| 105 | { |
| 106 | AccessWindowStatic beta_access(beta, 0, 0, beta->dimension(0) + 3, beta->dimension(1)); |
| 107 | if(gamma != nullptr) |
| 108 | { |
| 109 | AccessWindowStatic gamma_access(gamma, 0, 0, gamma->dimension(0) + 3, gamma->dimension(1)); |
| 110 | window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access, beta_access, gamma_access); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access, beta_access); |
| 115 | } |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | if(gamma != nullptr) |
| 120 | { |
| 121 | AccessWindowStatic gamma_access(gamma, 0, 0, gamma->dimension(0) + 3, gamma->dimension(1)); |
| 122 | window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access, gamma_access); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | window_changed = update_window_and_padding(win, input_access, output_access, mean_access, var_access); |
| 127 | } |
| 128 | } |
| 129 | output_access.set_valid_region(win, input->valid_region()); |
| 130 | |
| 131 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 132 | return std::make_pair(err, win); |
| 133 | } |
| 134 | } // namespace |
| 135 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 136 | GCBatchNormalizationLayerKernel::GCBatchNormalizationLayerKernel() |
| 137 | : _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _beta(nullptr), _gamma(nullptr), _epsilon(0.0f) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | void GCBatchNormalizationLayerKernel::configure(const IGCTensor *input, IGCTensor *output, const IGCTensor *mean, const IGCTensor *var, const IGCTensor *beta, const IGCTensor *gamma, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 142 | float epsilon, ActivationLayerInfo act_info) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 143 | { |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 144 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, mean, var); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 145 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 146 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mean->info(), var->info(), |
| 147 | (beta != nullptr) ? beta->info() : nullptr, (gamma != nullptr) ? gamma->info() : nullptr, |
| 148 | epsilon, act_info)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 149 | |
| 150 | _input = input; |
| 151 | _output = output; |
| 152 | _mean = mean; |
| 153 | _var = var; |
| 154 | _beta = beta; |
| 155 | _gamma = gamma; |
| 156 | _epsilon = epsilon; |
| 157 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 158 | // Set build options |
| 159 | std::set<std::string> build_opts; |
| 160 | std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16"; |
| 161 | build_opts.emplace(("#define " + dt_name)); |
| 162 | build_opts.emplace(("#define ESPILON " + float_to_string_with_full_precision(_epsilon))); |
| 163 | build_opts.emplace(("#define LOCAL_SIZE_X " + support::cpp11::to_string(1))); |
| 164 | build_opts.emplace(("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1))); |
| 165 | build_opts.emplace(("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1))); |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 166 | if(beta == nullptr) |
| 167 | { |
| 168 | build_opts.emplace("#define USE_DEFAULT_BETA"); |
| 169 | } |
| 170 | if(gamma == nullptr) |
| 171 | { |
| 172 | build_opts.emplace("#define USE_DEFAULT_GAMMA"); |
| 173 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 174 | |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 175 | if(act_info.enabled()) |
| 176 | { |
| 177 | build_opts.emplace("#define " + string_from_activation_func(act_info.activation())); |
| 178 | build_opts.emplace("#define A_VAL " + float_to_string_with_full_precision(act_info.a())); |
| 179 | build_opts.emplace("#define B_VAL " + float_to_string_with_full_precision(act_info.b())); |
| 180 | } |
| 181 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 182 | // Create kernel |
| 183 | _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("batchnormalization_layer", build_opts)); |
| 184 | |
| 185 | // Configure kernel window |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 186 | auto win_config = validate_and_configure_window(input->info(), output->info(), mean->info(), var->info(), |
| 187 | (beta != nullptr) ? beta->info() : nullptr, (gamma != nullptr) ? gamma->info() : nullptr); |
| 188 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 189 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 190 | IGCKernel::configure(win_config.second); |
| 191 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 192 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 193 | Status GCBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 194 | const ITensorInfo *mean, const ITensorInfo *var, |
| 195 | const ITensorInfo *beta, const ITensorInfo *gamma, |
| 196 | float epsilon, ActivationLayerInfo act_info) |
| 197 | { |
| 198 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info)); |
| 199 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), |
| 200 | mean->clone().get(), var->clone().get(), |
| 201 | beta->clone().get(), gamma->clone().get()) |
| 202 | .first); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 203 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 204 | return Status{}; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void GCBatchNormalizationLayerKernel::run(const Window &window) |
| 208 | { |
| 209 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 210 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 211 | |
| 212 | _kernel.use(); |
| 213 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 214 | _output->set_needs_shifting(true); |
| 215 | |
| 216 | Window slice = window.first_slice_window_3D(); |
| 217 | Window slice_in = window.first_slice_window_3D(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 218 | |
| 219 | Window vector_slice = window.first_slice_window_1D(); |
| 220 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 221 | |
Michele Di Giorgio | 4d33630 | 2018-03-02 09:43:54 +0000 | [diff] [blame] | 222 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); |
| 223 | unsigned int binding_point = 3; |
| 224 | add_1D_tensor_argument(idx, _mean, binding_point, vector_slice); |
| 225 | add_1D_tensor_argument(idx, _var, ++binding_point, vector_slice); |
| 226 | if(_beta != nullptr) |
| 227 | { |
| 228 | add_1D_tensor_argument(idx, _beta, ++binding_point, vector_slice); |
| 229 | } |
| 230 | if(_gamma != nullptr) |
| 231 | { |
| 232 | add_1D_tensor_argument(idx, _gamma, ++binding_point, vector_slice); |
| 233 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 234 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 235 | slice.shift(Window::DimX, -(_output->info()->padding()).left); |
| 236 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 237 | do |
| 238 | { |
| 239 | idx = 0; |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 240 | add_3D_tensor_argument(idx, _input, 1, slice_in); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 241 | add_3D_tensor_argument(idx, _output, 2, slice); |
| 242 | |
| 243 | _kernel.update_shader_params(); |
| 244 | enqueue(*this, slice); |
| 245 | } |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 246 | while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_in)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 247 | } |