blob: 7c803babb34eb55298223418c80479941602f5df [file] [log] [blame]
Manuel Bottini769c6382019-08-22 13:13:48 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019 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"
27#include "arm_compute/runtime/NEON/NEScheduler.h"
28
29namespace arm_compute
30{
31NEInstanceNormalizationLayer::NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager)
32 : _memory_group(std::move(memory_manager)), _normalization_kernel(), _is_nchw(false), _permute_input(), _permute_output(), _permuted_input(), _permuted_output()
33{
34}
35
36void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon)
37{
38 const DataLayout data_layout = input->info()->data_layout();
39
40 // Configure Kernels
41 _is_nchw = data_layout == DataLayout::NCHW;
42
43 if(!_is_nchw)
44 {
45 _memory_group.manage(&_permuted_input);
46 _memory_group.manage(&_permuted_output);
47
48 // Configure the function to transform the input tensor from NHWC -> NCHW
49 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
50 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
51
52 _normalization_kernel.configure(&_permuted_input, &_permuted_output, gamma, beta, epsilon);
53 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
54
55 _permute_output.configure(&_permuted_output, output != nullptr ? output : input, PermutationVector(2U, 0U, 1U));
56 _permuted_input.allocator()->allocate();
57 _permuted_output.allocator()->allocate();
58 }
59 else
60 {
61 _normalization_kernel.configure(input, output, gamma, beta, epsilon);
62 }
63}
64
65Status NEInstanceNormalizationLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon)
66{
Manuel Bottini581f1782019-11-13 17:24:43 +000067 return NEInstanceNormalizationLayerKernel::validate(&input->clone()->set_data_layout(DataLayout::NCHW), &output->clone()->set_data_layout(DataLayout::NCHW), gamma, beta, epsilon);
Manuel Bottini769c6382019-08-22 13:13:48 +010068}
69
70void NEInstanceNormalizationLayer::run()
71{
72 MemoryGroupResourceScope scope_mg(_memory_group);
73
74 // Permute input
75 if(!_is_nchw)
76 {
77 _permute_input.run();
78 }
79
80 NEScheduler::get().schedule(&_normalization_kernel, Window::DimZ);
81
82 // Permute output
83 if(!_is_nchw)
84 {
85 _permute_output.run();
86 }
87}
88} // namespace arm_compute