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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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-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. #pragma once
  12. #include "megbrain/common.h"
  13. #include "megbrain/imperative/op_def.h"
  14. #include "megbrain/utils/persistent_cache.h"
  15. #include <Python.h>
  16. #include <iterator>
  17. #include <string>
  18. #if __cplusplus > 201703L
  19. #include <ranges>
  20. #endif
  21. #include <pybind11/functional.h>
  22. #include <pybind11/numpy.h>
  23. #include <pybind11/pybind11.h>
  24. #include <pybind11/stl.h>
  25. #include "./numpy_dtypes.h"
  26. pybind11::module submodule(
  27. pybind11::module parent, const char* name, const char* doc = nullptr);
  28. pybind11::module rel_import(pybind11::str name, pybind11::module m, int level);
  29. #if __cplusplus > 201703L
  30. using std::ranges::range_value_t;
  31. #else
  32. template <typename T>
  33. using range_value_t =
  34. std::remove_cv_t<std::remove_reference_t<decltype(*std::declval<T>().begin())>>;
  35. #endif
  36. template <typename T>
  37. auto to_list(const T& x) {
  38. using elem_t = range_value_t<T>;
  39. std::vector<elem_t> ret(x.begin(), x.end());
  40. return pybind11::cast(ret);
  41. }
  42. template <typename T>
  43. auto to_tuple(
  44. const T& x, pybind11::return_value_policy policy =
  45. pybind11::return_value_policy::automatic) {
  46. auto ret = pybind11::tuple(x.size());
  47. for (size_t i = 0; i < x.size(); ++i) {
  48. ret[i] = pybind11::cast(x[i], policy);
  49. }
  50. return ret;
  51. }
  52. template <typename T>
  53. auto to_tuple(
  54. T begin, T end,
  55. pybind11::return_value_policy policy =
  56. pybind11::return_value_policy::automatic) {
  57. auto ret = pybind11::tuple(end - begin);
  58. for (size_t i = 0; begin < end; ++begin, ++i) {
  59. ret[i] = pybind11::cast(*begin, policy);
  60. }
  61. return ret;
  62. }
  63. class PyTaskDipatcher {
  64. struct Queue : mgb::AsyncQueueSC<std::function<void(void)>, Queue> {
  65. using Task = std::function<void(void)>;
  66. // set max_spin=0 to prevent Queue fetch task in busy wait manner.
  67. // this won't affect throughput when python interpreter is sending enough task,
  68. // but will significantly save CPU time when waiting for task, e.g. wait for
  69. // data input
  70. Queue() : mgb::AsyncQueueSC<std::function<void(void)>, Queue>(0) {}
  71. void process_one_task(Task& f) {
  72. if (!Py_IsInitialized())
  73. return;
  74. pybind11::gil_scoped_acquire _;
  75. f();
  76. }
  77. void on_async_queue_worker_thread_start() override {
  78. mgb::sys::set_thread_name("py_task_worker");
  79. }
  80. };
  81. Queue queue;
  82. bool finalized = false;
  83. public:
  84. template <typename T>
  85. void add_task(T&& task) {
  86. // CPython never dlclose an extension so
  87. // finalized means the interpreter has been shutdown
  88. if (!finalized) {
  89. queue.add_task(std::forward<T>(task));
  90. }
  91. }
  92. void wait_all_task_finish() { queue.wait_all_task_finish(); }
  93. ~PyTaskDipatcher() {
  94. finalized = true;
  95. queue.wait_all_task_finish();
  96. }
  97. };
  98. extern PyTaskDipatcher py_task_q;
  99. class GILManager {
  100. PyGILState_STATE gstate;
  101. public:
  102. GILManager() : gstate(PyGILState_Ensure()) {}
  103. ~GILManager() { PyGILState_Release(gstate); }
  104. };
  105. #define PYTHON_GIL GILManager __gil_manager
  106. //! wraps a shared_ptr and decr PyObject ref when destructed
  107. class PyObjRefKeeper {
  108. std::shared_ptr<PyObject> m_ptr;
  109. public:
  110. static void deleter(PyObject* p) {
  111. if (p) {
  112. py_task_q.add_task([p]() { Py_DECREF(p); });
  113. }
  114. }
  115. PyObjRefKeeper() = default;
  116. PyObjRefKeeper(PyObject* p) : m_ptr{p, deleter} {}
  117. PyObject* get() const { return m_ptr.get(); }
  118. //! create a shared_ptr as an alias of the underlying ptr
  119. template <typename T>
  120. std::shared_ptr<T> make_shared(T* ptr) const {
  121. return {m_ptr, ptr};
  122. }
  123. };
  124. //! exception to be thrown when python callback fails
  125. class PyExceptionForward : public std::exception {
  126. PyObject *m_type, *m_value, *m_traceback;
  127. std::string m_msg;
  128. PyExceptionForward(
  129. PyObject* type, PyObject* value, PyObject* traceback,
  130. const std::string& msg)
  131. : m_type{type}, m_value{value}, m_traceback{traceback}, m_msg{msg} {}
  132. public:
  133. PyExceptionForward(const PyExceptionForward&) = delete;
  134. PyExceptionForward& operator=(const PyExceptionForward&) = delete;
  135. ~PyExceptionForward();
  136. PyExceptionForward(PyExceptionForward&& rhs)
  137. : m_type{rhs.m_type},
  138. m_value{rhs.m_value},
  139. m_traceback{rhs.m_traceback},
  140. m_msg{std::move(rhs.m_msg)} {
  141. rhs.m_type = rhs.m_value = rhs.m_traceback = nullptr;
  142. }
  143. //! throw PyExceptionForward from current python error state
  144. static void throw_() __attribute__((noreturn));
  145. //! restore python error
  146. void restore();
  147. const char* what() const noexcept override { return m_msg.c_str(); }
  148. };
  149. //! numpy utils
  150. namespace npy {
  151. //! convert tensor shape to raw vector
  152. static inline std::vector<size_t> shape2vec(const mgb::TensorShape& shape) {
  153. return {shape.shape, shape.shape + shape.ndim};
  154. }
  155. //! change numpy dtype to megbrain supported dtype
  156. PyObject* to_mgb_supported_dtype(PyObject* dtype);
  157. //! convert raw vector to tensor shape
  158. mgb::TensorShape vec2shape(const std::vector<size_t>& vec);
  159. struct PyArrayDescrDeleter {
  160. void operator()(PyArray_Descr* obj) { Py_XDECREF(obj); }
  161. };
  162. //! Convert MegBrain DType to NumPy DType descriptor, the caller receives a new
  163. //! reference to the descriptor.
  164. std::unique_ptr<PyArray_Descr, PyArrayDescrDeleter> dtype_mgb2np_descr(
  165. mgb::DType dtype);
  166. mgb::DType dtype_np2mgb_descr(PyArray_Descr* descr);
  167. //! convert megbrain dtype to numpy dtype object; return new reference
  168. PyObject* dtype_mgb2np(mgb::DType dtype);
  169. //! convert numpy dtype object or string to megbrain dtype
  170. mgb::DType dtype_np2mgb(PyObject* obj);
  171. //! buffer sharing type
  172. enum class ShareType {
  173. MUST_SHARE, //!< must be shared
  174. MUST_UNSHARE, //!< must not be shared
  175. TRY_SHARE //!< share if possible
  176. };
  177. //! get ndarray from HostTensorND
  178. PyObject* ndarray_from_tensor(const mgb::HostTensorND& val, ShareType share_type);
  179. //! specify how to convert numpy array to tensor
  180. struct Meth {
  181. bool must_borrow_ = false;
  182. mgb::HostTensorND* dest_tensor_ = nullptr;
  183. mgb::CompNode dest_cn_;
  184. //! make a Meth that allows borrowing numpy array memory
  185. static Meth borrow(mgb::CompNode dest_cn = mgb::CompNode::default_cpu()) {
  186. return {false, nullptr, dest_cn};
  187. }
  188. //! make a Meth that requires the numpy array to be borrowed
  189. static Meth must_borrow(mgb::CompNode dest_cn = mgb::CompNode::default_cpu()) {
  190. return {true, nullptr, dest_cn};
  191. }
  192. //! make a Meth that requires copying the value into another
  193. //! tensor
  194. static Meth copy_into(mgb::HostTensorND* tensor) {
  195. return {false, tensor, tensor->comp_node()};
  196. }
  197. };
  198. /*!
  199. * \brief convert an object to megbrain tensor
  200. * \param meth specifies how the conversion should take place
  201. * \param dtype desired dtype; it can be set as invalid to allow arbitrary
  202. * dtype
  203. */
  204. mgb::HostTensorND np2tensor(PyObject* obj, const Meth& meth, mgb::DType dtype);
  205. } // namespace npy
  206. // Note: following macro was copied from pybind11/detail/common.h
  207. // Robust support for some features and loading modules compiled against different
  208. // pybind versions requires forcing hidden visibility on pybind code, so we enforce this
  209. // by setting the attribute on the main `pybind11` namespace.
  210. #if !defined(PYBIND11_NAMESPACE)
  211. #ifdef __GNUG__
  212. #define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
  213. #else
  214. #define PYBIND11_NAMESPACE pybind11
  215. #endif
  216. #endif
  217. namespace PYBIND11_NAMESPACE {
  218. namespace detail {
  219. template <typename T, unsigned N>
  220. struct type_caster<megdnn::SmallVector<T, N>>
  221. : list_caster<megdnn::SmallVector<T, N>, T> {};
  222. template <>
  223. struct type_caster<mgb::DType> {
  224. PYBIND11_TYPE_CASTER(mgb::DType, _("DType"));
  225. public:
  226. bool load(handle src, bool convert) {
  227. auto obj = reinterpret_borrow<object>(src);
  228. if (!convert && !isinstance<dtype>(obj)) {
  229. return false;
  230. }
  231. if (obj.is_none()) {
  232. return true;
  233. }
  234. try {
  235. obj = pybind11::dtype::from_args(obj);
  236. } catch (pybind11::error_already_set&) {
  237. return false;
  238. }
  239. try {
  240. value = npy::dtype_np2mgb(obj.ptr());
  241. } catch (...) {
  242. return false;
  243. }
  244. return true;
  245. }
  246. static handle cast(
  247. mgb::DType dt, return_value_policy /* policy */, handle /* parent */) {
  248. // ignore policy and parent because we always return a pure python object
  249. return npy::dtype_mgb2np(std::move(dt));
  250. }
  251. };
  252. template <>
  253. struct type_caster<mgb::TensorShape> {
  254. PYBIND11_TYPE_CASTER(mgb::TensorShape, _("TensorShape"));
  255. public:
  256. bool load(handle src, bool convert) {
  257. auto obj = reinterpret_borrow<object>(src);
  258. if (!convert && !isinstance<tuple>(obj)) {
  259. return false;
  260. }
  261. if (obj.is_none()) {
  262. return true;
  263. }
  264. value.ndim = len(obj);
  265. mgb_assert(value.ndim <= mgb::TensorShape::MAX_NDIM);
  266. size_t i = 0;
  267. for (auto v : obj) {
  268. mgb_assert(i < value.ndim);
  269. value.shape[i] = reinterpret_borrow<object>(v).cast<size_t>();
  270. ++i;
  271. }
  272. return true;
  273. }
  274. static handle cast(
  275. mgb::TensorShape shape, return_value_policy /* policy */,
  276. handle /* parent */) {
  277. // ignore policy and parent because we always return a pure python object
  278. return to_tuple(shape.shape, shape.shape + shape.ndim).release();
  279. }
  280. };
  281. // hack to make custom object implicitly convertible from None
  282. template <typename T>
  283. struct from_none_caster : public type_caster_base<T> {
  284. using base = type_caster_base<T>;
  285. bool load(handle src, bool convert) {
  286. if (!convert || !src.is_none()) {
  287. return base::load(src, convert);
  288. }
  289. // adapted from pybind11::implicitly_convertible
  290. auto temp = reinterpret_steal<object>(
  291. PyObject_Call((PyObject*)this->typeinfo->type, tuple().ptr(), nullptr));
  292. if (!temp) {
  293. PyErr_Clear();
  294. return false;
  295. }
  296. // adapted from pybind11::detail::type_caster_generic
  297. if (base::load(temp, false)) {
  298. loader_life_support::add_patient(temp);
  299. return true;
  300. }
  301. return false;
  302. }
  303. };
  304. template <>
  305. struct type_caster<mgb::CompNode> : public from_none_caster<mgb::CompNode> {};
  306. template <>
  307. struct type_caster<mgb::PersistentCache::Blob> {
  308. PYBIND11_TYPE_CASTER(mgb::PersistentCache::Blob, _("Blob"));
  309. public:
  310. bool load(handle src, bool convert) {
  311. if (!isinstance<bytes>(src)) {
  312. return false;
  313. }
  314. value.ptr = PYBIND11_BYTES_AS_STRING(src.ptr());
  315. value.size = PYBIND11_BYTES_SIZE(src.ptr());
  316. return true;
  317. }
  318. static handle cast(
  319. mgb::PersistentCache::Blob blob, return_value_policy /* policy */,
  320. handle /* parent */) {
  321. return bytes((const char*)blob.ptr, blob.size).release();
  322. }
  323. };
  324. template <typename T>
  325. struct type_caster<mgb::Maybe<T>> {
  326. using value_conv = make_caster<T>;
  327. PYBIND11_TYPE_CASTER(mgb::Maybe<T>, _("Optional[") + value_conv::name + _("]"));
  328. public:
  329. bool load(handle src, bool convert) {
  330. if (!src) {
  331. return false;
  332. }
  333. if (src.is_none()) {
  334. return true;
  335. }
  336. value_conv inner_caster;
  337. if (!inner_caster.load(src, convert)) {
  338. return false;
  339. }
  340. value.emplace(cast_op<T&&>(std::move(inner_caster)));
  341. return true;
  342. }
  343. static handle cast(mgb::Maybe<T> src, return_value_policy policy, handle parent) {
  344. if (!src.valid()) {
  345. return none().inc_ref();
  346. }
  347. return pybind11::cast(src.val(), policy, parent);
  348. }
  349. };
  350. template <>
  351. struct type_caster<mgb::imperative::OpDef> {
  352. protected:
  353. std::shared_ptr<mgb::imperative::OpDef> value;
  354. public:
  355. static constexpr auto name = _("OpDef");
  356. operator mgb::imperative::OpDef&() { return *value; }
  357. operator const mgb::imperative::OpDef&() { return *value; }
  358. operator std::shared_ptr<mgb::imperative::OpDef>&() { return value; }
  359. operator std::shared_ptr<mgb::imperative::OpDef>&&() && { return std::move(value); }
  360. template <typename T>
  361. using cast_op_type = T;
  362. bool load(handle src, bool convert);
  363. static handle cast(
  364. const mgb::imperative::OpDef& op, return_value_policy /* policy */,
  365. handle /* parent */);
  366. static handle cast(
  367. std::shared_ptr<mgb::imperative::OpDef> op, return_value_policy policy,
  368. handle parent) {
  369. return cast(*op, policy, parent);
  370. }
  371. };
  372. template <>
  373. struct type_caster<std::shared_ptr<mgb::imperative::OpDef>>
  374. : public type_caster<mgb::imperative::OpDef> {
  375. template <typename T>
  376. using cast_op_type = pybind11::detail::movable_cast_op_type<T>;
  377. };
  378. } // namespace detail
  379. } // namespace PYBIND11_NAMESPACE
  380. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}