blob: 2ad5233de891f4b964ecc302188d4526f5b2a820 [file] [log] [blame]
Manuel Bottini79f88e62019-09-18 15:02:53 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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 */
24#include "arm_compute/core/CL/kernels/CLInstanceNormalizationLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
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/Window.h"
34
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}
56
57std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
58{
59 // We handle the planes manually
60 Window win = calculate_max_window(*input, Steps(1));
61
62 // Output auto initialization if not yet initialized
63 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
64
65 // CLInstanceNormalizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
66 Coordinates coord;
67 coord.set_num_dimensions(output->num_dimensions());
68 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
69 return std::make_pair(Status{}, win);
70}
71} // namespace
72
73CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel()
Georgios Pinitas55a687d2020-01-30 12:00:23 +000074 : _input(nullptr), _output(nullptr), _run_in_place(false)
Manuel Bottini79f88e62019-09-18 15:02:53 +010075{
76}
77
Georgios Pinitas55a687d2020-01-30 12:00:23 +000078void CLInstanceNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +010079{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010080 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
81}
82
Manuel Bottini679fc962020-04-21 16:08:53 +010083void CLInstanceNormalizationLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010084{
Manuel Bottini79f88e62019-09-18 15:02:53 +010085 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
86
Georgios Pinitas55a687d2020-01-30 12:00:23 +000087 _input = input;
88 _output = output == nullptr ? input : output;
Manuel Bottini79f88e62019-09-18 15:02:53 +010089
90 _run_in_place = (output == nullptr) || (output == input);
Georgios Pinitas55a687d2020-01-30 12:00:23 +000091 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info));
Manuel Bottini79f88e62019-09-18 15:02:53 +010092 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
93
94 CLBuildOptions build_opts;
95 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Georgios Pinitas55a687d2020-01-30 12:00:23 +000096 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 +010097 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
98 build_opts.add_option("-DDIM_X=" + support::cpp11::to_string(input->info()->dimension(0)));
99 build_opts.add_option("-DDIM_Y=" + support::cpp11::to_string(input->info()->dimension(1)));
100 build_opts.add_option("-DDIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000101 build_opts.add_option("-DGAMMA=" + float_to_string_with_full_precision(info.gamma));
102 build_opts.add_option("-DBETA=" + float_to_string_with_full_precision(info.beta));
103 build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(info.epsilon));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100104 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
105 build_opts.add_option_if(_input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
106
107 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100108 _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options());
Manuel Bottini79f88e62019-09-18 15:02:53 +0100109
110 // Configure kernel window
111 auto win_config = validate_and_configure_window(_input->info(), _output->info());
112 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
113 ICLKernel::configure_internal(std::get<1>(win_config));
114}
115
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000116Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +0100117{
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000118 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100119 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get()))));
120 return Status{};
121}
122
123void CLInstanceNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
124{
125 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
126 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
127
128 Window collapsed_window = window.collapse(window, Window::DimZ);
129
130 // We will process the planes together
131 if(_input->info()->data_layout() == DataLayout::NCHW)
132 {
133 collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
134 collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
135 }
136 else
137 {
138 collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
139 collapsed_window.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(3), 1));
140 }
141
142 unsigned int idx = 0;
143 add_4D_tensor_argument(idx, _input, collapsed_window);
144 if(!_run_in_place)
145 {
146 add_4D_tensor_argument(idx, _output, collapsed_window);
147 }
148
149 enqueue(queue, *this, collapsed_window, lws_hint());
150}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000151} // namespace arm_compute