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  String func_name;
72 
80 
81  static constexpr const char* _type_key = "relax.vm.Closure";
83 };
84 
86 class VMClosure : public ObjectRef {
87  public:
88  VMClosure(String func_name, ffi::Function impl);
90 
99  static ffi::Function BindLastArgs(ffi::Function func, std::vector<ffi::Any> last_args);
100 };
101 
110 class VMExtensionNode : public Object {
111  protected:
112  static constexpr const char* _type_key = "runtime.VMExtension";
114 };
115 
117 class VMExtension : public ObjectRef {
118  public:
120 };
121 
133 class VirtualMachine : public ffi::ModuleObj {
134  public:
140  virtual void Init(const std::vector<Device>& devices,
141  const std::vector<AllocatorType>& alloc_types) = 0;
146  virtual void LoadExecutable(ObjectPtr<VMExecutable> exec) = 0;
152  virtual VMClosure GetClosure(const String& func_name) = 0;
159  virtual void InvokeClosurePacked(const ObjectRef& closure_or_packedfunc, ffi::PackedArgs args,
160  ffi::Any* rv) = 0;
180  virtual void SetInstrument(ffi::Function instrument) = 0;
181 
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();
193  if (auto it = extensions.find(key); it != extensions.end()) {
194  ffi::Any value = (*it).second;
195  return value.cast<T>();
196  }
197  auto [it, _] = extensions.emplace(key, T::Create());
198  ffi::Any value = (*it).second;
199  return value.cast<T>();
200  }
201 
206  static ObjectPtr<VirtualMachine> Create();
211  static ObjectPtr<VirtualMachine> CreateProfiler();
216  static VirtualMachine* GetContextPtr(ffi::AnyView arg) {
217  return static_cast<VirtualMachine*>(arg.cast<void*>());
218  }
219 
221 
222  //--------------------------------------------------------------------------
223  // The following section contains states that other builtin can depend on
224  //--------------------------------------------------------------------------
226  std::vector<Allocator*> allocators;
228  std::vector<Device> devices;
231  std::unordered_map<uint32_t, Any> extensions;
232 };
233 
234 } // namespace vm
235 } // namespace runtime
236 } // namespace tvm
237 
238 #endif // TVM_RUNTIME_VM_VM_H_
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.
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