• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 Arm Limited.
3  *
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/core/KernelDescriptors.h"
28 #include "arm_compute/runtime/NEON/NEScheduler.h"
29 #include "src/core/NEON/kernels/NEInstanceNormalizationLayerKernel.h"
30 #include "support/MemorySupport.h"
31 
32 namespace arm_compute
33 {
34 NEInstanceNormalizationLayer::~NEInstanceNormalizationLayer() = default;
35 
NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager)36 NEInstanceNormalizationLayer::NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager)
37     : _memory_group(std::move(memory_manager)), _normalization_kernel(), _is_nchw(false), _permute_input(), _permute_output(), _permuted_input(), _permuted_output()
38 {
39 }
40 
configure(ITensor * input,ITensor * output,float gamma,float beta,float epsilon)41 void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon)
42 {
43     const DataLayout data_layout       = input->info()->data_layout();
44     const auto       kernel_descriptor = InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true };
45 
46     // Configure Kernels
47     _is_nchw = data_layout == DataLayout::NCHW;
48 
49     _normalization_kernel = arm_compute::support::cpp14::make_unique<NEInstanceNormalizationLayerKernel>();
50 
51     if(!_is_nchw)
52     {
53         _memory_group.manage(&_permuted_input);
54         _memory_group.manage(&_permuted_output);
55 
56         // Configure the function to transform the input tensor from NHWC -> NCHW
57         _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
58         _permuted_input.info()->set_data_layout(DataLayout::NCHW);
59 
60         _normalization_kernel->configure(&_permuted_input, &_permuted_output, kernel_descriptor);
61         _permuted_output.info()->set_data_layout(DataLayout::NCHW);
62 
63         _permute_output.configure(&_permuted_output, output != nullptr ? output : input, PermutationVector(2U, 0U, 1U));
64         _permuted_input.allocator()->allocate();
65         _permuted_output.allocator()->allocate();
66     }
67     else
68     {
69         _normalization_kernel->configure(input, output, kernel_descriptor);
70     }
71 }
72 
validate(const ITensorInfo * input,const ITensorInfo * output,float gamma,float beta,float epsilon)73 Status NEInstanceNormalizationLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon)
74 {
75     return NEInstanceNormalizationLayerKernel::validate(&input->clone()->set_data_layout(DataLayout::NCHW),
76                                                         &output->clone()->set_data_layout(DataLayout::NCHW),
77                                                         InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true });
78 }
79 
run()80 void NEInstanceNormalizationLayer::run()
81 {
82     MemoryGroupResourceScope scope_mg(_memory_group);
83 
84     // Permute input
85     if(!_is_nchw)
86     {
87         _permute_input.run();
88     }
89 
90     NEScheduler::get().schedule(_normalization_kernel.get(), Window::DimZ);
91 
92     // Permute output
93     if(!_is_nchw)
94     {
95         _permute_output.run();
96     }
97 }
98 } // namespace arm_compute
99