blob: 5965b9722f0a04aaf92243abd30c78dd58e5f460 [file] [log] [blame]
Manuel Bottini769c6382019-08-22 13:13:48 +01001/*
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +01002 * Copyright (c) 2019-2020 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"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010029#include "src/core/NEON/kernels/NEInstanceNormalizationLayerKernel.h"
Manuel Bottini769c6382019-08-22 13:13:48 +010030
31namespace arm_compute
32{
Michalis Spyrouebcebf12020-10-21 00:04:14 +010033NEInstanceNormalizationLayer::~NEInstanceNormalizationLayer() = default;
34
Manuel Bottini769c6382019-08-22 13:13:48 +010035NEInstanceNormalizationLayer::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
40void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon)
41{
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010042 const DataLayout data_layout = input->info()->data_layout();
43 const auto kernel_descriptor = InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true };
Manuel Bottini769c6382019-08-22 13:13:48 +010044
45 // Configure Kernels
46 _is_nchw = data_layout == DataLayout::NCHW;
47
Georgios Pinitas40f51a62020-11-21 03:04:18 +000048 _normalization_kernel = std::make_unique<NEInstanceNormalizationLayerKernel>();
Michalis Spyrouebcebf12020-10-21 00:04:14 +010049
Manuel Bottini769c6382019-08-22 13:13:48 +010050 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 Spyrouebcebf12020-10-21 00:04:14 +010059 _normalization_kernel->configure(&_permuted_input, &_permuted_output, kernel_descriptor);
Manuel Bottini769c6382019-08-22 13:13:48 +010060 _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 Spyrouebcebf12020-10-21 00:04:14 +010068 _normalization_kernel->configure(input, output, kernel_descriptor);
Manuel Bottini769c6382019-08-22 13:13:48 +010069 }
70}
71
72Status NEInstanceNormalizationLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon)
73{
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010074 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 Bottini769c6382019-08-22 13:13:48 +010077}
78
79void 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 Spyrouebcebf12020-10-21 00:04:14 +010089 NEScheduler::get().schedule(_normalization_kernel.get(), Window::DimZ);
Manuel Bottini769c6382019-08-22 13:13:48 +010090
91 // Permute output
92 if(!_is_nchw)
93 {
94 _permute_output.run();
95 }
96}
97} // namespace arm_compute