blob: e97b8564565c7db2be91978d02deb91f88ea7656 [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"
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"
Manuel Bottini79f88e62019-09-18 15:02:53 +010035
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Manuel Bottini79f88e62019-09-18 15:02:53 +010037
38namespace arm_compute
39{
40namespace
41{
Georgios Pinitas55a687d2020-01-30 12:00:23 +000042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +010043{
Georgios Pinitas55a687d2020-01-30 12:00:23 +000044 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.epsilon == 0.f, "Epsilon must be different than 0");
Manuel Bottini581f1782019-11-13 17:24:43 +000045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32);
Manuel Bottini79f88e62019-09-18 15:02:53 +010046
47 if(output != nullptr && output->total_size() != 0)
48 {
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Manuel Bottini581f1782019-11-13 17:24:43 +000052 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 +010053 }
54
55 return Status{};
56}
57
58std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
59{
60 // We handle the planes manually
61 Window win = calculate_max_window(*input, Steps(1));
62
63 // Output auto initialization if not yet initialized
64 auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type());
65
66 // CLInstanceNormalizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
67 Coordinates coord;
68 coord.set_num_dimensions(output->num_dimensions());
69 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
70 return std::make_pair(Status{}, win);
71}
72} // namespace
73
74CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel()
Georgios Pinitas55a687d2020-01-30 12:00:23 +000075 : _input(nullptr), _output(nullptr), _run_in_place(false)
Manuel Bottini79f88e62019-09-18 15:02:53 +010076{
77}
78
Georgios Pinitas55a687d2020-01-30 12:00:23 +000079void CLInstanceNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +010080{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010081 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
82}
83
Manuel Bottini679fc962020-04-21 16:08:53 +010084void CLInstanceNormalizationLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010085{
Manuel Bottini79f88e62019-09-18 15:02:53 +010086 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
87
Georgios Pinitas55a687d2020-01-30 12:00:23 +000088 _input = input;
89 _output = output == nullptr ? input : output;
Manuel Bottini79f88e62019-09-18 15:02:53 +010090
91 _run_in_place = (output == nullptr) || (output == input);
Georgios Pinitas55a687d2020-01-30 12:00:23 +000092 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info));
Manuel Bottini79f88e62019-09-18 15:02:53 +010093 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
94
95 CLBuildOptions build_opts;
96 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Georgios Pinitas55a687d2020-01-30 12:00:23 +000097 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 +010098 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
99 build_opts.add_option("-DDIM_X=" + support::cpp11::to_string(input->info()->dimension(0)));
100 build_opts.add_option("-DDIM_Y=" + support::cpp11::to_string(input->info()->dimension(1)));
101 build_opts.add_option("-DDIM_Z=" + support::cpp11::to_string(input->info()->dimension(2)));
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000102 build_opts.add_option("-DGAMMA=" + float_to_string_with_full_precision(info.gamma));
103 build_opts.add_option("-DBETA=" + float_to_string_with_full_precision(info.beta));
104 build_opts.add_option("-DEPSILON=" + float_to_string_with_full_precision(info.epsilon));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100105 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
106 build_opts.add_option_if(_input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
107
108 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100109 _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options());
Manuel Bottini79f88e62019-09-18 15:02:53 +0100110
111 // Configure kernel window
112 auto win_config = validate_and_configure_window(_input->info(), _output->info());
113 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
114 ICLKernel::configure_internal(std::get<1>(win_config));
115}
116
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000117Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Manuel Bottini79f88e62019-09-18 15:02:53 +0100118{
Georgios Pinitas55a687d2020-01-30 12:00:23 +0000119 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
Manuel Bottini79f88e62019-09-18 15:02:53 +0100120 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get()))));
121 return Status{};
122}
123
124void CLInstanceNormalizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
125{
126 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
127 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
128
129 Window collapsed_window = window.collapse(window, Window::DimZ);
130
131 // We will process the planes together
132 if(_input->info()->data_layout() == DataLayout::NCHW)
133 {
134 collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
135 collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
136 }
137 else
138 {
139 collapsed_window.set(Window::DimY, Window::Dimension(0, 1, 1));
140 collapsed_window.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(3), 1));
141 }
142
143 unsigned int idx = 0;
144 add_4D_tensor_argument(idx, _input, collapsed_window);
145 if(!_run_in_place)
146 {
147 add_4D_tensor_argument(idx, _output, collapsed_window);
148 }
149
150 enqueue(queue, *this, collapsed_window, lws_hint());
151}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000152} // namespace arm_compute