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 
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 <vector>
38 
39 namespace tvm {
40 namespace runtime {
41 namespace vm {
42 
43 struct VMFunction;
44 
57 class Executable : public ModuleNode {
58  public:
67  PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final;
68 
77  void SaveToBinary(dmlc::Stream* stream) final;
78 
88  void SaveToFile(const std::string& path, const std::string& format) final;
89 
100 
111  static runtime::Module Load(const std::string& code, const runtime::Module lib);
112 
122  void MoveLateBoundConstantsToStream(dmlc::Stream* stream, size_t byte_limit);
123 
127  void MoveLateBoundConstantsToFile(const std::string& path, size_t byte_limit);
128 
135  void LoadLateBoundConstantsFromStream(dmlc::Stream* stream);
136 
140  void LoadLateBoundConstantsFromFile(const std::string& path);
141 
174  std::string GetBytecode() const;
175 
180  std::string GetConstants() const;
181 
186  std::string GetVirtualDevices() const;
187 
193  std::string GetPrimitives() const;
194 
199  std::string Stats() const;
200 
207  runtime::Module GetLib() const;
208 
219  void SetLib(const runtime::Module& lib);
220 
226  const VMFunction& GetVMFunctionWithName(const std::string& func_name) const;
227 
233  int GetFunctionArity(std::string func) const;
234 
241  std::string GetFunctionParameterName(std::string func, uint32_t index) const;
242 
243  virtual ~Executable() {}
244 
245  const char* type_key() const final { return "VMExecutable"; }
246 
251  std::vector<Device> virtual_devices;
263  std::vector<ObjectRef> constants;
268  std::vector<String> late_bound_constant_names;
269 
271  std::unordered_map<std::string, Index> global_map;
275  std::unordered_map<std::string, Index> primitive_map;
277  std::map<Index, Map<String, ObjectRef>> op_attrs;
279  std::vector<VMFunction> functions;
281  std::vector<Index> const_device_indexes;
282 
283  private:
289  void SaveVirtualDevicesSection(dmlc::Stream* strm);
290 
296  void SaveGlobalSection(dmlc::Stream* strm);
297 
303  void SaveConstantSection(dmlc::Stream* stream);
304 
310  void LoadConstantSection(dmlc::Stream* stream);
311 
317  void SavePrimitiveOpNames(dmlc::Stream* strm);
318 
324  void SaveCodeSection(dmlc::Stream* strm);
325 
331  void LoadVirtualDevicesSection(dmlc::Stream* strm);
332 
338  void LoadGlobalSection(dmlc::Stream* strm);
339 
345  void LoadPrimitiveOpNames(dmlc::Stream* strm);
346 
352  void LoadCodeSection(dmlc::Stream* strm);
353 
355  std::string code_;
356 };
357 
358 } // namespace vm
359 } // namespace runtime
360 } // namespace tvm
361 
362 #endif // TVM_RUNTIME_VM_EXECUTABLE_H_
std::map< Index, Map< String, ObjectRef > > op_attrs
The structural hashes of the operators in this function.
Definition: executable.h:277
void MoveLateBoundConstantsToFile(const std::string &path, size_t byte_limit)
As for MoveLateBoundConstantsToStream, but save to file at path.
The executable emitted by the VM compiler.
Definition: executable.h:57
A custom smart pointer for Object.
Definition: object.h:358
std::vector< VMFunction > functions
The virtual machine&#39;s function table.
Definition: executable.h:279
Runtime String container types.
std::string GetFunctionParameterName(std::string func, uint32_t index) const
Get the parameter name given the function name and parameter index.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
std::string GetBytecode() const
Get the serialized form of the functions. This is essentially bytecode serialization.
std::string GetVirtualDevices() const
Returns a description of all the (virtual) devices in the executable in human-readable format...
void LoadLateBoundConstantsFromFile(const std::string &path)
As for LoadLateBoundConstantsFromStream, but load from file at path.
void SetLib(const runtime::Module &lib)
Set the lib module in an executable.
std::vector< ObjectRef > constants
The global constant array.
Definition: executable.h:263
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:157
TVMByteArray Save()
Serialize the executable into global section, constant section, and code section. This object must ou...
void MoveLateBoundConstantsToStream(dmlc::Stream *stream, size_t byte_limit)
Returns the late-bound constants for the executable (if any) as a byte-stream. Leaves the executable&#39;...
std::vector< Index > const_device_indexes
The index of the device holding each constant.
Definition: executable.h:281
int host_device_index
The device index corresponding to the &#39;host&#39; device. That will hold and evaluate shape-related data a...
Definition: executable.h:256
std::unordered_map< std::string, Index > primitive_map
A mapping from the packed function&#39;s global name (as string) to the index that corresponds to the pos...
Definition: executable.h:275
const char * type_key() const final
Definition: executable.h:245
void LoadLateBoundConstantsFromStream(dmlc::Stream *stream)
Restores the late-bound constants for the executable (if any) from given byte-stream.
void SaveToBinary(dmlc::Stream *stream) final
Write the Executable to the binary stream in serialized form.
The bytecode for Relay virtual machine.
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:271
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:268
std::string GetPrimitives() const
Returns a description of all the &#39;primitive&#39; (ie PackedFuncs) in the executable in human-readable for...
A representation of a Relay function in the VM.
Definition: vm.h:77
std::string Stats() const
Print the detailed statistics of the given code, i.e. number of globls and constants, etc.
int GetFunctionArity(std::string func) const
Get the arity of the VMFunction.
Base container of module.
Definition: module.h:113
A managed object in the TVM runtime.
Runtime container of the functions generated by TVM, This is used to support dynamically link...
Module container of TVM.
Definition: module.h:50
std::vector< Device > virtual_devices
The (compile-time, virtual) devices corresponding to each device index. Currently we only support at ...
Definition: executable.h:251
Runtime Map container types.
const VMFunction & GetVMFunctionWithName(const std::string &func_name) const
Get VMFunction.
PackedFunc GetFunction(const std::string &name, const ObjectPtr< Object > &sptr_to_self) final
Get a PackedFunc from an executable module.
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
static runtime::Module Load(const std::string &code, const runtime::Module lib)
Load the saved VM executable.
void SaveToFile(const std::string &path, const std::string &format) final
Write the Executable to the provided path as a file containing its serialized content.
runtime::Module GetLib() const
Get the lib module in an executable. Users have the flexibility to call export_library from the front...
std::string GetConstants() const
Returns a description of all the constants in the executable in human-readable format. Intended for debugging and diff-testing.
Type-erased function used across TVM API.
virtual ~Executable()
Definition: executable.h:243