aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/QuantizerStrategy.hpp
blob: f7829590204a94172300d0b0a55d5b60e3ce5ea2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "Network.hpp"
#include "NetworkQuantizerUtils.hpp"
#include "StaticRangeStrategy.hpp"

#include <armnn/utility/NumericCast.hpp>
#include <armnn/utility/PolymorphicDowncast.hpp>

namespace armnn
{
class QuantizerStrategy : public IStrategy
{
public :
    QuantizerStrategy(const RangeTracker& rangeTracker,
                      const IQuantizationScheme* quantizationScheme,
                      bool preserveType);

    ~QuantizerStrategy() = default;

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

    /// Extract the quantized network
    INetworkPtr RetrieveFinalNetwork() { return std::move(m_QuantizedNetwork); }

private:
    /// Connects the layer to preceeding layers and sets the quantization parameters based on recorded ranges
    void SetQuantizedInputConnections(const IConnectableLayer* srcLayer, IConnectableLayer* quantizedLayer);

    /// Record the guids so we can easily find the layers later
    void RecordLayer(const IConnectableLayer* srcLayer, IConnectableLayer* qLayer);

    /// Sets the bias quantization scale based on input and weight scales
    ConstTensor CreateQuantizedBias(const IConnectableLayer* srcLayer,
                                    const ConstTensor& weights,
                                    const Optional<ConstTensor>& biases,
                                    std::vector<int32_t>& weightsBacking);

    /// Reference to the static range visitor used to retrieve the quantization ranges
    const RangeTracker& m_Ranges;

    /// Quantized version of the model we are building up
    INetworkPtr m_QuantizedNetwork;

    /// Mapping from input network guids to quantized network guids
    std::unordered_map<LayerGuid, LayerGuid> m_OriginalToQuantizedGuidMap;

    /// Mapping from guid to layer in quantized network
    std::unordered_map<LayerGuid, IConnectableLayer*> m_QuantizedGuidToLayerMap;

    const IQuantizationScheme* m_QuantizationScheme;

    const bool m_PreserveType;
};

} //namespace armnn