ArmNN
 20.02
StaticRangeVisitor.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "StaticRangeVisitor.hpp"
7 
9 #include <armnn/Descriptors.hpp>
10 #include <armnn/Types.hpp>
11 
12 #include <limits>
13 
14 namespace armnn
15 {
16 
18  : m_RangeTracker(rangeTracker)
19 {}
20 
21 void StaticRangeVisitor::SetRange(const IConnectableLayer* layer, unsigned int outputIdx, float min, float max)
22 {
23  m_RangeTracker.SetRange(layer, outputIdx, min, max);
24 }
25 
26 void StaticRangeVisitor::ForwardParentParameters(const IConnectableLayer* layer)
27 {
28  const auto parentRange = m_RangeTracker.GetRange(layer->GetInputSlot(0).GetConnection()->GetOwningLayerGuid(), 0);
29  SetRange(layer, 0, parentRange.first, parentRange.second);
30 }
31 
32 void StaticRangeVisitor::VisitAdditionLayer(const IConnectableLayer* layer, const char* name)
33 {
34  IgnoreUnused(name);
35  SetRange(layer, 0, -20.f, 20.f);
36 }
37 
39  const BatchNormalizationDescriptor& desc,
40  const ConstTensor& mean,
41  const ConstTensor& variance,
42  const ConstTensor& beta,
43  const ConstTensor& gamma,
44  const char* name)
45 {
46  IgnoreUnused(desc);
47  IgnoreUnused(mean);
48  IgnoreUnused(variance);
49  IgnoreUnused(beta);
50  IgnoreUnused(gamma);
51  IgnoreUnused(name);
52  SetRange(layer, 0, -15.0f, 15.0f);
53 }
54 
56  const Convolution2dDescriptor& convolution2dDescriptor,
57  const ConstTensor& weights,
58  const Optional<ConstTensor>& biases,
59  const char* name)
60 {
61  IgnoreUnused(convolution2dDescriptor);
62  IgnoreUnused(weights);
63  IgnoreUnused(biases);
64  IgnoreUnused(name);
65  SetRange(layer, 0, -15.0f, 15.0f);
66 }
67 
70  const ConstTensor& weights,
71  const Optional<ConstTensor>& biases,
72  const char* name)
73 {
74  IgnoreUnused(desc);
75  IgnoreUnused(weights);
76  IgnoreUnused(biases);
77  IgnoreUnused(name);
78  SetRange(layer, 0, -15.0f, 15.0f);
79 }
80 
82  const ActivationDescriptor& activationDescriptor,
83  const char* name)
84 {
85  IgnoreUnused(name);
86  switch (activationDescriptor.m_Function)
87  {
88  // Range is 0, 15 for Abs, Linear, ReLu and Soft ReLu
93  SetRange(layer, 0, 0.f, 15.f);
94  break;
96  SetRange(layer, 0, 0.f, activationDescriptor.m_A);
97  break;
99  SetRange(layer, 0, -1.f, 1.f);
100  break;
102  SetRange(layer, 0, -5.f, 15.f);
103  break;
104  default:
105  SetRange(layer, 0, -15.f, 15.f);
106  break;
107  }
108 }
109 
111  const FullyConnectedDescriptor& desc,
112  const ConstTensor& weights,
113  const Optional<ConstTensor>& biases,
114  const char *name)
115 {
116  IgnoreUnused(desc);
117  IgnoreUnused(weights);
118  IgnoreUnused(biases);
119  IgnoreUnused(name);
120  SetRange(layer, 0, -15.0f, 15.0f);
121 }
122 
124  const PermuteDescriptor& permuteDescriptor,
125  const char* name)
126 {
127  IgnoreUnused(permuteDescriptor);
128  IgnoreUnused(name);
129  ForwardParentParameters(layer);
130 }
131 
133  const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
134  const char* name)
135 {
136  IgnoreUnused(spaceToBatchNdDescriptor);
137  IgnoreUnused(name);
138  ForwardParentParameters(layer);
139 }
140 
142  const Pooling2dDescriptor& pooling2dDescriptor,
143  const char* name)
144 {
145  IgnoreUnused(pooling2dDescriptor);
146  IgnoreUnused(name);
147  ForwardParentParameters(layer);
148 }
149 
151  const SoftmaxDescriptor& softmaxDescriptor,
152  const char* name)
153 {
154  IgnoreUnused(softmaxDescriptor);
155  IgnoreUnused(name);
156  SetRange(layer, 0, 0.f, 1.f);
157 }
158 
160  const OriginsDescriptor& originsDescriptor,
161  const char* name)
162 {
163  IgnoreUnused(originsDescriptor);
164  IgnoreUnused(name);
165  float min = std::numeric_limits<float>::max();
166  float max = std::numeric_limits<float>::lowest();
167  for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
168  {
169  const IOutputSlot* outputSlot = layer->GetInputSlot(i).GetConnection();
170  LayerGuid layerId = outputSlot->GetOwningLayerGuid();
171  unsigned int slotIndex = outputSlot->CalculateIndexOnOwner();
172  RangeTracker::MinMaxRange range = m_RangeTracker.GetRange(layerId, slotIndex);
173  min = std::min(min, range.first);
174  max = std::max(max, range.second);
175  }
176  SetRange(layer, 0, min, max);
177 }
178 
180  const ConstTensor& input,
181  const char* name)
182 {
183  IgnoreUnused(name);
184 
185  if (input.GetDataType() != DataType::Float32)
186  {
187  throw InvalidArgumentException("Quantization is supported only for FP32 tensors");
188  }
189 
190  // Work out the range based on the input constants
191  unsigned int inputNumElements = input.GetNumElements();
192  const float* inputData = reinterpret_cast<const float*>(input.GetMemoryArea());
193 
194  float min = std::numeric_limits<float>::max();
195  float max = std::numeric_limits<float>::lowest();
196 
197  for (unsigned int i = 0; i < inputNumElements; i++)
198  {
199  const float inputValue = inputData[i];
200 
201  min = std::min(min, inputValue);
202  max = std::max(max, inputValue);
203  }
204  SetRange(layer, 0, min, max);
205 }
206 
208  const ReshapeDescriptor& reshapeDescriptor,
209  const char* name)
210 {
211  IgnoreUnused(reshapeDescriptor);
212  IgnoreUnused(name);
213  ForwardParentParameters(layer);
214 }
215 
217  const SplitterDescriptor& splitterDescriptor,
218  const char* name)
219 {
220  IgnoreUnused(splitterDescriptor);
221  IgnoreUnused(name);
222  ForwardParentParameters(layer);
223 }
224 
226  const ResizeBilinearDescriptor& resizeDesc,
227  const char* name)
228 {
229  IgnoreUnused(resizeDesc);
230  IgnoreUnused(name);
231  ForwardParentParameters(layer);
232 }
233 
235  const ResizeDescriptor& resizeDescriptor,
236  const char* name)
237 {
238  IgnoreUnused(resizeDescriptor);
239  IgnoreUnused(name);
240  ForwardParentParameters(layer);
241 }
242 
244  const StridedSliceDescriptor& stridedSliceDescriptor,
245  const char* name)
246 {
247  IgnoreUnused(stridedSliceDescriptor);
248  IgnoreUnused(name);
249  ForwardParentParameters(layer);
250 }
251 
253  const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
254  const char* name)
255 {
256  IgnoreUnused(batchToSpaceNdDescriptor);
257  IgnoreUnused(name);
258  ForwardParentParameters(layer);
259 }
260 
261 } //namespace armnn
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
void VisitConvolution2dLayer(const IConnectableLayer *layer, const Convolution2dDescriptor &convolution2dDescriptor, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name=nullptr) override
Function that a 2D convolution layer should call back to when its Accept(ILayerVisitor&) function is ...
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
A ReshapeDescriptor for the ReshapeLayer.
void VisitFullyConnectedLayer(const IConnectableLayer *layer, const FullyConnectedDescriptor &desc, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name) override
Function that a fully connected layer should call back to when its Accept(ILayerVisitor&) function is...
A Convolution2dDescriptor for the Convolution2dLayer.
void VisitConcatLayer(const IConnectableLayer *layer, const OriginsDescriptor &originsDescriptor, const char *name=nullptr) override
Function that a concat layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void VisitActivationLayer(const IConnectableLayer *layer, const ActivationDescriptor &activationDescriptor, const char *name=nullptr) override
Function that an activation layer should call back to when its Accept(ILayerVisitor&) function is inv...
void VisitReshapeLayer(const IConnectableLayer *layer, const ReshapeDescriptor &reshapeDescriptor, const char *name=nullptr) override
Function a reshape layer should call back to when its Accept(ILayerVisitor&) function is invoked...
unsigned int GetNumElements() const
Definition: Tensor.hpp:175
void VisitResizeBilinearLayer(const IConnectableLayer *layer, const ResizeBilinearDescriptor &resizeDesc, const char *name=nullptr) override
Function that a resize bilinear layer should call back to when its Accept(ILayerVisitor&) function is...
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:177
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
void VisitBatchNormalizationLayer(const IConnectableLayer *layer, const BatchNormalizationDescriptor &desc, const ConstTensor &mean, const ConstTensor &variance, const ConstTensor &beta, const ConstTensor &gamma, const char *name=nullptr) override
Function that a batch normalization layer should call back to when its Accept(ILayerVisitor&) functio...
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
std::pair< float, float > MinMaxRange
A ResizeDescriptor for the ResizeLayer.
MinMaxRange GetRange(LayerGuid guid, unsigned int idx) const
Retrieve the Range for a particular output slot on a particular layer.
void VisitAdditionLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Functions to set the Range on a per-layer-type basis.
void SetRange(const IConnectableLayer *layer, unsigned int outputIdx, float min, float max)
Set the range for an output slot on a layer.
An output connection slot for a layer.
Definition: INetwork.hpp:37
An OriginsDescriptor for the ConcatLayer.
A FullyConnectedDescriptor for the FullyConnectedLayer.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
void VisitStridedSliceLayer(const IConnectableLayer *layer, const StridedSliceDescriptor &stridedSliceDescriptor, const char *name=nullptr) override
Function a strided slice layer should call back to when its Accept(ILayerVisitor&) function is invoke...
void VisitPermuteLayer(const IConnectableLayer *layer, const PermuteDescriptor &permuteDescriptor, const char *name) override
Function that a permute layer should call back to when its Accept(ILayerVisitor&) function is invoked...
virtual unsigned int CalculateIndexOnOwner() const =0
void VisitSpaceToBatchNdLayer(const IConnectableLayer *layer, const SpaceToBatchNdDescriptor &spaceToBatchNdDescriptor, const char *name=nullptr) override
Function a space to batch layer should call back to when its Accept(ILayerVisitor&) function is invok...
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:20
min(a, max(b, input)) ReLu1 & ReLu6.
StaticRangeVisitor(RangeTracker &rangeTracker)
void VisitConstantLayer(const IConnectableLayer *layer, const ConstTensor &input, const char *name=nullptr) override
Function a layer with no inputs and a single output, which always corresponds to the passed in consta...
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH).
Definition: Descriptors.hpp:37
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
virtual const IOutputSlot * GetConnection() const =0
void VisitResizeLayer(const IConnectableLayer *layer, const ResizeDescriptor &resizeDescriptor, const char *name=nullptr) override
Function that a resize layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void VisitSoftmaxLayer(const IConnectableLayer *layer, const SoftmaxDescriptor &softmaxDescriptor, const char *name=nullptr) override
Function that a softmax layer should call back to when its Accept(ILayerVisitor&) function is invoked...
A StridedSliceDescriptor for the StridedSliceLayer.
void VisitSplitterLayer(const IConnectableLayer *layer, const SplitterDescriptor &splitterDescriptor, const char *name=nullptr) override
Function that a splitter layer should call back to when its Accept(ILayerVisitor&) function is invoke...
DataType GetDataType() const
Definition: Tensor.hpp:172
void VisitBatchToSpaceNdLayer(const IConnectableLayer *layer, const BatchToSpaceNdDescriptor &batchToSpaceNdDescriptor, const char *name=nullptr) override
Function that a batch to space ND layer should call back to when its Accept(ILayerVisitor&) function ...
virtual LayerGuid GetOwningLayerGuid() const =0
void VisitPooling2dLayer(const IConnectableLayer *layer, const Pooling2dDescriptor &pooling2dDescriptor, const char *name) override
Function that a pooling layer should call back to when its Accept(ILayerVisitor&) function is invoked...
A Pooling2dDescriptor for the Pooling2dLayer.
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
void VisitDepthwiseConvolution2dLayer(const IConnectableLayer *layer, const DepthwiseConvolution2dDescriptor &desc, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name=nullptr) override
Function that a 2D depthwise convolution layer with biases should call back to when its Accept(ILayer...
A SoftmaxDescriptor for the SoftmaxLayer.
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square).
Definition: Descriptors.hpp:35
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
A PermuteDescriptor for the PermuteLayer.