tvm
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
tvm::runtime::vm::VirtualMachine Class Reference

The virtual machine. More...

#include <vm.h>

Inheritance diagram for tvm::runtime::vm::VirtualMachine:
Collaboration diagram for tvm::runtime::vm::VirtualMachine:

Public Member Functions

virtual PackedFunc GetFunction (const String &name, const ObjectPtr< Object > &sptr_to_self)
 Get a PackedFunc from module. More...
 
virtual ~VirtualMachine ()
 
const char * type_key () const final
 
 VirtualMachine ()
 
virtual void LoadExecutable (const ObjectPtr< Executable > &exec)
 load the executable for the virtual machine. More...
 
int GetPropertyMask () const final
 Get the property of the runtime module . More...
 
- Public Member Functions inherited from tvm::runtime::ModuleNode
virtual ~ModuleNode ()=default
 virtual destructor More...
 
virtual void SaveToFile (const String &file_name, const String &format)
 Save the module to file. More...
 
virtual void SaveToBinary (dmlc::Stream *stream)
 Save the module to binary stream. More...
 
virtual String GetSource (const String &format="")
 Get the source code of module, when available. More...
 
virtual String GetFormat ()
 Get the format of the module, when available. More...
 
PackedFunc GetFunction (const String &name, bool query_imports=false)
 Get packed function from current module by name. More...
 
void Import (Module other)
 Import another module into this module. More...
 
const PackedFuncGetFuncFromEnv (const String &name)
 Get a function from current environment The environment includes all the imports as well as Global functions. More...
 
void ClearImports ()
 Clear all imports of the module. More...
 
const std::vector< Module > & imports () const
 
bool IsDSOExportable () const
 Returns true if this module is 'DSO exportable'. More...
 
bool IsBinarySerializable () const
 Returns true if this module is 'Binary Serializable'. More...
 
virtual bool ImplementsFunction (const String &name, bool query_imports=false)
 Returns true if this module has a definition for a function of name. If query_imports is true, also search in any imported modules. More...
 
 TVM_DECLARE_FINAL_OBJECT_INFO (ModuleNode, Object)
 
- Public Member Functions inherited from tvm::runtime::Object
uint32_t type_index () const
 
std::string GetTypeKey () const
 
size_t GetTypeKeyHash () const
 
template<typename TargetType >
bool IsInstance () const
 
bool unique () const
 
 Object ()
 
 Object (const Object &other)
 
 Object (Object &&other)
 
Objectoperator= (const Object &other)
 
Objectoperator= (Object &&other)
 

Protected Member Functions

void PushFrame (Index arg_count, Index ret_pc, const VMFunction &vm_func)
 Push a call frame on to the call stack. More...
 
Index PopFrame ()
 Pop a frame off the call stack. More...
 
void WriteRegister (RegName reg, const ObjectRef &obj)
 Write to a VM register. More...
 
ObjectRef ReadRegister (RegName reg) const
 Read a VM register. More...
 
int64_t LoadScalarInt (RegName reg) const
 Read a VM register and cast it to int32_t. More...
 
ObjectRef Invoke (const VMFunction &func, const std::vector< ObjectRef > &args)
 Invoke a VM function. More...
 
ObjectRef Invoke (const std::string &name, const std::vector< ObjectRef > &args)
 Invoke a VM function by name. More...
 
ObjectRef Invoke (const VMFunction &func, const std::vector< ObjectRef > &input_args, const std::vector< ObjectRef > &output_args)
 Invoke a VM function. More...
 
virtual void InvokePacked (Index packed_index, const PackedFunc &func, Index arg_count, Index output_size, const std::vector< ObjectRef > &args)
 Invoke a PackedFunction. More...
 
void Init (const std::vector< Device > &physical_devices, const std::vector< AllocatorType > &alloc_types)
 Initialize the virtual machine for a set of (physical) devices. More...
 
void RunLoop (const std::vector< Index > &output_tensor_reg_indices={})
 Run VM dispatch loop. More...
 
Device GetDevice (Index device_index) const
 Get device from the device list based on a given device index. More...
 
