Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 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 | */ |
| 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 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 | } |
| 56 | |
| 57 | std::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 | |
| 73 | CLInstanceNormalizationLayerKernel::CLInstanceNormalizationLayerKernel() |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 74 | : _input(nullptr), _output(nullptr), _run_in_place(false) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 75 | { |
| 76 | } |
| 77 | |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 78 | void CLInstanceNormalizationLayerKernel::configure(ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 79 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 80 | configure(CLKernelLibrary::get().get_compile_context(), input, output, info); |
| 81 | } |
| 82 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 83 | void CLInstanceNormalizationLayerKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, ICLTensor *output, const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 84 | { |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 86 | |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 87 | _input = input; |
| 88 | _output = output == nullptr ? input : output; |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 89 | |
| 90 | _run_in_place = (output == nullptr) || (output == input); |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 91 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), info)); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 92 | 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 Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 96 | 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] | 97 | 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 Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 101 | 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 Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 104 | 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 Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 108 | _kernel = create_kernel(compile_context, "instance_normalization", build_opts.options()); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 109 | |
| 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 Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 116 | Status CLInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 117 | { |
Georgios Pinitas | 55a687d | 2020-01-30 12:00:23 +0000 | [diff] [blame] | 118 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info)); |
Manuel Bottini | 79f88e6 | 2019-09-18 15:02:53 +0100 | [diff] [blame] | 119 | 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 | |
| 123 | void 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 Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 151 | } // namespace arm_compute |