TVM  0.9.4
Log.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <tvm/api.h>
6 
7 #include <cstdint>
8 #include <fstream>
9 #include <map>
10 #include <memory>
11 #include <set>
12 #include <string>
13 #include <typeindex>
14 #include <typeinfo>
15 #include <vector>
16 
17 namespace tvm
18 {
19 
20 namespace graph
21 {
22 class CallGraph;
23 
24 namespace internal
25 {
26 class Inputs;
27 
31 template<typename ObjType, typename MemberType, typename... Args>
32 bool lexLess(const ObjType & l, const ObjType & r, MemberType ObjType::* member, Args &&... args)
33 { return (l.*member) < (r.*member) || ((l.*member == r.*member) && lexLess(l, r, std::forward<Args>(args)...)); }
34 
38 template<typename ObjType, typename MemberType, typename... Args>
39 bool eq(const ObjType & l, const ObjType & r, MemberType ObjType::* member, Args &&... args)
40 { return (l.*member == r.*member) && eq(l, r, std::forward<Args>(args)...); }
41 
43 template<typename ObjType, typename MemberType>
44 bool lexLess(const ObjType & l, const ObjType & r, MemberType ObjType::* member)
45 { return (l.*member) < (r.*member); }
46 
48 template<typename ObjType, typename MemberType>
49 bool eq(const ObjType & l, const ObjType & r, MemberType ObjType::* member)
50 { return (l.*member) == (r.*member); }
51 
56 {
57 public:
59  struct EnumValue
60  {
62  template<typename E>
63  EnumValue(E e);
64 
65  std::type_index type; // representation of the enumeration type
66  int value; // value of the enumeration
67 
68  bool operator<(const EnumValue & other) const { return lexLess(*this, other, &EnumValue::type, &EnumValue::value); }
69  bool operator==(const EnumValue & other) const { return eq(*this, other, &EnumValue::type, &EnumValue::value); }
70  };
71 
73  struct Pointer
74  {
76  template<typename T>
77  Pointer(T * p);
78 
80  Pointer(const std::type_index & t, std::uintptr_t v);
81 
82  std::type_index type; // representation of the pointer type
83  std::uintptr_t value; // address of the pointer
84 
85  bool operator<(const Pointer & other) const { return lexLess(*this, other, &Pointer::value); }
86  bool operator==(const Pointer & other) const { return eq(*this, other, &Pointer::value); }
87  };
88 
90  struct Update
91  {
92  EnumValue id; // id of the update
93  std::string name; // name of the update
94  std::uintptr_t function; // address of the update function
95  Pointer owner; // address of the instance registering the update
96  bool operator<(const Update & other) const
97  { return lexLess(*this, other, &Update::owner, &Update::id, &Update::function); }
98  bool operator==(const Update & other) const
99  { return eq(*this, other, &Update::owner, &Update::id, &Update::function); }
100  };
101 
103  struct Output
104  {
105  EnumValue id; // id of the output
106  std::string name; // name of the output
107  Pointer owner; // address of the instance registering the output
108  bool operator<(const Output & other) const { return lexLess(*this, other, &Output::owner, &Output::id); }
109  bool operator==(const Output & other) const { return eq(*this, other, &Output::owner, &Output::id); }
110  };
111 
113  struct Input
114  {
115  EnumValue id; // id of the input
116  std::string name; // name of the input
117  Pointer source; // address of the instance providing the input
118  Pointer owner; // address of the instance registering the input
119  bool operator<(const Input & other) const
120  { return lexLess(*this, other, &Input::owner, &Input::id, &Input::source); }
121  bool operator==(const Input & other) const { return eq(*this, other, &Input::owner, &Input::id, &Input::source); }
122  };
123 
126  {
127  EnumValue input; // the input
128  EnumValue update; // the update
129  Pointer source; // address of the instance providing the input
130  Pointer owner; // address of the instance registering the dependency
131  bool operator<(const InputDependency & other) const
132  {
133  return lexLess(*this, other, &InputDependency::owner, &InputDependency::update, &InputDependency::source,
134  &InputDependency::input);
135  }
136  bool operator==(const InputDependency & other) const
137  {
138  return eq(*this, other, &InputDependency::owner, &InputDependency::update, &InputDependency::source,
139  &InputDependency::input);
140  }
141  };
142 
145  {
146  EnumValue update; // the update
147  EnumValue output; // the output
148  Pointer owner; // address of the instance registering the dependency
149  bool operator<(const OutputDependency & other) const
150  { return lexLess(*this, other, &OutputDependency::owner, &OutputDependency::update, &OutputDependency::output); }
151  bool operator==(const OutputDependency & other) const
152  { return eq(*this, other, &OutputDependency::owner, &OutputDependency::update, &OutputDependency::output); }
153  };
154 
157  {
158  EnumValue from; // the update depended upon
159  EnumValue to; // the depending update
160  Pointer owner; // address of the instance registering the dependency
161  bool operator<(const InternalDependency & other) const
162  { return lexLess(*this, other, &InternalDependency::owner, &InternalDependency::from, &InternalDependency::to); }
163  bool operator==(const InternalDependency & other) const
164  { return eq(*this, other, &InternalDependency::owner, &InternalDependency::from, &InternalDependency::to); }
165  };
166 
169  {
170  EnumValue input; // the input
171  EnumValue output; // the output
172  Pointer source; // address of the instance providing the input
173  Pointer owner; // address of the instance registering the dependency
174  bool operator<(const DirectDependency & other) const
175  {
176  return lexLess(*this, other, &DirectDependency::owner, &DirectDependency::output, &DirectDependency::source,
177  &DirectDependency::input);
178  }
179  bool operator==(const DirectDependency & other) const
180  {
181  return eq(*this, other, &DirectDependency::owner, &DirectDependency::output, &DirectDependency::source,
182  &DirectDependency::input);
183  }
184  };
185 
187  std::pair<std::vector<Log::Output>, std::vector<Log::Update>> subGraph(const CallGraph * const g) const;
188 
190  std::pair<std::vector<Log::Output>, std::vector<Log::Update>> subGraph(const Output out) const;
191 
193  std::string generateDot(const Pointer & p) const;
194 
196  std::string generateDot(const CallGraph * const g) const;
197 
201  std::string generateDot(const std::vector<Log::Output> & outHighlight = {},
202  const std::vector<Log::Update> & upHighlight = {}) const;
203 
208  const std::type_index & getPromotedType(const Pointer & p) const;
209 
210  // raw logs
211  std::vector<Update> updates_;
212  std::vector<Input> inputs_;
213  std::vector<Output> outputs_;
214  std::vector<InputDependency> inputDependencies_;
215  std::vector<OutputDependency> outputDependencies_;
216  std::vector<InternalDependency> internalDependencies_;
217  std::vector<DirectDependency> directDependencies_;
218 
222  std::map<Pointer, std::vector<Pointer>> graphOutputs_;
223 
232  std::map<std::uintptr_t, std::vector<std::type_index>> types_;
233 
234 private:
235  std::pair<std::vector<Log::Output>, std::vector<Log::Update>> followUpDependency(
236  const std::vector<Output> & allOutputs,
237  const std::vector<Output> & startingPoints) const;
238 
242  std::string nodeName(const Log::Output & output) const;
246  std::string nodeName(const Log::Input & input) const;
250  std::string nodeName(const Log::Update & update) const;
251 };
252 
253 template<typename E>
254 inline Log::EnumValue::EnumValue(E e) : type(typeid(e)), value(static_cast<int>(e))
255 { static_assert(std::is_enum<E>::value, "EnumValue can only be defined for enums."); }
256 
257 template<typename T>
258 inline Log::Pointer::Pointer(T * p) : type(typeid(*p)), value(reinterpret_cast<std::uintptr_t>(p))
259 {}
260 
261 inline Log::Pointer::Pointer(const std::type_index & t, std::uintptr_t v) : type(t), value(v) {}
262 
263 } // namespace internal
264 
265 } // namespace graph
266 
267 } // namespace tvm
#define TVM_DLLAPI
Definition: api.h:35
Definition: CallGraph.h:23
Definition: Log.h:56
const std::type_index & getPromotedType(const Pointer &p) const
std::vector< InternalDependency > internalDependencies_
Definition: Log.h:216
std::vector< OutputDependency > outputDependencies_
Definition: Log.h:215
std::pair< std::vector< Log::Output >, std::vector< Log::Update > > subGraph(const Output out) const
std::string generateDot(const std::vector< Log::Output > &outHighlight={}, const std::vector< Log::Update > &upHighlight={}) const
std::vector< Output > outputs_
Definition: Log.h:213
std::vector< Input > inputs_
Definition: Log.h:212
std::pair< std::vector< Log::Output >, std::vector< Log::Update > > subGraph(const CallGraph *const g) const
std::vector< Update > updates_
Definition: Log.h:211
std::vector< InputDependency > inputDependencies_
Definition: Log.h:214
std::map< Pointer, std::vector< Pointer > > graphOutputs_
Definition: Log.h:222
std::string generateDot(const CallGraph *const g) const
std::vector< DirectDependency > directDependencies_
Definition: Log.h:217
std::map< std::uintptr_t, std::vector< std::type_index > > types_
Definition: Log.h:232
std::string generateDot(const Pointer &p) const
Definition: probe.h:44
bool lexLess(const ObjType &l, const ObjType &r, MemberType ObjType::*member, Args &&... args)
Definition: Log.h:32
bool eq(const ObjType &l, const ObjType &r, MemberType ObjType::*member, Args &&... args)
Definition: Log.h:39
Definition: Clock.h:12
bool operator==(const DirectDependency &other) const
Definition: Log.h:179
Pointer owner
Definition: Log.h:173
Pointer source
Definition: Log.h:172
bool operator<(const DirectDependency &other) const
Definition: Log.h:174
EnumValue output
Definition: Log.h:171
EnumValue input
Definition: Log.h:170
std::type_index type
Definition: Log.h:65
bool operator==(const EnumValue &other) const
Definition: Log.h:69
bool operator<(const EnumValue &other) const
Definition: Log.h:68
int value
Definition: Log.h:66
EnumValue(E e)
Definition: Log.h:254
bool operator<(const InputDependency &other) const
Definition: Log.h:131
EnumValue input
Definition: Log.h:127
Pointer source
Definition: Log.h:129
EnumValue update
Definition: Log.h:128
Pointer owner
Definition: Log.h:130
bool operator==(const InputDependency &other) const
Definition: Log.h:136
Pointer owner
Definition: Log.h:118
std::string name
Definition: Log.h:116
bool operator<(const Input &other) const
Definition: Log.h:119
Pointer source
Definition: Log.h:117
bool operator==(const Input &other) const
Definition: Log.h:121
EnumValue id
Definition: Log.h:115
bool operator==(const InternalDependency &other) const
Definition: Log.h:163
EnumValue to
Definition: Log.h:159
bool operator<(const InternalDependency &other) const
Definition: Log.h:161
EnumValue from
Definition: Log.h:158
EnumValue update
Definition: Log.h:146
Pointer owner
Definition: Log.h:148
EnumValue output
Definition: Log.h:147
bool operator==(const OutputDependency &other) const
Definition: Log.h:151
bool operator<(const OutputDependency &other) const
Definition: Log.h:149
bool operator==(const Output &other) const
Definition: Log.h:109
std::string name
Definition: Log.h:106
Pointer owner
Definition: Log.h:107
EnumValue id
Definition: Log.h:105
bool operator<(const Output &other) const
Definition: Log.h:108
std::type_index type
Definition: Log.h:82
bool operator<(const Pointer &other) const
Definition: Log.h:85
bool operator==(const Pointer &other) const
Definition: Log.h:86
Pointer(T *p)
Definition: Log.h:258
std::uintptr_t value
Definition: Log.h:83
bool operator==(const Update &other) const
Definition: Log.h:98
bool operator<(const Update &other) const
Definition: Log.h:96
Pointer owner
Definition: Log.h:95
EnumValue id
Definition: Log.h:92
std::string name
Definition: Log.h:93