AllocatorGetAllocator (Index device_index) const
 
void InvokeGlobal (const VMFunction &func, const std::vector< ObjectRef > &args)
 Invoke a global setting up the VM state to execute. More...
 
void SetInput (std::string name, TVMArgs args, int offset)
 Set inputs to a function. More...
 
void SetOneInput (std::string name, const TVMArgValue &tag, const TVMArgValue &tensor)
 Set one input tensor with index or name to a function. More...
 
void SetOutputs (std::string name, TVMArgs args)
 Set pre-allocated output tensors to a function. It is native implementation of 'set_outputs' python method. It is used in scenario when output tensors are allocated outside each invocation. Note: it sets set_outputs_enabled_[name] true and fill outputs_[name] but after invocation the first is switched off and the second is cleared. More...
 
void PrintInfoAndSetInputArgs (const VMFunction &func, const std::vector< ObjectRef > &args)
 Preparation part of Invoke method before RunLoop. More...
 
void SetOutputTensorsToRegister (const std::string &func_name, const std::vector< ObjectRef > &outputs)
 Set pre-allocated outputs to register for specified function. More...
 
virtual void OpStartHook (Instruction instr)
 Internal hook for profiling the start of an op. More...
 
virtual void OpStopHook ()
 Internal hook for profiling the end of an op. More...
 
- Protected Member Functions inherited from tvm::runtime::Object
void IncRef ()
 developer function, increases reference counter. More...
 
void DecRef ()
 developer function, decrease reference counter. More...
 

Protected Attributes

std::vector< PackedFuncpacked_funcs_
 The virtual machine's packed function table. More...
 
std::vector< VMFrameframes_
 The current stack of call frames. More...
 
Index func_index_
 The fuction table index of the current function. More...
 
const Instructioncode_
 The current pointer to the code section. More...
 
Index pc_
 The virtual machine PC. More...
 
ObjectRef return_register_
 The special return register. More...
 
ObjectPtr< Executableexec_
 The executable the VM will operate on. More...
 
std::unordered_map< std::string, std::vector< ObjectRef > > inputs_
 The function name to inputs mapping. More...
 
std::unordered_map< std::string, bool > set_outputs_enabled_
 The function name to flag enabling scenario with set outputs. More...
 
Index preresult_op_index_ = -1
 The index of operation which destination is result. More...
 
std::unordered_map< std::string, std::vector< Index > > output_tensor_reg_indices_
 The function name to indices of output tensors in register file. More...
 
std::unordered_map< std::string, std::vector< ObjectRef > > outputs_
 The function name to pre-allocated outputs mapping. More...
 
std::vector< Devicedevices_
 The "physical" devices the VM can execute primitives on. All "device indexes" are w.r.t. this vector. Each entry in this vector must match the corresponding entry in the executable's "virtual" devices vector. More...
 
std::vector< Allocator * > allocators_
 The cached memory allocators, one per device. More...
 
std::vector< ObjectRefconst_pool_
 The constant pool for runtime. It caches the device dependent object to avoid rellocation of constants during inference. More...
 
- Protected Attributes inherited from tvm::runtime::ModuleNode
std::vector< Moduleimports_
 The modules this module depend on. More...
 
- Protected Attributes inherited from tvm::runtime::Object
uint32_t type_index_ {0}
 Type index(tag) that indicates the type of the object. More...
 
RefCounterType ref_counter_ {0}
 The internal reference counter. More...
 
FDeleter deleter_ = nullptr
 deleter of this object to enable customized allocation. If the deleter is nullptr, no deletion will be performed. The creator of the object must always set the deleter field properly. More...
 

Additional Inherited Members

- Public Types inherited from tvm::runtime::Object
typedef void(* FDeleter) (Object *self)
 Object deleter. More...
 
using RefCounterType = std::atomic< int32_t >
 
- Static Public Member Functions inherited from tvm::runtime::Object
static std::string TypeIndex2Key (uint32_t tindex)
 Get the type key of the corresponding index from runtime. More...
 
static size_t TypeIndex2KeyHash (uint32_t tindex)
 Get the type key hash of the corresponding index from runtime. More...
 
