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_RELAX_VM_VM_H_
24 #define TVM_RUNTIME_RELAX_VM_VM_H_
25 
26 #ifndef TVM_RELAX_VM_ENABLE_PROFILER
27 #define TVM_RELAX_VM_ENABLE_PROFILER 1
28 #endif
29 
30 #include <memory>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 #include "../memory/memory_manager.h"
36 #include "./bytecode.h"
37 #include "./executable.h"
38 
39 namespace tvm {
40 namespace runtime {
41 
42 using memory::Allocator;
44 using memory::MemoryManager;
45 using memory::Storage;
46 using memory::StorageObj;
47 
48 namespace relax_vm {
49 
53 enum class VMInstrumentReturnKind : int {
55  kNoOp = 0,
57  kSkipRun = 1,
58 };
59 
63 class VMClosureObj : public ClosureObj {
64  public:
70 
78 
79  static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
80  static constexpr const char* _type_key = "relax.vm.Closure";
82 };
83 
85 class VMClosure : public Closure {
86  public:
87  VMClosure(String func_name, PackedFunc impl);
89 
98  static PackedFunc BindLastArgs(PackedFunc func, std::vector<TVMRetValue> last_args);
99 };
100 
109 class VMExtensionNode : public Object {
110  protected:
111  static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
112  static constexpr const char* _type_key = "runtime.VMExtension";
114 };
115 
117 class VMExtension : public ObjectRef {
118  public:
120 };
121 
134  public:
140  virtual void Init(const std::vector<Device>& devices,
141  const std::vector<AllocatorType>& alloc_types) = 0;
146  virtual void LoadExecutable(ObjectPtr<Executable> exec) = 0;
152  virtual VMClosure GetClosure(const String& func_name) = 0;
159  virtual void InvokeClosurePacked(const ObjectRef& closure_or_packedfunc, TVMArgs args,
160  TVMRetValue* rv) = 0;
180  virtual void SetInstrument(PackedFunc 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  return Downcast<T>((*it).second);
195  }
196  auto [it, _] = extensions.emplace(key, T::Create());
197  return Downcast<T>((*it).second);
198  }
199 
215  return static_cast<VirtualMachine*>(arg.operator void*());
216  }
217 
219 
220  //--------------------------------------------------------------------------
221  // The following section contains states that other builtin can depend on
222  //--------------------------------------------------------------------------
224  std::vector<Allocator*> allocators;
226  std::vector<Device> devices;
229  std::unordered_map<uint32_t, VMExtension> extensions;
230 };
231 
232 } // namespace relax_vm
233 } // namespace runtime
234 } // namespace tvm
235 
236 #endif // TVM_RUNTIME_RELAX_VM_VM_H_
An object representing a closure. This object is used by both the Relay VM and interpreter.
Definition: closure.h:36
reference to closure.
Definition: closure.h:44
Base container of module.
Definition: module.h:142
A custom smart pointer for Object.
Definition: object.h:362
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:141
Reference to string objects.
Definition: string.h:98
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:796
Arguments into TVM functions.
Definition: packed_func.h:394
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
An object representing a vm closure.
Definition: vm.h:63
PackedFunc impl
The implementation of the Closure.
Definition: vm.h:77
static constexpr const char * _type_key
Definition: vm.h:80
String func_name
The function name. The function could be any function object that is compatible to the VM runtime.
Definition: vm.h:69
TVM_DECLARE_FINAL_OBJECT_INFO(VMClosureObj, ClosureObj)
static constexpr const uint32_t _type_index
Definition: vm.h:79
reference to closure.
Definition: vm.h:85
TVM_DEFINE_OBJECT_REF_METHODS(VMClosure, Closure, VMClosureObj)
static PackedFunc BindLastArgs(PackedFunc func, std::vector< TVMRetValue > last_args)
Create another PackedFunc with last arguments already bound to last_args.
VMClosure(String func_name, PackedFunc impl)
Represent a VM extension. A VM extension allows the user to extend the VM with target specific functi...
Definition: vm.h:109
static constexpr const uint32_t _type_index
Definition: vm.h:111
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
std::vector< Device > devices
Runtime physical device list.
Definition: vm.h:226
std::unordered_map< uint32_t, VMExtension > extensions
The VM extensions. Mapping from the type index of the extension to the extension instance.
Definition: vm.h:229
static VirtualMachine * GetContextPtr(TVMArgValue arg)
Helper function for vm closure functions to get the context ptr.
Definition: vm.h:214
virtual void InvokeClosurePacked(const ObjectRef &closure_or_packedfunc, TVMArgs args, TVMRetValue *rv)=0
Invoke closure or packed function using PackedFunc convention.
std::vector< Allocator * > allocators
The memory allocators.
Definition: vm.h:224
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
static ObjectPtr< VirtualMachine > Create()
Create a specific instance of VM.
~VirtualMachine()
Definition: vm.h:218
virtual void SetInstrument(PackedFunc instrument)=0
Set an instrumentation function.
static ObjectPtr< VirtualMachine > CreateProfiler()
Create an instance of VM with the profiling feature enabled.
virtual void LoadExecutable(ObjectPtr< Executable > exec)=0
Load the executable for the virtual machine.
virtual VMClosure GetClosure(const String &func_name)=0
Get global function in the VM.
virtual void Init(const std::vector< Device > &devices, const std::vector< AllocatorType > &alloc_types)=0
Initialize the virtual machine for a set of devices.
AllocatorType
Definition: memory_manager.h:42
VMInstrumentReturnKind
Possible instrument actions.
Definition: vm.h:53
@ kSkipRun
Skip the following run, only valid in before.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:84
The bytecode for Relay virtual machine.
The Relay virtual machine executable.