TVM  0.9.4
AbstractNode.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-2020 CNRS-AIST JRL and CNRS-UM LIRMM
3  */
4 
5 #pragma once
6 
7 #include <tvm/api.h>
8 
10 
11 #include <cassert>
12 #include <functional>
13 #include <vector>
14 
15 namespace tvm
16 {
17 
18 namespace graph
19 {
20 
21 class CallGraph;
22 
23 namespace abstract
24 {
25 template<typename T>
26 class Node;
27 }
28 
29 namespace internal
30 {
31 
36 class AbstractNode : public Inputs, public abstract::Outputs
37 {
38 public:
39  template<typename T>
41 
42  friend class tvm::graph::CallGraph;
43 
45  enum class Update_
46  {
47  };
49  struct Update
50  {};
51 
53  static constexpr unsigned int UpdateSize = 0;
54 
57 
60 
62  static constexpr auto UpdateBaseName = "AbstractNode";
63 
65  static constexpr const char * UpdateName(Update_) { return "INVALID"; }
66 
70  template<typename EnumT>
71  bool isUpdateEnabled(EnumT e) const
72  {
73  int i = static_cast<int>(e);
75  }
76 
84  virtual bool isUpdateStaticallyEnabled(int) const { return true; }
85 
91  virtual bool isUpdateCustomEnabled(int) const { return true; }
92 
98  template<typename EnumT>
99  static constexpr bool UpdateStaticallyEnabled(EnumT)
100  { return true; }
101 
102  virtual ~AbstractNode() = default;
103 
105  inline void update(int i)
106  {
107  assert(updates_.count(i));
108  updates_[i](*this);
109  }
110 
111 protected:
113  std::map<int, std::function<void(AbstractNode &)>> updates_;
114 
116  std::map<int, std::vector<int>> outputDependencies_;
118  std::map<int, std::vector<int>> internalDependencies_;
120  using input_dependency_t = std::map<Outputs *, std::set<int>>;
121  std::map<int, input_dependency_t> inputDependencies_;
123  std::map<int, std::pair<Outputs *, int>> directDependencies_;
124 
125 private:
126  AbstractNode() { is_node_ = true; }
127 };
128 
138 #define SET_UPDATES(SelfT, ...) PP_ID(EXTEND_ENUM(Update, SelfT, __VA_ARGS__))
139 
141 #define DISABLE_UPDATES(...) PP_ID(DISABLE_SIGNALS(Update, __VA_ARGS__))
142 
144 #define CLEAR_DISABLED_UPDATES() PP_ID(CLEAR_DISABLED_SIGNALS(Update))
145 
147 template<typename T, typename EnumT>
148 constexpr bool is_valid_update(EnumT v)
149 {
150  static_assert(std::is_base_of<AbstractNode, T>::value,
151  "Cannot test update validity for a type that is not derived of Updates");
152  static_assert(std::is_enum<EnumT>::value, "Cannot test update validity for a value that is not an enumeration");
153  return std::is_same<typename T::Update_, EnumT>::value
154  || ((!std::is_same<typename T::UpdateParent, typename T::UpdateBase>::value)
155  && is_valid_update<typename T::UpdateParent>(v));
156 }
157 
159 template<typename T, typename EnumT, typename... Args>
160 constexpr bool is_valid_update(EnumT v, Args... args)
161 { return is_valid_update<T>(v) && is_valid_update<T>(args...); }
162 
163 } // namespace internal
164 
165 } // namespace graph
166 
167 } // namespace tvm
Definition: CallGraph.h:23
Definition: Node.h:27
Definition: Outputs.h:31
bool is_node_
Definition: Outputs.h:101
Definition: AbstractNode.h:37
std::map< Outputs *, std::set< int > > input_dependency_t
Definition: AbstractNode.h:120
virtual bool isUpdateCustomEnabled(int) const
Definition: AbstractNode.h:91
static constexpr bool UpdateStaticallyEnabled(EnumT)
Definition: AbstractNode.h:99
static constexpr auto UpdateBaseName
Definition: AbstractNode.h:62
bool isUpdateEnabled(EnumT e) const
Definition: AbstractNode.h:71
Update_
Definition: AbstractNode.h:46
std::map< int, std::vector< int > > outputDependencies_
Definition: AbstractNode.h:116
static constexpr unsigned int UpdateSize
Definition: AbstractNode.h:53
void update(int i)
Definition: AbstractNode.h:105
virtual bool isUpdateStaticallyEnabled(int) const
Definition: AbstractNode.h:84
std::map< int, std::pair< Outputs *, int > > directDependencies_
Definition: AbstractNode.h:123
std::map< int, std::vector< int > > internalDependencies_
Definition: AbstractNode.h:118
std::map< int, std::function< void(AbstractNode &)> > updates_
Definition: AbstractNode.h:113
std::map< int, input_dependency_t > inputDependencies_
Definition: AbstractNode.h:121
static constexpr const char * UpdateName(Update_)
Definition: AbstractNode.h:65
Definition: Inputs.h:25
constexpr bool is_valid_update(EnumT v)
Definition: AbstractNode.h:148
std::map< KeyWithId, Value, IdLess< KeyWithId >, Allocator > map
Definition: map.h:41
Definition: Clock.h:12
Definition: AbstractNode.h:50