Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/CL/kernels/CLBatchNormalizationLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/FixedPoint.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 36 | #include "support/ToolchainSupport.h" |
| 37 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | using namespace arm_compute; |
| 39 | |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, |
| 43 | const ITensorInfo *mean, const ITensorInfo *var, |
| 44 | const ITensorInfo *beta, const ITensorInfo *gamma, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 45 | float epsilon, ActivationLayerInfo act_info) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 46 | { |
| 47 | ARM_COMPUTE_UNUSED(epsilon); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(mean, var, beta, gamma); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, mean, var, beta, gamma); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, mean, var, beta, gamma); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != mean->dimension(0)); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 53 | if(act_info.enabled()) |
| 54 | { |
| 55 | ActivationLayerInfo::ActivationFunction act = act_info.activation(); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32 && input->data_type() != DataType::F16); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::RELU && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU |
| 58 | && act != ActivationLayerInfo::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(act_info.b() > act_info.a()); |
| 60 | } |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 61 | |
| 62 | if(output != nullptr && output->total_size() != 0) |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 67 | } |
| 68 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 69 | return Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 72 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 73 | { |
Giorgio Arena | f6a43c5 | 2017-12-01 12:16:25 +0000 | [diff] [blame] | 74 | if(output != nullptr) |
| 75 | { |
| 76 | // Output tensor auto initialization if not yet initialized |
| 77 | auto_init_if_empty(*output, *input->clone()); |
| 78 | } |
| 79 | |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 80 | const unsigned int num_elems_processed_per_iteration = 16 / input->element_size(); |
| 81 | |
| 82 | // Configure kernel window |
| 83 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 84 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 85 | |
Giorgio Arena | 2995f5b | 2017-11-29 17:33:59 +0000 | [diff] [blame] | 86 | bool window_changed = false; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 87 | if(output != nullptr) |
| 88 | { |
| 89 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 90 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 91 | output_access.set_valid_region(win, input->valid_region()); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | window_changed = update_window_and_padding(win, input_access); |
| 96 | } |
| 97 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 98 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 99 | return std::make_pair(err, win); |
| 100 | } |
| 101 | } // namespace |
| 102 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | CLBatchNormalizationLayerKernel::CLBatchNormalizationLayerKernel() |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 104 | : _input(nullptr), _output(nullptr), _mean(nullptr), _var(nullptr), _beta(nullptr), _gamma(nullptr), _epsilon(0), _run_in_place(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | { |
| 106 | } |
| 107 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 108 | void CLBatchNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const ICLTensor *mean, const ICLTensor *var, const ICLTensor *beta, const ICLTensor *gamma, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 109 | float epsilon, ActivationLayerInfo act_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | { |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, var, beta, gamma); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | _input = input; |
| 114 | _output = output; |
| 115 | _mean = mean; |
| 116 | _var = var; |
| 117 | _beta = beta; |
| 118 | _gamma = gamma; |
| 119 | _epsilon = epsilon; |
| 120 | |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 121 | _run_in_place = (output == nullptr) || (output == input); |
| 122 | |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 123 | if(output != nullptr) |
| 124 | { |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 125 | ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), output->info()); |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 126 | // Output tensor auto initialization if not yet initialized |
Giorgio Arena | f6a43c5 | 2017-12-01 12:16:25 +0000 | [diff] [blame] | 127 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Giorgio Arena | f6a43c5 | 2017-12-01 12:16:25 +0000 | [diff] [blame] | 130 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 131 | mean->info(), var->info(), beta->info(), gamma->info(), epsilon, act_info)); |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 132 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 133 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
| 134 | |
| 135 | // Set build options |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 136 | CLBuildOptions build_opts; |
| 137 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 138 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 139 | build_opts.add_option_if(act_info.enabled(), "-DFUSED_ACTIVATION=" + lower_string(string_from_activation_func(act_info.activation()))); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 140 | build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a())); |
| 141 | build_opts.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b())); |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 142 | build_opts.add_option_if(_run_in_place, "-DIN_PLACE"); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 143 | build_opts.add_option_if(is_data_type_fixed_point(input->info()->data_type()), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 144 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | // Create kernel |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 146 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("batchnormalization_layer", build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | |
| 148 | // Set kernel static arguments |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 149 | unsigned int include_output = (!_run_in_place) ? 1 : 0; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 150 | unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor() + 4 * num_arguments_per_1D_tensor(); // Skip the input and output parameters |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | _kernel.setArg<cl_float>(idx++, _epsilon); |
| 152 | |
| 153 | // Configure kernel window |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 154 | auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info()); |
Giorgio Arena | 7062382 | 2017-11-27 15:50:10 +0000 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 156 | ICLKernel::configure(win_config.second); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 157 | |
| 158 | _config_id = "batch_normalization_layer_"; |
| 159 | _config_id += string_from_data_type(input->info()->data_type()); |
| 160 | _config_id += "_"; |
| 161 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 162 | _config_id += "_"; |
| 163 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 164 | _config_id += "_"; |
| 165 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 168 | Status CLBatchNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, |
| 169 | const ITensorInfo *mean, const ITensorInfo *var, |
| 170 | const ITensorInfo *beta, const ITensorInfo *gamma, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 171 | float epsilon, ActivationLayerInfo act_info) |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 172 | { |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 173 | const bool run_in_place = (output == nullptr) || (output == input); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 174 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mean, var, beta, gamma, epsilon, act_info)); |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 175 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (run_in_place) ? nullptr : output->clone().get()).first); |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 176 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 177 | return Status{}; |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | void CLBatchNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 181 | { |
| 182 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 183 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 184 | |
| 185 | Window slice = window.first_slice_window_3D(); |
| 186 | |
| 187 | Window vector_slice = window.first_slice_window_1D(); |
| 188 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 189 | |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 190 | unsigned int include_output = (!_run_in_place) ? 1 : 0; |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 191 | unsigned int idx = (1 + include_output) * num_arguments_per_3D_tensor(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | add_1D_tensor_argument(idx, _mean, vector_slice); |
| 193 | add_1D_tensor_argument(idx, _var, vector_slice); |
| 194 | add_1D_tensor_argument(idx, _beta, vector_slice); |
| 195 | add_1D_tensor_argument(idx, _gamma, vector_slice); |
| 196 | |
| 197 | do |
| 198 | { |
| 199 | idx = 0; |
| 200 | add_3D_tensor_argument(idx, _input, slice); |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 201 | if(!_run_in_place) |
Georgios Pinitas | 409ee0a | 2017-08-18 10:16:09 +0100 | [diff] [blame] | 202 | { |
| 203 | add_3D_tensor_argument(idx, _output, slice); |
| 204 | } |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 205 | enqueue(queue, *this, slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 206 | } |
| 207 | while(window.slide_window_slice_3D(slice)); |
| 208 | } |