tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
24 #ifndef TVM_RUNTIME_VM_EXECUTABLE_H_
25 #define TVM_RUNTIME_VM_EXECUTABLE_H_
26 
29 #include <tvm/runtime/module.h>
30 #include <tvm/runtime/object.h>
33 
34 #include <map>
35 #include <string>
36 #include <unordered_map>
37 #include <utility>
38 #include <vector>
39 
40 namespace tvm {
41 namespace runtime {
42 namespace vm {
43 
44 struct VMFunction;
45 
58 class TVM_DLL Executable : public ModuleNode {
59  public:
60  TVM_MODULE_VTABLE_BEGIN("VMExecutable");
74  TVM_MODULE_VTABLE_ENTRY("load_late_bound_consts_from_map",
77 
90  void SaveToBinary(dmlc::Stream* stream) final;
91 
101  void SaveToFile(const String& path, const String& format) final;
102 
113 
124  static runtime::Module Load(const std::string& code, const runtime::Module lib);
125 
135  void MoveLateBoundConstantsToStream(dmlc::Stream* stream, int64_t byte_limit);
136 
140  void MoveLateBoundConstantsToFile(const std::string& path, int64_t byte_limit);
141 
146 
153  void LoadLateBoundConstantsFromStream(dmlc::Stream* stream);
154 
162 
166  void LoadLateBoundConstantsFromFile(const std::string& path);
167 
200  std::string GetBytecode() const;
201 
206  std::string GetConstants() const;
207 
212  std::string GetVirtualDevices() const;
213 
219  std::string GetPrimitives() const;
220 
225  std::string Stats() const;
226 
234 
245  void SetLib(const runtime::Module& lib);
246 
252  const VMFunction& GetVMFunctionWithName(const std::string& func_name) const;
253 
259  int GetFunctionArity(std::string func) const;
260 
267  std::string GetFunctionParameterName(std::string func, int index) const;
268 
269  virtual ~Executable() {}
270 
275  std::vector<std::pair<Device, std::string>> virtual_devices;
280  int host_device_index = -1;
287  std::vector<ObjectRef> constants;
292  std::vector<String> late_bound_constant_names;
293 
295  std::unordered_map<std::string, Index> global_map;
299  std::unordered_map<std::string, Index> primitive_map;
301  std::map<Index, Map<String, ObjectRef>> op_attrs;
303  std::vector<VMFunction> functions;
305  std::vector<Index> const_device_indexes;
306 
307  private:
313  void SaveVirtualDevicesSection(dmlc::Stream* strm);
314 
320  void SaveGlobalSection(dmlc::Stream* strm);
321 
327  void SaveConstantSection(dmlc::Stream* stream);
328 
334  void LoadConstantSection(dmlc::Stream* stream);
335 
341  void SavePrimitiveOpNames(dmlc::Stream* strm);
342 
348  void SaveCodeSection(dmlc::Stream* strm);
349 
355  void LoadVirtualDevicesSection(dmlc::Stream* strm);
356 
362  void LoadGlobalSection(dmlc::Stream* strm);
363 
369  void LoadPrimitiveOpNames(dmlc::Stream* strm);
370 
376  void LoadCodeSection(dmlc::Stream* strm);
377 
379  std::string code_;
380 };
381 
382 } // namespace vm
383 } // namespace runtime
384 } // namespace tvm
385 
386 #endif // TVM_RUNTIME_VM_EXECUTABLE_H_
The bytecode for Relay virtual machine.
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
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:58
void SetLib(const runtime::Module &lib)
Set the lib module in an executable.
std::map< Index, Map< String, ObjectRef > > op_attrs
The structural hashes of the operators in this function.
Definition: executable.h:301
void LoadLateBoundConstantsFromFile(const std::string &path)
As for LoadLateBoundConstantsFromStream, but load from file at path.
TVM_MODULE_VTABLE_ENTRY("get_stats", &Executable::Stats)
TVM_MODULE_VTABLE_ENTRY("load_late_bound_consts", &Executable::LoadLateBoundConstantsFromFile)
std::vector< String > late_bound_constant_names
For each constant index the name of the late-bound constant, or null if constant is immediate....
Definition: executable.h:292
void LoadLateBoundConstantsFromStream(dmlc::Stream *stream)
Restores the late-bound constants for the executable (if any) from given byte-stream.
std::string GetVirtualDevices() const
Returns a description of all the (virtual) devices in the executable in human-readable format....
std::string GetFunctionParameterName(std::string func, int index) const
Get the parameter name given the function name and parameter index.
Map< String, NDArray > GetLateBoundConstants(int64_t byte_limit)
Get a map of all constants with larger that byte_limit in size.
TVM_MODULE_VTABLE_ENTRY("get_function_param_name", &Executable::GetFunctionParameterName)
TVM_MODULE_VTABLE_ENTRY("get_late_bound_consts", &Executable::GetLateBoundConstants)
std::vector< Index > const_device_indexes
The index of the device holding each constant.
Definition: executable.h:305
static runtime::Module Load(const std::string &code, const runtime::Module lib)
Load the saved VM executable.
TVM_MODULE_VTABLE_ENTRY("get_primitives", &Executable::GetPrimitives)
std::string Stats() const
Print the detailed statistics of the given code, i.e. number of globls and constants,...
TVM_MODULE_VTABLE_ENTRY("get_constants", &Executable::GetConstants)
void MoveLateBoundConstantsToFile(const std::string &path, int64_t byte_limit)
As for MoveLateBoundConstantsToStream, but save to file at path.
void MoveLateBoundConstantsToStream(dmlc::Stream *stream, int64_t byte_limit)
Returns the late-bound constants for the executable (if any) as a byte-stream. Leaves the executable'...
std::vector< ObjectRef > constants
The global constant array.
Definition: executable.h:287
int GetFunctionArity(std::string func) const
Get the arity of the VMFunction.
void SaveToBinary(dmlc::Stream *stream) final
Write the Executable to the binary stream in serialized form.
std::string GetPrimitives() const
Returns a description of all the 'primitive' (ie PackedFuncs) in the executable in human-readable for...
Module VMLoadExecutable()
Creates a VM that loads this as the executable.
TVM_MODULE_VTABLE_BEGIN("VMExecutable")
int GetPropertyMask() const final
Get the property of the runtime module .
Definition: executable.h:79
virtual ~Executable()
Definition: executable.h:269
std::unordered_map< std::string, Index > global_map
A map from globals (as strings) to their index in the Relay function map.
Definition: executable.h:295
void LoadLateBoundConstantsFromMap(Map< String, NDArray > map)
Restores the late-bound constants for the executable (if any) from given map.
void SaveToFile(const String &path, const String &format) final
Write the Executable to the provided path as a file containing its serialized content.
TVM_MODULE_VTABLE_ENTRY("save", &Executable::Save)
runtime::Module GetLib() const
Get the lib module in an executable. Users have the flexibility to call export_library from the front...
TVM_MODULE_VTABLE_ENTRY("vm_load_executable", &Executable::VMLoadExecutable)
TVM_MODULE_VTABLE_ENTRY("get_lib", &Executable::GetLib)
std::unordered_map< std::string, Index > primitive_map
A mapping from the packed function's global name (as string) to the index that corresponds to the pos...
Definition: executable.h:299
TVM_MODULE_VTABLE_ENTRY("get_bytecode", &Executable::GetBytecode)
std::vector< std::pair< Device, std::string > > virtual_devices
The (compile-time, virtual) devices corresponding to each device index. This vector contains a pair D...
Definition: executable.h:275
TVM_MODULE_VTABLE_ENTRY("get_virtual_devices", &Executable::GetVirtualDevices)
TVM_MODULE_VTABLE_ENTRY("move_late_bound_consts", &Executable::MoveLateBoundConstantsToFile)
TVM_MODULE_VTABLE_ENTRY("load_late_bound_consts_from_map", &Executable::LoadLateBoundConstantsFromMap)
TVM_MODULE_VTABLE_ENTRY("get_function_arity", &Executable::GetFunctionArity)
std::string GetBytecode() const
Get the serialized form of the functions. This is essentially bytecode serialization.
std::vector< VMFunction > functions
The virtual machine's function table.
Definition: executable.h:303
std::string GetConstants() const
Returns a description of all the constants in the executable in human-readable format....
TVMByteArray Save()
Serialize the executable into global section, constant section, and code section. This object must ou...
const VMFunction & GetVMFunctionWithName(const std::string &func_name) const
Get VMFunction.
Runtime Map container types.
@ 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.
Runtime container of the functions generated by TVM, This is used to support dynamically link,...
Runtime String container types.
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:221
A representation of a Relay function in the VM.
Definition: vm.h:84