tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
module.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 
26 #ifndef TVM_RUNTIME_MODULE_H_
27 #define TVM_RUNTIME_MODULE_H_
28 
29 #include <dmlc/io.h>
32 #include <tvm/runtime/memory.h>
33 #include <tvm/runtime/object.h>
34 
35 #include <memory>
36 #include <mutex>
37 #include <string>
38 #include <unordered_map>
39 #include <vector>
40 
41 namespace tvm {
42 namespace runtime {
43 
48 enum ModulePropertyMask : int {
62  kRunnable = 0b010,
71 };
72 
73 class ModuleNode;
74 class PackedFunc;
75 
79 class Module : public ObjectRef {
80  public:
81  Module() {}
82  // constructor from container.
83  explicit Module(ObjectPtr<Object> n) : ObjectRef(n) {}
93  inline PackedFunc GetFunction(const std::string& name, bool query_imports = false);
94  // The following functions requires link with runtime.
102  inline void Import(Module other);
104  inline ModuleNode* operator->();
106  inline const ModuleNode* operator->() const;
114  TVM_DLL static Module LoadFromFile(const std::string& file_name, const std::string& format = "");
115  // refer to the corresponding container.
117  friend class ModuleNode;
118 };
119 
142 class TVM_DLL ModuleNode : public Object {
143  public:
145  virtual ~ModuleNode() = default;
150  virtual const char* type_key() const = 0;
168  virtual PackedFunc GetFunction(const std::string& name,
169  const ObjectPtr<Object>& sptr_to_self) = 0;
175  virtual void SaveToFile(const std::string& file_name, const std::string& format);
183  virtual void SaveToBinary(dmlc::Stream* stream);
189  virtual std::string GetSource(const std::string& format = "");
194  virtual std::string GetFormat();
204  PackedFunc GetFunction(const std::string& name, bool query_imports = false);
212  void Import(Module other);
220  const PackedFunc* GetFuncFromEnv(const std::string& name);
222  const std::vector<Module>& imports() const { return imports_; }
223 
229  virtual int GetPropertyMask() const { return 0b000; }
230 
232  bool IsDSOExportable() const {
233  return (GetPropertyMask() & ModulePropertyMask::kDSOExportable) != 0;
234  }
235 
245  virtual bool ImplementsFunction(const String& name, bool query_imports = false);
246 
247  // integration with the existing components.
248  static constexpr const uint32_t _type_index = TypeIndex::kRuntimeModule;
249  static constexpr const char* _type_key = "runtime.Module";
250  // NOTE: ModuleNode can still be sub-classed
251  //
253 
254  protected:
255  friend class Module;
256  friend class ModuleInternal;
258  std::vector<Module> imports_;
259 
260  private:
262  std::unordered_map<std::string, std::shared_ptr<PackedFunc>> import_cache_;
263  std::mutex mutex_;
264 };
265 
271 TVM_DLL bool RuntimeEnabled(const std::string& target);
272 
274 namespace symbol {
276 constexpr const char* tvm_get_c_metadata = "get_c_metadata";
278 constexpr const char* tvm_module_ctx = "__tvm_module_ctx";
280 constexpr const char* tvm_dev_mblob = "__tvm_dev_mblob";
282 constexpr const char* tvm_dev_mblob_nbytes = "__tvm_dev_mblob_nbytes";
284 constexpr const char* tvm_set_device = "__tvm_set_device";
286 constexpr const char* tvm_global_barrier_state = "__tvm_global_barrier_state";
288 constexpr const char* tvm_prepare_global_barrier = "__tvm_prepare_global_barrier";
290 constexpr const char* tvm_module_main = "__tvm_main__";
292 constexpr const char* tvm_param_prefix = "__tvm_param__";
294 constexpr const char* tvm_lookup_linked_param = "_lookup_linked_param";
296 constexpr const char* tvm_entrypoint_suffix = "run";
297 } // namespace symbol
298 
299 // implementations of inline functions.
300 
301 inline void Module::Import(Module other) { return (*this)->Import(other); }
302 
303 inline ModuleNode* Module::operator->() { return static_cast<ModuleNode*>(get_mutable()); }
304 
305 inline const ModuleNode* Module::operator->() const {
306  return static_cast<const ModuleNode*>(get());
307 }
308 
309 inline std::ostream& operator<<(std::ostream& out, const Module& module) {
310  out << "Module(type_key= ";
311  out << module->type_key();
312  out << ")";
313 
314  return out;
315 }
316 
317 } // namespace runtime
318 } // namespace tvm
319 
320 #include <tvm/runtime/packed_func.h> // NOLINT(*)
321 #endif // TVM_RUNTIME_MODULE_H_
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
constexpr const char * tvm_set_device
global function to set device
Definition: module.h:284
PackedFunc GetFunction(const std::string &name, bool query_imports=false)
Get packed function from current module by name.
Definition: packed_func.h:1945
void Import(Module other)
Import another module into this module.
Definition: module.h:301
A custom smart pointer for Object.
Definition: object.h:358
runtime::Module.
Definition: object.h:62
constexpr const char * tvm_lookup_linked_param
A PackedFunc that looks up linked parameters by storage_id.
Definition: module.h:294
bool RuntimeEnabled(const std::string &target)
Check if runtime module is enabled for target.
Runtime String container types.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
virtual const char * type_key() const =0
bool IsDSOExportable() const
Returns true if this module is &#39;DSO exportable&#39;.
Definition: module.h:232
Runtime memory management.
kBinarySerializable we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON runtime are ...
Definition: module.h:56
base class of all object containers.
Definition: object.h:167
constexpr const char * tvm_dev_mblob_nbytes
Number of bytes of device module blob.
Definition: module.h:282
kDSOExportable we can export the module as DSO. A DSO exportable module (e.g., a CSourceModuleNode of...
Definition: module.h:70
friend class ModuleNode
Definition: module.h:117
constexpr const char * tvm_global_barrier_state
Auxiliary counter to global barrier.
Definition: module.h:286
constexpr const char * tvm_module_main
Placeholder for the module&#39;s entry function.
Definition: module.h:290
constexpr const char * tvm_prepare_global_barrier
Prepare the global barrier before kernels that uses global barrier.
Definition: module.h:288
std::vector< Module > imports_
The modules this module depend on.
Definition: module.h:258
Reference to string objects.
Definition: string.h:98
ModulePropertyMask
Property of runtime module We classify the property of runtime module into the following categories...
Definition: module.h:48
Base class of all object reference.
Definition: object.h:511
kRunnable we can run the module directly. LLVM/CUDA/JSON runtime, executors (e.g, virtual machine) ru...
Definition: module.h:62
Base container of module.
Definition: module.h:142
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
const std::vector< Module > & imports() const
Definition: module.h:222
virtual int GetPropertyMask() const
Returns bitmap of property. By default, none of the property is set. Derived class can override this ...
Definition: module.h:229
ModuleNode * operator->()
Definition: module.h:303
Module container of TVM.
Definition: module.h:79
constexpr const char * tvm_dev_mblob
Global variable to store device module blob.
Definition: module.h:280
static Module LoadFromFile(const std::string &file_name, const std::string &format="")
Load a module from file.
constexpr const char * tvm_param_prefix
Prefix for parameter symbols emitted into the main program.
Definition: module.h:292
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
constexpr const char * tvm_entrypoint_suffix
Model entrypoint generated as an interface to the AOT function outside of TIR.
Definition: module.h:296
Module()
Definition: module.h:81
Object * get_mutable() const
Definition: object.h:576
Module(ObjectPtr< Object > n)
Definition: module.h:83
Type-erased function used across TVM API.
constexpr const char * tvm_get_c_metadata
A PackedFunc that retrieves exported metadata.
Definition: module.h:276
constexpr const char * tvm_module_ctx
Global variable to store module context.
Definition: module.h:278