24 #ifndef TVM_RUNTIME_VM_VM_H_ 25 #define TVM_RUNTIME_VM_VM_H_ 38 #include <unordered_map> 60 static constexpr
const char*
_type_key =
"vm.Closure";
89 VMFunction(std::string name, std::vector<std::string> params,
90 std::vector<Instruction> instructions,
Index register_file_size,
91 std::vector<Index> param_device_indexes)
92 : name(
std::move(name)),
93 params(
std::move(params)),
94 instructions(
std::move(instructions)),
95 register_file_size(register_file_size),
96 param_device_indexes(
std::move(param_device_indexes)) {
97 ICHECK_EQ(this->params.size(), this->param_device_indexes.size());
130 func_index(func_index),
133 register_file(register_file_size),
134 caller_return_register(0) {}
171 const char*
type_key() const final {
return "VirtualMachine"; }
173 VirtualMachine() : frames_(), func_index_(0), code_(nullptr), pc_(0), exec_(nullptr) {}
213 int64_t LoadScalarInt(
RegName reg)
const;
230 ObjectRef Invoke(
const std::string& name,
const std::vector<ObjectRef>& args);
240 const std::vector<ObjectRef>& output_args);
254 Index output_size,
const std::vector<ObjectRef>& args);
261 void Init(
const std::vector<Device>& physical_devices,
262 const std::vector<AllocatorType>& alloc_types);
265 void RunLoop(
const std::vector<Index>& output_tensor_reg_indices = {});
276 void InvokeGlobal(
const VMFunction& func,
const std::vector<ObjectRef>& args);
286 void SetInput(std::string name,
TVMArgs args,
int offset);
306 void SetOutputs(std::string name,
TVMArgs args);
313 void PrintInfoAndSetInputArgs(
const VMFunction& func,
const std::vector<ObjectRef>& args);
320 void SetOutputTensorsToRegister(
const std::string& func_name,
321 const std::vector<ObjectRef>& outputs);
337 virtual void OpStopHook();
346 int64_t GetInputIndexFromVMFunction(
const std::string& func_name,
347 const std::string& input_name)
const;
355 int64_t GetInputIndexFromName(
const std::vector<std::string>& params,
356 const std::string& input_name)
const;
363 const VMFunction& CheckAndGetVMFunction(
const std::string& func_name)
const;
371 void CreateInputsOrCheckSize(
const std::string& func_name,
size_t size);
378 void SetInputTensorWithIndex(std::vector<ObjectRef>& tensors,
393 Index GetResultRegisterIndex()
const;
399 void CalculatePreResultOpIndex(
Index res_index);
407 std::vector<Index> GetOutputTensorRegIndices();
413 void WriteAllocatedTensor(
const Instruction& instr);
422 void WriteAllocatedTensorFromOutside(
const Instruction& instr);
424 bool FindIndex(
const std::vector<Index>& indices,
Index val)
const;
442 std::unordered_map<std::string, std::vector<ObjectRef>>
inputs_;
450 std::unordered_map<std::string, std::vector<ObjectRef>>
outputs_;
470 #endif // TVM_RUNTIME_VM_VM_H_ std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
std::unordered_map< std::string, std::vector< ObjectRef > > inputs_
The function name to inputs mapping.
Definition: vm.h:442
std::unordered_map< std::string, std::vector< Index > > output_tensor_reg_indices_
The function name to indices of output tensors in register file.
Definition: vm.h:448
A custom smart pointer for Object.
Definition: object.h:358
std::vector< ObjectRef > const_pool_
The constant pool for runtime. It caches the device dependent object to avoid rellocation of constant...
Definition: vm.h:463
RegName caller_return_register
Register in caller's frame to put return value.
Definition: vm.h:126
std::string name
The function's name.
Definition: vm.h:79
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Index func_index
The index into the function table, points to the caller.
Definition: vm.h:116
Index func_index_
The fuction table index of the current function.
Definition: vm.h:432
Definition: loop_state.h:456
A single virtual machine instruction.
Definition: bytecode.h:83
Runtime Closure container types.
reference to closure.
Definition: closure.h:44
Abstract device memory management API.
ObjectPtr< Executable > exec_
The executable the VM will operate on.
Definition: vm.h:440
const char * type_key() const final
Definition: vm.h:171
The Relay virtual machine executable.
std::vector< Device > devices_
The "physical" devices the VM can execute primitives on. All "device indexes" are w...
Definition: vm.h:456
Arguments into TVM functions.
Definition: packed_func.h:391
std::vector< ObjectRef > register_file
Statically allocated space for objects.
Definition: vm.h:123
VMFunction(std::string name, std::vector< std::string > params, std::vector< Instruction > instructions, Index register_file_size, std::vector< Index > param_device_indexes)
Definition: vm.h:89
int64_t RegName
A register name.
Definition: bytecode.h:38
The bytecode for Relay virtual machine.
std::vector< PackedFunc > packed_funcs_
The virtual machine's packed function table.
Definition: vm.h:428
std::vector< Allocator * > allocators_
The cached memory allocators, one per device.
Definition: vm.h:458
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:713
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:112
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:511
kRunnable we can run the module directly. LLVM/CUDA/JSON runtime, executors (e.g, virtual machine) ru...
Definition: module.h:62
static constexpr const uint32_t _type_index
Definition: vm.h:59
Base container of module.
Definition: module.h:142
std::vector< VMFrame > frames_
The current stack of call frames.
Definition: vm.h:430
TVM_DECLARE_FINAL_OBJECT_INFO(VMClosureObj, ClosureObj)
VirtualMachine()
Definition: vm.h:173
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:436
virtual ~VirtualMachine()
Definition: vm.h:169
std::unordered_map< std::string, bool > set_outputs_enabled_
The function name to flag enabling scenario with set outputs.
Definition: vm.h:444
Runtime container of the functions generated by TVM, This is used to support dynamically link...
Index args
The number of arguments.
Definition: vm.h:118
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:646
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
std::unordered_map< std::string, std::vector< ObjectRef > > outputs_
The function name to pre-allocated outputs mapping.
Definition: vm.h:450
const Instruction * code_
The current pointer to the code section.
Definition: vm.h:434
ObjectRef return_register_
The special return register.
Definition: vm.h:438
Definition: memory_manager.h:55
VMFrame(Index pc, Index func_index, Index args, const Instruction *code, Index register_file_size)
Definition: vm.h:128
BlockInitFrame Init()
The block initialization statement.
int GetPropertyMask() const final
Get the property of the runtime module .
Definition: vm.h:182
const Instruction * code
A pointer into the caller function's instructions.
Definition: vm.h:120
std::vector< Index > param_device_indexes
The indexes for the device holding each function parameter.
Definition: vm.h:87
The virtual machine.
Definition: vm.h:148
Type index is allocated during runtime.
Definition: object.h:80
Index pc
The return program counter.
Definition: vm.h:114
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