aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/subgraph_traverser.h
blob: d6b0e8d7f27823b506fd4bd60282c4a1e22a8a71 (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

// Copyright (c) 2020-2023, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#ifndef SUBGRAPH_TRAVERSER_H
#define SUBGRAPH_TRAVERSER_H

#include "graph_node.h"
#include "graph_status.h"
#include "model_common.h"
#include "ops/op_factory.h"
#include "tensor.h"
#include "tosa_serialization_handler.h"
#include <unordered_set>

namespace TosaReference
{

class SubgraphTraverser
{
public:
    SubgraphTraverser(TosaSerializationBasicBlock* block, TosaSerializationHandler* tsh, SubgraphTraverser* parent_sgt);
    ~SubgraphTraverser();

    int initializeGraph();
    int isFullyEvaluated() const;
    int evaluateNextNode();
    int evaluateAll();

    GraphStatus getGraphStatus() const
    {
        return graph_status;
    }
    void setGraphStatus(GraphStatus status)
    {
        graph_status = status;
    }

    int linkTensorsAndNodes();
    int validateGraph();
    int allocateInputTensors();
    int allocateTensor(std::string name);

    int dumpGraph(FILE* out) const;
    int dumpNextNodeList(FILE* out) const;
    int clearAllNodeMarkings();

    std::string getBlockName() const
    {
        return block->GetName();
    }
    std::string getRegionName() const
    {
        return block->GetRegionName();
    }
    TosaSerializationHandler* getTsh() const
    {
        return tsh;
    }
    int getNumInputTensors() const;
    Tensor* getInputTensor(const unsigned int idx) const;
    Tensor* getInputTensorByName(const std::string name) const;
    int getNumOutputTensors() const;
    Tensor* getOutputTensor(const unsigned int idx) const;
    Tensor* getOutputTensorByName(const std::string name) const;
    int getNumVariableTensors() const;
    Tensor* getVariableTensor(const unsigned int idx) const;
    Tensor* getVariableTensorByName(const std::string name) const;
    int registerVariableTensor(Tensor* tensor);
    int addToNextNodeList(GraphNode*);

private:
    int addTensor(const TosaSerializationTensor* ts);
    int addNode(GraphNode* cn);

    Tensor* findTensorByName(const std::string& name) const;

    GraphNode* getNextNode();

    GraphStatus graph_status;

    // pointer to the parent subgraph traversal if exists
    // e.g., Control Flow Ops will have nested blocks (subgraph traversals)
    SubgraphTraverser* parent_sgt;

    // pointer to serialization library and corresponding basic block
    TosaSerializationBasicBlock* block;
    TosaSerializationHandler* tsh;

    // The definitive list of all tensors
    std::vector<Tensor*> tensors;

    // The subset of tensors that are also input tensors
    std::vector<Tensor*> inputTensors;

    // The subset of tensors that are also output tensors
    std::vector<Tensor*> outputTensors;

    // The subset of tensors that are also variable tensors
    std::vector<Tensor*> variableTensors;

    // The definitive list of all nodes in the graph
    std::vector<GraphNode*> nodes;

    // The subset of node that have all of their input tensors ready, but
    // have not yet been evaluated to produce their output tensors.
    // With control flow, a node may appear on this list more than once during its
    // lifetime, although the list itself should only contain unique nodes.
    std::list<GraphNode*> nextNodeList;

    // tensor name set which contains all the name used by operator
    std::unordered_set<std::string> used_tensor_name_set;

    // Maximum number of times to evalute a node before
    // warning.
    const int MAX_EVAL_COUNT = 10000;
};
};    // namespace TosaReference

#endif