tvm
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>
31 #include <tvm/runtime/memory.h>
32 #include <tvm/runtime/object.h>
33 
34 #include <memory>
35 #include <string>
36 #include <unordered_map>
37 #include <vector>
38 
39 namespace tvm {
40 namespace runtime {
41 
42 class ModuleNode;
43 class PackedFunc;
44 
48 class Module : public ObjectRef {
49  public:
50  Module() {}
51  // constructor from container.
52  explicit Module(ObjectPtr<Object> n) : ObjectRef(n) {}
62  inline PackedFunc GetFunction(const std::string& name, bool query_imports = false);
63  // The following functions requires link with runtime.
71  inline void Import(Module other);
73  inline ModuleNode* operator->();
75  inline const ModuleNode* operator->() const;
83  TVM_DLL static Module LoadFromFile(const std::string& file_name, const std::string& format = "");
84  // refer to the corresponding container.
86  friend class ModuleNode;
87 };
88 
111 class TVM_DLL ModuleNode : public Object {
112  public:
114  virtual ~ModuleNode() {}
119  virtual const char* type_key() const = 0;
137  virtual PackedFunc GetFunction(const std::string& name,
138  const ObjectPtr<Object>& sptr_to_self) = 0;
144  virtual void SaveToFile(const std::string& file_name, const std::string& format);
152  virtual void SaveToBinary(dmlc::Stream* stream);
158  virtual std::string GetSource(const std::string& format = "");
168  PackedFunc GetFunction(const std::string& name, bool query_imports = false);
176  void Import(Module other);
184  const PackedFunc* GetFuncFromEnv(const std::string& name);
186  const std::vector<Module>& imports() const { return imports_; }
187 
188  // integration with the existing components.
189  static constexpr const uint32_t _type_index = TypeIndex::kRuntimeModule;
190  static constexpr const char* _type_key = "runtime.Module";
191  // NOTE: ModuleNode can still be sub-classed
192  //
194 
195  protected:
196  friend class Module;
197  friend class ModuleInternal;
199  std::vector<Module> imports_;
200 
201  private:
203  std::unordered_map<std::string, std::shared_ptr<PackedFunc> > import_cache_;
204 };
205 
211 TVM_DLL bool RuntimeEnabled(const std::string& target);
212 
214 namespace symbol {
216 constexpr const char* tvm_module_ctx = "__tvm_module_ctx";
218 constexpr const char* tvm_dev_mblob = "__tvm_dev_mblob";
220 constexpr const char* tvm_dev_mblob_nbytes = "__tvm_dev_mblob_nbytes";
222 constexpr const char* tvm_set_device = "__tvm_set_device";
224 constexpr const char* tvm_global_barrier_state = "__tvm_global_barrier_state";
226 constexpr const char* tvm_prepare_global_barrier = "__tvm_prepare_global_barrier";
228 constexpr const char* tvm_module_main = "__tvm_main__";
230 constexpr const char* tvm_param_prefix = "__tvm_param__";
232 constexpr const char* tvm_lookup_linked_param = "_lookup_linked_param";
234 constexpr const char* tvm_run_func_suffix = "run_model";
236 constexpr const char* tvm_entrypoint_suffix = "run";
237 } // namespace symbol
238 
239 // implementations of inline functions.
240 
241 inline void Module::Import(Module other) { return (*this)->Import(other); }
242 
243 inline ModuleNode* Module::operator->() { return static_cast<ModuleNode*>(get_mutable()); }
244 
245 inline const ModuleNode* Module::operator->() const {
246  return static_cast<const ModuleNode*>(get());
247 }
248 
249 } // namespace runtime
250 } // namespace tvm
251 
252 #include <tvm/runtime/packed_func.h> // NOLINT(*)
253 #endif // TVM_RUNTIME_MODULE_H_
constexpr const char * tvm_set_device
global function to set device
Definition: module.h:222
PackedFunc GetFunction(const std::string &name, bool query_imports=false)
Get packed function from current module by name.
Definition: packed_func.h:1660
void Import(Module other)
Import another module into this module.
Definition: module.h:241
A custom smart pointer for Object.
Definition: object.h:356
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:232
bool RuntimeEnabled(const std::string &target)
Check if runtime module is enabled for target.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Runtime memory management.
base class of all object containers.
Definition: object.h:165
constexpr const char * tvm_dev_mblob_nbytes
Number of bytes of device module blob.
Definition: module.h:220
friend class ModuleNode
Definition: module.h:86
constexpr const char * tvm_global_barrier_state
Auxiliary counter to global barrier.
Definition: module.h:224
constexpr const char * tvm_module_main
Placeholder for the module&#39;s entry function.
Definition: module.h:228
constexpr const char * tvm_prepare_global_barrier
Prepare the global barrier before kernels that uses global barrier.
Definition: module.h:226
std::vector< Module > imports_
The modules this module depend on.
Definition: module.h:199
constexpr const char * tvm_run_func_suffix
The main AOT executor function generated from TIR.
Definition: module.h:234
virtual ~ModuleNode()
virtual destructor
Definition: module.h:114
Base class of all object reference.
Definition: object.h:504
Base container of module.
Definition: module.h:111
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:664
const std::vector< Module > & imports() const
Definition: module.h:186
ModuleNode * operator->()
Definition: module.h:243
Module container of TVM.
Definition: module.h:48
constexpr const char * tvm_dev_mblob
Global variable to store device module blob.
Definition: module.h:218
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:230
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:68
constexpr const char * tvm_entrypoint_suffix
Model entrypoint generated as an interface to the AOT function outside of TIR.
Definition: module.h:236
Module()
Definition: module.h:50
Object * get_mutable() const
Definition: object.h:569
Module(ObjectPtr< Object > n)
Definition: module.h:52
Type-erased function used across TVM API.
constexpr const char * tvm_module_ctx
Global variable to store module context.
Definition: module.h:216