File size: 3,741 Bytes
be94e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

// Copyright (C) 2020, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.

#ifndef __OPENCV_DNN_GRAPH_SIMPLIFIER_HPP__
#define __OPENCV_DNN_GRAPH_SIMPLIFIER_HPP__

#include <string>

#include <opencv2/core.hpp>

namespace cv { namespace dnn {

class ImportNodeWrapper
{
public:
    virtual ~ImportNodeWrapper() {}

    virtual int getNumInputs() const = 0;

    virtual std::string getInputName(int idx) const = 0;

    virtual std::string getType() const = 0;

    virtual void setType(const std::string& type) = 0;

    virtual void setInputNames(const std::vector<std::string>& inputs) = 0;
};

class ImportGraphWrapper
{
public:
    virtual ~ImportGraphWrapper() {}

    virtual Ptr<ImportNodeWrapper> getNode(int idx) const = 0;

    virtual int getNumNodes() const = 0;

    virtual int getNumOutputs(int nodeId) const = 0;

    virtual std::string getOutputName(int nodeId, int outId) const = 0;

    virtual void removeNode(int idx) = 0;

    virtual bool isCommutativeOp(const std::string& type) const = 0;
};

class Subgraph  // Interface to match and replace subgraphs.
{
public:
    virtual ~Subgraph();

    // Add a node to be matched in the origin graph. Specify ids of nodes that
    // are expected to be inputs. Returns id of a newly added node.
    // TODO: Replace inputs to std::vector<int> in C++11
    int addNodeToMatch(const std::string& op, int input_0 = -1, int input_1 = -1,

                       int input_2 = -1, int input_3 = -1);

    int addNodeToMatch(const std::string& op, const std::vector<int>& inputs_);

    // Specify resulting node. All the matched nodes in subgraph excluding
    // input nodes will be fused into this single node.
    // TODO: Replace inputs to std::vector<int> in C++11
    void setFusedNode(const std::string& op, int input_0 = -1, int input_1 = -1,

                      int input_2 = -1, int input_3 = -1, int input_4 = -1,

                      int input_5 = -1);

    void setFusedNode(const std::string& op, const std::vector<int>& inputs_);

    static int getInputNodeId(const Ptr<ImportGraphWrapper>& net,

                              const Ptr<ImportNodeWrapper>& node,

                              int inpId);

    // Match TensorFlow subgraph starting from <nodeId> with a set of nodes to be fused.
    // Const nodes are skipped during matching. Returns true if nodes are matched and can be fused.
    virtual bool match(const Ptr<ImportGraphWrapper>& net, int nodeId,

                       std::vector<int>& matchedNodesIds);

    // Fuse matched subgraph.
    void replace(const Ptr<ImportGraphWrapper>& net, const std::vector<int>& matchedNodesIds);

    virtual void finalize(const Ptr<ImportGraphWrapper>& net,

                          const Ptr<ImportNodeWrapper>& fusedNode,

                          std::vector<Ptr<ImportNodeWrapper> >& inputs);

private:
    std::vector<std::string> nodes;         // Nodes to be matched in the origin graph.
    std::vector<std::vector<int> > inputs;  // Connections of an every node to it's inputs.

    std::string fusedNodeOp;           // Operation name of resulting fused node.
    std::vector<int> fusedNodeInputs;  // Inputs of fused node.
};

void simplifySubgraphs(const Ptr<ImportGraphWrapper>& net,

                       const std::vector<Ptr<Subgraph> >& patterns);

}}  // namespace dnn, namespace cv

#endif  // __OPENCV_DNN_GRAPH_SIMPLIFIER_HPP__