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

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