dependency_graph.h 2.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

20
/*!
21
 * \file src/relay/analysis/dependency_graph.h
22
 * \brief create a dependency graph.
23
 */
24 25
#ifndef TVM_RELAY_ANALYSIS_DEPENDENCY_GRAPH_H_
#define TVM_RELAY_ANALYSIS_DEPENDENCY_GRAPH_H_
26 27 28 29

#include <tvm/relay/expr.h>
#include <unordered_map>
#include <vector>
30
#include "../transforms/let_list.h"
31
#include "../../support/arena.h"
32 33 34 35

namespace tvm {
namespace relay {

36 37
using support::LinkNode;
using support::LinkedList;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

/* DependencyGraph track input and output of an Expr.
 * Additionally, dummy scope is created to model scope.
 * It allow us to traverse the graph in reverse order.
 */
class DependencyGraph {
 public:
  /*! \brief A node in the graph. */
  struct Node {
    // Determine scope boundaries. Used for calculating scopes, not for
    // constructing dependency graph.
    bool new_scope = false;
    // incoming edges
    LinkedList<Node*> children;
    // outgoing edges
    LinkedList<Node*> parents;
  };

  /*! \brief Maps a Relay Expr to its node in the dependency graph. */
57
  std::unordered_map<Expr, Node*, ObjectHash, ObjectEqual> expr_node;
58 59 60 61 62 63 64 65 66

  /*! \brief The dependency graph in post DFS order. */
  std::vector<Node*> post_dfs_order;

  /*!
   * \brief Create a dependency graph.
   * \param arena The arena used for data allocation.
   * \param body The body of the expression to create a graph.
   */
67
  static DependencyGraph Create(support::Arena* arena, const Expr& body);
68 69 70 71 72 73 74

 private:
  class Creator;
};

}  // namespace relay
}  // namespace tvm
75
#endif  // TVM_RELAY_ANALYSIS_DEPENDENCY_GRAPH_H_