blob: 74cbef151b3fdd31de4e22e0c7043fbe4c2dbd15 [file] [log] [blame]
Manuel Bottini79f88e62019-09-18 15:02:53 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2019-2021 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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Manuel Bottini79f88e62019-09-18 15:02:53 +010036
37namespace arm_compute
38{
39namespace
40{
Georgios Pinitas55a687d2020-01-30 12:00:23 +000041Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +010042{
Georgios Pinitas55a687d2020-01-30 12:00:23 +000043 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.epsilon == 0.f, "Epsilon must be different than 0");
Manuel Bottini581f1782019-11-13 17:24:43 +000044 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32);
Manuel Bottini79f88e62019-09-18 15:02:53 +010045
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 Bottini581f1782019-11-13 17:24:43 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), "Input and output have different number of channels");
Manuel Bottini79f88e62019-09-18 15:02:53 +010052 }
53
54 return Status{};
55}
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000056
57Status 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 Bottini79f88e62019-09-18 15:02:53 +010070} // namespace
71
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000072CLComputeMeanVariance::CLComputeMeanVariance()
73 : _input(nullptr), _output(nullptr)
Manuel Bottini79f88e62019-09-18 15:02:53 +010074{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010075 _type = CLKernelType::ELEMENTWISE;
Manuel Bottini79f88e62019-09-18 15:02:53 +010076}
77
Pablo Tello5c3eeec2021-04-26 15:39:05 +010078void CLComputeMeanVariance::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, bool use_mixed_precision)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079{
Manuel Bottini79f88e62019-09-18 15:02:53 +010080 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
Manuel Bottinib6869dd2020-12-16 15:34:25 +000081 auto padding_info = get_padding_info({ input, output });
Manuel Bottini79f88e62019-09-18 15:02:53 +010082
Georgios Pinitas55a687d2020-01-30 12:00:23 +000083 _input = input;
84 _output = output == nullptr ? input : output;
Manuel Bottini79f88e62019-09-18 15:02:53 +010085
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000086 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 Tello5c3eeec2021-04-26 15:39:05 +010090 build_opts.add_option("-DINTERNAL_DATA_TYPE=" + (use_mixed_precision ? "float" : get_cl_type_from_data_type(input->info()->data_type())));
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +000091 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 Tello5c3eeec2021-04-26 15:39:05 +0100110 if(use_mixed_precision)
111 {
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100112 auto_init_if_empty(*_output->info(), out_shape, 1, DataType::F32);
Pablo Tello5c3eeec2021-04-26 15:39:05 +0100113 }
114 else
115 {
Michalis Spyrou7f8caf72021-05-13 13:35:30 +0100116 auto_init_if_empty(*_output->info(), out_shape, 1, input->info()->data_type());
Pablo Tello5c3eeec2021-04-26 15:39:05 +0100117 }
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000118 ICLKernel::configure_internal(win);
119 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
120}
121
122Status 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
128void 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
153CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel()
154 : _input(nullptr), _output(nullptr), _mean(nullptr), _run_in_place(false)
155{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100156 _type = CLKernelType::ELEMENTWISE;
Pablo Marquez Tellofe7ae812021-03-03 12:12:35 +0000157}
158
159void 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 Bottini79f88e62019-09-18 15:02:53 +0100168 _run_in_place = (output == nullptr) || (output == input);
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000169 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100170 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 Pinitas55a687d2020-01-30 12:00:23 +0000174 build_opts.add_option("-DINTERNAL_DATA_TYPE=" + (info.use_mixed_precision ? "float" : get_cl_type_from_data_type(input->info()->data_type())));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100175 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 Pinitas55a687d2020-01-30 12:00:23 +0000179 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 Bottini79f88e62019-09-18 15:02:53 +0100182 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 Bottini4c6bd512020-04-08 10:15:51 +0100186 _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options());
Manuel Bottini79f88e62019-09-18 15:02:53 +0100187
188 // Configure kernel window
Michalis Spyrou702dc0c2021-03-19 15:06:07 +0000189 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 Bottinib6869dd2020-12-16 15:34:25 +0000196 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100197}
198
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000199Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +0100200{
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000201 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100202 return Status{};
203}
204
205void 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 Tellofe7ae812021-03-03 12:12:35 +0000226 add_3D_tensor_argument(idx, _mean, collapsed_window);
227
Manuel Bottini79f88e62019-09-18 15:02:53 +0100228 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 Bentham758b5ba2020-03-05 23:37:48 +0000235} // namespace arm_compute