tvm
Loading...
Searching...
No Matches
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#include <tvm/ffi/extra/module.h>
28#include <tvm/runtime/tensor.h>
29
30#include <memory>
31#include <string>
32#include <unordered_map>
33#include <vector>
34
35#include "./bytecode.h"
36#include "./executable.h"
37
38namespace tvm {
39namespace runtime {
40namespace vm {
41
45enum class VMInstrumentReturnKind : int {
47 kNoOp = 0,
49 kSkipRun = 1,
50};
51
55class VMClosureObj : public ffi::Object {
56 public:
61 ffi::String func_name;
62
69 ffi::Function impl;
70 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.vm.Closure", VMClosureObj, ffi::Object);
71};
72
74class VMClosure : public ffi::ObjectRef {
75 public:
76 VMClosure(ffi::String func_name, ffi::Function impl);
78
87 static ffi::Function BindLastArgs(ffi::Function func, std::vector<ffi::Any> last_args);
88};
89
98class VMExtensionObj : public ffi::Object {
99 protected:
100 TVM_FFI_DECLARE_OBJECT_INFO("runtime.VMExtension", VMExtensionObj, ffi::Object);
101};
102
104class VMExtension : public ffi::ObjectRef {
105 public:
107};
108
120class VirtualMachine : public ffi::ModuleObj {
121 public:
126 virtual void Init(const std::vector<Device>& devices) = 0;
131 virtual void LoadExecutable(ffi::ObjectPtr<VMExecutable> exec) = 0;
137 virtual VMClosure GetClosure(const ffi::String& func_name) = 0;
144 virtual void InvokeClosurePacked(const ffi::ObjectRef& closure_or_packedfunc,
145 ffi::PackedArgs args, ffi::Any* rv) = 0;
165 virtual void SetInstrument(ffi::Function instrument) = 0;
166
174 template <typename T, typename = std::enable_if_t<std::is_base_of<VMExtension, T>::value>>
176 using ContainerType = typename T::ContainerType;
177 uint32_t key = ContainerType::RuntimeTypeIndex();
178 if (auto it = extensions.find(key); it != extensions.end()) {
179 ffi::Any value = (*it).second;
180 return value.cast<T>();
181 }
182 auto [it, _] = extensions.emplace(key, T::Create());
183 ffi::Any value = (*it).second;
184 return value.cast<T>();
185 }
186
191 static ffi::ObjectPtr<VirtualMachine> Create();
196 static VirtualMachine* GetContextPtr(ffi::AnyView arg) {
197 return static_cast<VirtualMachine*>(arg.cast<void*>());
198 }
199
201
202 //--------------------------------------------------------------------------
203 // The following section contains states that other builtin can depend on
204 //--------------------------------------------------------------------------
206 std::vector<ffi::ObjectPtr<TensorMemoryPool>> memory_pools;
208 std::vector<Device> devices;
211 std::unordered_map<uint32_t, Any> extensions;
212};
213
214} // namespace vm
215} // namespace runtime
216} // namespace tvm
217
218#endif // TVM_RUNTIME_VM_VM_H_
The bytecode for the virtual machine.
An object representing a vm closure.
Definition vm.h:55
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.vm.Closure", VMClosureObj, ffi::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:61
ffi::Function impl
The implementation of the Closure.
Definition vm.h:69
reference to closure.
Definition vm.h:74
VMClosure(ffi::String func_name, ffi::Function impl)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(VMClosure, ffi::ObjectRef, VMClosureObj)
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:98
TVM_FFI_DECLARE_OBJECT_INFO("runtime.VMExtension", VMExtensionObj, ffi::Object)
Managed reference to VM extension.
Definition vm.h:104
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(VMExtension, ffi::ObjectRef, VMExtensionObj)
The virtual machine.
Definition vm.h:120
static ffi::ObjectPtr< VirtualMachine > Create()
Create a specific instance of VM.
~VirtualMachine()
Definition vm.h:200
virtual void LoadExecutable(ffi::ObjectPtr< VMExecutable > exec)=0
Load the executable for the virtual machine.
virtual void Init(const std::vector< Device > &devices)=0
Initialize the virtual machine for a set of devices.
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:196
virtual void InvokeClosurePacked(const ffi::ObjectRef &closure_or_packedfunc, ffi::PackedArgs args, ffi::Any *rv)=0
Invoke closure or packed function using ffi::Function convention.
virtual void SetInstrument(ffi::Function instrument)=0
Set an instrumentation function.
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:211
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:175
std::vector< Device > devices
Runtime physical device list.
Definition vm.h:208
std::vector< ffi::ObjectPtr< TensorMemoryPool > > memory_pools
Owning memory pools for the physical devices.
Definition vm.h:206
Abstract device memory management API.
Definition builtin.h:29
@ kNoOp
skip and do not do anything.
VMInstrumentReturnKind
Possible instrument actions.
Definition vm.h:45
@ kSkipRun
Skip the following run, only valid in before.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Device-independent tensor allocation, copies, views and serialization.