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