23 #ifndef TVM_RUNTIME_VM_VM_H_
24 #define TVM_RUNTIME_VM_VM_H_
26 #ifndef TVM_VM_ENABLE_PROFILER
27 #define TVM_VM_ENABLE_PROFILER 1
30 #include <tvm/ffi/extra/module.h>
34 #include <unordered_map>
37 #include "../memory/memory_manager.h"
44 using memory::Allocator;
46 using memory::MemoryManager;
47 using memory::Storage;
48 using memory::StorageObj;
81 static constexpr
const char*
_type_key =
"relax.vm.Closure";
112 static constexpr
const char*
_type_key =
"runtime.VMExtension";
141 const std::vector<AllocatorType>& alloc_types) = 0;
189 template <typename T, typename = std::enable_if_t<std::is_base_of<VMExtension, T>::value>>
191 using ContainerType =
typename T::ContainerType;
192 uint32_t key = ContainerType::RuntimeTypeIndex();
194 ffi::Any value = (*it).second;
195 return value.cast<T>();
197 auto [it, _] =
extensions.emplace(key, T::Create());
198 ffi::Any value = (*it).second;
199 return value.cast<T>();
206 static ObjectPtr<VirtualMachine>
Create();
The bytecode for the virtual machine.
An object representing a vm closure.
Definition: vm.h:65
static constexpr const char * _type_key
Definition: vm.h:81
ffi::Function impl
The implementation of the Closure.
Definition: vm.h:79
String func_name
The function name. The function could be any function object that is compatible to the VM runtime.
Definition: vm.h:71
TVM_DECLARE_FINAL_OBJECT_INFO(VMClosureObj, Object)
reference to closure.
Definition: vm.h:86
TVM_DEFINE_OBJECT_REF_METHODS(VMClosure, ObjectRef, VMClosureObj)
VMClosure(String func_name, ffi::Function impl)
static ffi::Function BindLastArgs(ffi::Function func, std::vector< ffi::Any > last_args)
Create another ffi::Function with last arguments already bound to last_args.
Represent a VM extension. A VM extension allows the user to extend the VM with target specific functi...
Definition: vm.h:110
TVM_DECLARE_BASE_OBJECT_INFO(VMExtensionNode, Object)
static constexpr const char * _type_key
Definition: vm.h:112
Managed reference to VM extension.
Definition: vm.h:117
TVM_DEFINE_OBJECT_REF_METHODS(VMExtension, ObjectRef, VMExtensionNode)
The virtual machine.
Definition: vm.h:133
~VirtualMachine()
Definition: vm.h:220
virtual void InvokeClosurePacked(const ObjectRef &closure_or_packedfunc, ffi::PackedArgs args, ffi::Any *rv)=0
Invoke closure or packed function using ffi::Function convention.
static ObjectPtr< VirtualMachine > Create()
Create a specific instance of VM.
static VirtualMachine * GetContextPtr(ffi::AnyView arg)
Helper function for vm closure functions to get the context ptr.
Definition: vm.h:216
virtual void SetInstrument(ffi::Function instrument)=0
Set an instrumentation function.
virtual void Init(const std::vector< Device > &devices, const std::vector< AllocatorType > &alloc_types)=0
Initialize the virtual machine for a set of devices.
static ObjectPtr< VirtualMachine > CreateProfiler()
Create an instance of VM with the profiling feature enabled.
std::unordered_map< uint32_t, Any > extensions
The VM extensions. Mapping from the type index of the extension to the extension instance.
Definition: vm.h:231
std::vector< Allocator * > allocators
The memory allocators.
Definition: vm.h:226
virtual VMClosure GetClosure(const String &func_name)=0
Get global function in the VM.
T GetOrCreateExtension()
Get or create a VM extension. Once created, the extension will be stored in the VM and held until the...
Definition: vm.h:190
std::vector< Device > devices
Runtime physical device list.
Definition: vm.h:228
virtual void LoadExecutable(ObjectPtr< VMExecutable > exec)=0
Load the executable for the virtual machine.
AllocatorType
Definition: memory_manager.h:42
VMInstrumentReturnKind
Possible instrument actions.
Definition: vm.h:55
@ kNoOp
Running as normal.
@ kSkipRun
Skip the following run, only valid in before.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37