You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

global.cpp 9.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * \file src/global.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include <lite_build_config.h>
  12. #include "lite/global.h"
  13. #include "decryption/aes_decrypt.h"
  14. #include "decryption/decrypt_base.h"
  15. #include "decryption/rc4_cryption.h"
  16. #include "misc.h"
  17. #include "parse_info/parse_info_base.h"
  18. #include "parse_info/default_parse.h"
  19. #if LITE_BUILD_WITH_MGE
  20. #include "megbrain/common.h"
  21. #include "megbrain/comp_node.h"
  22. #include "megbrain/serialization/extern_c_opr.h"
  23. #include "megbrain/version.h"
  24. #include "megcore_opencl.h"
  25. #include "mge/algo_cache/file_cache.h"
  26. #include "mge/common.h"
  27. #if MGB_ENABLE_TENSOR_RT
  28. #include "megbrain/tensorrt/tensorrt_engine_cache.h"
  29. #endif
  30. #if LITE_WITH_CUDA
  31. #include "mge/algo_cache/redis_cache.h"
  32. #endif
  33. #endif
  34. #include <mutex>
  35. #include <unordered_map>
  36. using namespace lite;
  37. lite::DecryptionStaticData& lite::decryption_static_data() {
  38. static lite::DecryptionStaticData global_map;
  39. return global_map;
  40. }
  41. void lite::get_version(int& major, int& minor, int& patch) {
  42. #if LITE_BUILD_WITH_MGE
  43. auto version = mgb::get_version();
  44. major = version.major;
  45. minor = version.minor;
  46. patch = version.patch;
  47. #else
  48. //! without mge, the version set the max version
  49. major = 8;
  50. minor = 9999;
  51. patch = 0;
  52. #endif
  53. }
  54. size_t lite::get_device_count(LiteDeviceType device_type) {
  55. #if LITE_BUILD_WITH_MGE
  56. auto mgb_device_type = to_compnode_locator(device_type).type;
  57. return mgb::CompNode::get_device_count(mgb_device_type);
  58. #else
  59. LITE_MARK_USED_VAR(device_type);
  60. LITE_THROW("no lite backend avialible, please check build macro.");
  61. #endif
  62. }
  63. bool lite::register_decryption_and_key(std::string decrypt_name,
  64. const DecryptionFunc& func,
  65. const std::vector<uint8_t>& key) {
  66. LITE_LOCK_GUARD(decryption_static_data().map_mutex);
  67. auto& global_map = decryption_static_data().decryption_methods;
  68. if (global_map.find(decrypt_name) != global_map.end()) {
  69. LITE_THROW(ssprintf("The decryption method %s is already registered.",
  70. decrypt_name.c_str()));
  71. return false;
  72. } else {
  73. auto key_pointer = std::make_shared<std::vector<uint8_t>>(key);
  74. global_map[decrypt_name] = {func, key_pointer};
  75. LITE_LOG("Registered ecryption method %s.", decrypt_name.c_str());
  76. return true;
  77. }
  78. }
  79. bool lite::update_decryption_or_key(std::string decrypt_name,
  80. const DecryptionFunc& func,
  81. const std::vector<uint8_t>& key) {
  82. LITE_LOCK_GUARD(decryption_static_data().map_mutex);
  83. auto& global_map = decryption_static_data().decryption_methods;
  84. if (global_map.find(decrypt_name) != global_map.end()) {
  85. std::shared_ptr<std::vector<uint8_t>> key_pointer;
  86. DecryptionFunc new_func;
  87. if (func) {
  88. new_func = func;
  89. LITE_LOG("%s decryption function is updated.",
  90. decrypt_name.c_str());
  91. } else {
  92. new_func = global_map[decrypt_name].first;
  93. }
  94. if (key.size()) {
  95. key_pointer = std::make_shared<std::vector<uint8_t>>(key);
  96. LITE_LOG("%s decryption key is updated.", decrypt_name.c_str());
  97. } else {
  98. key_pointer = global_map[decrypt_name].second;
  99. }
  100. global_map[decrypt_name] = {new_func, key_pointer};
  101. return true;
  102. } else {
  103. LITE_THROW(ssprintf("The decryption method %s is not registered.",
  104. decrypt_name.c_str()));
  105. return false;
  106. }
  107. }
  108. lite::ParseInfoStaticData& lite::parse_info_static_data() {
  109. static lite::ParseInfoStaticData global_map;
  110. return global_map;
  111. }
  112. bool lite::register_parse_info_func(std::string info_type,
  113. const ParseInfoFunc& parse_func) {
  114. LITE_LOCK_GUARD(parse_info_static_data().map_mutex);
  115. auto& global_map = parse_info_static_data().parse_info_methods;
  116. if (global_map.find(info_type) != global_map.end()) {
  117. LITE_THROW(ssprintf("The parse info method %s is already registered.",
  118. info_type.c_str()));
  119. return false;
  120. } else {
  121. global_map[info_type] = parse_func;
  122. LITE_LOG("Registered infomation parser method %s.", info_type.c_str());
  123. return true;
  124. }
  125. }
  126. #if LITE_BUILD_WITH_MGE
  127. namespace {
  128. struct CacheControl {
  129. LITE_MUTEX cache_mutex;
  130. std::string cache_type = "file";
  131. std::atomic_size_t config_algo_times{0};
  132. std::atomic_size_t config_trt_times{0};
  133. };
  134. CacheControl cache_control;
  135. } // namespace
  136. void lite::try_coalesce_all_free_memory() {
  137. mgb::CompNode::try_coalesce_all_free_memory();
  138. }
  139. void lite::set_loader_lib_path(const std::string& loader_path) {
  140. const char* lib_path = loader_path.c_str();
  141. LITE_LOG("load a device loader of path %s.", lib_path);
  142. auto handle = dlopen(lib_path, RTLD_LAZY);
  143. LITE_ASSERT(handle, "failed to open c opr lib %s: %s", lib_path, dlerror());
  144. const char* entry = MGB_C_OPR_INIT_FUNC_STR;
  145. auto func = dlsym(handle, entry);
  146. LITE_ASSERT(func, "can not resolve %s: %s", entry, dlerror());
  147. typedef void (*entry_f_t)(void*);
  148. reinterpret_cast<entry_f_t>(func)(
  149. reinterpret_cast<void*>(&mgb_get_extern_c_opr_api_versioned));
  150. }
  151. void lite::set_persistent_cache(const std::string& cache_path,
  152. bool always_sync) {
  153. LITE_LOCK_GUARD(cache_control.cache_mutex);
  154. cache_control.cache_type = "file";
  155. if (cache_control.config_algo_times >= 1) {
  156. LITE_WARN(
  157. "The cache has been set,maybe some model is using now, change "
  158. "it now may cause unknow error!!");
  159. }
  160. cache_control.config_algo_times++;
  161. mgb::PersistentCache::set_impl(std::make_shared<InFilePersistentCache>(
  162. cache_path.c_str(), always_sync));
  163. }
  164. void lite::dump_persistent_cache(const std::string& cache_path) {
  165. LITE_LOCK_GUARD(cache_control.cache_mutex);
  166. LITE_ASSERT(cache_control.cache_type == "file",
  167. "now cache type is redis, it can't be dumped.");
  168. static_cast<InFilePersistentCache&>(mgb::PersistentCache::inst())
  169. .dump_cache(cache_path.c_str());
  170. }
  171. //! Set the TensorRT engine cache path for serialized prebuilt ICudaEngine
  172. void lite::set_tensor_rt_cache(std::string tensorrt_cache_path) {
  173. #if MGB_ENABLE_TENSOR_RT
  174. LITE_LOCK_GUARD(cache_control.cache_mutex);
  175. if (cache_control.config_trt_times >= 1) {
  176. LITE_WARN(
  177. "The trt cache has been set,maybe some model is using now, "
  178. "change it now may cause unknow error!!");
  179. }
  180. cache_control.config_trt_times++;
  181. mgb::TensorRTEngineCache::enable_engine_cache(true);
  182. mgb::TensorRTEngineCache::set_impl(
  183. std::make_shared<mgb::TensorRTEngineCacheIO>(tensorrt_cache_path));
  184. #else
  185. LITE_MARK_USED_VAR(tensorrt_cache_path);
  186. LITE_THROW("TensorRT is disable at compile time.");
  187. #endif
  188. }
  189. void lite::dump_tensor_rt_cache() {
  190. #if MGB_ENABLE_TENSOR_RT
  191. if (mgb::TensorRTEngineCache::enable_engine_cache()) {
  192. mgb::TensorRTEngineCache::inst().dump_cache();
  193. }
  194. #else
  195. LITE_THROW("TensorRT is disable at compile time.");
  196. #endif
  197. }
  198. #else //LITE_BUILD_WITH_MGE
  199. void lite::try_coalesce_all_free_memory() {}
  200. void lite::set_loader_lib_path(const std::string& ) {
  201. LITE_THROW("mge is disbale at build time, please build with mge");
  202. }
  203. void lite::set_persistent_cache(const std::string&, bool) {
  204. LITE_THROW("mge is disbale at build time, please build with mge");
  205. }
  206. void lite::dump_persistent_cache(const std::string& ) {
  207. LITE_THROW("mge is disbale at build time, please build with mge");
  208. }
  209. //! Set the TensorRT engine cache path for serialized prebuilt ICudaEngine
  210. void lite::set_tensor_rt_cache(std::string ) {
  211. LITE_THROW("mge is disbale at build time, please build with mge");
  212. }
  213. void lite::dump_tensor_rt_cache() {
  214. LITE_THROW("mge is disbale at build time, please build with mge");
  215. }
  216. #endif
  217. namespace lite {
  218. REGIST_DECRYPTION_METHOD("AES_default", lite::AESDcryption::decrypt_model,
  219. lite::AESDcryption::get_decrypt_key());
  220. REGIST_DECRYPTION_METHOD("RC4_default", lite::RC4::decrypt_model,
  221. lite::RC4::get_decrypt_key());
  222. REGIST_DECRYPTION_METHOD("SIMPLE_FAST_RC4_default",
  223. lite::SimpleFastRC4::decrypt_model,
  224. lite::SimpleFastRC4::get_decrypt_key());
  225. REGIST_PARSE_INFO_FUNCTION("LITE_default", lite::default_parse_info);
  226. } // namespace lite
  227. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台