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