static uint32_t TypeKey2Index (const std::string &key)
 Get the type index of the corresponding key from runtime. More...
 
static uint32_t _GetOrAllocRuntimeTypeIndex ()
 
static uint32_t RuntimeTypeIndex ()
 
- Static Public Attributes inherited from tvm::runtime::ModuleNode
static constexpr const uint32_t _type_index = TypeIndex::kRuntimeModule
 
static constexpr const char * _type_key = "runtime.Module"
 
- Static Public Attributes inherited from tvm::runtime::Object
static constexpr const char * _type_key = "runtime.Object"
 
static constexpr bool _type_final = false
 
static constexpr uint32_t _type_child_slots = 0
 
static constexpr bool _type_child_slots_can_overflow = true
 
static constexpr bool _type_has_method_visit_attrs = true
 
static constexpr bool _type_has_method_sequal_reduce = false
 
static constexpr bool _type_has_method_shash_reduce = false
 
static constexpr uint32_t _type_index = TypeIndex::kDynamic
 
- Static Protected Member Functions inherited from tvm::runtime::Object
static uint32_t GetOrAllocRuntimeTypeIndex (const std::string &key, uint32_t static_tindex, uint32_t parent_tindex, uint32_t type_child_slots, bool type_child_slots_can_overflow)
 Get the type index using type key. More...
 

Detailed Description

The virtual machine.

The virtual machine contains all the current execution state, as well as the executable.

The goal is to have a single self-contained object, enabling one to easily pass around VMs, execute them on multiple threads, or serialize them to disk or over the wire.

Constructor & Destructor Documentation

◆ ~VirtualMachine()

virtual tvm::runtime::vm::VirtualMachine::~VirtualMachine ( )
inlinevirtual

◆ VirtualMachine()

tvm::runtime::vm::VirtualMachine::VirtualMachine ( )
inline

Member Function Documentation

◆ GetAllocator()

Allocator* tvm::runtime::vm::VirtualMachine::GetAllocator ( Index  device_index) const
protected

◆ GetDevice()

Device tvm::runtime::vm::VirtualMachine::GetDevice ( Index  device_index) const
protected

Get device from the device list based on a given device index.

◆ GetFunction()

virtual PackedFunc tvm::runtime::vm::VirtualMachine::GetFunction ( const String name,
const ObjectPtr< Object > &  sptr_to_self 
)
virtual

Get a PackedFunc from module.

The PackedFunc may not be fully initialized, there might still be first time running overhead when executing the function on certain devices. For benchmarking, use prepare to eliminate

Parameters
namethe name of the function.
sptr_to_selfThe shared_ptr that points to this module node.
Returns
PackedFunc(nullptr) when it is not available.
Note
The function will always remain valid. If the function needs resource from the module(e.g. late linking), it should capture sptr_to_self.

Implements tvm::runtime::ModuleNode.

◆ GetPropertyMask()

int tvm::runtime::vm::VirtualMachine::GetPropertyMask ( ) const
inlinefinalvirtual

Get the property of the runtime module .

Reimplemented from tvm::runtime::ModuleNode.

◆ Init()

void tvm::runtime::vm::VirtualMachine::Init ( const std::vector< Device > &  physical_devices,
const std::vector< AllocatorType > &  alloc_types 
)
protected

Initialize the virtual machine for a set of (physical) devices.

Parameters
physical_devicesThe set of TVM devices.
alloc_typesThe allocator types for each device.

◆ Invoke() [1/3]

ObjectRef tvm::runtime::vm::VirtualMachine::Invoke ( const std::string &  name,
const std::vector< ObjectRef > &  args 
)
protected

Invoke a VM function by name.

Parameters
nameThe function's name.
argsThe arguments to the function.
Returns
The object representing the result.

◆ Invoke() [2/3]

ObjectRef tvm::runtime::vm::VirtualMachine::Invoke ( const VMFunction func,
const std::vector< ObjectRef > &  args 
)
protected

Invoke a VM function.

