blob: 78218cbdee1834c8bbf2ece444ffcf91ee1abd7d [file] [log] [blame]
Manuel Bottini769c6382019-08-22 13:13:48 +01001/*
ramelg01cbbb0382021-09-17 17:36:57 +01002 * Copyright (c) 2019-2021 Arm Limited.
Manuel Bottini769c6382019-08-22 13:13:48 +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/runtime/NEON/functions/NEInstanceNormalizationLayer.h"
25
26#include "arm_compute/core/Helpers.h"
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010027#include "arm_compute/core/KernelDescriptors.h"
Manuel Bottini769c6382019-08-22 13:13:48 +010028#include "arm_compute/runtime/NEON/NEScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
ramelg01cbbb0382021-09-17 17:36:57 +010030#include "src/common/utils/Log.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010031#include "src/core/NEON/kernels/NEInstanceNormalizationLayerKernel.h"
Manuel Bottini769c6382019-08-22 13:13:48 +010032
33namespace arm_compute
34{
Michalis Spyrouebcebf12020-10-21 00:04:14 +010035NEInstanceNormalizationLayer::~NEInstanceNormalizationLayer() = default;
36
Manuel Bottini769c6382019-08-22 13:13:48 +010037NEInstanceNormalizationLayer::NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038 : _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 Bottini769c6382019-08-22 13:13:48 +010045{
46}
47
48void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon)
49{
ramelg01cbbb0382021-09-17 17:36:57 +010050 ARM_COMPUTE_LOG_PARAMS(input, output, gamma, beta, epsilon);
51
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010052 const DataLayout data_layout = input->info()->data_layout();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 const auto kernel_descriptor = InstanceNormalizationLayerKernelInfo{gamma, beta, epsilon, true};
Manuel Bottini769c6382019-08-22 13:13:48 +010054
55 // Configure Kernels
56 _is_nchw = data_layout == DataLayout::NCHW;
57
Georgios Pinitas40f51a62020-11-21 03:04:18 +000058 _normalization_kernel = std::make_unique<NEInstanceNormalizationLayerKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +010059
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 if (!_is_nchw)
Manuel Bottini769c6382019-08-22 13:13:48 +010061 {
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 Spyrouebcebf12020-10-21 00:04:14 +010069 _normalization_kernel->configure(&_permuted_input, &_permuted_output, kernel_descriptor);
Manuel Bottini769c6382019-08-22 13:13:48 +010070 _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 Spyrouebcebf12020-10-21 00:04:14 +010078 _normalization_kernel->configure(input, output, kernel_descriptor);
Manuel Bottini769c6382019-08-22 13:13:48 +010079 }
80}
81
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082Status NEInstanceNormalizationLayer::validate(
83 const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon)
Manuel Bottini769c6382019-08-22 13:13:48 +010084{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 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 Bottini769c6382019-08-22 13:13:48 +010088}
89
90void NEInstanceNormalizationLayer::run()
91{
92 MemoryGroupResourceScope scope_mg(_memory_group);
93
94 // Permute input
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 if (!_is_nchw)
Manuel Bottini769c6382019-08-22 13:13:48 +010096 {
97 _permute_input.run();
98 }
99
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100100 NEScheduler::get().schedule(_normalization_kernel.get(), Window::DimZ);
Manuel Bottini769c6382019-08-22 13:13:48 +0100101
102 // Permute output
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 if (!_is_nchw)
Manuel Bottini769c6382019-08-22 13:13:48 +0100104 {
105 _permute_output.run();
106 }
107}
108} // namespace arm_compute