19 : m_Ranges(rangeTracker)
20 , m_QuantizedNetwork(
INetwork::Create())
21 , m_QuantizationScheme(quantizationScheme)
22 , m_PreserveType(preserveType)
26 void QuantizerVisitor::SetQuantizedInputConnections(
const IConnectableLayer* srcLayer,
33 const InputSlot* inputSlot = PolymorphicDowncast<const InputSlot*>(&srcInputSlot);
41 auto found = m_OriginalToQuantizedGuidMap.find(layerToFind.
GetGuid());
42 if (found == m_OriginalToQuantizedGuidMap.end())
53 newOutputSlot.
Connect(newInputSlot);
62 info.SetQuantizationOffset(qParams.second);
63 info.SetQuantizationScale(qParams.first);
71 std::vector<int32_t>& backing)
75 auto inputSlot = PolymorphicDowncast<const InputSlot*>(&srcInputSlot);
77 const OutputSlot* outputSlot = inputSlot->GetConnectedOutputSlot();
83 auto found = m_OriginalToQuantizedGuidMap.find(layerToFind.
GetGuid());
84 if (found == m_OriginalToQuantizedGuidMap.end())
88 return biases.
value();
100 backing.resize(biases.
value().GetInfo().GetNumElements());
103 for (
size_t i = 0; i < backing.size(); ++i)
105 float fp32Value =
static_cast<const float*
>(biases.
value().GetMemoryArea())[i];
114 m_OriginalToQuantizedGuidMap.insert(std::make_pair(srcLayer->
GetGuid(), quantizedLayer->
GetGuid()));
115 m_QuantizedGuidToLayerMap.insert(std::make_pair(quantizedLayer->
GetGuid(), quantizedLayer));
127 IConnectableLayer* newLayer = m_QuantizedNetwork->AddActivationLayer(activationDescriptor, name);
128 RecordLayer(layer, newLayer);
129 SetQuantizedInputConnections(layer, newLayer);
135 RecordLayer(layer, newLayer);
136 SetQuantizedInputConnections(layer, newLayer);
143 IConnectableLayer* newLayer = m_QuantizedNetwork->AddArgMinMaxLayer(argMinMaxDescriptor, name);
144 RecordLayer(layer, newLayer);
145 SetQuantizedInputConnections(layer, newLayer);
156 std::vector<uint8_t> meanBacking;
159 std::vector<uint8_t> varianceBacking;
162 std::vector<uint8_t> betaBacking;
165 std::vector<uint8_t> gammaBacking;
168 IConnectableLayer* newLayer = m_QuantizedNetwork->AddBatchNormalizationLayer(desc,
175 RecordLayer(layer, newLayer);
176 SetQuantizedInputConnections(layer, newLayer);
183 IConnectableLayer* newLayer = m_QuantizedNetwork->AddBatchToSpaceNdLayer(batchToSpaceNdDescriptor, name);
184 RecordLayer(layer, newLayer);
185 SetQuantizedInputConnections(layer, newLayer);
192 IConnectableLayer* newLayer = m_QuantizedNetwork->AddComparisonLayer(comparisonDescriptor, name);
193 RecordLayer(layer, newLayer);
194 SetQuantizedInputConnections(layer, newLayer);
201 IConnectableLayer* newLayer = m_QuantizedNetwork->AddConcatLayer(originsDescriptor, name);
202 RecordLayer(layer, newLayer);
203 SetQuantizedInputConnections(layer, newLayer);
210 std::vector<uint8_t> inputBacking;
213 IConnectableLayer* newLayer = m_QuantizedNetwork->AddConstantLayer(qInput, name);
214 RecordLayer(layer, newLayer);
223 std::vector<uint8_t> weightsBacking;
226 std::vector<int32_t> biasesBacking;
230 ConstTensor qBiases = CreateQuantizedBias(layer, qWeights, biases, biasesBacking);
234 IConnectableLayer* newLayer = m_QuantizedNetwork->AddConvolution2dLayer(convolution2dDescriptor,
239 RecordLayer(layer, newLayer);
240 SetQuantizedInputConnections(layer, newLayer);
247 IConnectableLayer* newLayer = m_QuantizedNetwork->AddDepthToSpaceLayer(descriptor, name);
248 RecordLayer(layer, newLayer);
249 SetQuantizedInputConnections(layer, newLayer);
258 std::vector<uint8_t> weightsBacking;
261 std::vector<int32_t> biasesBacking;
265 ConstTensor qBiases = CreateQuantizedBias(layer, qWeights, biases, biasesBacking);
269 IConnectableLayer* newLayer = m_QuantizedNetwork->AddDepthwiseConvolution2dLayer(desc,
274 RecordLayer(layer, newLayer);
275 SetQuantizedInputConnections(layer, newLayer);
282 IConnectableLayer* newLayer = m_QuantizedNetwork->AddElementwiseUnaryLayer(elementwiseUnaryDescriptor, name);
283 RecordLayer(layer, newLayer);
284 SetQuantizedInputConnections(layer, newLayer);
293 std::vector<uint8_t> weightsBacking;
296 std::vector<int32_t> biasesBacking;
300 ConstTensor qBiases = CreateQuantizedBias(layer, qWeights, biases, biasesBacking);
309 RecordLayer(layer, newLayer);
310 SetQuantizedInputConnections(layer, newLayer);
323 RecordLayer(layer, quantizeLayer);
327 RecordLayer(layer, inputLayer);
335 IConnectableLayer* newLayer = m_QuantizedNetwork->AddInstanceNormalizationLayer(descriptor, name);
336 RecordLayer(layer, newLayer);
337 SetQuantizedInputConnections(layer, newLayer);
344 IConnectableLayer* newLayer = m_QuantizedNetwork->AddLogSoftmaxLayer(logSoftmaxDescriptor, name);
345 RecordLayer(layer, newLayer);
346 SetQuantizedInputConnections(layer, newLayer);
353 IConnectableLayer* newLayer = m_QuantizedNetwork->AddMeanLayer(meanDescriptor, name);
354 RecordLayer(layer, newLayer);
355 SetQuantizedInputConnections(layer, newLayer);
362 RecordLayer(layer, newLayer);
363 SetQuantizedInputConnections(layer, newLayer);
370 IConnectableLayer* newLayer = m_QuantizedNetwork->AddNormalizationLayer(normalizationDescriptor, name);
371 RecordLayer(layer, newLayer);
372 SetQuantizedInputConnections(layer, newLayer);
384 RecordLayer(layer, dequantizeLayer);
385 SetQuantizedInputConnections(layer, dequantizeLayer);
391 RecordLayer(layer, outputLayer);
392 SetQuantizedInputConnections(layer, outputLayer);
400 IConnectableLayer* newLayer = m_QuantizedNetwork->AddPadLayer(padDescriptor, name);
401 RecordLayer(layer, newLayer);
402 SetQuantizedInputConnections(layer, newLayer);
409 IConnectableLayer* newLayer = m_QuantizedNetwork->AddPermuteLayer(permuteDescriptor, name);
410 RecordLayer(layer, newLayer);
411 SetQuantizedInputConnections(layer, newLayer);
418 IConnectableLayer* newLayer = m_QuantizedNetwork->AddPooling2dLayer(pooling2dDescriptor, name);
419 RecordLayer(layer, newLayer);
420 SetQuantizedInputConnections(layer, newLayer);
427 RecordLayer(layer, newLayer);
428 SetQuantizedInputConnections(layer, newLayer);
435 IConnectableLayer* newLayer = m_QuantizedNetwork->AddReshapeLayer(reshapeDescriptor, name);
436 RecordLayer(layer, newLayer);
437 SetQuantizedInputConnections(layer, newLayer);
457 IConnectableLayer* newLayer = m_QuantizedNetwork->AddResizeLayer(resizeDescriptor, name);
458 RecordLayer(layer, newLayer);
459 SetQuantizedInputConnections(layer, newLayer);
471 IConnectableLayer* newLayer = m_QuantizedNetwork->AddSliceLayer(sliceDescriptor, name);
472 RecordLayer(layer, newLayer);
473 SetQuantizedInputConnections(layer, newLayer);
480 IConnectableLayer* newLayer = m_QuantizedNetwork->AddSoftmaxLayer(softmaxDescriptor, name);
481 RecordLayer(layer, newLayer);
482 SetQuantizedInputConnections(layer, newLayer);
489 IConnectableLayer* newLayer = m_QuantizedNetwork->AddSpaceToBatchNdLayer(spaceToBatchNdDescriptor, name);
490 RecordLayer(layer, newLayer);
491 SetQuantizedInputConnections(layer, newLayer);
498 IConnectableLayer* newLayer = m_QuantizedNetwork->AddSpaceToDepthLayer(spaceToDepthDescriptor, name);
499 RecordLayer(layer, newLayer);
500 SetQuantizedInputConnections(layer, newLayer);
507 IConnectableLayer* newLayer = m_QuantizedNetwork->AddSplitterLayer(splitterDescriptor, name);
508 RecordLayer(layer, newLayer);
509 SetQuantizedInputConnections(layer, newLayer);
516 IConnectableLayer* newLayer = m_QuantizedNetwork->AddStackLayer(stackDescriptor, name);
517 RecordLayer(layer, newLayer);
518 SetQuantizedInputConnections(layer, newLayer);
525 IConnectableLayer* newLayer = m_QuantizedNetwork->AddStridedSliceLayer(stridedSliceDescriptor, name);
526 RecordLayer(layer, newLayer);
527 SetQuantizedInputConnections(layer, newLayer);
534 RecordLayer(layer, newLayer);
535 SetQuantizedInputConnections(layer, newLayer);
545 std::vector<uint8_t> weightsBacking;
549 std::vector<int32_t> biasesBacking;
553 ConstTensor qBiases = CreateQuantizedBias(layer, qWeights, biases, biasesBacking);
557 IConnectableLayer* newLayer = m_QuantizedNetwork->AddTransposeConvolution2dLayer(descriptor,
562 RecordLayer(layer, newLayer);
563 SetQuantizedInputConnections(layer, newLayer);
570 IConnectableLayer* newLayer = m_QuantizedNetwork->AddTransposeLayer(transposeDescriptor, name);
571 RecordLayer(layer, newLayer);
572 SetQuantizedInputConnections(layer, newLayer);
void VisitInputLayer(const IConnectableLayer *layer, LayerBindingId id, const char *name=nullptr) override
Function that an InputLayer should call back to when its Accept(ILayerVisitor&) function is invoked...
std::pair< float, int > OffsetScalePair
void VisitTransposeConvolution2dLayer(const IConnectableLayer *layer, const TransposeConvolution2dDescriptor &descriptor, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name=nullptr) override
Function that a 2D transpose convolution layer should call back to when its Accept(ILayerVisitor&) fu...
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
QuantizerVisitor(const RangeTracker &rangeTracker, const IQuantizationScheme *quantizationScheme, bool preserveType=false)
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
void VisitRsqrtLayer(const IConnectableLayer *, const char *name=nullptr) override
Function a Reciprocal of square root layer should call back to when its Accept(ILayerVisitor&) functi...
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
A ReshapeDescriptor for the ReshapeLayer.
void VisitPadLayer(const IConnectableLayer *, const PadDescriptor &, const char *name=nullptr) override
Function a pad layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void VisitMultiplicationLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Function that a multiplication layer should call back to when its Accept(ILayerVisitor&) function is ...
A ComparisonDescriptor for the ComparisonLayer.
uint32_t m_TargetWidth
Target width value.
void VisitInstanceNormalizationLayer(const IConnectableLayer *layer, const InstanceNormalizationDescriptor &instanceNormalizationDescriptor, const char *name=nullptr) override
Function that an instance normalization layer should call back to when its Accept(ILayerVisitor&) fun...
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
A Convolution2dDescriptor for the Convolution2dLayer.
Layer & GetOwningLayer() const
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...
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
void VisitAdditionLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Function that an addition layer should call back to when its Accept(ILayerVisitor&) function is invok...
void VisitLogSoftmaxLayer(const IConnectableLayer *layer, const LogSoftmaxDescriptor &logSoftmaxDescriptor, const char *name=nullptr) override
Function that a log softmax layer should call back to when its Accept(ILayerVisitor&) function is inv...
Main network class which provides the interface for building up a neural network. ...
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...
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...
Copyright (c) 2020 ARM Limited.
void VisitStackLayer(const IConnectableLayer *layer, const StackDescriptor &stackDescriptor, const char *name=nullptr) override
Function a stack layer should call back to when its Accept(ILayerVisitor&) function is invoked...
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
virtual OffsetScalePair ComputeScheme(double min, double max) const =0
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...
void VisitSubtractionLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Function a subtraction layer should call back to when its Accept(ILayerVisitor&) function is invoked...
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
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...
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
void VisitAbsLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Functions to quantize the individual layers, overridden from ILayerVisitor.
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.
A StackDescriptor for the StackLayer.
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...
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...
A PadDescriptor for the PadLayer.
#define ARMNN_ASSERT_MSG(COND, MSG)
An output connection slot for a layer.
An ArgMinMaxDescriptor for ArgMinMaxLayer.
float GetQuantizationScale() const
DataType GetDataType() const
An OriginsDescriptor for the ConcatLayer.
bool has_value() const noexcept
A FullyConnectedDescriptor for the FullyConnectedLayer.
virtual LayerGuid GetGuid() const =0
Returns the unique id of the layer.
void VisitTransposeLayer(const IConnectableLayer *layer, const TransposeDescriptor &descriptor, const char *name=nullptr) override
Function that a transpose layer should call back to when its Accept(ILayerVisitor&) function is invok...
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
void VisitDepthToSpaceLayer(const IConnectableLayer *layer, const DepthToSpaceDescriptor &depthToSpaceDescriptor, const char *name=nullptr) override
Function a depth to space layer should call back to when its Accept(ILayerVisitor&) function is invok...
uint32_t m_TargetWidth
Target width value.
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...
#define ARMNN_ASSERT(COND)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
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 ...
void VisitPermuteLayer(const IConnectableLayer *layer, const PermuteDescriptor &permuteDescriptor, const char *name=nullptr) override
Function that a permute layer should call back to when its Accept(ILayerVisitor&) function is invoked...
An ActivationDescriptor for the ActivationLayer.
const TensorInfo & GetInfo() const
uint32_t m_TargetHeight
Target height value.
uint32_t m_TargetHeight
Target height value.
A SliceDescriptor for the SliceLayer.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
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 VisitComparisonLayer(const IConnectableLayer *layer, const ComparisonDescriptor &comparisonDescriptor, const char *name=nullptr) override
Function a Comparison layer should call back to when its Accept(ILayerVisitor&) function is invoked...
virtual DataType GetDataType() const =0
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
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...
void VisitMeanLayer(const IConnectableLayer *layer, const MeanDescriptor &meanDescriptor, const char *name=nullptr) override
Function a Mean layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void VisitArgMinMaxLayer(const IConnectableLayer *layer, const ArgMinMaxDescriptor &argMinMaxDescriptor, const char *name=nullptr) override
Function that an arg min max layer should call back to when its Accept(ILayerVisitor&) function is in...
void VisitElementwiseUnaryLayer(const IConnectableLayer *layer, const ElementwiseUnaryDescriptor &elementwiseUnaryDescriptor, const char *name=nullptr) override
Function a ElementwiseUnary layer should call back to when its Accept(ILayerVisitor&) function is inv...
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
A MeanDescriptor for the MeanLayer.
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
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...
virtual const TensorInfo & GetTensorInfo() const =0
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
virtual int Connect(IInputSlot &destination)=0
void VisitSpaceToDepthLayer(const IConnectableLayer *layer, const SpaceToDepthDescriptor &spaceToDepthDescriptor, const char *name=nullptr) override
Function a space to depth layer should call back to when its Accept(ILayerVisitor&) function is invok...
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 ...
A Pooling2dDescriptor for the Pooling2dLayer.
ConstTensor CreateQuantizedConst(const ConstTensor &tensor, std::vector< uint8_t > &backing)
A NormalizationDescriptor for the NormalizationLayer.
void VisitSliceLayer(const IConnectableLayer *layer, const SliceDescriptor &sliceDescriptor, const char *name=nullptr) override
Function that a slice layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void VisitOutputLayer(const IConnectableLayer *layer, LayerBindingId id, const char *name=nullptr) override
Function an output layer should call back to when its Accept(ILayerVisitor&) function is invoked...
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
An InstanceNormalizationDescriptor for InstanceNormalizationLayer.
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
const TensorInfo & GetTensorInfo() const override
A SoftmaxDescriptor for the SoftmaxLayer.
void VisitPreluLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Function that a PReLU activation layer should call back to when its Accept(ILayerVisitor&) function i...
void VisitNormalizationLayer(const IConnectableLayer *layer, const NormalizationDescriptor &normalizationDescriptor, const char *name=nullptr) override
Function that a normalization layer should call back to when its Accept(ILayerVisitor&) function is i...
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
void VisitPooling2dLayer(const IConnectableLayer *layer, const Pooling2dDescriptor &pooling2dDescriptor, const char *name=nullptr) override
Function that a pooling layer should call back to when its Accept(ILayerVisitor&) function is invoked...
A PermuteDescriptor for the PermuteLayer.
void VisitFullyConnectedLayer(const IConnectableLayer *layer, const FullyConnectedDescriptor &desc, const ConstTensor &weights, const Optional< ConstTensor > &biases, const char *name=nullptr) override
Function that a fully connected layer should call back to when its Accept(ILayerVisitor&) function is...
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...
LayerGuid GetGuid() const final
Returns the unique id of the layer.
unsigned int CalculateIndexOnOwner() const override