Parameters
funcThe function.
argsThe arguments to the function.
Returns
The object representing the result.

◆ Invoke() [3/3]

ObjectRef tvm::runtime::vm::VirtualMachine::Invoke ( const VMFunction func,
const std::vector< ObjectRef > &  input_args,
const std::vector< ObjectRef > &  output_args 
)
protected

Invoke a VM function.

Parameters
funcThe function.
input_argsThe input arguments to the function.
output_argsThe pre-allocated output arguments of the function.
Returns
The object(s) representing the result.

◆ InvokeGlobal()

void tvm::runtime::vm::VirtualMachine::InvokeGlobal ( const VMFunction func,
const std::vector< ObjectRef > &  args 
)
protected

Invoke a global setting up the VM state to execute.

This does not begin execution of the VM.

◆ InvokePacked()

virtual void tvm::runtime::vm::VirtualMachine::InvokePacked ( Index  packed_index,
const PackedFunc func,
Index  arg_count,
Index  output_size,
const std::vector< ObjectRef > &  args 
)
protectedvirtual

Invoke a PackedFunction.

Parameters
packed_indexThe offset of the PackedFunction in all functions.
funcThe PackedFunction to be invoked.
arg_countThe number of arguments to the PackedFunction.
output_sizeThe number of outputs of the PackedFunction.
argsArguments to the PackedFunction.
Note
The return value will be stored in the last output_size slots of args.

◆ LoadExecutable()

virtual void tvm::runtime::vm::VirtualMachine::LoadExecutable ( const ObjectPtr< Executable > &  exec)
virtual

load the executable for the virtual machine.

Parameters
execThe executable.

◆ LoadScalarInt()

int64_t tvm::runtime::vm::VirtualMachine::LoadScalarInt ( RegName  reg) const
protected

Read a VM register and cast it to int32_t.

Parameters
regThe register to read from.
Returns
The read scalar.

◆ OpStartHook()

virtual void tvm::runtime::vm::VirtualMachine::OpStartHook ( Instruction  instr)
protectedvirtual

Internal hook for profiling the start of an op.

This hook is only called on certain ops that are likely to take a significant amount of runtime (normally because they alloc or transfer to device).

Parameters
instrInstruction that will be executed after this hook fires

◆ OpStopHook()

virtual void tvm::runtime::vm::VirtualMachine::OpStopHook ( )
protectedvirtual

Internal hook for profiling the end of an op.

◆ PopFrame()

Index tvm::runtime::vm::VirtualMachine::PopFrame ( )
protected

Pop a frame off the call stack.

Returns
The number of frames left.

◆ PrintInfoAndSetInputArgs()

void tvm::runtime::vm::VirtualMachine::PrintInfoAndSetInputArgs ( const VMFunction func,
const std::vector< ObjectRef > &  args 
)
protected

Preparation part of Invoke method before RunLoop.

Parameters
functhe function.
argsinput args

◆ PushFrame()

void tvm::runtime::vm::VirtualMachine::PushFrame ( Index  arg_count,
Index  ret_pc,
const VMFunction vm_func 
)
protected

Push a call frame on to the call stack.

◆ ReadRegister()

ObjectRef tvm::runtime::vm::VirtualMachine::ReadRegister ( RegName  reg) const
protected

Read a VM register.

Parameters
regThe register to read from.
Returns
The read object.

◆ RunLoop()

void tvm::runtime::vm::VirtualMachine::RunLoop ( const std::vector< Index > &  output_tensor_reg_indices = {})
protected

Run VM dispatch loop.

◆ SetInput()

void tvm::runtime::vm::VirtualMachine::SetInput ( std::string  name,
TVMArgs  args,
int  offset 
)
protected

Set inputs to a function.

Parameters
nameThe function name
argsargs[offset:] are arguments to the function. If the arguments are not of the correct device for the function, they will be copied to the device.
offsetStarting offset of the arguments in args.

◆ SetOneInput()

void tvm::runtime::vm::VirtualMachine::SetOneInput ( std::string  name,
const TVMArgValue tag,
const TVMArgValue tensor 
)
protected

Set one input tensor with index or name to a function.

