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 
23 #ifndef TVM_RUNTIME_VM_VM_H_
24 #define TVM_RUNTIME_VM_VM_H_
25 
26 #ifndef TVM_VM_ENABLE_PROFILER
27 #define TVM_VM_ENABLE_PROFILER 1
28 #endif
29 
30 #include <tvm/ffi/extra/module.h>
31 
32 #include <memory>
33 #include <string>
34 #include <unordered_map>
35 #include <vector>
36 
37 #include "../memory/memory_manager.h"
38 #include "./bytecode.h"
39 #include "./executable.h"
40 
41 namespace tvm {
42 namespace runtime {
43 
44 using memory::Allocator;
46 using memory::MemoryManager;
47 using memory::Storage;
48 using memory::StorageObj;
49 
50 namespace vm {
51 
55 enum class VMInstrumentReturnKind : int {
57  kNoOp = 0,
59  kSkipRun = 1,
60 };
61 
65 class VMClosureObj : public Object {
66  public:
71  ffi::String func_name;
72 
80  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.vm.Closure", VMClosureObj, Object);
81 };
82 
84 class VMClosure : public ObjectRef {
85  public:
86  VMClosure(ffi::String func_name, ffi::Function impl);
88 
97  static ffi::Function BindLastArgs(ffi::Function func, std::vector<ffi::Any> last_args);
98 };
99 
108 class VMExtensionNode : public Object {
109  protected:
110  TVM_FFI_DECLARE_OBJECT_INFO("runtime.VMExtension", VMExtensionNode, Object);
111 };
112 
114 class VMExtension : public ObjectRef {
115  public:
117 };
118 
130 class VirtualMachine : public ffi::ModuleObj {
131  public:
137  virtual void Init(const std::vector<Device>& devices,
138  const std::vector<AllocatorType>& alloc_types) = 0;
143  virtual void LoadExecutable(ObjectPtr<VMExecutable> exec) = 0;
149  virtual VMClosure GetClosure(const ffi::String& func_name) = 0;
156  virtual void InvokeClosurePacked(const ObjectRef& closure_or_packedfunc, ffi::PackedArgs args,
157  ffi::Any* rv) = 0;
177  virtual void SetInstrument(ffi::Function instrument) = 0;
178 
186  template <typename T, typename = std::enable_if_t<std::is_base_of<VMExtension, T>::value>>
188  using ContainerType = typename T::ContainerType;
189  uint32_t key = ContainerType::RuntimeTypeIndex();
190  if (auto it = extensions.find(key); it != extensions.end()) {
191  ffi::Any value = (*it).second;
192  return value.cast<T>();
193  }
194  auto [it, _] = extensions.emplace(key, T::Create());
195  ffi::Any value = (*it).second;
196  return value.cast<T>();
197  }
198 
203  static ObjectPtr<VirtualMachine> Create();
208  static ObjectPtr<VirtualMachine> CreateProfiler();
213  static VirtualMachine* GetContextPtr(ffi::AnyView arg) {
214  return static_cast<VirtualMachine*>(arg.cast<void*>());
215  }
216 
218 
219  //--------------------------------------------------------------------------
220  // The following section contains states that other builtin can depend on
221  //--------------------------------------------------------------------------
223  std::vector<Allocator*> allocators;
225  std::vector<Device> devices;
228  std::unordered_map<uint32_t, Any> extensions;
229 };
230 
231 } // namespace vm
232 } // namespace runtime
233 } // namespace tvm
234 
235 #endif // TVM_RUNTIME_VM_VM_H_
The bytecode for the virtual machine.
An object representing a vm closure.
Definition: vm.h:65
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.vm.Closure", VMClosureObj, Object)
ffi::String func_name
The function name. The function could be any function object that is compatible to the VM runtime.
Definition: vm.h:71
ffi::Function impl
The implementation of the Closure.
Definition: vm.h:79
reference to closure.
Definition: vm.h:84
VMClosure(ffi::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.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(VMClosure, ObjectRef, VMClosureObj)
Represent a VM extension. A VM extension allows the user to extend the VM with target specific functi...
Definition: vm.h:108
TVM_FFI_DECLARE_OBJECT_INFO("runtime.VMExtension", VMExtensionNode, Object)
Managed reference to VM extension.
Definition: vm.h:114
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(VMExtension, ObjectRef, VMExtensionNode)
The virtual machine.
Definition: vm.h:130
~VirtualMachine()
Definition: vm.h:217
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.
virtual VMClosure GetClosure(const ffi::String &func_name)=0
Get global function in the VM.
static VirtualMachine * GetContextPtr(ffi::AnyView arg)
Helper function for vm closure functions to get the context ptr.
Definition: vm.h:213
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:228
std::vector< Allocator * > allocators
The memory allocators.
Definition: vm.h:223
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:187
std::vector< Device > devices
Runtime physical device list.
Definition: vm.h:225
virtual void LoadExecutable(ObjectPtr< VMExecutable > exec)=0
Load the executable for the virtual machine.
tvm::relax::Function Function
Definition: transform.h:42
AllocatorType
Definition: memory_manager.h:42
Definition: builtin.h:29
VMInstrumentReturnKind
Possible instrument actions.
Definition: vm.h:55
@ kSkipRun
Skip the following run, only valid in before.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37