Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 1 | /* |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 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/runtime/NEON/functions/NEInstanceNormalizationLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/KernelDescriptors.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 31 | #include "src/core/NEON/kernels/NEInstanceNormalizationLayerKernel.h" |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 35 | NEInstanceNormalizationLayer::~NEInstanceNormalizationLayer() = default; |
| 36 | |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 37 | NEInstanceNormalizationLayer::NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 38 | : _memory_group(std::move(memory_manager)), |
| 39 | _normalization_kernel(), |
| 40 | _is_nchw(false), |
| 41 | _permute_input(), |
| 42 | _permute_output(), |
| 43 | _permuted_input(), |
| 44 | _permuted_output() |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon) |
| 49 | { |
ramelg01 | cbbb038 | 2021-09-17 17:36:57 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_LOG_PARAMS(input, output, gamma, beta, epsilon); |
| 51 | |
Sang-Hoon Park | 3351f2a | 2020-07-16 14:26:16 +0100 | [diff] [blame] | 52 | const DataLayout data_layout = input->info()->data_layout(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | const auto kernel_descriptor = InstanceNormalizationLayerKernelInfo{gamma, beta, epsilon, true}; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 54 | |
| 55 | // Configure Kernels |
| 56 | _is_nchw = data_layout == DataLayout::NCHW; |
| 57 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 58 | _normalization_kernel = std::make_unique<NEInstanceNormalizationLayerKernel>(); |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | if (!_is_nchw) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 61 | { |
| 62 | _memory_group.manage(&_permuted_input); |
| 63 | _memory_group.manage(&_permuted_output); |
| 64 | |
| 65 | // Configure the function to transform the input tensor from NHWC -> NCHW |
| 66 | _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); |
| 67 | _permuted_input.info()->set_data_layout(DataLayout::NCHW); |
| 68 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 69 | _normalization_kernel->configure(&_permuted_input, &_permuted_output, kernel_descriptor); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 70 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
| 71 | |
| 72 | _permute_output.configure(&_permuted_output, output != nullptr ? output : input, PermutationVector(2U, 0U, 1U)); |
| 73 | _permuted_input.allocator()->allocate(); |
| 74 | _permuted_output.allocator()->allocate(); |
| 75 | } |
| 76 | else |
| 77 | { |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 78 | _normalization_kernel->configure(input, output, kernel_descriptor); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | Status NEInstanceNormalizationLayer::validate( |
| 83 | const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 84 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | return NEInstanceNormalizationLayerKernel::validate( |
| 86 | &input->clone()->set_data_layout(DataLayout::NCHW), &output->clone()->set_data_layout(DataLayout::NCHW), |
| 87 | InstanceNormalizationLayerKernelInfo{gamma, beta, epsilon, true}); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void NEInstanceNormalizationLayer::run() |
| 91 | { |
| 92 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 93 | |
| 94 | // Permute input |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | if (!_is_nchw) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 96 | { |
| 97 | _permute_input.run(); |
| 98 | } |
| 99 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 100 | NEScheduler::get().schedule(_normalization_kernel.get(), Window::DimZ); |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 101 | |
| 102 | // Permute output |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | if (!_is_nchw) |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 104 | { |
| 105 | _permute_output.run(); |
| 106 | } |
| 107 | } |
| 108 | } // namespace arm_compute |