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 TVM_DLL 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 
99  TVMByteArray Save();
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 
132  Map<String, NDArray> GetLateBoundConstants(size_t byte_limit);
133 
140  void LoadLateBoundConstantsFromStream(dmlc::Stream* stream);
141 
148  void LoadLateBoundConstantsFromMap(Map<String, NDArray> map);
149 
153  void LoadLateBoundConstantsFromFile(const std::string& path);
154 
187  std::string GetBytecode() const;
188 
193  std::string GetConstants() const;
194 
199  std::string GetVirtualDevices() const;
200 
206  std::string GetPrimitives() const;
207 
212  std::string Stats() const;
213 
220  runtime::Module GetLib() const;
221 
232  void SetLib(const runtime::Module& lib);
233 
239  const VMFunction& GetVMFunctionWithName(const std::string& func_name) const;
240 
246  int GetFunctionArity(std::string func) const;
247 
254  std::string GetFunctionParameterName(std::string func, uint32_t index) const;
255 
256  virtual ~Executable() {}
257 
258  const char* type_key() const final { return "VMExecutable"; }
259 
264  std::vector<Device> virtual_devices;
269  int host_device_index = -1;
276  std::vector<ObjectRef> constants;
281  std::vector<String> late_bound_constant_names;
282 
284  std::unordered_map<std::string, Index> global_map;
288  std::unordered_map<std::string, Index> primitive_map;
290  std::map<Index, Map<String, ObjectRef>> op_attrs;
292  std::vector<VMFunction> functions;
294  std::vector<Index> const_device_indexes;
295 
296  private:
302  void SaveVirtualDevicesSection(dmlc::Stream* strm);
303 
309  void SaveGlobalSection(dmlc::Stream* strm);
310 
316  void SaveConstantSection(dmlc::Stream* stream);
317 
323  void LoadConstantSection(dmlc::Stream* stream);
324 
330  void SavePrimitiveOpNames(dmlc::Stream* strm);
331 
337  void SaveCodeSection(dmlc::Stream* strm);
338 
344  void LoadVirtualDevicesSection(dmlc::Stream* strm);
345 
351  void LoadGlobalSection(dmlc::Stream* strm);
352 
358  void LoadPrimitiveOpNames(dmlc::Stream* strm);
359 
365  void LoadCodeSection(dmlc::Stream* strm);
366 
368  std::string code_;
369 };
370 
371 } // namespace vm
372 } // namespace runtime
373 } // namespace tvm
374 
375 #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:290
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:292
Runtime String container types.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
std::vector< ObjectRef > constants
The global constant array.
Definition: executable.h:276
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:214
std::vector< Index > const_device_indexes
The index of the device holding each constant.
Definition: executable.h:294
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:288
const char * type_key() const final
Definition: executable.h:258
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:284
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:281
A representation of a Relay function in the VM.
Definition: vm.h:77
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
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
std::vector< Device > virtual_devices
The (compile-time, virtual) devices corresponding to each device index. Currently we only support at ...
Definition: executable.h:264
Runtime Map container types.
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
Type-erased function used across TVM API.
virtual ~Executable()
Definition: executable.h:256