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/extra/module.h>
27 #include <tvm/ffi/function.h>
28 #include <tvm/support/io.h>
29 #include <tvm/support/serializer.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 VM_VERSION "0.14"
42 
43 namespace tvm {
44 namespace runtime {
45 namespace 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(support::Stream* writer) const;
80  bool Load(support::Stream* reader);
81 };
82 
89 class TVM_RUNTIME_DLL VMExecutable : public ffi::ModuleObj {
90  public:
92  int GetPropertyMask() const final { return ffi::Module::kBinarySerializable; };
93 
99  std::string Stats() const;
117  ffi::String AsText() const;
122  ffi::String AsPython() const;
127  ffi::Bytes SaveToBytes() const final;
133  static ffi::Module LoadFromBytes(const ffi::Bytes& bytes);
139  void WriteToFile(const ffi::String& file_name, const ffi::String& format) const final;
141  ffi::Module VMLoadExecutable() const;
143  bool HasFunction(const ffi::String& name) const;
149  static ffi::Module LoadFromFile(const ffi::String& file_name);
150 
152  std::vector<VMFuncInfo> func_table;
154  std::unordered_map<std::string, Index> func_map;
156  std::vector<ffi::Any> constants;
158  std::unordered_map<Index, std::string> memory_scopes;
160  std::vector<Index> instr_offset;
162  std::vector<ExecWord> instr_data;
163 
164  virtual ~VMExecutable() {}
165 
167  const char* kind() const final;
173  ffi::Optional<ffi::Function> GetFunction(const ffi::String& name) override;
174 
175  private:
180  void SaveGlobalSection(support::Stream* strm) const;
185  void SaveMemoryScopeSection(support::Stream* strm) const;
190  void SaveConstantSection(support::Stream* strm) const;
195  void SaveCodeSection(support::Stream* strm) const;
200  void SavePackedFuncNames(support::Stream* strm) const;
205  void LoadGlobalSection(support::Stream* strm);
210  void LoadMemoryScopeSection(support::Stream* strm);
215  void LoadConstantSection(support::Stream* strm);
220  void LoadCodeSection(support::Stream* strm);
225  void LoadPackedFuncNames(support::Stream* strm);
226 };
227 
228 } // namespace vm
229 } // namespace runtime
230 } // namespace tvm
231 
232 namespace tvm {
233 namespace support {
234 template <>
236  static constexpr bool enabled = true;
237  static void Write(Stream* strm, const ::tvm::runtime::vm::VMFuncInfo& data) { data.Save(strm); }
238  static bool Read(Stream* strm, ::tvm::runtime::vm::VMFuncInfo* data) { return data->Load(strm); }
239 };
240 } // namespace support
241 } // namespace tvm
242 #endif // TVM_RUNTIME_VM_EXECUTABLE_H_
The bytecode for the virtual machine.
The virtual machine executable emitted by the VM compiler.
Definition: executable.h:89
Instruction GetInstruction(Index i) const
Get the i-th instruction from the executable.
const char * kind() const final
Module type key.
void SetInstructionData(Index i, Index j, ExecWord val)
Set j-th byte data of i-th instruction to val.
ffi::String AsText() const
Print the instructions as text format.
ffi::Bytes SaveToBytes() const final
Write the VMExecutable to the binary stream in serialized form.
ffi::String AsPython() const
Print the instructions as python program.
int GetPropertyMask() const final
Get the property of the runtime module .
Definition: executable.h:92
std::string Stats() const
Print the detailed statistics of the given code, i.e. number of globals and constants,...
Abstract binary stream for serialization.
Definition: io.h:57
Binary stream I/O interface.
tvm::relax::Function Function
Definition: transform.h:38
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
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
#define TVM_RUNTIME_DLL
Definition: base.h:88
Serializer<T> specializations for tvm::support::Stream.
A single virtual machine instruction.
Definition: bytecode.h:72
Information entry in executable function table.
Definition: executable.h:53
Index register_file_size
The register file size of the function.
Definition: executable.h:74
Index num_args
The number of arguments of the function.
Definition: executable.h:72
FuncKind kind
The kind of function.
Definition: executable.h:64
std::vector< std::string > param_names
The function parameter names.
Definition: executable.h:76
Index start_instr
The start instruction index of the function.
Definition: executable.h:68
FuncKind
kind of the function.
Definition: executable.h:55
@ kPackedFunc
system level packed function
std::string name
The function's name, global symbol.
Definition: executable.h:66
bool Load(support::Stream *reader)
void Save(support::Stream *writer) const
Index end_instr
The end instruction index of the function.
Definition: executable.h:70
static void Write(Stream *strm, const ::tvm::runtime::vm::VMFuncInfo &data)
Definition: executable.h:237
static bool Read(Stream *strm, ::tvm::runtime::vm::VMFuncInfo *data)
Definition: executable.h:238
Primary Serializer template. Specialize for each serializable type.
Definition: io.h:42