Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +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/NEON/kernels/NEInstanceNormalizationLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/Validate.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/KernelDescriptors.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 35 | #include "src/core/NEON/NEMath.h" |
| 36 | #include "src/core/NEON/wrapper/wrapper.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 37 | |
| 38 | #include <arm_neon.h> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 44 | template <typename InputType, typename AccType = InputType> |
| 45 | void vector_float_sum(AccType &result, AccType &result_square, const InputType &inputs) |
| 46 | { |
| 47 | result = wrapper::vadd(result, inputs); |
| 48 | result_square = wrapper::vadd(result_square, wrapper::vmul(inputs, inputs)); |
| 49 | } |
| 50 | |
| 51 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 52 | template <> |
| 53 | inline void vector_float_sum(float32x4_t &result, float32x4_t &result_square, const float16x8_t &inputs) |
| 54 | { |
| 55 | vector_float_sum(result, result_square, wrapper::vcvt<float>(wrapper::vgetlow(inputs))); |
| 56 | vector_float_sum(result, result_square, wrapper::vcvt<float>(wrapper::vgethigh(inputs))); |
| 57 | } |
| 58 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 59 | |
| 60 | template <typename InputType, typename AccType = InputType> |
| 61 | InputType vector_float_norm(const InputType &inputs, const AccType &vec_mean, const AccType &vec_multip, const AccType &vec_beta) |
| 62 | { |
| 63 | return wrapper::vadd(wrapper::vmul(wrapper::vsub(inputs, vec_mean), vec_multip), vec_beta); |
| 64 | } |
| 65 | |
| 66 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 67 | template <> |
| 68 | inline float16x8_t vector_float_norm(const float16x8_t &inputs, const float32x4_t &vec_mean, const float32x4_t &vec_multip, const float32x4_t &vec_beta) |
| 69 | { |
| 70 | const auto input_low = wrapper::vcvt<float>(wrapper::vgetlow(inputs)); |
| 71 | const auto input_high = wrapper::vcvt<float>(wrapper::vgethigh(inputs)); |
| 72 | const auto result_low = wrapper::vcvt<float16_t>(vector_float_norm(input_low, vec_mean, vec_multip, vec_beta)); |
| 73 | const auto result_high = wrapper::vcvt<float16_t>(vector_float_norm(input_high, vec_mean, vec_multip, vec_beta)); |
| 74 | float16x8_t result = wrapper::vcombine(result_low, result_high); |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 79 | |
| 80 | template <typename T, typename AccType = T> |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 81 | void instance_normalization_nchw(ITensor *input, ITensor *output, float gamma, float beta, float epsilon, const Window &window) |
| 82 | { |
| 83 | /** NEON vector tag type. */ |
| 84 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
| 85 | |
| 86 | // Clear X/Y dimensions on execution window as we handle the planes manually |
| 87 | Window win = window; |
| 88 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 89 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 90 | |
| 91 | constexpr int window_step_x = 16 / sizeof(T); |
| 92 | const unsigned int elements_plane = input->info()->dimension(0) * output->info()->dimension(1); |
| 93 | |
| 94 | Iterator input_it(input, win); |
| 95 | execute_window_loop(win, [&](const Coordinates & id) |
| 96 | { |
| 97 | Window win_plane = window; |
| 98 | win_plane.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 99 | win_plane.set(Window::DimZ, Window::Dimension(id[2], id[2] + 1, 1)); |
| 100 | win_plane.set(3, Window::Dimension(id[3], id[3] + 1, 1)); |
| 101 | |
| 102 | Iterator input_plane_it(input, win_plane); |
| 103 | Iterator output_plane_it(output, win_plane); |
| 104 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 105 | auto sum_h_w = static_cast<AccType>(0.f); |
| 106 | auto sum_squares_h_w = static_cast<AccType>(0.f); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 107 | |
| 108 | execute_window_loop(win_plane, [&](const Coordinates &) |
| 109 | { |
| 110 | const auto input_ptr = reinterpret_cast<const T *>(input_plane_it.ptr()); |
| 111 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 112 | auto vec_sum_h_w = wrapper::vdup_n(static_cast<AccType>(0.f), ExactTagType{}); |
| 113 | auto vec_sum_squares_h_w = wrapper::vdup_n(static_cast<AccType>(0.f), ExactTagType{}); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 114 | |
| 115 | // Compute S elements per iteration |
| 116 | int x = window.x().start(); |
| 117 | for(; x <= (window.x().end() - window_step_x); x += window_step_x) |
| 118 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 119 | auto vec_input_val = wrapper::vloadq(input_ptr + x); |
| 120 | vector_float_sum(vec_sum_h_w, vec_sum_squares_h_w, vec_input_val); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | auto vec2_sum_h_w = wrapper::vpadd(wrapper::vgethigh(vec_sum_h_w), wrapper::vgetlow(vec_sum_h_w)); |
| 124 | auto vec2_sum_squares_h_w = wrapper::vpadd(wrapper::vgethigh(vec_sum_squares_h_w), wrapper::vgetlow(vec_sum_squares_h_w)); |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 125 | |
| 126 | vec2_sum_h_w = wrapper::vpadd(vec2_sum_h_w, vec2_sum_h_w); |
| 127 | vec2_sum_squares_h_w = wrapper::vpadd(vec2_sum_squares_h_w, vec2_sum_squares_h_w); |
| 128 | |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 129 | sum_h_w += wrapper::vgetlane(vec2_sum_h_w, 0); |
| 130 | sum_squares_h_w += wrapper::vgetlane(vec2_sum_squares_h_w, 0); |
| 131 | |
| 132 | // Compute left-over elements |
| 133 | for(; x < window.x().end(); ++x) |
| 134 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 135 | const auto value = static_cast<AccType>(*(input_ptr + x)); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 136 | sum_h_w += value; |
| 137 | sum_squares_h_w += value * value; |
| 138 | } |
| 139 | }, |
| 140 | input_plane_it, output_plane_it); |
| 141 | |
| 142 | const auto mean_h_w = sum_h_w / elements_plane; |
| 143 | const auto var_h_w = sum_squares_h_w / elements_plane - mean_h_w * mean_h_w; |
| 144 | |
| 145 | const auto multip_h_w = gamma / std::sqrt(var_h_w + epsilon); |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 146 | const auto vec_mean_h_w = wrapper::vdup_n(static_cast<AccType>(mean_h_w), ExactTagType{}); |
| 147 | const auto vec_multip_h_w = wrapper::vdup_n(static_cast<AccType>(multip_h_w), ExactTagType{}); |
| 148 | const auto vec_beta = wrapper::vdup_n(static_cast<AccType>(beta), ExactTagType{}); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 149 | |
| 150 | execute_window_loop(win_plane, [&](const Coordinates &) |
| 151 | { |
| 152 | auto input_ptr = reinterpret_cast<T *>(input_plane_it.ptr()); |
| 153 | auto output_ptr = reinterpret_cast<T *>(output_plane_it.ptr()); |
| 154 | |
| 155 | // Compute S elements per iteration |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 156 | int x = window.x().start(); |
| 157 | //auto vec_val = wrapper::vdup_n(static_cast<T>(0.0f), ExactTagType{}); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 158 | for(; x <= (window.x().end() - window_step_x); x += window_step_x) |
| 159 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 160 | const auto vec_val = wrapper::vloadq(input_ptr + x); |
| 161 | const auto normalized_vec = vector_float_norm(vec_val, vec_mean_h_w, vec_multip_h_w, vec_beta); |
| 162 | wrapper::vstore(output_ptr + x, normalized_vec); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Compute left-over elements |
| 166 | for(; x < window.x().end(); ++x) |
| 167 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 168 | const auto val = static_cast<AccType>(*(input_ptr + x)); |
| 169 | *(output_ptr + x) = static_cast<T>((val - mean_h_w) * multip_h_w + beta); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 170 | } |
| 171 | }, |
| 172 | input_plane_it, output_plane_it); |
| 173 | }, |
| 174 | input_it); |
| 175 | } |
| 176 | |
| 177 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon) |
| 178 | { |
| 179 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 180 | ARM_COMPUTE_UNUSED(gamma); |
| 181 | ARM_COMPUTE_UNUSED(beta); |
| 182 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(epsilon == 0.f, "Epsilon must be different than 0"); |
| 183 | |
Manuel Bottini | 581f178 | 2019-11-13 17:24:43 +0000 | [diff] [blame] | 184 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F16, DataType::F32); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 185 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() == DataLayout::NHWC, "NHWC data layout is not supported by the kernel directly"); |
| 186 | |
| 187 | if(output != nullptr && output->total_size() != 0) |
| 188 | { |
| 189 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 190 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 191 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Manuel Bottini | 581f178 | 2019-11-13 17:24:43 +0000 | [diff] [blame] | 192 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_channels() != output->num_channels(), "Input and output have different number of channels"); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 193 | } |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 194 | return Status{}; |
| 195 | } |
| 196 | |
| 197 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 198 | { |
| 199 | // We handle the planes manually |
| 200 | Window win = calculate_max_window(*input, Steps(1)); |
| 201 | |
| 202 | // Output auto initialization if not yet initialized |
| 203 | auto_init_if_empty(*output, input->tensor_shape(), 1, input->data_type()); |
| 204 | |
| 205 | // NEInstanceNormalizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
| 206 | Coordinates coord; |
| 207 | coord.set_num_dimensions(output->num_dimensions()); |
| 208 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
| 209 | return std::make_pair(Status{}, win); |
| 210 | } |
| 211 | } // namespace |
| 212 | |
| 213 | NEInstanceNormalizationLayerKernel::NEInstanceNormalizationLayerKernel() |
| 214 | : _func(nullptr), _input(nullptr), _output(nullptr), _gamma(1), _beta(0), _epsilon(1e-12) |
| 215 | { |
| 216 | } |
| 217 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 218 | void NEInstanceNormalizationLayerKernel::configure(ITensor *input, ITensor *output, const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 219 | { |
| 220 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 221 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 222 | _input = input; |
| 223 | _output = output == nullptr ? input : output; |
| 224 | _gamma = info.gamma; |
| 225 | _beta = info.beta; |
| 226 | _epsilon = info.epsilon; |
| 227 | _use_mixed_precision = info.use_mixed_precision; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 228 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 229 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _output->info(), _gamma, _beta, _epsilon)); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 230 | |
| 231 | if(_input->info()->data_type() == DataType::F32) |
| 232 | { |
| 233 | _func = &instance_normalization_nchw<float>; |
| 234 | } |
| 235 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 236 | else if(_input->info()->data_type() == DataType::F16) |
| 237 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 238 | if(_use_mixed_precision) |
| 239 | { |
| 240 | _func = &instance_normalization_nchw<float16_t, float>; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | _func = &instance_normalization_nchw<float16_t>; |
| 245 | } |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 246 | } |
| 247 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 248 | else |
| 249 | { |
| 250 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 251 | } |
| 252 | |
| 253 | // Configure kernel window |
| 254 | auto win_config = validate_and_configure_window(_input->info(), _output->info()); |
| 255 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
| 256 | |
| 257 | INEKernel::configure(std::get<1>(win_config)); |
| 258 | } |
| 259 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 260 | Status NEInstanceNormalizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 261 | { |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 262 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info.gamma, info.beta, info.epsilon)); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 263 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), (output == nullptr ? input->clone().get() : output->clone().get())))); |
| 264 | return Status{}; |
| 265 | } |
| 266 | |
| 267 | void NEInstanceNormalizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 268 | { |
| 269 | ARM_COMPUTE_UNUSED(info); |
| 270 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 271 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 272 | (*_func)(_input, _output, _gamma, _beta, _epsilon, window); |
| 273 | } |
| 274 | } // namespace arm_compute |