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.

utils.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * \file imperative/python/src/utils.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 "utils.h"
  12. #ifdef WIN32
  13. #include <stdio.h>
  14. #include <windows.h>
  15. #endif
  16. #include <pybind11/operators.h>
  17. #include <atomic>
  18. #include <cstdint>
  19. #include <shared_mutex>
  20. #include "./imperative_rt.h"
  21. #include "megbrain/common.h"
  22. #include "megbrain/comp_node.h"
  23. #include "megbrain/imperative/blob_manager.h"
  24. #include "megbrain/imperative/profiler.h"
  25. #include "megbrain/imperative/tensor_sanity_check.h"
  26. #include "megbrain/serialization/helper.h"
  27. #include "megbrain/utils/persistent_cache.h"
  28. #if MGB_ENABLE_OPR_MM
  29. #include "megbrain/opr/mm_handler.h"
  30. #endif
  31. namespace py = pybind11;
  32. namespace {
  33. bool g_global_finalized = false;
  34. class LoggerWrapper {
  35. public:
  36. using LogLevel = mgb::LogLevel;
  37. using LogHandler = mgb::LogHandler;
  38. static void set_log_handler(py::object logger_p) {
  39. logger = logger_p;
  40. mgb::set_log_handler(py_log_handler);
  41. }
  42. static LogLevel set_log_level(LogLevel log_level) {
  43. return mgb::set_log_level(log_level);
  44. }
  45. private:
  46. static py::object logger;
  47. static void py_log_handler(
  48. mgb::LogLevel level, const char* file, const char* func, int line,
  49. const char* fmt, va_list ap) {
  50. using mgb::LogLevel;
  51. MGB_MARK_USED_VAR(file);
  52. MGB_MARK_USED_VAR(func);
  53. MGB_MARK_USED_VAR(line);
  54. if (g_global_finalized)
  55. return;
  56. const char* py_type;
  57. switch (level) {
  58. case LogLevel::DEBUG:
  59. py_type = "debug";
  60. break;
  61. case LogLevel::INFO:
  62. py_type = "info";
  63. break;
  64. case LogLevel::WARN:
  65. py_type = "warning";
  66. break;
  67. case LogLevel::ERROR:
  68. py_type = "error";
  69. break;
  70. default:
  71. throw std::runtime_error("bad log level");
  72. }
  73. std::string msg = mgb::svsprintf(fmt, ap);
  74. auto do_log = [msg = msg, py_type]() {
  75. if (logger.is_none())
  76. return;
  77. py::object _call = logger.attr(py_type);
  78. _call(py::str(msg));
  79. };
  80. if (PyGILState_Check()) {
  81. do_log();
  82. } else {
  83. py_task_q.add_task(do_log);
  84. }
  85. }
  86. };
  87. py::object LoggerWrapper::logger = py::none{};
  88. uint32_t _get_dtype_num(py::object dtype) {
  89. return static_cast<uint32_t>(npy::dtype_np2mgb(dtype.ptr()).enumv());
  90. }
  91. py::bytes _get_serialized_dtype(py::object dtype) {
  92. std::string sdtype;
  93. auto write = [&sdtype](const void* data, size_t size) {
  94. auto pos = sdtype.size();
  95. sdtype.resize(pos + size);
  96. memcpy(&sdtype[pos], data, size);
  97. };
  98. mgb::serialization::serialize_dtype(npy::dtype_np2mgb(dtype.ptr()), write);
  99. return py::bytes(sdtype.data(), sdtype.size());
  100. }
  101. int fork_exec_impl(
  102. const std::string& arg0, const std::string& arg1, const std::string& arg2) {
  103. #ifdef WIN32
  104. STARTUPINFO si;
  105. PROCESS_INFORMATION pi;
  106. ZeroMemory(&si, sizeof(si));
  107. si.cb = sizeof(si);
  108. ZeroMemory(&pi, sizeof(pi));
  109. auto args_str = " " + arg1 + " " + arg2;
  110. // Start the child process.
  111. if (!CreateProcess(
  112. arg0.c_str(), // exe name
  113. const_cast<char*>(args_str.c_str()), // Command line
  114. NULL, // Process handle not inheritable
  115. NULL, // Thread handle not inheritable
  116. FALSE, // Set handle inheritance to FALSE
  117. 0, // No creation flags
  118. NULL, // Use parent's environment block
  119. NULL, // Use parent's starting directory
  120. &si, // Pointer to STARTUPINFO structure
  121. &pi) // Pointer to PROCESS_INFORMATION structure
  122. ) {
  123. mgb_log_warn("CreateProcess failed (%lu).\n", GetLastError());
  124. fprintf(stderr, "[megbrain] failed to execl %s [%s, %s]\n", arg0.c_str(),
  125. arg1.c_str(), arg2.c_str());
  126. __builtin_trap();
  127. }
  128. return pi.dwProcessId;
  129. #else
  130. auto pid = fork();
  131. if (!pid) {
  132. execl(arg0.c_str(), arg0.c_str(), arg1.c_str(), arg2.c_str(), nullptr);
  133. fprintf(stderr, "[megbrain] failed to execl %s [%s, %s]: %s\n", arg0.c_str(),
  134. arg1.c_str(), arg2.c_str(), std::strerror(errno));
  135. std::terminate();
  136. }
  137. mgb_assert(pid > 0, "failed to fork: %s", std::strerror(errno));
  138. return pid;
  139. #endif
  140. }
  141. } // namespace
  142. void init_utils(py::module m) {
  143. auto atexit = py::module::import("atexit");
  144. atexit.attr("register")(py::cpp_function([]() { g_global_finalized = true; }));
  145. py::class_<std::atomic<uint64_t>>(m, "AtomicUint64")
  146. .def(py::init<>())
  147. .def(py::init<uint64_t>())
  148. .def("load", [](const std::atomic<uint64_t>& self) { return self.load(); })
  149. .def("store", [](std::atomic<uint64_t>& self,
  150. uint64_t value) { return self.store(value); })
  151. .def("fetch_add", [](std::atomic<uint64_t>& self,
  152. uint64_t value) { return self.fetch_add(value); })
  153. .def("fetch_sub", [](std::atomic<uint64_t>& self,
  154. uint64_t value) { return self.fetch_sub(value); })
  155. .def(py::self += uint64_t())
  156. .def(py::self -= uint64_t());
  157. // FIXME!!! Should add a submodule instead of using a class for logger
  158. py::class_<LoggerWrapper> logger(m, "Logger");
  159. logger.def(py::init<>())
  160. .def_static("set_log_level", &LoggerWrapper::set_log_level)
  161. .def_static("set_log_handler", &LoggerWrapper::set_log_handler);
  162. py::enum_<LoggerWrapper::LogLevel>(logger, "LogLevel")
  163. .value("Debug", LoggerWrapper::LogLevel::DEBUG)
  164. .value("Info", LoggerWrapper::LogLevel::INFO)
  165. .value("Warn", LoggerWrapper::LogLevel::WARN)
  166. .value("Error", LoggerWrapper::LogLevel::ERROR);
  167. m.def("_get_dtype_num", &_get_dtype_num, "Convert numpy dtype to internal dtype");
  168. m.def("_get_serialized_dtype", &_get_serialized_dtype,
  169. "Convert numpy dtype to internal dtype for serialization");
  170. m.def("_get_device_count", &mgb::CompNode::get_device_count,
  171. "Get total number of specific devices on this system");
  172. m.def("_try_coalesce_all_free_memory", &mgb::CompNode::try_coalesce_all_free_memory,
  173. "This function will try it best to free all consecutive free chunks back to "
  174. "operating system");
  175. using mgb::imperative::TensorSanityCheck;
  176. py::class_<TensorSanityCheck>(m, "TensorSanityCheckImpl")
  177. .def(py::init<>())
  178. .def("enable",
  179. [](TensorSanityCheck& checker) -> TensorSanityCheck& {
  180. checker.enable();
  181. return checker;
  182. })
  183. .def("disable", [](TensorSanityCheck& checker) { checker.disable(); });
  184. #if MGB_ENABLE_OPR_MM
  185. m.def("create_mm_server", &create_zmqrpc_server, py::arg("addr"),
  186. py::arg("port") = 0);
  187. #else
  188. m.def("create_mm_server", []() {});
  189. #endif
  190. // Debug code, internal only
  191. m.def("_defrag", [](const mgb::CompNode& cn) {
  192. mgb::imperative::BlobManager::inst()->defrag(cn);
  193. });
  194. m.def("_set_fork_exec_path_for_timed_func",
  195. [](const std::string& arg0, const ::std::string arg1) {
  196. using namespace std::placeholders;
  197. mgb::sys::TimedFuncInvoker::ins().set_fork_exec_impl(std::bind(
  198. fork_exec_impl, std::string{arg0}, std::string{arg1}, _1));
  199. });
  200. m.def("_timed_func_exec_cb", [](const std::string& user_data) {
  201. mgb::sys::TimedFuncInvoker::ins().fork_exec_impl_mainloop(user_data.c_str());
  202. });
  203. using mgb::PersistentCache;
  204. class PyPersistentCache : public mgb::PersistentCache {
  205. private:
  206. using KeyPair = std::pair<std::string, std::string>;
  207. using BlobPtr = std::unique_ptr<Blob, void (*)(Blob*)>;
  208. std::shared_mutex m_mutex;
  209. std::unordered_map<KeyPair, BlobPtr, mgb::pairhash> m_local_cache;
  210. static size_t hash_key_pair(const KeyPair& kp) {
  211. std::hash<std::string> hasher;
  212. return hasher(kp.first) ^ hasher(kp.second);
  213. }
  214. std::string blob_to_str(const Blob& key) {
  215. return std::string(reinterpret_cast<const char*>(key.ptr), key.size);
  216. }
  217. BlobPtr copy_blob(const Blob& blob) {
  218. auto blob_deleter = [](Blob* blob) {
  219. if (blob) {
  220. std::free(const_cast<void*>(blob->ptr));
  221. delete blob;
  222. }
  223. };
  224. auto blob_ptr = BlobPtr{new Blob(), blob_deleter};
  225. blob_ptr->ptr = std::malloc(blob.size);
  226. std::memcpy(const_cast<void*>(blob_ptr->ptr), blob.ptr, blob.size);
  227. blob_ptr->size = blob.size;
  228. return blob_ptr;
  229. }
  230. BlobPtr str_to_blob(const std::string& str) {
  231. auto blob = Blob{str.data(), str.size()};
  232. return copy_blob(blob);
  233. }
  234. std::unique_ptr<Blob, void (*)(Blob*)> empty_blob() {
  235. return BlobPtr{nullptr, [](Blob* blob) {}};
  236. }
  237. public:
  238. mgb::Maybe<Blob> get(const std::string& category, const Blob& key) override {
  239. auto py_get = [this](const std::string& category,
  240. const Blob& key) -> mgb::Maybe<Blob> {
  241. PYBIND11_OVERLOAD_PURE(
  242. mgb::Maybe<Blob>, PersistentCache, get, category, key);
  243. };
  244. KeyPair kp = {category, blob_to_str(key)};
  245. std::shared_lock<decltype(m_mutex)> rlock;
  246. auto iter = m_local_cache.find(kp);
  247. if (iter == m_local_cache.end()) {
  248. auto py_ret = py_get(category, key);
  249. if (!py_ret.valid()) {
  250. iter = m_local_cache.insert({kp, empty_blob()}).first;
  251. } else {
  252. iter = m_local_cache.insert({kp, copy_blob(py_ret.val())}).first;
  253. }
  254. }
  255. if (iter->second) {
  256. return *iter->second;
  257. } else {
  258. return {};
  259. }
  260. }
  261. void put(const std::string& category, const Blob& key, const Blob& value)
  262. override {
  263. KeyPair kp = {category, blob_to_str(key)};
  264. std::unique_lock<decltype(m_mutex)> wlock;
  265. m_local_cache.insert_or_assign(kp, copy_blob(value));
  266. PYBIND11_OVERLOAD_PURE(void, PersistentCache, put, category, key, value);
  267. }
  268. };
  269. py::class_<PersistentCache, PyPersistentCache, std::shared_ptr<PersistentCache>>(
  270. m, "PersistentCache")
  271. .def(py::init<>())
  272. .def("get", &PersistentCache::get)
  273. .def("put", &PersistentCache::put)
  274. .def("reg", &PersistentCache::set_impl);
  275. }

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