Parameters
nameThe function name.
tagindex or name of the input tensor .
tensorthe input tensor. If the tensor is not of the correct device for the function, they will be copied to the device.

◆ SetOutputs()

void tvm::runtime::vm::VirtualMachine::SetOutputs ( std::string  name,
TVMArgs  args 
)
protected

Set pre-allocated output tensors to a function. It is native implementation of 'set_outputs' python method. It is used in scenario when output tensors are allocated outside each invocation. Note: it sets set_outputs_enabled_[name] true and fill outputs_[name] but after invocation the first is switched off and the second is cleared.

Parameters
nameThe function name
argsoutputs to the function.

◆ SetOutputTensorsToRegister()

void tvm::runtime::vm::VirtualMachine::SetOutputTensorsToRegister ( const std::string &  func_name,
const std::vector< ObjectRef > &  outputs 
)
protected

Set pre-allocated outputs to register for specified function.

Parameters
func_nameThe function's name.
outputsset of output tensors.

◆ type_key()

const char* tvm::runtime::vm::VirtualMachine::type_key ( ) const
inlinefinalvirtual
Returns
The per module type key.
Note
This key is used to for serializing custom modules.

Implements tvm::runtime::ModuleNode.

◆ WriteRegister()

void tvm::runtime::vm::VirtualMachine::WriteRegister ( RegName  reg,
const ObjectRef obj 
)
inlineprotected

Write to a VM register.

Parameters
regThe register to write to.
objThe object to write to.

Member Data Documentation

◆ allocators_

std::vector<Allocator*> tvm::runtime::vm::VirtualMachine::allocators_
protected

The cached memory allocators, one per device.

◆ code_

const Instruction* tvm::runtime::vm::VirtualMachine::code_
protected

The current pointer to the code section.

◆ const_pool_

std::vector<ObjectRef> tvm::runtime::vm::VirtualMachine::const_pool_
protected

The constant pool for runtime. It caches the device dependent object to avoid rellocation of constants during inference.

◆ devices_

std::vector<Device> tvm::runtime::vm::VirtualMachine::devices_
protected

The "physical" devices the VM can execute primitives on. All "device indexes" are w.r.t. this vector. Each entry in this vector must match the corresponding entry in the executable's "virtual" devices vector.

◆ exec_

ObjectPtr<Executable> tvm::runtime::vm::VirtualMachine::exec_
protected

The executable the VM will operate on.

◆ frames_

std::vector<VMFrame> tvm::runtime::vm::VirtualMachine::frames_
protected

The current stack of call frames.

◆ func_index_

Index tvm::runtime::vm::VirtualMachine::func_index_
protected

The fuction table index of the current function.

◆ inputs_

std::unordered_map<std::string, std::vector<ObjectRef> > tvm::runtime::vm::VirtualMachine::inputs_
protected

The function name to inputs mapping.

◆ output_tensor_reg_indices_

std::unordered_map<std::string, std::vector<Index> > tvm::runtime::vm::VirtualMachine::output_tensor_reg_indices_
protected

The function name to indices of output tensors in register file.

◆ outputs_

std::unordered_map<std::string, std::vector<ObjectRef> > tvm::runtime::vm::VirtualMachine::outputs_
protected

The function name to pre-allocated outputs mapping.

◆ packed_funcs_

std::vector<PackedFunc> tvm::runtime::vm::VirtualMachine::packed_funcs_
protected

The virtual machine's packed function table.

◆ pc_

Index tvm::runtime::vm::VirtualMachine::pc_
protected

The virtual machine PC.

◆ preresult_op_index_

Index tvm::runtime::vm::VirtualMachine::preresult_op_index_ = -1
protected

The index of operation which destination is result.

◆ return_register_

ObjectRef tvm::runtime::vm::VirtualMachine::return_register_
protected

The special return register.

◆ set_outputs_enabled_

std::unordered_map<std::string, bool> tvm::runtime::vm::VirtualMachine::set_outputs_enabled_
protected

The function name to flag enabling scenario with set outputs.


The documentation for this class was generated from the following file: