Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLInstanceNormalizationLayerKernel.h" |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 43 | Status validate_arguments(const ITensorInfo *input, |
| 44 | const ITensorInfo *output, |
| 45 | const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 46 | { |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.epsilon == 0.f, "Epsilon must be different than 0"); |
Manuel Bottini | 581f178 | 2019-11-13 17:24:43 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 49 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | if (output != nullptr && output->total_size() != 0) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), |
| 56 | "Input and output have different number of channels"); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return Status{}; |
| 60 | } |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 61 | |
| 62 | Status validate_arguments_meanvar(const ITensorInfo *input, const ITensorInfo *output) |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32); |
| 65 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | if (output != nullptr && output->total_size() != 0) |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), |
| 71 | "Input and output have different number of channels"); |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return Status{}; |
| 75 | } |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 76 | } // namespace |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | CLComputeMeanVariance::CLComputeMeanVariance() : _input(nullptr), _output(nullptr) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 79 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 80 | _type = CLKernelType::ELEMENTWISE; |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | void CLComputeMeanVariance::configure(const CLCompileContext &compile_context, |
| 84 | ICLTensor *input, |
| 85 | ICLTensor *output, |
| 86 | bool use_mixed_precision) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 87 | { |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | auto padding_info = get_padding_info({input, output}); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 90 | |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 91 | _input = input; |
| 92 | _output = output == nullptr ? input : output; |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 93 | |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 94 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_meanvar(_input->info(), _output->info())); |
| 95 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
| 96 | |
| 97 | CLBuildOptions build_opts; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | build_opts.add_option("-DINTERNAL_DATA_TYPE=" + |
| 99 | (use_mixed_precision ? "float" : get_cl_type_from_data_type(input->info()->data_type()))); |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 100 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 101 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 102 | build_opts.add_option("-DDIM_X=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 103 | build_opts.add_option("-DDIM_Y=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 104 | build_opts.add_option("-DDIM_Z=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 105 | build_opts.add_option_if(_input->info()->data_layout() == DataLayout::NHWC, "-DNHWC"); |
| 106 | // Create kernel |
| 107 | _kernel = create_kernel(compile_context, "compute_mean_var", build_opts.options()); |
| 108 | |
| 109 | // We handle the planes manually |
| 110 | Window win = calculate_max_window(*(input->info()), Steps(1)); |
| 111 | const auto data_layout = input->info()->data_layout(); |
| 112 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 113 | const unsigned int batches_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 114 | const unsigned int input_channel = input->info()->dimension(channel_idx); |
| 115 | const unsigned int input_batches = input->info()->dimension(batches_idx); |
| 116 | const TensorShape out_shape(input_channel, 2u, input_batches); |
| 117 | |
| 118 | // Output auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | if (use_mixed_precision) |
Pablo Tello | 5c3eeec | 2021-04-26 15:39:05 +0100 | [diff] [blame] | 120 | { |
Michalis Spyrou | 7f8caf7 | 2021-05-13 13:35:30 +0100 | [diff] [blame] | 121 | auto_init_if_empty(*_output->info(), out_shape, 1, DataType::F32); |
Pablo Tello | 5c3eeec | 2021-04-26 15:39:05 +0100 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
Michalis Spyrou | 7f8caf7 | 2021-05-13 13:35:30 +0100 | [diff] [blame] | 125 | auto_init_if_empty(*_output->info(), out_shape, 1, input->info()->data_type()); |
Pablo Tello | 5c3eeec | 2021-04-26 15:39:05 +0100 | [diff] [blame] | 126 | } |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 127 | ICLKernel::configure_internal(win); |
| 128 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
| 129 | } |
| 130 | |
| 131 | Status CLComputeMeanVariance::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 132 | { |
| 133 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_meanvar(input, output)); |
| 134 | return Status{}; |
| 135 | } |
| 136 | |
| 137 | void CLComputeMeanVariance::run(const Window &window, cl::CommandQueue &queue) |
| 138 | { |
| 139 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 140 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 141 | |
| 142 | Window collapsed_window = window.collapse(window, Window::DimZ); |
| 143 | |
| 144 | // We will process the planes together |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | if (_input->info()->data_layout() == DataLayout::NCHW) |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 146 | { |
| 147 | collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 148 | collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | collapsed_window.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 153 | collapsed_window.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(3), 1)); |
| 154 | } |
| 155 | unsigned int idx = 0; |
| 156 | add_4D_tensor_argument(idx, _input, collapsed_window); |
| 157 | add_3D_tensor_argument(idx, _output, collapsed_window); |
| 158 | |
| 159 | enqueue(queue, *this, collapsed_window, lws_hint()); |
| 160 | } |
| 161 | |
| 162 | CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel() |
| 163 | : _input(nullptr), _output(nullptr), _mean(nullptr), _run_in_place(false) |
| 164 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 165 | _type = CLKernelType::ELEMENTWISE; |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 168 | void CLInstanceNormalizationLayerKernel::configure(const CLCompileContext &compile_context, |
| 169 | ICLTensor *input, |
| 170 | ICLTensor *mean_var, |
| 171 | ICLTensor *output, |
| 172 | const InstanceNormalizationLayerKernelInfo &info) |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 173 | { |
| 174 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 175 | auto padding_info = get_padding_info({input, output}); |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 176 | |
| 177 | _input = input; |
| 178 | _output = output == nullptr ? input : output; |
| 179 | _mean = mean_var; |
| 180 | |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 181 | _run_in_place = (output == nullptr) || (output == input); |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 182 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info)); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 183 | const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size(); |
| 184 | |
| 185 | CLBuildOptions build_opts; |
| 186 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | build_opts.add_option("-DINTERNAL_DATA_TYPE=" + (info.use_mixed_precision |
| 188 | ? "float" |
| 189 | : get_cl_type_from_data_type(input->info()->data_type()))); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 190 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 191 | build_opts.add_option("-DDIM_X=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 192 | build_opts.add_option("-DDIM_Y=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 193 | build_opts.add_option("-DDIM_Z=" + support::cpp11::to_string(input->info()->dimension(2))); |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 194 | build_opts.add_option("-DGAMMA=" + float_to_string_with_full_precision(info.gamma)); |
| 195 | build_opts.add_option("-DBETA=" + float_to_string_with_full_precision(info.beta)); |
| 196 | build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(info.epsilon)); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 197 | build_opts.add_option_if(_run_in_place, "-DIN_PLACE"); |
| 198 | build_opts.add_option_if(_input->info()->data_layout() == DataLayout::NHWC, "-DNHWC"); |
| 199 | |
| 200 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 201 | _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options()); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 202 | |
| 203 | // Configure kernel window |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 204 | Window win = calculate_max_window(*input->info(), Steps(1)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | if (output != nullptr) |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 206 | { |
| 207 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type()); |
| 208 | } |
| 209 | |
| 210 | ICLKernel::configure_internal(win); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 211 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 214 | Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, |
| 215 | const ITensorInfo *output, |
| 216 | const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 217 | { |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 218 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info)); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 219 | return Status{}; |
| 220 | } |
| 221 | |
| 222 | void CLInstanceNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 223 | { |
| 224 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 225 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 226 | |
| 227 | Window collapsed_window = window.collapse(window, Window::DimZ); |
| 228 | |
| 229 | // We will process the planes together |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 230 | if (_input->info()->data_layout() == DataLayout::NCHW) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 231 | { |
| 232 | collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 233 | collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 238 | collapsed_window.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(3), 1)); |
| 239 | } |
| 240 | |
| 241 | unsigned int idx = 0; |
| 242 | add_4D_tensor_argument(idx, _input, collapsed_window); |
Pablo Marquez Tello | fe7ae81 | 2021-03-03 12:12:35 +0000 | [diff] [blame] | 243 | add_3D_tensor_argument(idx, _mean, collapsed_window); |
| 244 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 245 | if (!_run_in_place) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 246 | { |
| 247 | add_4D_tensor_argument(idx, _output, collapsed_window); |
| 248 | } |
| 249 | |
| 250 | enqueue(queue, *this, collapsed_window, lws_hint()); |
| 251 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 252 | } // namespace arm_compute |