ArmNN
 25.11
Loading...
Searching...
No Matches
BatchNormalizationLayer.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
6
7#include "LayerCloneBase.hpp"
8
12
13namespace armnn
14{
15
20
21std::unique_ptr<IWorkload> BatchNormalizationLayer::CreateWorkload(const IWorkloadFactory& factory) const
22{
23 // on this level constant data should not be released..
24 if (!m_Mean)
25 {
26 throw armnn::NullPointerException("BatchNormalizationLayer: Mean data should not be null.");
27 }
28
29 if (!m_Variance)
30 {
31 throw armnn::NullPointerException("BatchNormalizationLayer: Variance data should not be null.");
32 }
33
34 if (!m_Beta)
35 {
36 throw armnn::NullPointerException("BatchNormalizationLayer: Beta data should not be null.");
37 }
38
39 if (!m_Gamma)
40 {
41 throw armnn::NullPointerException("BatchNormalizationLayer: Gamma data should not be null.");
42 }
43
45 SetAdditionalInfo(descriptor);
46
47 descriptor.m_Mean = m_Mean.get();
48 descriptor.m_Variance = m_Variance.get();
49 descriptor.m_Beta = m_Beta.get();
50 descriptor.m_Gamma = m_Gamma.get();
51
52 return factory.CreateWorkload(LayerType::BatchNormalization, descriptor, PrepInfoAndDesc(descriptor));
53}
54
56{
58
59 layer->m_Mean = m_Mean ? m_Mean : nullptr;
60 layer->m_Variance = m_Variance ? m_Variance : nullptr;
61 layer->m_Beta = m_Beta ? m_Beta : nullptr;
62 layer->m_Gamma = m_Gamma ? m_Gamma : nullptr;
63
64 return std::move(layer);
65}
66
68{
70
71 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
72
74
75 auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetTensorInfo().GetShape() });
76
77 if (inferredShapes.size() != 1)
78 {
79 throw armnn::LayerValidationException("inferredShapes has "
80 + std::to_string(inferredShapes.size()) +
81 " elements - should only have 1.");
82 }
83
84 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "BatchNormalizationLayer");
85
86}
87
89{
90 // For API stability DO NOT ALTER order and add new members to the end of vector
91 return {m_Mean, m_Variance, m_Beta, m_Gamma};
92}
93
95{
97 ManagedConstTensorHandle managedVariance(m_Variance);
100
101 std::vector<armnn::ConstTensor> constTensors { { managedMean.GetTensorInfo(), managedMean.Map() },
102 { managedVariance.GetTensorInfo(), managedVariance.Map() },
103 { managedBeta.GetTensorInfo(), managedBeta.Map() },
104 { managedGamma.GetTensorInfo(), managedGamma.Map() } };
105
106 strategy.ExecuteStrategy(this, GetParameters(), constTensors, GetName());
107}
108
109} // namespace armnn
#define CHECK_LOCATION()
BatchNormalizationLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
ImmutableConstantTensors GetConstantTensorsByRef() const override
Retrieve the handles to the constant values stored by the layer.
std::shared_ptr< ConstTensorHandle > m_Mean
A unique pointer to store Mean values.
BatchNormalizationLayer(const BatchNormalizationDescriptor &param, const char *name)
Constructor to create a BatchNormalizationLayer.
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
std::shared_ptr< ConstTensorHandle > m_Gamma
A unique pointer to store Gamma values.
std::shared_ptr< ConstTensorHandle > m_Beta
A unique pointer to store Beta values.
std::shared_ptr< ConstTensorHandle > m_Variance
A unique pointer to store Variance values.
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of BatchNormalizationLayer.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the BatchNormalization type.
std::vector< std::reference_wrapper< const std::shared_ptr< ConstTensorHandle > > > ImmutableConstantTensors
Definition INetwork.hpp:141
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const =0
Backends should implement their own CreateWorkload function with a switch statement.
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition Layer.cpp:614
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition Layer.cpp:410
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition Layer.cpp:526
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infer the shape of the output(s) based on the provided input shape(s)
Definition Layer.cpp:432
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
LayerType * CloneBase(Graph &graph, Params &&... params) const
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition Layer.cpp:457
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition Layer.cpp:303
ShapeInferenceMethod m_ShapeInferenceMethod
Definition Layer.hpp:441
LayerWithParameters(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const BatchNormalizationDescriptor &param, const char *name)
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
const BatchNormalizationDescriptor & GetParameters() const override
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
const TensorInfo & GetTensorInfo() const
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
const TensorShape & GetShape() const
Definition Tensor.hpp:193
Copyright (c) 2021 ARM Limited and Contributors.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition Types.hpp:494
A BatchNormalizationDescriptor for the BatchNormalizationLayer.