Compute Library
 21.02
NEInstanceNormalizationLayer.cpp
Go to the documentation of this file.
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  */
25 
30 
31 namespace arm_compute
32 {
34 
35 NEInstanceNormalizationLayer::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 
40 void NEInstanceNormalizationLayer::configure(ITensor *input, ITensor *output, float gamma, float beta, float epsilon)
41 {
42  const DataLayout data_layout = input->info()->data_layout();
43  const auto kernel_descriptor = InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true };
44 
45  // Configure Kernels
46  _is_nchw = data_layout == DataLayout::NCHW;
47 
48  _normalization_kernel = std::make_unique<NEInstanceNormalizationLayerKernel>();
49 
50  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 
59  _normalization_kernel->configure(&_permuted_input, &_permuted_output, kernel_descriptor);
60  _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  {
68  _normalization_kernel->configure(input, output, kernel_descriptor);
69  }
70 }
71 
72 Status NEInstanceNormalizationLayer::validate(const ITensorInfo *input, const ITensorInfo *output, float gamma, float beta, float epsilon)
73 {
74  return NEInstanceNormalizationLayerKernel::validate(&input->clone()->set_data_layout(DataLayout::NCHW),
75  &output->clone()->set_data_layout(DataLayout::NCHW),
76  InstanceNormalizationLayerKernelInfo{ gamma, beta, epsilon, true });
77 }
78 
80 {
81  MemoryGroupResourceScope scope_mg(_memory_group);
82 
83  // Permute input
84  if(!_is_nchw)
85  {
86  _permute_input.run();
87  }
88 
89  NEScheduler::get().schedule(_normalization_kernel.get(), Window::DimZ);
90 
91  // Permute output
92  if(!_is_nchw)
93  {
94  _permute_output.run();
95  }
96 }
97 } // namespace arm_compute
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const InstanceNormalizationLayerKernelInfo &info)
Static function to check if given info will lead to a valid configuration of NEInstanceNormalizationL...
void configure(ITensor *input, ITensor *output, float gamma=1.0f, float beta=0.0f, float epsilon=1e-12f)
Set the input and output tensors.
Strides PermutationVector
Permutation vector.
Definition: Types.h:49
const DataLayout data_layout
Definition: Im2Col.cpp:151
~NEInstanceNormalizationLayer()
Default destructor.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
NEInstanceNormalizationLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Status class.
Definition: Error.h:52
Interface for Neon tensor.
Definition: ITensor.h:36
void run() override
Run the kernels contained in the function.
Copyright (c) 2017-2021 Arm Limited.
TensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: Tensor.cpp:48
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Definition: Tensor.cpp:33
static Status validate(const ITensorInfo *input, const ITensorInfo *output, float gamma=1.0f, float beta=0.0f, float epsilon=1e-12f)
Static function to check if given info will lead to a valid configuration of NEInstanceNormalizationL...
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
virtual ITensorInfo & set_data_layout(const DataLayout &data_layout)=0
Set the data layout of the tensor.
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Num samples, channels, height, width.
void run() override
Run the kernels contained in the function.
Definition: NEPermute.cpp:67
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
Configure the permute Neon kernel.
Definition: NEPermute.cpp:49
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94