aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/NetworkQuantizer.cpp
blob: bc25d5e4d2362c5f3a1f08b7598a15f0515c926f (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
64
65
66
67
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <armnn/ILayerVisitor.hpp>
#include <armnn/INetwork.hpp>
#include <armnn/Tensor.hpp>
#include <armnn/Types.hpp>

#include "Graph.hpp"
#include "Layer.hpp"
#include "Network.hpp"
#include "NetworkQuantizer.hpp"
#include "NetworkQuantizerUtils.hpp"

#include "StaticRangeVisitor.hpp"
#include "QuantizerVisitor.hpp"
#include "OverrideInputRangeVisitor.hpp"

#include <vector>
#include <cmath>

namespace armnn
{

INetworkQuantizer* INetworkQuantizer::CreateRaw(INetwork* inputNetwork)
{
    return new NetworkQuantizer(inputNetwork);
}

INetworkQuantizerPtr INetworkQuantizer::Create(INetwork* inputNetwork)
{
    return INetworkQuantizerPtr(CreateRaw(inputNetwork), &INetworkQuantizer::Destroy);
}

void INetworkQuantizer::Destroy(INetworkQuantizer *quantizer)
{
    delete boost::polymorphic_downcast<NetworkQuantizer*>(quantizer);
}

void NetworkQuantizer::OverrideInputRange(LayerBindingId layerId, float min, float max)
{
    const Graph& graph = boost::polymorphic_downcast<const Network*>(m_InputNetwork)->GetGraph();
    auto inputLayers = graph.GetInputLayers();

    // Walk the input layers of the graph and override the quantization parameters of the one with the given id
    OverrideInputRangeVisitor overrideInputRangeVisitor(m_GuidToRangesMap, layerId, MinMaxRange{min, max});
    VisitLayers(inputLayers, overrideInputRangeVisitor);
}

INetworkPtr NetworkQuantizer::ExportNetwork()
{
    const Graph& graph = boost::polymorphic_downcast<const Network*>(m_InputNetwork)->GetGraph().TopologicalSort();

    // Step 1) Walk the graph and register min/max values for intermediate tensors
    StaticRangeVisitor rangeVisitor(m_GuidToRangesMap);
    VisitLayers(graph, rangeVisitor);

    // Step 2) Convert input InputNetwork to Quantized InputNetwork
    QuantizerVisitor quantizerVisitor(&rangeVisitor);
    VisitLayers(graph, quantizerVisitor);

    return quantizerVisitor.RetrieveFinalNetwork();
}

} //namespace armn