tvm
vm.h
Go to the documentation of this file.
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 
24 #ifndef TVM_RUNTIME_VM_VM_H_
25 #define TVM_RUNTIME_VM_VM_H_
26 
28 #include <tvm/runtime/module.h>
29 #include <tvm/runtime/object.h>
31 #include <tvm/runtime/registry.h>
35 
36 #include <memory>
37 #include <string>
38 #include <unordered_map>
39 #include <utility>
40 #include <vector>
41 
42 namespace tvm {
43 namespace runtime {
44 namespace vm {
45 
49 class VMClosureObj : public ClosureObj {
50  public:
55  size_t func_index;
57  std::vector<ObjectRef> free_vars;
58 
59  static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
60  static constexpr const char* _type_key = "vm.Closure";
62 };
63 
65 class VMClosure : public Closure {
66  public:
67  VMClosure(size_t func_index, std::vector<ObjectRef> free_vars);
69 };
70 
77 struct VMFunction {
79  std::string name;
81  std::vector<std::string> params;
83  std::vector<Instruction> instructions;
87  std::vector<Index> params_device_type;
88 
89  VMFunction(const std::string& name, std::vector<std::string> params,
90  const std::vector<Instruction>& instructions, Index register_file_size,
91  const std::vector<Index> params_device_type = {})
92  : name(name),
93  params(params),
94  instructions(instructions),
95  register_file_size(register_file_size),
96  params_device_type(params_device_type) {}
97 
99 
100  friend std::ostream& operator<<(std::ostream& os, const VMFunction&);
101 };
102 
110 struct VMFrame {
119 
121  std::vector<ObjectRef> register_file;
122 
125 
126  VMFrame(Index pc, Index func_index, Index args, const Instruction* code, Index register_file_size)
127  : pc(pc),
128  func_index(func_index),
129  args(args),
130  code(code),
131  register_file(register_file_size),
132  caller_return_register(0) {}
133 };
134 
147  public:
165  virtual PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self);
166 
167  virtual ~VirtualMachine() {}
168 
169  const char* type_key() const final { return "VirtualMachine"; }
170 
171  VirtualMachine() : frames_(), func_index_(0), code_(nullptr), pc_(0), exec_(nullptr) {}
172 
177  virtual void LoadExecutable(const Executable* exec);
178 
179  protected:
181  void PushFrame(Index arg_count, Index ret_pc, const VMFunction& vm_func);
182 
187  Index PopFrame();
188 
194  inline void WriteRegister(RegName reg, const ObjectRef& obj);
195 
201  ObjectRef ReadRegister(RegName reg) const;
202 
208  int64_t LoadScalarInt(RegName reg) const;
209 
216  ObjectRef Invoke(const VMFunction& func, const std::vector<ObjectRef>& args);
217 
218  // TODO(@jroesch): I really would like this to be a global variable.
225  ObjectRef Invoke(const std::string& name, const std::vector<ObjectRef>& args);
226 
238  virtual void InvokePacked(Index packed_index, const PackedFunc& func, Index arg_count,
239  Index output_size, const std::vector<ObjectRef>& args);
240 
246  void Init(const std::vector<Device>& devices, const std::vector<AllocatorType>& alloc_types);
247 
249  void RunLoop();
250 
252  Device GetDevice(Index device_type) const;
253 
259  void InvokeGlobal(const VMFunction& func, const std::vector<ObjectRef>& args);
260 
269  void SetInput(std::string name, TVMArgs args, int offset);
270 
280  virtual void OpStartHook(Instruction instr);
281 
285  virtual void OpStopHook();
286 
287  protected:
289  std::vector<PackedFunc> packed_funcs_;
291  std::vector<VMFrame> frames_;
303  std::unordered_map<std::string, std::vector<ObjectRef>> inputs_;
305  std::vector<Device> devices_;
307  std::vector<Allocator*> allocators_;
312  std::vector<ObjectRef> const_pool_;
313 };
314 
315 } // namespace vm
316 } // namespace runtime
317 } // namespace tvm
318 
319 #endif // TVM_RUNTIME_VM_VM_H_
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:69
std::unordered_map< std::string, std::vector< ObjectRef > > inputs_
The function name to inputs mapping.
Definition: vm.h:303
constexpr const char * device_type
The device type.
Definition: stmt.h:1261
The executable emitted by the VM compiler.
Definition: executable.h:57
A custom smart pointer for Object.
Definition: object.h:356
std::vector< ObjectRef > const_pool_
The constant pool for runtime. It caches the device dependent object to avoid rellocation of constant...
Definition: vm.h:312
RegName caller_return_register
Register in caller&#39;s frame to put return value.
Definition: vm.h:124
std::string name
The function&#39;s name.
Definition: vm.h:79
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Index func_index
The index into the function table, points to the caller.
Definition: vm.h:114
Index func_index_
The fuction table index of the current function.
Definition: vm.h:293
A single virtual machine instruction.
Definition: bytecode.h:82
Runtime Closure container types.
reference to closure.
Definition: closure.h:44
Abstract device memory management API.
VMFunction()
Definition: vm.h:98
const char * type_key() const final
Definition: vm.h:169
The Relay virtual machine executable.
std::vector< Device > devices_
The set of TVM devices the VM is currently executing on.
Definition: vm.h:305
Arguments into TVM functions.
Definition: packed_func.h:335
VMFunction(const std::string &name, std::vector< std::string > params, const std::vector< Instruction > &instructions, Index register_file_size, const std::vector< Index > params_device_type={})
Definition: vm.h:89
std::vector< ObjectRef > register_file
Statically allocated space for objects.
Definition: vm.h:121
int64_t RegName
A register name.
Definition: bytecode.h:38
The bytecode for Relay virtual machine.
std::vector< PackedFunc > packed_funcs_
The virtual machine&#39;s packed function table.
Definition: vm.h:289
std::vector< Allocator * > allocators_
The cached memory allocators.
Definition: vm.h:307
std::vector< Instruction > instructions
The instructions representing the function.
Definition: vm.h:83
size_t func_index
The index into the function list. The function could be any function object that is compatible to the...
Definition: vm.h:55
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:706
DLDevice Device
Definition: ndarray.h:43
std::vector< std::string > params
The function parameter names.
Definition: vm.h:81
int64_t Index
An alias for the integer type used ubiquitously in the VM.
Definition: bytecode.h:43
std::vector< ObjectRef > free_vars
The free variables of the closure.
Definition: vm.h:57
A representation of a stack frame.
Definition: vm.h:110
A representation of a Relay function in the VM.
Definition: vm.h:77
static constexpr const char * _type_key
Definition: vm.h:60
Base class of all object reference.
Definition: object.h:504
static constexpr const uint32_t _type_index
Definition: vm.h:59
Base container of module.
Definition: module.h:111
std::vector< VMFrame > frames_
The current stack of call frames.
Definition: vm.h:291
TVM_DECLARE_FINAL_OBJECT_INFO(VMClosureObj, ClosureObj)
VirtualMachine()
Definition: vm.h:171
An object representing a closure. This object is used by both the Relay VM and interpreter.
Definition: closure.h:36
A managed object in the TVM runtime.
Index pc_
The virtual machine PC.
Definition: vm.h:297
virtual ~VirtualMachine()
Definition: vm.h:167
Index register_file_size
The size of the frame for this function.
Definition: vm.h:85
Runtime container of the functions generated by TVM, This is used to support dynamically link...
Index args
The number of arguments.
Definition: vm.h:116
std::vector< Index > params_device_type
The device type of each parameter for this function.
Definition: vm.h:87
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:68
const Instruction * code_
The current pointer to the code section.
Definition: vm.h:295
ObjectRef return_register_
The special return register.
Definition: vm.h:299
VMFrame(Index pc, Index func_index, Index args, const Instruction *code, Index register_file_size)
Definition: vm.h:126
const Executable * exec_
The executable the VM will operate on.
Definition: vm.h:301
const Instruction * code
A pointer into the caller function&#39;s instructions.
Definition: vm.h:118
The virtual machine.
Definition: vm.h:146
Type index is allocated during runtime.
Definition: object.h:78
Index pc
The return program counter.
Definition: vm.h:112
Type-erased function used across TVM API.
This file defines the TVM global function registry.
An object representing a vm closure.
Definition: vm.h:49
reference to closure.
Definition: vm.h:65