1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file src/relay/collage/dataflow_graph.h
22 * \brief A representation of the dataflow for an overall Relay expression.
23 */
24#ifndef TVM_RELAY_COLLAGE_DATAFLOW_GRAPH_H_
25#define TVM_RELAY_COLLAGE_DATAFLOW_GRAPH_H_
26
27#include <tvm/relay/expr.h>
28
29#include <memory>
30#include <vector>
31
32#include "../ir/indexed_graph.h"
33#include "./index_set.h"
34
35namespace tvm {
36namespace relay {
37namespace collage {
38
39/*!
40 * \brief Represents the dataflow of an overall Relay expression.
41 */
42class DataflowGraph {
43 public:
44 using Node = IndexedGraph<Expr>::Node;
45
46 explicit DataflowGraph(Expr expr);
47
48 size_t size() const { return indexed_graph_->size(); }
49 const Node* index_to_node(PostDfsIndex index) const {
50 return indexed_graph_->index_to_node(index);
51 }
52 const Node* item_to_node(const Expr& expr) const { return indexed_graph_->item_to_node(expr); }
53 const Node* item_to_node(const ExprNode* expr_node) const {
54 return indexed_graph_->item_to_node(expr_node);
55 }
56 const Expr& expr() const { return expr_; }
57 const IndexedGraph<Expr>& indexed_graph() const { return *indexed_graph_; }
58
59 const IndexSet& downstream_of(PostDfsIndex index) const {
60 ICHECK_LT(index, indexed_graph_->size());
61 return downstream_map_[index];
62 }
63
64 private:
65 /*! \brief The overall expression. */
66 Expr expr_;
67 /*! \brief The indexed graph which captures the main dataflow. */
68 std::unique_ptr<IndexedGraph<Expr>> indexed_graph_;
69 /*! \brief Map from a node's PostDfsIndex to the set of its downstream dataflow node indexes. */
70 std::vector<IndexSet> downstream_map_;
71};
72
73} // namespace collage
74} // namespace relay
75} // namespace tvm
76
77#endif // TVM_RELAY_COLLAGE_DATAFLOW_GRAPH_H_
78