ArmNN
 20.02
DynamicQuantizationVisitor.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
9 #include "RangeTracker.hpp"
10 #include "layers/DebugLayer.hpp"
11 
12 #include <armnn/INetwork.hpp>
14 
15 namespace armnn
16 {
17 
18 /// Visitor class to establish min/max ranges based on the type of the layer
19 class DynamicQuantizationVisitor : public LayerVisitorBase<VisitorNoThrowPolicy>
20 {
21 public:
22  DynamicQuantizationVisitor(RangeTracker& rangeTracker, Graph& graph);
23  ~DynamicQuantizationVisitor() = default;
24 
25  /// Functions to set the Range on a per-layer-type basis
26  void VisitAdditionLayer(const IConnectableLayer* layer, const char* name = nullptr) override;
27 
29  const BatchNormalizationDescriptor& desc,
30  const ConstTensor& mean,
31  const ConstTensor& variance,
32  const ConstTensor& beta,
33  const ConstTensor& gamma,
34  const char* name = nullptr) override;
35 
37  const Convolution2dDescriptor& convolution2dDescriptor,
38  const ConstTensor& weights,
39  const Optional<ConstTensor>& biases,
40  const char* name = nullptr) override;
41 
44  const ConstTensor& weights,
45  const Optional<ConstTensor>& biases,
46  const char* name = nullptr) override;
47 
48  void VisitActivationLayer(const IConnectableLayer* layer,
49  const ActivationDescriptor& activationDescriptor,
50  const char* name = nullptr) override;
51 
53  const FullyConnectedDescriptor& desc,
54  const ConstTensor& weights,
55  const Optional<ConstTensor>& biases,
56  const char *name) override;
57 
58  void VisitPermuteLayer(const IConnectableLayer* layer,
59  const PermuteDescriptor& permuteDescriptor,
60  const char* name) override;
61 
63  const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
64  const char* name = nullptr) override;
65 
66  void VisitPooling2dLayer(const IConnectableLayer* layer,
67  const Pooling2dDescriptor& pooling2dDescriptor,
68  const char* name) override;
69 
70  void VisitSoftmaxLayer(const IConnectableLayer* layer,
71  const SoftmaxDescriptor& softmaxDescriptor,
72  const char* name = nullptr) override;
73 
74  void VisitConcatLayer(const IConnectableLayer* layer,
75  const ConcatDescriptor& originsDescriptor,
76  const char* name = nullptr) override;
77 
78  void VisitConstantLayer(const IConnectableLayer* layer,
79  const ConstTensor& input,
80  const char* name = nullptr) override;
81 
82  void VisitReshapeLayer(const IConnectableLayer* layer,
83  const ReshapeDescriptor& reshapeDescriptor,
84  const char* name = nullptr) override;
85 
86  void VisitSplitterLayer(const IConnectableLayer* layer,
87  const SplitterDescriptor& splitterDescriptor,
88  const char* name = nullptr) override;
89 
91  const ResizeBilinearDescriptor& resizeDesc,
92  const char* name = nullptr) override;
93 
94  void VisitStridedSliceLayer(const IConnectableLayer* layer,
95  const StridedSliceDescriptor& stridedSliceDescriptor,
96  const char* name = nullptr) override;
97 
99  const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
100  const char* name = nullptr) override;
101 
102  void VisitInputLayer(const IConnectableLayer* layer,
103  LayerBindingId id,
104  const char* name = nullptr) override;
105 
106  void VisitOutputLayer(const IConnectableLayer* layer,
107  LayerBindingId id,
108  const char* name = nullptr) override;
109 
110  void FinishVisit() override;
112 
113  const std::vector<armnn::LayerBindingId>& GetOutputLayers();
114 
115 private:
116  /// Set the range for an output slot on a layer
117  void SetRange(const IConnectableLayer* layer, unsigned int outputIdx, float min, float max);
118 
119  void ForwardParentParameters(const IConnectableLayer* layer);
120 
121  /// Mapping from a layer Guid to an array of ranges for outputs
122  RangeTracker& m_RangeTracker;
123 
124  Graph& m_Graph;
125 
126  std::vector<const IConnectableLayer*> m_LayersToCalibrate;
127  std::vector<const IConnectableLayer*> m_LayersNotToCalibrate;
128  std::vector<DebugLayer*> m_DebugLayers;
129 
130  std::vector<armnn::LayerBindingId> m_OutputLayers;
131 
132  void AddToCalibratedLayers(const IConnectableLayer* layer);
133  void AddToNonCalibratedLayers(const IConnectableLayer* layer);
134  void RemoveDebugLayers();
135 };
136 
137 } //namespace armnn
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...
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 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...
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
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 ReshapeDescriptor for the ReshapeLayer.
void VisitConcatLayer(const IConnectableLayer *layer, const ConcatDescriptor &originsDescriptor, const char *name=nullptr) override
Function that a concat layer should call back to when its Accept(ILayerVisitor&) function is invoked...
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...
A Convolution2dDescriptor for the Convolution2dLayer.
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 ...
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...
Copyright (c) 2020 ARM Limited.
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 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 BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
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...
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:171
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...
DynamicQuantizationVisitor(RangeTracker &rangeTracker, Graph &graph)
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 VisitAdditionLayer(const IConnectableLayer *layer, const char *name=nullptr) override
Functions to set the Range on a per-layer-type basis.
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
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:20
Visitor base class with empty implementations.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
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...
A StridedSliceDescriptor for the StridedSliceLayer.
Visitor class to establish min/max ranges based on the type of the layer.
A Pooling2dDescriptor for the Pooling2dLayer.
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...
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...
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
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...
A SoftmaxDescriptor for the SoftmaxLayer.
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...
const std::vector< armnn::LayerBindingId > & GetOutputLayers()
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
A PermuteDescriptor for the PermuteLayer.