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.

helper.h 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * \file imperative/python/src/helper.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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. #pragma once
  12. #include "megbrain/graph.h"
  13. #include <Python.h>
  14. #include <string>
  15. #include <iterator>
  16. #if __cplusplus > 201703L
  17. #include <ranges>
  18. #endif
  19. #include <pybind11/pybind11.h>
  20. #include <pybind11/stl.h>
  21. #include <pybind11/numpy.h>
  22. #include <pybind11/functional.h>
  23. pybind11::module submodule(pybind11::module parent, const char* name, const char* doc = nullptr);
  24. pybind11::module rel_import(pybind11::str name, pybind11::module m, int level);
  25. #if __cplusplus > 201703L
  26. using std::ranges::range_value_t;
  27. #else
  28. template<typename T>
  29. using range_value_t = std::remove_cv_t<std::remove_reference_t<decltype(*std::declval<T>().begin())>>;
  30. #endif
  31. template<typename T>
  32. auto to_list(const T& x) {
  33. using elem_t = range_value_t<T>;
  34. std::vector<elem_t> ret(x.begin(), x.end());
  35. return pybind11::cast(ret);
  36. }
  37. template<typename T>
  38. auto to_tuple(const T& x, pybind11::return_value_policy policy = pybind11::return_value_policy::automatic) {
  39. auto ret = pybind11::tuple(x.size());
  40. for (size_t i = 0; i < x.size(); ++i) {
  41. ret[i] = pybind11::cast(x[i], policy);
  42. }
  43. return ret;
  44. }
  45. template<typename T>
  46. auto to_tuple(T begin, T end, pybind11::return_value_policy policy = pybind11::return_value_policy::automatic) {
  47. auto ret = pybind11::tuple(end - begin);
  48. for (size_t i = 0; begin < end; ++begin, ++i) {
  49. ret[i] = pybind11::cast(*begin, policy);
  50. }
  51. return ret;
  52. }
  53. class PyTaskDipatcher {
  54. struct Queue : mgb::AsyncQueueSC<std::function<void(void)>, Queue> {
  55. using Task = std::function<void(void)>;
  56. void process_one_task(Task& f) {
  57. if (!Py_IsInitialized()) return;
  58. pybind11::gil_scoped_acquire _;
  59. f();
  60. }
  61. };
  62. Queue queue;
  63. bool finalized = false;
  64. public:
  65. template<typename T>
  66. void add_task(T&& task) {
  67. // CPython never dlclose an extension so
  68. // finalized means the interpreter has been shutdown
  69. if (!finalized) {
  70. queue.add_task(std::forward<T>(task));
  71. }
  72. }
  73. void wait_all_task_finish() {
  74. queue.wait_all_task_finish();
  75. }
  76. ~PyTaskDipatcher() {
  77. finalized = true;
  78. queue.wait_all_task_finish();
  79. }
  80. };
  81. extern PyTaskDipatcher py_task_q;
  82. class GILManager {
  83. PyGILState_STATE gstate;
  84. public:
  85. GILManager():
  86. gstate(PyGILState_Ensure())
  87. {
  88. }
  89. ~GILManager() {
  90. PyGILState_Release(gstate);
  91. }
  92. };
  93. #define PYTHON_GIL GILManager __gil_manager
  94. //! wraps a shared_ptr and decr PyObject ref when destructed
  95. class PyObjRefKeeper {
  96. std::shared_ptr<PyObject> m_ptr;
  97. public:
  98. static void deleter(PyObject* p) {
  99. if (p) {
  100. py_task_q.add_task([p](){Py_DECREF(p);});
  101. }
  102. }
  103. PyObjRefKeeper() = default;
  104. PyObjRefKeeper(PyObject* p) : m_ptr{p, deleter} {}
  105. PyObject* get() const { return m_ptr.get(); }
  106. //! create a shared_ptr as an alias of the underlying ptr
  107. template <typename T>
  108. std::shared_ptr<T> make_shared(T* ptr) const {
  109. return {m_ptr, ptr};
  110. }
  111. };
  112. //! exception to be thrown when python callback fails
  113. class PyExceptionForward : public std::exception {
  114. PyObject *m_type, *m_value, *m_traceback;
  115. std::string m_msg;
  116. PyExceptionForward(PyObject* type, PyObject* value, PyObject* traceback,
  117. const std::string& msg)
  118. : m_type{type},
  119. m_value{value},
  120. m_traceback{traceback},
  121. m_msg{msg} {}
  122. public:
  123. PyExceptionForward(const PyExceptionForward&) = delete;
  124. PyExceptionForward& operator=(const PyExceptionForward&) = delete;
  125. ~PyExceptionForward();
  126. PyExceptionForward(PyExceptionForward&& rhs)
  127. : m_type{rhs.m_type},
  128. m_value{rhs.m_value},
  129. m_traceback{rhs.m_traceback},
  130. m_msg{std::move(rhs.m_msg)} {
  131. rhs.m_type = rhs.m_value = rhs.m_traceback = nullptr;
  132. }
  133. //! throw PyExceptionForward from current python error state
  134. static void throw_() __attribute__((noreturn));
  135. //! restore python error
  136. void restore();
  137. const char* what() const noexcept override { return m_msg.c_str(); }
  138. };
  139. //! numpy utils
  140. namespace npy {
  141. //! convert tensor shape to raw vector
  142. static inline std::vector<size_t> shape2vec(const mgb::TensorShape &shape) {
  143. return {shape.shape, shape.shape + shape.ndim};
  144. }
  145. //! change numpy dtype to megbrain supported dtype
  146. PyObject* to_mgb_supported_dtype(PyObject *dtype);
  147. //! convert raw vector to tensor shape
  148. mgb::TensorShape vec2shape(const std::vector<size_t> &vec);
  149. //! convert megbrain dtype to numpy dtype object; return new reference
  150. PyObject* dtype_mgb2np(mgb::DType dtype);
  151. //! convert numpy dtype object or string to megbrain dtype
  152. mgb::DType dtype_np2mgb(PyObject *obj);
  153. //! buffer sharing type
  154. enum class ShareType {
  155. MUST_SHARE, //!< must be shared
  156. MUST_UNSHARE, //!< must not be shared
  157. TRY_SHARE //!< share if possible
  158. };
  159. //! get ndarray from HostTensorND
  160. PyObject* ndarray_from_tensor(const mgb::HostTensorND &val,
  161. ShareType share_type);
  162. //! specify how to convert numpy array to tensor
  163. struct Meth {
  164. bool must_borrow_ = false;
  165. mgb::HostTensorND *dest_tensor_ = nullptr;
  166. mgb::CompNode dest_cn_;
  167. //! make a Meth that allows borrowing numpy array memory
  168. static Meth borrow(
  169. mgb::CompNode dest_cn = mgb::CompNode::default_cpu()) {
  170. return {false, nullptr, dest_cn};
  171. }
  172. //! make a Meth that requires the numpy array to be borrowed
  173. static Meth must_borrow(
  174. mgb::CompNode dest_cn = mgb::CompNode::default_cpu()) {
  175. return {true, nullptr, dest_cn};
  176. }
  177. //! make a Meth that requires copying the value into another
  178. //! tensor
  179. static Meth copy_into(mgb::HostTensorND *tensor) {
  180. return {false, tensor, tensor->comp_node()};
  181. }
  182. };
  183. /*!
  184. * \brief convert an object to megbrain tensor
  185. * \param meth specifies how the conversion should take place
  186. * \param dtype desired dtype; it can be set as invalid to allow arbitrary
  187. * dtype
  188. */
  189. mgb::HostTensorND np2tensor(PyObject *obj, const Meth &meth,
  190. mgb::DType dtype);
  191. }
  192. // Note: following macro was copied from pybind11/detail/common.h
  193. // Robust support for some features and loading modules compiled against different pybind versions
  194. // requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute on
  195. // the main `pybind11` namespace.
  196. #if !defined(PYBIND11_NAMESPACE)
  197. # ifdef __GNUG__
  198. # define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
  199. # else
  200. # define PYBIND11_NAMESPACE pybind11
  201. # endif
  202. #endif
  203. namespace PYBIND11_NAMESPACE {
  204. namespace detail {
  205. template<typename T, unsigned N> struct type_caster<megdnn::SmallVector<T, N>>
  206. : list_caster<megdnn::SmallVector<T, N>, T> {};
  207. template <> struct type_caster<mgb::DType> {
  208. PYBIND11_TYPE_CASTER(mgb::DType, _("DType"));
  209. public:
  210. bool load(handle src, bool convert) {
  211. auto obj = reinterpret_borrow<object>(src);
  212. if (!convert && !isinstance<dtype>(obj)) {
  213. return false;
  214. }
  215. if (obj.is_none()) {
  216. return true;
  217. }
  218. try {
  219. obj = pybind11::dtype::from_args(obj);
  220. } catch (pybind11::error_already_set&) {
  221. return false;
  222. }
  223. try {
  224. value = npy::dtype_np2mgb(obj.ptr());
  225. } catch (...) {
  226. return false;
  227. }
  228. return true;
  229. }
  230. static handle cast(mgb::DType dt, return_value_policy /* policy */, handle /* parent */) {
  231. // ignore policy and parent because we always return a pure python object
  232. return npy::dtype_mgb2np(std::move(dt));
  233. }
  234. };
  235. template <> struct type_caster<mgb::TensorShape> {
  236. PYBIND11_TYPE_CASTER(mgb::TensorShape, _("TensorShape"));
  237. public:
  238. bool load(handle src, bool convert) {
  239. auto obj = reinterpret_steal<object>(src);
  240. if (!isinstance<tuple>(obj)) {
  241. return false;
  242. }
  243. value.ndim = len(obj);
  244. mgb_assert(value.ndim <= mgb::TensorShape::MAX_NDIM);
  245. size_t i = 0;
  246. for (auto v : obj) {
  247. mgb_assert(i < value.ndim);
  248. value.shape[i] = reinterpret_borrow<object>(v).cast<size_t>();
  249. ++i;
  250. }
  251. return true;
  252. }
  253. static handle cast(mgb::TensorShape shape, return_value_policy /* policy */, handle /* parent */) {
  254. // ignore policy and parent because we always return a pure python object
  255. return to_tuple(shape.shape, shape.shape + shape.ndim).release();
  256. }
  257. };
  258. // hack to make custom object implicitly convertible from None
  259. template <typename T> struct from_none_caster : public type_caster_base<T> {
  260. using base = type_caster_base<T>;
  261. bool load(handle src, bool convert) {
  262. if (!convert || !src.is_none()) {
  263. return base::load(src, convert);
  264. }
  265. // adapted from pybind11::implicitly_convertible
  266. auto temp = reinterpret_steal<object>(PyObject_Call(
  267. (PyObject*) this->typeinfo->type, tuple().ptr(), nullptr));
  268. if (!temp) {
  269. PyErr_Clear();
  270. return false;
  271. }
  272. // adapted from pybind11::detail::type_caster_generic
  273. if (base::load(temp, false)) {
  274. loader_life_support::add_patient(temp);
  275. return true;
  276. }
  277. return false;
  278. }
  279. };
  280. template<> struct type_caster<mgb::CompNode> : public from_none_caster<mgb::CompNode> {};
  281. } // detail
  282. } // PYBIND11_NAMESPACE
  283. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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