Tensor Comprehensions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
execution_engine.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <dlpack/dlpack.h>
24 
26 #include "tc/core/tc_executor.h"
27 
28 namespace tc {
29 
35  public:
36  struct ExecutorInfo {
38  std::string id,
39  std::vector<const DLTensor*> inputsInfo,
40  std::unique_ptr<MappingOptions> options,
41  lang::TreeRef tc,
42  size_t handle)
43  : identifier(id),
44  inputsInfo(dlutils::makeDLTensorVector(inputsInfo)),
45  options(std::move(options)),
46  exec(tc, inputsInfo),
47  objectLocalHandle(handle) {}
48 
49  void clear() {
50  exec.clearRTC();
51  }
52 
53  std::string identifier;
54  std::vector<dlutils::DLTensorUPtr> inputsInfo;
55  std::unique_ptr<MappingOptions> options;
60  };
61 
62  ExecutionEngine() = default;
63 
66  void define(const std::string& language);
67 
70  void define(const std::vector<lang::TreeRef>& treeRefs);
71 
72  void addTC(const std::string& tc);
73 
76  std::vector<const DLTensor*> inferOutputTensorInfo(
77  const std::string& name,
78  const std::vector<const DLTensor*>& inTensorPtrs);
79 
80  lang::TreeRef treeForFunction(const std::string& name) {
81  return tcNameMap_.at(name);
82  }
83 
84  // TODO: Pass autotuning info (none by default, otherwise some struct with
85  // maxtime and other things)
86 
88  size_t compile(
89  const std::string& name,
90  const std::vector<const DLTensor*>& inputs,
91  const MappingOptions& options);
92 
93  // TODO: sanity check on name and input / output sizes.
102  Duration run(
103  size_t handle,
104  const std::vector<const DLTensor*>& inputs,
105  const std::vector<DLTensor*>& outputs,
106  bool profile = false,
107  std::function<bool(const ExecutorInfo*)> pruningFunction =
108  [](const ExecutorInfo*) { return false; });
109 
115  void uncheckedRun(
116  size_t handle,
117  const std::vector<const void*>& inputs,
118  const std::vector<void*>& outputs);
119 
120  void clear(size_t handle);
121 
122  private:
123  size_t getHandle(
124  const std::string& name,
125  const std::vector<const DLTensor*>& inputsInfo,
126  const MappingOptions& options);
127  std::unique_ptr<ExecutorInfo> makeExecutorInfo(
128  const std::string& name,
129  const std::vector<const DLTensor*>& inputsInfo,
130  const MappingOptions& options);
131  size_t emplaceExecutor(std::unique_ptr<ExecutorInfo> p);
132 
134  std::mutex executorInfoMutex;
135 
136  // XXX:if ExecutorInfo is moved/copied (even when the vector's underlying
137  // storage is extended) something inside isl segfaults, unique_ptr is used as
138  // a workaround
139  std::vector<std::unique_ptr<ExecutorInfo>> executors_;
140  std::map<std::string, lang::TreeRef> tcNameMap_;
141 
142  size_t uidCounter = 0;
143 };
144 
145 } // namespace tc
Duration run(size_t handle, const std::vector< const DLTensor * > &inputs, const std::vector< DLTensor * > &outputs, bool profile=false, std::function< bool(const ExecutorInfo *)> pruningFunction=[](const ExecutorInfo *){return false;})
std::map< std::string, lang::TreeRef > tcNameMap_
Definition: execution_engine.h:140
Definition: execution_engine.h:34
void clear(size_t handle)
std::unique_ptr< ExecutorInfo > makeExecutorInfo(const std::string &name, const std::vector< const DLTensor * > &inputsInfo, const MappingOptions &options)
ExecutorInfo(std::string id, std::vector< const DLTensor * > inputsInfo, std::unique_ptr< MappingOptions > options, lang::TreeRef tc, size_t handle)
Definition: execution_engine.h:37
void addTC(const std::string &tc)
void define(const std::string &language)
size_t uidCounter
Definition: execution_engine.h:142
void clearRTC()
Definition: tc_executor.h:91
Definition: execution_engine.h:36
Definition: mapping_options.h:336
size_t objectLocalHandle
Definition: execution_engine.h:59
size_t emplaceExecutor(std::unique_ptr< ExecutorInfo > p)
std::vector< dlutils::DLTensorUPtr > inputsInfo
Definition: execution_engine.h:54
std::chrono::high_resolution_clock::duration Duration
Definition: rtc.h:31
lang::TreeRef treeForFunction(const std::string &name)
Definition: execution_engine.h:80
void clear()
Definition: execution_engine.h:49
std::mutex executorInfoMutex
For thread-safety perform all cheap operations under lock.
Definition: execution_engine.h:134
void uncheckedRun(size_t handle, const std::vector< const void * > &inputs, const std::vector< void * > &outputs)
std::vector< DLTensorUPtr > makeDLTensorVector(const std::vector< T * > &ptrs)
Definition: dlpack-inl.h:181
std::vector< std::unique_ptr< ExecutorInfo > > executors_
Definition: execution_engine.h:139
TcExecutor exec
Definition: execution_engine.h:56
std::string identifier
Definition: execution_engine.h:53
ExecutionEngine()=default
size_t compile(const std::string &name, const std::vector< const DLTensor * > &inputs, const MappingOptions &options)
Returns a handle for the compiled kernel.
std::unique_ptr< MappingOptions > options
Definition: execution_engine.h:55
size_t getHandle(const std::string &name, const std::vector< const DLTensor * > &inputsInfo, const MappingOptions &options)
std::shared_ptr< Tree > TreeRef
Definition: tree.h:44
std::vector< const DLTensor * > inferOutputTensorInfo(const std::string &name, const std::vector< const DLTensor * > &inTensorPtrs)
Definition: tc_executor.h:28