ArmNN
 21.02
DynamicQuantizationStrategy Class Reference

Visitor class implementation to gather the TensorInfo for LayerBindingID for creation of ConstTensor for Refine. More...

#include <DynamicQuantizationStrategy.hpp>

Inheritance diagram for DynamicQuantizationStrategy:
IStrategy

Public Member Functions

 DynamicQuantizationStrategy (RangeTracker &rangeTracker, Graph &graph)
 
 ~DynamicQuantizationStrategy ()=default
 
virtual void ExecuteStrategy (const armnn::IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0) override
 
const std::vector< armnn::LayerBindingId > & GetOutputLayers ()
 
void VisitNonCalibratedLayers ()
 
void FinishStrategy () override
 

Additional Inherited Members

- Protected Member Functions inherited from IStrategy
 IStrategy ()
 
virtual ~IStrategy ()
 

Detailed Description

Visitor class implementation to gather the TensorInfo for LayerBindingID for creation of ConstTensor for Refine.

Definition at line 19 of file DynamicQuantizationStrategy.hpp.

Constructor & Destructor Documentation

◆ DynamicQuantizationStrategy()

◆ ~DynamicQuantizationStrategy()

Member Function Documentation

◆ ExecuteStrategy()

void ExecuteStrategy ( const armnn::IConnectableLayer layer,
const armnn::BaseDescriptor descriptor,
const std::vector< armnn::ConstTensor > &  constants,
const char *  name,
const armnn::LayerBindingId  id = 0 
)
overridevirtual

Implements IStrategy.

Definition at line 87 of file DynamicQuantizationStrategy.cpp.

References armnn::Abs, armnn::Activation, armnn::Addition, armnn::ArgMinMax, armnn::BatchNormalization, armnn::BatchToSpaceNd, armnn::BoundedReLu, IOutputSlot::CalculateIndexOnOwner(), armnn::Concat, armnn::Constant, armnn::Convolution2d, armnn::DepthwiseConvolution2d, armnn::Float32, armnn::FullyConnected, IInputSlot::GetConnection(), IConnectableLayer::GetInputSlot(), IConnectableLayer::GetNumInputSlots(), IOutputSlot::GetOwningLayerGuid(), RangeTracker::GetRange(), IConnectableLayer::GetType(), armnn::IgnoreUnused(), armnn::Input, armnn::LeakyReLu, armnn::Linear, ActivationDescriptor::m_A, ActivationDescriptor::m_Function, armnn::Normalization, armnn::Output, armnn::Permute, armnn::Pooling2d, armnn::ReLu, armnn::Reshape, armnn::Resize, armnn::Softmax, armnn::SoftReLu, armnn::SpaceToBatchNd, armnn::Splitter, armnn::StridedSlice, and armnn::TanH.

