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>
27
28#include <memory>
29#include <string>
30#include <unordered_map>
31#include <vector>
32
33#include "../memory/memory_manager.h"
34#include "./bytecode.h"
35#include "./executable.h"
36
37namespace tvm {
38namespace runtime {
39
40using memory::Allocator;
42using memory::MemoryManager;
43using memory::Storage;
44using memory::StorageObj;
45
46namespace vm {
47
51enum class VMInstrumentReturnKind : int {
53 kNoOp = 0,
55 kSkipRun = 1,
56};
57
61class VMClosureObj : public ffi::Object {
62 public:
67 ffi::String func_name;
68
75 ffi::Function impl;
76 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.vm.Closure", VMClosureObj, ffi::Object);
77};
78
80class VMClosure : public ffi::ObjectRef {
81 public:
82 VMClosure(ffi::String func_name, ffi::Function impl);
84
93 static ffi::Function BindLastArgs(ffi::Function func, std::vector<ffi::Any> last_args);
94};
95
104class VMExtensionNode : public ffi::Object {
105 protected:
106 TVM_FFI_DECLARE_OBJECT_INFO("runtime.VMExtension", VMExtensionNode, ffi::Object);
107};
108
110class VMExtension : public ffi::ObjectRef {
111 public:
113};
114
126class VirtualMachine : public ffi::ModuleObj {
127 public:
133 virtual void Init(const std::vector<Device>& devices,
134 const std::vector<AllocatorType>& alloc_types) = 0;
139 virtual void LoadExecutable(ffi::ObjectPtr<VMExecutable> exec) = 0;
145 virtual VMClosure GetClosure(const ffi::String& func_name) = 0;
152 virtual void InvokeClosurePacked(const ffi::ObjectRef& closure_or_packedfunc,
153 ffi::PackedArgs args, ffi::Any* rv) = 0;
173 virtual void SetInstrument(ffi::Function instrument) = 0;
174
184 using ContainerType = typename T::ContainerType;
185 uint32_t key = ContainerType::RuntimeTypeIndex();
186 if (auto it = extensions.find(key); it != extensions.end()) {
187 ffi::Any value = (*it).second;
188 return value.cast<T>();
189 }
190 auto [it, _] = extensions.emplace(key, T::Create());
191 ffi::Any value = (*it).second;
192 return value.cast<T>();
193 }
194
199 static ffi::ObjectPtr<VirtualMachine> Create();
204 static VirtualMachine* GetContextPtr(ffi::AnyView arg) {
205 return static_cast<VirtualMachine*>(arg.cast<void*>());
206 }
207
209
210 //--------------------------------------------------------------------------
211 // The following section contains states that other builtin can depend on
212 //--------------------------------------------------------------------------
214 std::vector<Allocator*> allocators;
216 std::vector<Device> devices;
219 std::unordered_map<uint32_t, Any> extensions;
220};
221
222} // namespace vm
223} // namespace runtime
224} // namespace tvm
225
226#endif // TVM_RUNTIME_VM_VM_H_
The bytecode for the virtual machine.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
An object representing a vm closure.
Definition vm.h:61
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:67
ffi::Function impl
The implementation of the Closure.
Definition vm.h:75
reference to closure.
Definition vm.h:80
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(VMClosure, ffi::ObjectRef, VMClosureObj)
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.
Represent a VM extension. A VM extension allows the user to extend the VM with target specific functi...
Definition vm.h:104
TVM_FFI_DECLARE_OBJECT_INFO("runtime.VMExtension", VMExtensionNode, ffi::Object)
Managed reference to VM extension.
Definition vm.h:110
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(VMExtension, ffi::ObjectRef, VMExtensionNode)
The virtual machine.
Definition vm.h:126
static ffi::ObjectPtr< VirtualMachine > Create()
Create a specific instance of VM.
~VirtualMachine()
Definition vm.h:208
virtual void LoadExecutable(ffi::ObjectPtr< VMExecutable > exec)=0
Load the executable for the virtual machine.
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:204
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.
virtual void Init(const std::vector< Device > &devices, const std::vector< AllocatorType > &alloc_types)=0
Initialize the virtual machine for a set of devices.
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:219
std::vector< Allocator * > allocators
The memory allocators.
Definition vm.h:214
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:183
std::vector< Device > devices
Runtime physical device list.
Definition vm.h:216
AllocatorType
Definition memory_manager.h:41
Definition builtin.h:29
@ kNoOp
skip and do not do anything.
VMInstrumentReturnKind
Possible instrument actions.
Definition vm.h:51
@ kSkipRun
Skip the following run, only valid in before.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40