module.h 6.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20
/*!
tqchen committed
21
 * \file tvm/runtime/module.h
22 23 24 25 26 27 28
 * \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_

29
#include <dmlc/io.h>
30 31 32 33
#include <memory>
#include <vector>
#include <string>
#include <unordered_map>
34
#include "c_runtime_api.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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.
59
   * \note Implemented in packed_func.cc
60
   */
61 62 63 64 65 66
  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.
67 68 69 70 71 72 73
  /*!
   * \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.
   */
74
  TVM_DLL void Import(Module other);
75 76 77 78 79 80 81
  /*!
   * \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.
   */
82 83
  TVM_DLL static Module LoadFromFile(const std::string& file_name,
                                     const std::string& format = "");
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

 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.
   */
124 125
  TVM_DLL virtual void SaveToFile(const std::string& file_name,
                                  const std::string& format);
126
  /*!
127 128 129 130 131 132
   * \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.
   */
133
  TVM_DLL virtual void SaveToBinary(dmlc::Stream* stream);
134
  /*!
135 136 137 138
   * \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.
   */
139
  TVM_DLL virtual std::string GetSource(const std::string& format = "");
140 141 142 143 144 145 146
  /*!
   * \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.
   */
147
  TVM_DLL const PackedFunc* GetFuncFromEnv(const std::string& name);
148 149 150 151 152
  /*! \return The module it imports from */
  const std::vector<Module>& imports() const {
    return imports_;
  }

153
 protected:
154 155 156
  friend class Module;
  /*! \brief The modules this module depend on */
  std::vector<Module> imports_;
157 158

 private:
159 160 161 162 163 164 165 166 167
  /*! \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";
168 169 170 171
/*! \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";
172 173
/*! \brief global function to set device */
constexpr const char* tvm_set_device = "__tvm_set_device";
174 175 176 177
/*! \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";
178 179
/*! \brief Placeholder for the module's entry function. */
constexpr const char* tvm_module_main = "__tvm_main__";
180
}  // namespace symbol
181 182 183 184 185 186

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

187 188 189 190
inline const ModuleNode* Module::operator->() const {
  return node_.get();
}

191 192 193
}  // namespace runtime
}  // namespace tvm

194
#include "packed_func.h"
195
#endif  // TVM_RUNTIME_MODULE_H_