tvm
executable.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_EXECUTABLE_H_
24 #define TVM_RUNTIME_RELAX_VM_EXECUTABLE_H_
25 
27 #include <tvm/runtime/object.h>
29 #include <tvm/runtime/registry.h>
30 
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 #include "./bytecode.h"
36 
37 // Convention: this version should set to minimum TVM version it support
38 // NOTE: this file only changes if we change relax vm format
39 // for example if relax vm format do not change in 0.15, this should remain as 0.14
40 // if it changes in 0.16, we will change it to 0.16
41 #define RELAX_VM_VERSION "0.14"
42 
43 namespace tvm {
44 namespace runtime {
45 namespace relax_vm {
46 
53 struct VMFuncInfo {
55  enum class FuncKind : int {
57  kPackedFunc = 0,
59  kVMFunc = 1,
61  kVMTIRFunc = 2,
62  };
66  std::string name;
76  std::vector<std::string> param_names;
77 
78  // defined customized loader save
79  void Save(dmlc::Stream* writer) const;
80  bool Load(dmlc::Stream* reader);
81 };
82 
90  public:
93 
99  std::string Stats() const;
117  String AsText() const;
122  String AsPython() const;
127  void SaveToBinary(dmlc::Stream* stream) final;
133  static Module LoadFromBinary(void* stream);
139  void SaveToFile(const String& file_name, const String& format) final;
145  bool HasFunction(const String& name) const;
151  static Module LoadFromFile(const String& file_name);
152 
154  std::vector<VMFuncInfo> func_table;
156  std::unordered_map<std::string, Index> func_map;
158  std::vector<TVMRetValue> constants;
160  std::vector<Index> instr_offset;
162  std::vector<ExecWord> instr_data;
163 
164  virtual ~Executable() {}
165 
166  TVM_MODULE_VTABLE_BEGIN("relax.Executable");
174 
175  private:
180  void SaveGlobalSection(dmlc::Stream* strm);
185  void SaveConstantSection(dmlc::Stream* strm);
190  void SaveCodeSection(dmlc::Stream* strm);
195  void SavePackedFuncNames(dmlc::Stream* strm);
200  void LoadGlobalSection(dmlc::Stream* strm);
205  void LoadConstantSection(dmlc::Stream* strm);
210  void LoadCodeSection(dmlc::Stream* strm);
215  void LoadPackedFuncNames(dmlc::Stream* strm);
216 };
217 
218 } // namespace relax_vm
219 } // namespace runtime
220 } // namespace tvm
221 
222 namespace dmlc {
224 } // namespace dmlc
225 #endif // TVM_RUNTIME_RELAX_VM_EXECUTABLE_H_
Base container of module.
Definition: module.h:142
Module container of TVM.
Definition: module.h:79
Reference to string objects.
Definition: string.h:98
The executable emitted by the VM compiler.
Definition: executable.h:89
bool HasFunction(const String &name) const
Check if the Executable contains a specific function.
String AsText() const
Print the instructions as text format.
TVM_MODULE_VTABLE_BEGIN("relax.Executable")
int GetPropertyMask() const final
Get the property of the runtime module .
Definition: executable.h:92
TVM_MODULE_VTABLE_ENTRY("stats", &Executable::Stats)
static Module LoadFromFile(const String &file_name)
Load Executable from the file.
virtual ~Executable()
Definition: executable.h:164
std::unordered_map< std::string, Index > func_map
A map from globals (as strings) to their index in the function map.
Definition: executable.h:156
Instruction GetInstruction(Index i) const
Get the i-th instruction from the executable.
TVM_MODULE_VTABLE_ENTRY("as_text", &Executable::AsText)
Module VMLoadExecutable() const
Create a Relax virtual machine and load this as the executable.
static Module LoadFromBinary(void *stream)
Load Executable from the binary stream in serialized form.
TVM_MODULE_VTABLE_ENTRY("vm_load_executable", &Executable::VMLoadExecutable)
std::vector< VMFuncInfo > func_table
The virtual machine's function table.
Definition: executable.h:154
TVM_MODULE_VTABLE_ENTRY("has_function", &Executable::HasFunction)
String AsPython() const
Print the instructions as python program.
std::vector< TVMRetValue > constants
The global constant pool.
Definition: executable.h:158
void SaveToFile(const String &file_name, const String &format) final
Write the Executable to the provided path as a file containing its serialized content.
void SaveToBinary(dmlc::Stream *stream) final
Write the Executable to the binary stream in serialized form.
TVM_MODULE_VTABLE_ENTRY("vm_profiler_load_executable", &Executable::VMProfilerLoadExecutable)
std::vector< Index > instr_offset
The offset of instruction.
Definition: executable.h:160
TVM_MODULE_VTABLE_ENTRY("as_python", &Executable::AsPython)
std::vector< ExecWord > instr_data
The byte data of instruction.
Definition: executable.h:162
Module VMProfilerLoadExecutable() const
Create a Relax virtual machine with profiler and load this as the executable.
std::string Stats() const
Print the detailed statistics of the given code, i.e. number of globals and constants,...
void SetInstructionData(Index i, Index j, ExecWord val)
Set j-th byte data of i-th instruction to val.
Runtime Closure container types.
Definition: executable.h:222
DMLC_DECLARE_TRAITS(has_saveload, ::tvm::runtime::relax_vm::VMFuncInfo, true)
ExecWord Index
An alias for the integer type used ubiquitously in the VM.
Definition: bytecode.h:48
int64_t ExecWord
The storage type for the bytecode in the VM.
Definition: bytecode.h:40
@ kBinarySerializable
kBinarySerializable we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON runtime are ...
Definition: module.h:56
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A managed object in the TVM runtime.
Type-erased function used across TVM API.
This file defines the TVM global function registry.
A single virtual machine instruction.
Definition: bytecode.h:72
Information entry in executable function table.
Definition: executable.h:53
bool Load(dmlc::Stream *reader)
FuncKind kind
The kind of function.
Definition: executable.h:64
FuncKind
kind of the function.
Definition: executable.h:55
@ kPackedFunc
system level packed function
Index num_args
The number of arguments of the function.
Definition: executable.h:72
void Save(dmlc::Stream *writer) const
Index start_instr
The start instruction index of the function.
Definition: executable.h:68
Index register_file_size
The register file size of the function.
Definition: executable.h:74
Index end_instr
The end instruction index of the function.
Definition: executable.h:70
std::vector< std::string > param_names
The function parameter names.
Definition: executable.h:76
std::string name
The function's name, global symbol.
Definition: executable.h:66
The bytecode for Relay virtual machine.