blob: 57d01ff2d6022b5668739e39da7c98b701952f19 [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"
29
30namespace arm_compute
31{
32NEInstanceNormalizationLayer::NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager)
33 : _memory_group(std::move(memory_manager)), _normalization_kernel(), _is_nchw(false), _permute_input(), _permute_output(), _permuted_input(), _permuted_output()
34{
35}
36
37void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon)
38{
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010039 const DataLayout data_layout = input->info()->data_layout();
40 const auto kernel_descriptor = InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true };
Manuel Bottini769c6382019-08-22 13:13:48 +010041
42 // Configure Kernels
43 _is_nchw = data_layout == DataLayout::NCHW;
44
45 if(!_is_nchw)
46 {
47 _memory_group.manage(&_permuted_input);
48 _memory_group.manage(&_permuted_output);
49
50 // Configure the function to transform the input tensor from NHWC -> NCHW
51 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
52 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
53
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010054 _normalization_kernel.configure(&_permuted_input, &_permuted_output, kernel_descriptor);
Manuel Bottini769c6382019-08-22 13:13:48 +010055 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
56
57 _permute_output.configure(&_permuted_output, output != nullptr ? output : input, PermutationVector(2U, 0U, 1U));
58 _permuted_input.allocator()->allocate();
59 _permuted_output.allocator()->allocate();
60 }
61 else
62 {
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010063 _normalization_kernel.configure(input, output, kernel_descriptor);
Manuel Bottini769c6382019-08-22 13:13:48 +010064 }
65}
66
67Status NEInstanceNormalizationLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon)
68{
Sang-Hoon Park3351f2a2020-07-16 14:26:16 +010069 return NEInstanceNormalizationLayerKernel::validate(&input->clone()->set_data_layout(DataLayout::NCHW),
70 &output->clone()->set_data_layout(DataLayout::NCHW),
71 InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true });
Manuel Bottini769c6382019-08-22 13:13:48 +010072}
73
74void NEInstanceNormalizationLayer::run()
75{
76 MemoryGroupResourceScope scope_mg(_memory_group);
77
78 // Permute input
79 if(!_is_nchw)
80 {
81 _permute_input.run();
82 }
83
84 NEScheduler::get().schedule(&_normalization_kernel, Window::DimZ);
85
86 // Permute output
87 if(!_is_nchw)
88 {
89 _permute_output.run();
90 }
91}
92} // namespace arm_compute