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.8 kB

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