aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/backends/SubgraphView.hpp
blob: 19603a510fb8c28aecdeb05e4e91df55dbb149c1 (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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// Copyright © 2017, 2022-2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include <armnn/Deprecated.hpp>

#include <vector>
#include <list>
#include <iterator>
#include <memory>

namespace armnn
{
class Graph;
class IConnectableLayer;
class IInputSlot;
class IOutputSlot;
class InputSlot;
class Layer;
class OutputSlot;

///
/// The SubgraphView class represents a subgraph of a Graph.
/// The data it holds, points to data held by layers of the Graph, so the
/// the contents of the SubgraphView become invalid when the Layers are destroyed
/// or changed.
///
class SubgraphView final : public std::enable_shared_from_this<SubgraphView>
{
public:
    template <typename Func>
    void ForEachLayer(Func func) const
    {
        for (auto it = m_Layers.begin(); it != m_Layers.end(); )
        {
             auto next = std::next(it);
             func(*it);
             it = next;
        }
    }

    template <typename Func>
    void ForEachIConnectableLayer(Func func) const
    {
        for (auto it = m_IConnectableLayers.begin(); it != m_IConnectableLayers.end(); )
        {
             auto next = std::next(it);
             func(*it);
             it = next;
        }
    }

    using SubgraphViewPtr = std::shared_ptr<SubgraphView>;
    using InputSlots = std::vector<InputSlot*>;
    using IInputSlots = std::vector<IInputSlot*>;
    using OutputSlots = std::vector<OutputSlot*>;
    using IOutputSlots = std::vector<IOutputSlot*>;
    using Layers = std::list<Layer*>;
    using IConnectableLayers = std::list<IConnectableLayer*>;
    using Iterator = Layers::iterator;
    using IConnectableLayerIterator = IConnectableLayers::iterator;
    using ConstIterator = Layers::const_iterator;
    using ConstIConnectableIterator = IConnectableLayers::const_iterator;

    /// Constructs a sub-graph from the entire given graph.
    explicit SubgraphView(Graph& graph);

    /// Constructs a sub-graph with the given arguments.
    ARMNN_DEPRECATED_MSG_REMOVAL_DATE("This function has been deprecated, please use constructor with arguments: "
                                      "IConnectableLayers, IInputSlots and IOutputSlots", "23.08")
    SubgraphView(InputSlots&& inputs, OutputSlots&& outputs, Layers&& layers);

    /// Constructs a sub-graph with the given arguments.
    SubgraphView(IConnectableLayers&& layers, IInputSlots&& inputs, IOutputSlots&& outputs);

    /// Copy-constructor.
    SubgraphView(const SubgraphView& subgraph);

    /// Move-constructor.
    SubgraphView(SubgraphView&& subgraph);

    /// Constructs a sub-graph with only the given layer.
    SubgraphView(IConnectableLayer* layer);

    /// Move-assignment operator.
    SubgraphView& operator=(SubgraphView&& other);

    const IInputSlots& GetIInputSlots() const;

    const IOutputSlots& GetIOutputSlots() const;

    const IConnectableLayers& GetIConnectableLayers() const;

    const IInputSlot* GetIInputSlot(unsigned int index) const;
    IInputSlot* GetIInputSlot(unsigned int index);

    const IOutputSlot* GetIOutputSlot(unsigned int index) const;

    OutputSlot* GetOutputSlot(unsigned int index);
    IOutputSlot* GetIOutputSlot(unsigned int index);

    unsigned int GetNumInputSlots() const;
    unsigned int GetNumOutputSlots() const;

    IConnectableLayerIterator begin();
    ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
                                     "begin() returning public IConnectableIterator", "24.05")
    IConnectableLayerIterator beginIConnectable();
    IConnectableLayerIterator end();//
    ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
                                     "end() returning public IConnectableLayerIterator", "24.05")
    IConnectableLayerIterator endIConnectable();
    ConstIConnectableIterator begin() const;

    ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
                                     "begin() returning public ConstIConnectableIterator", "24.05")
    ConstIConnectableIterator beginIConnectable() const;
    ConstIConnectableIterator end() const;
    ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
                                     "end() returning public ConstIConnectableIterator", "24.05")
    ConstIConnectableIterator endIConnectable() const;

    ConstIConnectableIterator cbegin() const;
    ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
                                     "cbegin() returning public ConstIterator", "24.05")
    ConstIConnectableIterator cbeginIConnectable() const;

    ConstIConnectableIterator cend() const;
    ARMNN_DEPRECATED_MSG_CHANGE_DATE("This function is deprecated and will be removed; please use "
                                     "cend() returning public ConstIConnectableIterator", "24.05")
    ConstIConnectableIterator cendIConnectable() const;

    void Clear();

    /// This method returns a copy of the original SubgraphView provided by OptimizeSubgraphView with a separate
    /// underlying graph from the main ArmNN graph.
    /// Backend users should edit this working copy and then add it as a SubstitutionPair, along with original
    /// SubgraphView, to the OptimizationViews returned by OptimizeSubgraphView.
    /// ArmNN will then decide on whether or not to carry out Substitution of the two SubgraphViews.
    SubgraphView GetWorkingCopy() const;

    /// These methods should be called on a working copy subgraph created from GetWorkingCopy.
    /// They take a SubgraphView pattern to replace and the substitute layer or subgraphView to substitute in.
    void SubstituteSubgraph(SubgraphView&, IConnectableLayer*);
    void SubstituteSubgraph(SubgraphView&, const SubgraphView&);

    /// These methods should be called on a working copy subgraph created from GetWorkingCopy.
    /// They return pointers to the input and output Slots belonging to the original SubgraphView
    /// that the working copy was created from.
    /// This may be used to find the original TensorInfo of connected boundary OutputSlots.
    const IInputSlots& GetOriginalInputSlots() const;
    const IOutputSlots& GetOriginalOutputSlots() const;

private:
    struct SubgraphViewWorkingCopy;

    /// Constructs a sub-graph with the given arguments.
    SubgraphView(IConnectableLayers&& layers,
                 IInputSlots&& inputs,
                 IOutputSlots&& outputs,
                 std::shared_ptr<SubgraphViewWorkingCopy> ptr);

    void CheckSubgraph();

    /// Arrange the order of layers topologically so that nodes can be visited in valid order
    void ArrangeBySortOrder();

    /// Updates the IInputSlots and IOutputSlots variables assigned to a SubgraphView
    void UpdateSubgraphViewSlotPointers(SubgraphView&, const SubgraphView&);

    /// The list of pointers to the input slots of the parent graph.
    InputSlots m_InputSlots;
    IInputSlots m_IInputSlots;

    /// The list of pointers to the output slots of the parent graph.
    OutputSlots m_OutputSlots;
    IOutputSlots m_IOutputSlots;

    /// The list of pointers to the layers of the parent graph.
    Layers m_Layers;
    IConnectableLayers m_IConnectableLayers;

    /// Pointer to internal graph implementation. This stores a working copy of a graph, separate from the main
    /// ArmNN graph, for use by Backends so that they can edit it and add as a SubstitutionPair to OptimizationViews
    /// along with the original SubgraphView.
    /// ArmNN will then decide on whether or not to substitute in the provided SubgraphView working copy.
    std::shared_ptr<SubgraphViewWorkingCopy> p_WorkingCopyImpl;
};
} // namespace armnn