aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/NetworkQuantizer.cpp
blob: f8e5ed23a77b701f7553c3ec8feb00629cec1d3f (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 © 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 "StaticRangeVisitor.hpp"
#include "QuantizerVisitor.hpp"

#include <map>
#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);
}

INetworkPtr NetworkQuantizer::ExportNetwork()
{
    const Graph& graph = boost::polymorphic_downcast<const Network*>(m_InputNetwork)->GetGraph().TopologicalSort();
    auto VisitLayers = [&graph](ILayerVisitor& visitor)
        {
            for (auto layer : graph)
            {
                layer->Accept(visitor);
            }
        };

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

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

    return quantizerVisitor.RetrieveFinalNetwork();
}

} //namespace armn