aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/experimental/DependencyGraph.h
blob: 794bf0e344136f079a2a810d44abd10b4c8a6b5c (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (c) 2022 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
#error "This experimental feature must be enabled with -DENABLE_EXPERIMENTAL_DYNAMIC_FUSION"
#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */
#ifndef ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_DEPENDENCYGRAPH_H
#define ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_DEPENDENCYGRAPH_H

#include "arm_compute/core/Error.h"

#include <algorithm>
#include <map>
#include <vector>

namespace arm_compute
{
namespace experimental
{
namespace dynamic_fusion
{
template <typename T>
bool is_in(const T &v, const std::vector<T> &vec)
{
    return std::find(std::begin(vec), std::end(vec), v) != std::end(vec);
}

/** The dependency graph of a workload, where the nodes are of 2 types: Tensor or Operator
 *  Represented as a doubly-linked adjacency list with the differentiation between source and destination
 *
 * A "Merge Tensor" is an external tensor associated with the tensor within the graph, and serve as a merge point
 */
class DependencyGraph
{
public:
    /** A serial Id allocator
     *
     */
    class SerialIdAllocator
    {
    public:
        using Id = int;
        Id alloc()
        {
            return _counter++;
        }
        constexpr static Id empty()
        {
            return -1;
        }

    private:
        Id _counter{ 0 };
    };
    using Id = SerialIdAllocator::Id;
    /** Adjacency list
     *
     */
    using AdjList = std::map<Id, std::vector<Id>>;

    /** A pack of operator including its input and output tensors, used by traversing through the graph in topological order
     *
     */
    struct OpPack
    {
        Id              op{};
        std::vector<Id> inputs{};
        std::vector<Id> outputs{};
        friend bool operator==(const OpPack &opp0, const OpPack &opp1)
        {
            return std::make_tuple(
                       opp0.op, opp0.inputs, opp0.outputs)
                   == std::make_tuple(
                       opp1.op, opp1.inputs, opp1.outputs);
        }
    };

public:
    constexpr static Id empty_id()
    {
        return SerialIdAllocator::empty();
    }

    DependencyGraph() = default;
    // Used in cases where two DependencyGraphs may want to share the same configuration of tensors
    explicit DependencyGraph(const std::vector<Id> &imported_tensors);
    // Testing only
    DependencyGraph(const AdjList &adj_src_tensors, const AdjList &adj_dst_tensors, const AdjList &adj_src_ops, const AdjList &adj_dst_ops, std::map<Id, Id> merge_points = {});

    /** Add a new tensor
     *
     * @param merge_tensor The external merge point associated with the tensor. Leave empty if not needed.
     * @return Id  The newly allocated tensor, or a previously added tensor associated with @p merge_tensor
     */
    Id add_tensor(Id merge_tensor = empty_id());

    void remove_tensor(Id tensor);

    /** Add a new operator
     *
     * @param inputs  Input tensors to the operator
     * @param outputs  Output tensors to the operator
     * @return std::pair<Status, DependencyGraph::Id> where id is the newly allocated operator
     */
    std::pair<Status, DependencyGraph::Id> add_operator(const std::vector<Id> &inputs, const std::vector<Id> &outputs);

    void remove_operator(Id op);
    /** Sort the graph in a topological order
     *
     * @return std::pair<Status, std::vector<OpPack>>
     */
    std::pair<Status, std::vector<OpPack>> topological_sort() const;

    std::vector<Id> src_ops(Id op) const;
    std::vector<Id> dst_ops(Id op) const;

    std::vector<Id> src_ops_from_tensor(Id tensor) const;
    std::vector<Id> dst_ops_from_tensor(Id tensor) const;
    /** Get the merge points object
     *
     * @return std::map<Id, Id>
     */
    std::map<Id, Id> get_merge_points() const;
    /** Get all root ops. Root ops can also be referred to as "src ops" of the whole graph
     *
     * @return std::vector<Id>
     */
    std::vector<Id> get_root_ops() const;
    /** Get all dst ops of the whole graph
     *
     * @return std::vector<Id>
     */
    std::vector<Id> get_dst_ops() const;

    /** Get source tensors to an operator
     *
     * @param op
     * @return std::vector<Id>
     */
    std::vector<Id> src_tensors(Id op) const;
    /** Get destination tensors to an operator
     *
     * @param op
     * @return std::vector<Id>
     */
    std::vector<Id> dst_tensors(Id op) const;
    /** Get source tensors of the whole graph
     *
     * @return std::vector<Id>
     */
    std::vector<Id> src_tensors() const;
    /** Get destination tensors of the whole graph
     *
     * @return std::vector<Id>
     */
    std::vector<Id> dst_tensors() const;
    /** Get all operators
     *
     * @return std::vector<Id>
     */
    std::vector<Id> all_ops() const;
    /** Get all tensors
     *
     * @return std::vector<Id>
     */
    std::vector<Id> all_tensors() const;
    /** Number of operators
     *
     * @return unsigned int
     */
    unsigned int number_of_ops() const;
    /** Number of tensors
     *
     * @return unsigned int
     */
    unsigned int number_of_tensors() const;

    /** Update @p merge_point to point to @p t_id
     *
     * @param t_id
     * @param merge_point
     */
    Status update_merge_point(Id t_id, Id merge_point);

    /** Strict equality comparison (all internal ids and order of insertion matter).
     *        In the future this may be replaced with a topological comparison, allowing equivalent graphs with different internal ids to be equal
     *
     *
     * @param g0
     * @param g1
     * @return true
     * @return false
     */
    friend bool operator==(const DependencyGraph &g0, const DependencyGraph &g1)
    {
        // Do not compare id allocators
        return std::make_tuple(
                   g0._adj_src_tensors, g0._adj_dst_tensors, g0._adj_src_ops, g0._adj_dst_ops, g0._merge_to_internal)
               == std::make_tuple(
                   g1._adj_src_tensors, g1._adj_dst_tensors, g1._adj_src_ops, g1._adj_dst_ops, g1._merge_to_internal);
    }
    void link_input(Id op, Id in_tensor);
    void link_output(Id op, Id out_tensor);
    /** Check if there's a path from @p src_tensor to @p dst_op
     *
     * @param src_tensor
     * @param dst_op
     * @return true
     * @return false
     */
    bool path_exists_from_tensor_to_op(Id src_tensor, Id dst_op) const;
    /** Check if there's a path from @p src_op to @p dst_op
     *
     * @param src_op
     * @param dst_op
     * @return true
     * @return false
     */
    bool path_exists_from_op_to_op(Id src_op, Id dst_op) const;
    /** Check if tensor is the src tensor of the entire graph
     *
     * @param tensor
     * @return true
     * @return false
     */
    bool is_src_tensor(Id tensor) const;
    /** Check if tensor is the dst tensor of the entire graph
     *
     * @param tensor
     * @return true
     * @return false
     */
    bool is_dst_tensor(Id tensor) const;

private:
    Id   insert_new_tensor();
    Id   insert_new_op();
    bool tensor_exists(Id tensor) const;
    bool operator_exists(Id op) const;
    bool is_src_tensor_of(Id op, Id tensor) const;
    bool is_dst_tensor_of(Id op, Id tensor) const;
    bool are_connected(Id op, Id tensor) const;

private:
    AdjList _adj_src_tensors{};
    AdjList _adj_dst_tensors{};
    AdjList _adj_src_ops{};
    AdjList _adj_dst_ops{};
    std::map<Id, Id> _merge_to_internal{}; // From merge tensor to internal tensor
    SerialIdAllocator _operator_id{};
    SerialIdAllocator _tensor_id{};
};

} // namespace dynamic_fusion
} // namespace experimental
} // namespace arm_compute

#endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_DEPENDENCYGRAPH_H