blob: b13eb1655654678d1906a6eefcb3f8ef420a510b [file] [log] [blame]
Manuel Bottini79f88e62019-09-18 15:02:53 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2019-2021, 2023 Arm Limited.
Manuel Bottini79f88e62019-09-18 15:02:53 +01003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLInstanceNormalizationLayerKernel.h"
Manuel Bottini79f88e62019-09-18 15:02:53 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Manuel Bottini79f88e62019-09-18 15:02:53 +010028#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 Bentham314d3e22023-06-23 10:53:52 +000032#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/CL/CLValidate.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Manuel Bottini79f88e62019-09-18 15:02:53 +010038
39namespace arm_compute
40{
41namespace
42{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043Status validate_arguments(const ITensorInfo *input,
44 const ITensorInfo *output,
45 const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +010046{
Georgios Pinitas55a687d2020-01-30 12:00:23 +000047 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.epsilon == 0.f, "Epsilon must be different than 0");
Manuel Bottini581f1782019-11-13 17:24:43 +000048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32);
Manuel Bottini79f88e62019-09-18 15:02:53 +010049
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 if (output != nullptr && output->total_size() != 0)
Manuel Bottini79f88e62019-09-18 15:02:53 +010051 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(),
56 "Input and output have different number of channels");
Manuel Bottini79f88e62019-09-18 15:02:53 +010057 }
58
59 return Status{};
60}
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000061
62Status 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 if (output != nullptr && output->total_size() != 0)
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000067 {
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(),
71 "Input and output have different number of channels");
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000072 }
73
74 return Status{};
75}
Manuel Bottini79f88e62019-09-18 15:02:53 +010076} // namespace
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078CLComputeMeanVariance::CLComputeMeanVariance() : _input(nullptr), _output(nullptr)
Manuel Bottini79f88e62019-09-18 15:02:53 +010079{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010080 _type = CLKernelType::ELEMENTWISE;
Manuel Bottini79f88e62019-09-18 15:02:53 +010081}
82
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083void CLComputeMeanVariance::configure(const CLCompileContext &compile_context,
84 ICLTensor *input,
85 ICLTensor *output,
86 bool use_mixed_precision)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010087{
Manuel Bottini79f88e62019-09-18 15:02:53 +010088 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 auto padding_info = get_padding_info({input, output});
Manuel Bottini79f88e62019-09-18 15:02:53 +010090
Georgios Pinitas55a687d2020-01-30 12:00:23 +000091 _input = input;
92 _output = output == nullptr ? input : output;
Manuel Bottini79f88e62019-09-18 15:02:53 +010093
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000094 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 build_opts.add_option("-DINTERNAL_DATA_TYPE=" +
99 (use_mixed_precision ? "float" : get_cl_type_from_data_type(input->info()->data_type())));
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000100 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 if (use_mixed_precision)
Pablo Tello5c3eeec2021-04-26 15:39:05 +0100120 {
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100121 auto_init_if_empty(*_output->info(), out_shape, 1, DataType::F32);
Pablo Tello5c3eeec2021-04-26 15:39:05 +0100122 }
123 else
124 {
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100125 auto_init_if_empty(*_output->info(), out_shape, 1, input->info()->data_type());
Pablo Tello5c3eeec2021-04-26 15:39:05 +0100126 }
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000127 ICLKernel::configure_internal(win);
128 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
129}
130
131Status 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
137void 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 if (_input->info()->data_layout() == DataLayout::NCHW)
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000146 {
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
162CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel()
163 : _input(nullptr), _output(nullptr), _mean(nullptr), _run_in_place(false)
164{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100165 _type = CLKernelType::ELEMENTWISE;
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000166}
167
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168void CLInstanceNormalizationLayerKernel::configure(const CLCompileContext &compile_context,
169 ICLTensor *input,
170 ICLTensor *mean_var,
171 ICLTensor *output,
172 const InstanceNormalizationLayerKernelInfo &info)
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000173{
174 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 auto padding_info = get_padding_info({input, output});
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000176
177 _input = input;
178 _output = output == nullptr ? input : output;
179 _mean = mean_var;
180
Manuel Bottini79f88e62019-09-18 15:02:53 +0100181 _run_in_place = (output == nullptr) || (output == input);
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000182 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100183 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 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 Bottini79f88e62019-09-18 15:02:53 +0100190 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 Pinitas55a687d2020-01-30 12:00:23 +0000194 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 Bottini79f88e62019-09-18 15:02:53 +0100197 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 Bottini4c6bd512020-04-08 10:15:51 +0100201 _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options());
Manuel Bottini79f88e62019-09-18 15:02:53 +0100202
203 // Configure kernel window
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000204 Window win = calculate_max_window(*input->info(), Steps(1));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 if (output != nullptr)
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000206 {
207 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, input->info()->data_type());
208 }
209
210 ICLKernel::configure_internal(win);
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000211 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100212}
213
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input,
215 const ITensorInfo *output,
216 const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +0100217{
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000218 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100219 return Status{};
220}
221
222void 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100230 if (_input->info()->data_layout() == DataLayout::NCHW)
Manuel Bottini79f88e62019-09-18 15:02:53 +0100231 {
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 Tellofe7ae812021-03-03 12:12:35 +0000243 add_3D_tensor_argument(idx, _mean, collapsed_window);
244
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100245 if (!_run_in_place)
Manuel Bottini79f88e62019-09-18 15:02:53 +0100246 {
247 add_4D_tensor_argument(idx, _output, collapsed_window);
248 }
249
250 enqueue(queue, *this, collapsed_window, lws_hint());
251}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000252} // namespace arm_compute