Compute Library
 23.08
NEL2NormalizeLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 
28 #include "src/common/utils/Log.h"
31 
32 namespace arm_compute
33 {
34 namespace
35 {
36 constexpr int max_input_tensor_dim = 3;
37 } // namespace
39 
40 NEL2NormalizeLayer::NEL2NormalizeLayer(std::shared_ptr<IMemoryManager> memory_manager)
41  : _memory_group(std::move(memory_manager)), _reduce_func(), _normalize_kernel(), _sumsq()
42 {
43 }
44 
45 void NEL2NormalizeLayer::configure(ITensor *input, ITensor *output, int axis, float epsilon)
46 {
47  ARM_COMPUTE_LOG_PARAMS(input, output, axis, epsilon);
48 
49  // Manage intermediate buffers
50  _memory_group.manage(&_sumsq);
51 
52  // Configure Kernels
53  const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
54  _reduce_func.configure(input, &_sumsq, actual_axis, ReductionOperation::SUM_SQUARE);
55  _normalize_kernel = std::make_unique<NEL2NormalizeLayerKernel>();
56  _normalize_kernel->configure(input, &_sumsq, output, axis, epsilon);
57 
58  // Allocate intermediate tensors
59  _sumsq.allocator()->allocate();
60 }
61 
63 {
64  TensorShape shape(input->tensor_shape());
65 
66  // Create intermediate tensor info
67  TensorInfo sum_sq;
68  sum_sq.set_data_type(input->data_type());
69  sum_sq.set_tensor_shape(shape);
70 
71  const uint32_t actual_axis = wrap_around(axis, max_input_tensor_dim);
73 
74  // Reduce shape on axis
75  shape.set(actual_axis, 1);
76  sum_sq.set_tensor_shape(shape);
77 
79 
80  return Status{};
81 }
82 
84 {
85  MemoryGroupResourceScope scope_mg(_memory_group);
86 
87  _reduce_func.run();
88  NEScheduler::get().schedule(_normalize_kernel.get(), Window::DimY);
89 }
90 } // namespace arm_compute
NEL2NormalizeLayerKernel.h
arm_compute::MemoryGroup::manage
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
Helpers.h
arm_compute::NEL2NormalizeLayer::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *output, int axis, float epsilon=1e-6f)
Static function to check if given info will lead to a valid configuration of NEL2NormalizeLayer.
Definition: NEL2NormalizeLayer.cpp:62
arm_compute::NEL2NormalizeLayer::configure
void configure(ITensor *input, ITensor *output, int axis, float epsilon=1e-6f)
Set the input and output tensors.
Definition: NEL2NormalizeLayer.cpp:45
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::TensorInfo::set_data_type
ITensorInfo & set_data_type(DataType data_type) override
Set the data type to the specified value.
Definition: TensorInfo.cpp:321
arm_compute::wrap_around
T wrap_around(T x, T m)
Wrap-around a number within the range 0 <= x < m.
Definition: Helpers.h:268
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
arm_compute::test::validation::shape
shape
Definition: DFT.cpp:115
arm_compute::Tensor::allocator
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
arm_compute::ReductionOperation::SUM_SQUARE
@ SUM_SQUARE
Sum of squares.
arm_compute::NEL2NormalizeLayerKernel::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output, int axis, float epsilon)
Static function to check if given info will lead to a valid configuration of NEL2NormalizeLayerKernel...
arm_compute::Scheduler::get
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::NEReductionOperation::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
Static function to check if given info will lead to a valid configuration of NEReductionOperation.
Definition: NEReductionOperation.cpp:66
arm_compute::NEL2NormalizeLayer::run
void run() override
Run the kernels contained in the function.
Definition: NEL2NormalizeLayer.cpp:83
arm_compute::Window::DimY
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
arm_compute::IScheduler::schedule
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
NEScheduler.h
arm_compute::TensorInfo
Store the tensor's metadata.
Definition: TensorInfo.h:42
arm_compute::TensorAllocator::allocate
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
Definition: TensorAllocator.cpp:132
NEReductionOperationKernel.h
arm_compute::MemoryGroupResourceScope
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
NEL2NormalizeLayer.h
Log.h
arm_compute::NEL2NormalizeLayer::~NEL2NormalizeLayer
~NEL2NormalizeLayer()
Default destructor.
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:43
arm_compute::NEReductionOperation::configure
void configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op, bool keep_dims=true)
Set the input and output tensors.
Definition: NEReductionOperation.cpp:105
arm_compute::TensorInfo::set_tensor_shape
ITensorInfo & set_tensor_shape(const TensorShape &shape) override
Set the shape of an already initialized tensor.
Definition: TensorInfo.cpp:352
arm_compute::NEL2NormalizeLayer::NEL2NormalizeLayer
NEL2NormalizeLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Definition: NEL2NormalizeLayer.cpp:40
ARM_COMPUTE_LOG_PARAMS
#define ARM_COMPUTE_LOG_PARAMS(...)
Definition: Log.h:35
arm_compute::NEReductionOperation::run
void run() override
Run the kernels contained in the function.
Definition: NEReductionOperation.cpp:145
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
arm_compute::quantization::epsilon
constexpr float epsilon
Definition: AsymmHelpers.cpp:39