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 8.7 kB

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