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_VM_EXECUTABLE_H_
24 #define TVM_RUNTIME_VM_EXECUTABLE_H_
25 
26 #include <tvm/ffi/function.h>
27 #include <tvm/runtime/module.h>
28 #include <tvm/runtime/object.h>
29 
30 #include <string>
31 #include <unordered_map>
32 #include <vector>
33 
34 #include "./bytecode.h"
35 
36 // Convention: this version should set to minimum TVM version it support
37 // NOTE: this file only changes if we change relax vm format
38 // for example if relax vm format do not change in 0.15, this should remain as 0.14
39 // if it changes in 0.16, we will change it to 0.16
40 #define VM_VERSION "0.14"
41 
42 namespace tvm {
43 namespace runtime {
44 namespace vm {
45 
52 struct VMFuncInfo {
54  enum class FuncKind : int {
56  kPackedFunc = 0,
58  kVMFunc = 1,
60  kVMTIRFunc = 2,
61  };
65  std::string name;
75  std::vector<std::string> param_names;
76 
77  // defined customized loader save
78  void Save(dmlc::Stream* writer) const;
79  bool Load(dmlc::Stream* reader);
80 };
81 
88 class VMExecutable : public ffi::ModuleObj {
89  public:
91  int GetPropertyMask() const final { return ffi::Module::kBinarySerializable; };
92 
98  std::string Stats() const;
116  String AsText() const;
121  String AsPython() const;
126  ffi::Bytes SaveToBytes() const final;
132  static ffi::Module LoadFromBytes(const ffi::Bytes& bytes);
138  void WriteToFile(const String& file_name, const String& format) const final;
140  ffi::Module VMLoadExecutable() const;
142  ffi::Module VMProfilerLoadExecutable() const;
144  bool HasFunction(const String& name) const;
150  static ffi::Module LoadFromFile(const String& file_name);
151 
153  std::vector<VMFuncInfo> func_table;
155  std::unordered_map<std::string, Index> func_map;
157  std::vector<ffi::Any> constants;
159  std::vector<Index> instr_offset;
161  std::vector<ExecWord> instr_data;
162 
163  virtual ~VMExecutable() {}
164 
165  TVM_MODULE_VTABLE_BEGIN("relax.VMExecutable");
173 
174  private:
179  void SaveGlobalSection(dmlc::Stream* strm) const;
184  void SaveConstantSection(dmlc::Stream* strm) const;
189  void SaveCodeSection(dmlc::Stream* strm) const;
194  void SavePackedFuncNames(dmlc::Stream* strm) const;
199  void LoadGlobalSection(dmlc::Stream* strm);
204  void LoadConstantSection(dmlc::Stream* strm);
209  void LoadCodeSection(dmlc::Stream* strm);
214  void LoadPackedFuncNames(dmlc::Stream* strm);
215 };
216 
217 } // namespace vm
218 } // namespace runtime
219 } // namespace tvm
220 
221 namespace dmlc {
223 } // namespace dmlc
224 #endif // TVM_RUNTIME_VM_EXECUTABLE_H_
The bytecode for the virtual machine.
The virtual machine executable emitted by the VM compiler.
Definition: executable.h:88
ffi::Module VMLoadExecutable() const
Create a Relax virtual machine and load this as the executable.
TVM_MODULE_VTABLE_BEGIN("relax.VMExecutable")
Instruction GetInstruction(Index i) const
Get the i-th instruction from the executable.
std::vector< Index > instr_offset
The offset of instruction.
Definition: executable.h:159
String AsPython() const
Print the instructions as python program.
ffi::Module VMProfilerLoadExecutable() const
Create a Relax virtual machine with profiler and load this as the executable.
TVM_MODULE_VTABLE_ENTRY("as_python", &VMExecutable::AsPython)
static ffi::Module LoadFromFile(const String &file_name)
Load VMExecutable from the file.
void SetInstructionData(Index i, Index j, ExecWord val)
Set j-th byte data of i-th instruction to val.
String AsText() const
Print the instructions as text format.
std::vector< ffi::Any > constants
The global constant pool.
Definition: executable.h:157
std::unordered_map< std::string, Index > func_map
A map from globals (as strings) to their index in the function map.
Definition: executable.h:155
TVM_MODULE_VTABLE_ENTRY("stats", &VMExecutable::Stats)
TVM_MODULE_VTABLE_ENTRY("as_text", &VMExecutable::AsText)
ffi::Bytes SaveToBytes() const final
Write the VMExecutable to the binary stream in serialized form.
void WriteToFile(const String &file_name, const String &format) const final
Write the VMExecutable to the provided path as a file containing its serialized content.
TVM_MODULE_VTABLE_ENTRY("has_function", &VMExecutable::HasFunction)
static ffi::Module LoadFromBytes(const ffi::Bytes &bytes)
Load VMExecutable from the binary stream in serialized form.
std::vector< ExecWord > instr_data
The byte data of instruction.
Definition: executable.h:161
std::vector< VMFuncInfo > func_table
The virtual machine's function table.
Definition: executable.h:153
int GetPropertyMask() const final
Get the property of the runtime module .
Definition: executable.h:91
bool HasFunction(const String &name) const
Check if the VMExecutable contains a specific function.
TVM_MODULE_VTABLE_ENTRY("vm_load_executable", &VMExecutable::VMLoadExecutable)
std::string Stats() const
Print the detailed statistics of the given code, i.e. number of globals and constants,...
TVM_MODULE_VTABLE_ENTRY("vm_profiler_load_executable", &VMExecutable::VMProfilerLoadExecutable)
Definition: serializer.h:33
DMLC_DECLARE_TRAITS(has_saveload, ::tvm::runtime::vm::VMFuncInfo, true)
Definition: builtin.h:29
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
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.
Runtime container of the functions generated by TVM, This is used to support dynamically link,...
A single virtual machine instruction.
Definition: bytecode.h:72
Information entry in executable function table.
Definition: executable.h:52
Index register_file_size
The register file size of the function.
Definition: executable.h:73
Index num_args
The number of arguments of the function.
Definition: executable.h:71
FuncKind kind
The kind of function.
Definition: executable.h:63
std::vector< std::string > param_names
The function parameter names.
Definition: executable.h:75
Index start_instr
The start instruction index of the function.
Definition: executable.h:67
FuncKind
kind of the function.
Definition: executable.h:54
@ kPackedFunc
system level packed function
void Save(dmlc::Stream *writer) const
std::string name
The function's name, global symbol.
Definition: executable.h:65
bool Load(dmlc::Stream *reader)
Index end_instr
The end instruction index of the function.
Definition: executable.h:69