92 {
93  IgnoreUnused(name);
94  IgnoreUnused(id);
95  IgnoreUnused(descriptor);
96 
97  switch (layer->GetType())
98  {
100  {
101  const ActivationDescriptor& activationDescriptor = static_cast<const ActivationDescriptor&>(descriptor);
102  switch (activationDescriptor.m_Function)
103  {
104  // Range is 0, 15 for Abs, Linear, ReLu and Soft ReLu
109  SetRange(layer, 0, 0.f, 15.f);
110  break;
112  SetRange(layer, 0, 0.f, activationDescriptor.m_A);
113  break;
115  SetRange(layer, 0, -1.f, 1.f);
116  break;
118  SetRange(layer, 0, -5.f, 15.f);
119  break;
120  default:
121  SetRange(layer, 0, -15.f, 15.f);
122  break;
123  }
124  break;
125  }
127  {
128  SetRange(layer, 0, -20.f, 20.f);
129  AddToCalibratedLayers(layer);
130  break;
131  }
133  {
134  AddToNonCalibratedLayers(layer);
135  break;
136  }
138  {
139  SetRange(layer, 0, -15.0f, 15.0f);
140  AddToCalibratedLayers(layer);
141  break;
142  }
144  {
145  SetRange(layer, 0, -15.0f, 15.0f);
146  AddToCalibratedLayers(layer);
147  break;
148  }
150  {
151  SetRange(layer, 0, -15.0f, 15.0f);
152  AddToCalibratedLayers(layer);
153  break;
154  }
156  {
157  SetRange(layer, 0, -15.0f, 15.0f);
158  AddToCalibratedLayers(layer);
159  break;
160  }
162  {
163  SetRange(layer, 0, -15.0f, 15.0f);
164  AddToCalibratedLayers(layer);
165  break;
166  }
168  {
169  AddToNonCalibratedLayers(layer);
170  break;
171  }
173  {
174  AddToNonCalibratedLayers(layer);
175  break;
176  }
178  {
179  AddToNonCalibratedLayers(layer);
180  break;
181  }
183  {
184  SetRange(layer, 0, 0.f, 1.f);
185  AddToCalibratedLayers(layer);
186  break;
187  }
189  {
190  if (constants[0].GetDataType() != DataType::Float32)
191  {
192  throw InvalidArgumentException("Quantization is supported only for FP32 tensors");
193  }
194 
195  // Work out the range based on the input constants
196  unsigned int inputNumElements = constants[0].GetNumElements();
197  const float* inputData = reinterpret_cast<const float*>(constants[0].GetMemoryArea());
198 
199  float min = std::numeric_limits<float>::max();
200  float max = std::numeric_limits<float>::lowest();
201 
202  for (unsigned int i = 0; i < inputNumElements; i++)
203  {
204  const float inputValue = inputData[i];
205 
206  min = std::min(min, inputValue);
207  max = std::max(max, inputValue);
208  }
209  SetRange(layer, 0, min, max);
210  break;
211  }
213  {
214  float min = std::numeric_limits<float>::max();
215  float max = std::numeric_limits<float>::lowest();
216  for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
217  {
218  const IOutputSlot* outputSlot = layer->GetInputSlot(i).GetConnection();
219  LayerGuid layerId = outputSlot->GetOwningLayerGuid();
220  unsigned int slotIndex = outputSlot->CalculateIndexOnOwner();
221  RangeTracker::MinMaxRange range = m_RangeTracker.GetRange(layerId, slotIndex);
222  min = std::min(min, range.first);
223  max = std::max(max, range.second);
224  }
225  SetRange(layer, 0, min, max);
226  AddToCalibratedLayers(layer);
227  break;
228  }
230  {
231  AddToNonCalibratedLayers(layer);
232  break;
233  }
235  {
236  AddToNonCalibratedLayers(layer);
237  break;
238  }
240  {
241  AddToNonCalibratedLayers(layer);
242  break;
243  }
245  {
246  AddToNonCalibratedLayers(layer);
247  break;
248  }
250  {
251  AddToNonCalibratedLayers(layer);
252  break;
253  }
255  {
256  SetRange(layer, 0, -0.0f, 0.0f);
257  AddToCalibratedLayers(layer);
258  break;
259  }
261  {
262  AddToNonCalibratedLayers(layer);
263  m_OutputLayers.push_back(id);
264  break;
265  }
266  default:
267  {}
268  }
269 }
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
void IgnoreUnused(Ts &&...)
std::pair< float, float > MinMaxRange
MinMaxRange GetRange(LayerGuid guid, unsigned int idx) const
Retrieve the Range for a particular output slot on a particular layer.
min(a, max(b, input)) ReLu1 & ReLu6.
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
profiling::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:275
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
virtual const IOutputSlot * GetConnection() const =0

◆ FinishStrategy()

void FinishStrategy ( )
overridevirtual

Reimplemented from IStrategy.

Definition at line 50 of file DynamicQuantizationStrategy.cpp.

References OutputSlot::Connect(), OutputSlot::Disconnect(), Graph::EraseLayer(), and armnn::InsertDebugLayerAfter().

51 {
52  for (const IConnectableLayer* layer : m_LayersToCalibrate)
53  {
54  std::vector<DebugLayer*> newDebugLayers = InsertDebugLayerAfter(
55  m_Graph, *PolymorphicDowncast<Layer*>(const_cast<IConnectableLayer*>(layer)));
56  // record them so we can take them out again efficiently afterward
57  m_DebugLayers.insert(std::end(m_DebugLayers), std::begin(newDebugLayers), std::end(newDebugLayers));
58  }
59 }
std::vector< DebugLayer * > InsertDebugLayerAfter(Graph &graph, Layer &layer)

◆ GetOutputLayers()

const std::vector< LayerBindingId > & GetOutputLayers ( )

Definition at line 271 of file DynamicQuantizationStrategy.cpp.

272 {
273  return m_OutputLayers;
274 }

◆ VisitNonCalibratedLayers()

void VisitNonCalibratedLayers ( )

Definition at line 78 of file DynamicQuantizationStrategy.cpp.

78  {
79  RemoveDebugLayers();
80  for (const IConnectableLayer* layer : m_LayersNotToCalibrate)
81  {
82  ForwardParentParameters(layer);
83  }
84 }

The documentation for this class was generated from the following files: