16d74e62d7ce55e72d7af51b1e218bac8bae5f72
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / runtime / NEON / functions / NEInstanceNormalizationLayerEx.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2019 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40
41 #include "arm_compute/runtime/NEON/functions/NEInstanceNormalizationLayerEx.h"
42
43 #include "arm_compute/core/Helpers.h"
44 #include "arm_compute/runtime/NEON/NEScheduler.h"
45
46 namespace arm_compute
47 {
48 NEInstanceNormalizationLayerEx::NEInstanceNormalizationLayerEx(
49     std::shared_ptr<IMemoryManager> memory_manager)
50     : _memory_group(std::move(memory_manager)), _normalization_kernel(), _is_nchw(false),
51       _permute_input(), _permute_output(), _permuted_input(), _permuted_output()
52 {
53 }
54
55 void NEInstanceNormalizationLayerEx::configure(ITensor *input, ITensor *output, ITensor *gamma,
56                                                ITensor *beta, float epsilon)
57 {
58   const DataLayout data_layout = input->info()->data_layout();
59
60   // Configure Kernels
61   _is_nchw = data_layout == DataLayout::NCHW;
62
63   if (!_is_nchw)
64   {
65     _memory_group.manage(&_permuted_input);
66     _memory_group.manage(&_permuted_output);
67
68     // Configure the function to transform the input tensor from NHWC -> NCHW
69     _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
70     _permuted_input.info()->set_data_layout(DataLayout::NCHW);
71
72     _normalization_kernel.configure(&_permuted_input, &_permuted_output, gamma, beta, epsilon);
73     _permuted_output.info()->set_data_layout(DataLayout::NCHW);
74
75     _permute_output.configure(&_permuted_output, output != nullptr ? output : input,
76                               PermutationVector(2U, 0U, 1U));
77     _permuted_input.allocator()->allocate();
78     _permuted_output.allocator()->allocate();
79   }
80   else
81   {
82     _normalization_kernel.configure(input, output, gamma, beta, epsilon);
83   }
84 }
85
86 Status NEInstanceNormalizationLayerEx::validate(const ITensorInfo *input, const ITensorInfo *output,
87                                                 const ITensorInfo *gamma, const ITensorInfo *beta,
88                                                 float epsilon)
89 {
90   return NEInstanceNormalizationLayerKernelEx::validate(
91       &input->clone()->set_data_layout(DataLayout::NCHW),
92       &output->clone()->set_data_layout(DataLayout::NCHW), gamma, beta, epsilon);
93 }
94
95 void NEInstanceNormalizationLayerEx::run()
96 {
97   MemoryGroupResourceScope scope_mg(_memory_group);
98
99   // Permute input
100   if (!_is_nchw)
101   {
102     _permute_input.run();
103   }
104
105   NEScheduler::get().schedule(&_normalization_kernel, Window::DimZ);
106
107   // Permute output
108   if (!_is_nchw)
109   {
110     _permute_output.run();
111   }
112 }
113 } // namespace arm_compute