module.h 5.77 KB
Newer Older
1 2
/*!
 *  Copyright (c) 2017 by Contributors
tqchen committed
3
 * \file tvm/runtime/module.h
4 5 6 7 8 9 10
 * \brief Runtime container of the functions generated by TVM,
 *  This is used to support dynamically link, load and save
 *  functions from different convention under unified API.
 */
#ifndef TVM_RUNTIME_MODULE_H_
#define TVM_RUNTIME_MODULE_H_

11
#include <dmlc/io.h>
12 13 14 15
#include <memory>
#include <vector>
#include <string>
#include <unordered_map>
16
#include "c_runtime_api.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

namespace tvm {
namespace runtime {

// The internal container of module.
class ModuleNode;
class PackedFunc;

/*!
 * \brief Module container of TVM.
 */
class Module {
 public:
  Module() {}
  // constructor from container.
  explicit Module(std::shared_ptr<ModuleNode> n)
      : node_(n) {}
  /*!
   * \brief Get packed function from current module by name.
   *
   * \param name The name of the function.
   * \param query_imports Whether also query dependency modules.
   * \return The result function.
   *  This function will return PackedFunc(nullptr) if function do not exist.
41
   * \note Implemented in packed_func.cc
42
   */
43 44 45 46 47 48
  inline PackedFunc GetFunction(const std::string& name, bool query_imports = false);
  /*! \return internal container */
  inline ModuleNode* operator->();
  /*! \return internal container */
  inline const ModuleNode* operator->() const;
  // The following functions requires link with runtime.
49 50 51 52 53 54 55
  /*!
   * \brief Import another module into this module.
   * \param other The module to be imported.
   *
   * \note Cyclic dependency is not allowed among modules,
   *  An error will be thrown when cyclic dependency is detected.
   */
56
  TVM_DLL void Import(Module other);
57 58 59 60 61 62 63
  /*!
   * \brief Load a module from file.
   * \param file_name The name of the host function module.
   * \param format The format of the file.
   * \note This function won't load the import relationship.
   *  Re-create import relationship by calling Import.
   */
64 65
  TVM_DLL static Module LoadFromFile(const std::string& file_name,
                                     const std::string& format = "");
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

 private:
  std::shared_ptr<ModuleNode> node_;
};

/*!
 * \brief Base node container of module.
 *  Do not create this directly, instead use Module.
 */
class ModuleNode {
 public:
  /*! \brief virtual destructor */
  virtual ~ModuleNode() {}
  /*! \return The module type key */
  virtual const char* type_key() const = 0;
  /*!
   * \brief Get a PackedFunc from module.
   *
   *  The PackedFunc may not be fully initialized,
   *  there might still be first time running overhead when
   *  executing the function on certain devices.
   *  For benchmarking, use prepare to eliminate
   *
   * \param name the name of the function.
   * \param sptr_to_self The shared_ptr that points to this module node.
   *
   * \return PackedFunc(nullptr) when it is not available.
   *
   * \note The function will always remain valid.
   *   If the function need resource from the module(e.g. late linking),
   *   it should capture sptr_to_self.
   */
  virtual PackedFunc GetFunction(
      const std::string& name,
      const std::shared_ptr<ModuleNode>& sptr_to_self) = 0;
  /*!
   * \brief Save the module to file.
   * \param file_name The file to be saved to.
   * \param format The format of the file.
   */
  virtual void SaveToFile(const std::string& file_name,
107
                          const std::string& format);
108
  /*!
109 110 111 112 113 114
   * \brief Save the module to binary stream.
   * \param stream The binary stream to save to.
   * \note It is recommended to implement this for device modules,
   *   but not necessarily host modules.
   *   We can use this to do AOT loading of bundled device functions.
   */
115
  TVM_DLL virtual void SaveToBinary(dmlc::Stream* stream);
116
  /*!
117 118 119 120
   * \brief Get the source code of module, when available.
   * \param format Format of the source code, can be empty by default.
   * \return Possible source code when available.
   */
121
  TVM_DLL virtual std::string GetSource(const std::string& format = "");
122 123 124 125 126 127 128
  /*!
   * \brief Get a function from current environment
   *  The environment includes all the imports as well as Global functions.
   *
   * \param name name of the function.
   * \return The corresponding function.
   */
129
  TVM_DLL const PackedFunc* GetFuncFromEnv(const std::string& name);
130 131 132 133 134
  /*! \return The module it imports from */
  const std::vector<Module>& imports() const {
    return imports_;
  }

135
 protected:
136 137 138
  friend class Module;
  /*! \brief The modules this module depend on */
  std::vector<Module> imports_;
139 140

 private:
141 142 143 144 145 146 147 148 149
  /*! \brief Cache used by GetImport */
  std::unordered_map<std::string,
                     std::unique_ptr<PackedFunc> > import_cache_;
};

/*! \brief namespace for constant symbols */
namespace symbol {
/*! \brief Global variable to store module context. */
constexpr const char* tvm_module_ctx = "__tvm_module_ctx";
150 151 152 153
/*! \brief Global variable to store device module blob */
constexpr const char* tvm_dev_mblob = "__tvm_dev_mblob";
/*! \brief Number of bytes of device module blob. */
constexpr const char* tvm_dev_mblob_nbytes = "__tvm_dev_mblob_nbytes";
154 155
/*! \brief global function to set device */
constexpr const char* tvm_set_device = "__tvm_set_device";
156 157 158 159
/*! \brief Auxiliary counter to global barrier. */
constexpr const char* tvm_global_barrier_state = "__tvm_global_barrier_state";
/*! \brief Prepare the global barrier before kernels that uses global barrier. */
constexpr const char* tvm_prepare_global_barrier = "__tvm_prepare_global_barrier";
160 161
/*! \brief Placeholder for the module's entry function. */
constexpr const char* tvm_module_main = "__tvm_main__";
162
}  // namespace symbol
163 164 165 166 167 168

// implementations of inline functions.
inline ModuleNode* Module::operator->() {
  return node_.get();
}

169 170 171 172
inline const ModuleNode* Module::operator->() const {
  return node_.get();
}

173 174 175
}  // namespace runtime
}  // namespace tvm

176
#include "packed_func.h"
177
#endif  // TVM_RUNTIME_MODULE_H_