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.

ops.cpp 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * \file imperative/python/src/ops.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 "./ops.h"
  12. #include "megbrain/imperative.h"
  13. #include "megbrain/imperative/ops/backward_graph.h"
  14. #include "megbrain/imperative/ops/opr_attr.h"
  15. #include "megbrain/imperative/ops/utility.h"
  16. #include "megbrain/imperative/ops/autogen.h"
  17. #include <Python.h>
  18. #include <unordered_map>
  19. namespace py = pybind11;
  20. using namespace mgb::imperative;
  21. namespace {
  22. auto normalize_enum(const std::string& in) {
  23. std::string ret;
  24. for (auto&& c : in) {
  25. ret += toupper(c);
  26. }
  27. return ret;
  28. }
  29. } // anonymous namespace
  30. namespace {
  31. #define PyOp(name) Py##name
  32. #define PyOpType(name) PyOp(name)::py_type
  33. #define PyOpDefBegin(name) \
  34. struct PyOp(name) : PyOpDef { \
  35. using Ty = name; \
  36. Ty& inst() { return op->cast_final_safe<Ty>(); } \
  37. static PyTypeObject py_type;
  38. #define PyOpDefEnd(name) \
  39. }; \
  40. PyTypeObject PyOpType(name);
  41. #define RETURN_RICHCOMPARE(val1, val2, op) \
  42. do { \
  43. switch (op) { \
  44. case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  45. case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  46. case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  47. case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  48. case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  49. case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  50. default: \
  51. Py_FatalError("Unreachable C code path reached"); \
  52. } \
  53. } while (0)
  54. template<typename T, typename SFINAE=void>
  55. struct pyobj_convert_generic {
  56. static T from(PyObject* obj) {
  57. // TODO: remove this guard which is used for pybind11 implicit conversion
  58. py::detail::loader_life_support guard{};
  59. return py::cast<T>(py::handle(obj));
  60. }
  61. template<typename U,
  62. typename = std::enable_if_t<std::is_same_v<T, std::decay_t<U>>>>
  63. static PyObject* to(U&& t) {
  64. return py::cast(std::forward<U>(t)).release().ptr();
  65. }
  66. };
  67. template<typename T>
  68. PyObject* py_new_generic(PyTypeObject* type, PyObject*, PyObject*) {
  69. PyObject* obj = type->tp_alloc(type, 0);
  70. T* self = reinterpret_cast<T*>(obj);
  71. if (self != NULL) {
  72. self->op = T::Ty::make();
  73. }
  74. return obj;
  75. }
  76. template<typename T>
  77. void py_dealloc_generic(PyObject* obj) {
  78. reinterpret_cast<T*>(obj)->op.reset();
  79. Py_TYPE(obj)->tp_free(obj);
  80. }
  81. template<typename T, typename U, U T::Ty::*attr>
  82. PyObject* py_get_generic_impl(PyObject* obj, void* /* closure */) {
  83. auto& op = reinterpret_cast<T*>(obj)->inst();
  84. return pyobj_convert_generic<U>::to(op.*attr);
  85. }
  86. #define py_get_generic(name, attr) \
  87. py_get_generic_impl<PyOp(name), decltype(std::declval<name>().attr), &name::attr>
  88. template<typename T>
  89. PyObject* py_get_scope_impl(PyObject* obj, void* /* closure */) {
  90. // T: PyOpXXX inst(): return XXX in opdef.h.inl
  91. auto& op = reinterpret_cast<T*>(obj)->inst();
  92. return pyobj_convert_generic<std::string>::to(op.scope());
  93. }
  94. #define py_get_scope(class) py_get_scope_impl<PyOp(class)>
  95. template<typename T, typename U, U T::Ty::*attr>
  96. int py_set_generic_impl(PyObject* obj, PyObject* value, void* /* closure */) {
  97. if (value == NULL) {
  98. PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
  99. return -1;
  100. }
  101. auto& op = reinterpret_cast<T*>(obj)->inst();
  102. try {
  103. op.*attr = pyobj_convert_generic<U>::from(value);
  104. return 0;
  105. } catch(py::error_already_set& e) {
  106. e.restore();
  107. } catch(py::builtin_exception& e) {
  108. e.set_error();
  109. } catch(...) {
  110. PyErr_SetString(PyExc_RuntimeError, "Unknown Error");
  111. }
  112. return -1;
  113. }
  114. #define py_set_generic(name, attr) \
  115. py_set_generic_impl<PyOp(name), decltype(std::declval<name>().attr), &name::attr>
  116. template<typename T>
  117. int py_set_scope_impl(PyObject* obj, PyObject* value, void* /* closure */) {
  118. if (value == NULL) {
  119. PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
  120. return -1;
  121. }
  122. auto& op = reinterpret_cast<T*>(obj)->inst();
  123. try {
  124. op.set_scope(pyobj_convert_generic<std::string>::from(value));
  125. return 0;
  126. } catch(py::error_already_set& e) {
  127. e.restore();
  128. } catch(py::builtin_exception& e) {
  129. e.set_error();
  130. } catch(...) {
  131. PyErr_SetString(PyExc_RuntimeError, "Unknown Error");
  132. }
  133. return -1;
  134. }
  135. #define py_set_scope(class) py_set_scope_impl<PyOp(class)>
  136. struct PyOpDef {
  137. PyObject_HEAD
  138. std::shared_ptr<OpDef> op;
  139. static PyTypeObject py_type;
  140. static std::unordered_map<mgb::Typeinfo*, PyTypeObject*> ctype2pytype;
  141. static Py_hash_t tp_hash(PyObject *obj);
  142. static PyObject* tp_richcompare(PyObject *self, PyObject *other, int op);
  143. };
  144. PyTypeObject PyOpType(OpDef);
  145. std::unordered_map<mgb::Typeinfo*, PyTypeObject*> PyOp(OpDef)::ctype2pytype;
  146. Py_hash_t PyOp(OpDef)::tp_hash(PyObject *obj) {
  147. return static_cast<Py_hash_t>(
  148. reinterpret_cast<PyOp(OpDef)*>(obj)->op->hash());
  149. }
  150. PyObject* PyOp(OpDef)::tp_richcompare(PyObject *self, PyObject *other, int op) {
  151. bool same = reinterpret_cast<PyOp(OpDef)*>(self)->op->is_same(
  152. *reinterpret_cast<PyOp(OpDef)*>(other)->op);
  153. if (op == Py_EQ || op == Py_NE) {
  154. RETURN_RICHCOMPARE(same, true, op);
  155. }
  156. Py_RETURN_NOTIMPLEMENTED;
  157. }
  158. template<typename T>
  159. struct EnumWrapper {
  160. static_assert(std::is_enum_v<T>);
  161. PyObject_HEAD
  162. T value;
  163. static const char* name;
  164. static PyTypeObject type;
  165. static std::unordered_map<T, std::string> type2str;
  166. static std::unordered_map<std::string, T> str2type;
  167. EnumWrapper() = default;
  168. EnumWrapper(T v): value(v) {}
  169. EnumWrapper(std::string&& str): EnumWrapper(str2type.at(normalize_enum(str))) {}
  170. std::string to_string() const {
  171. return type2str.at(value);
  172. }
  173. static PyObject* py_repr(PyObject* self) {
  174. return pyobj_convert_generic<std::string>::to(
  175. std::string(name) + "." + reinterpret_cast<EnumWrapper*>(self)->to_string());
  176. }
  177. static PyObject* tp_richcompare(PyObject *self, PyObject *other, int op) {
  178. T lhs = reinterpret_cast<EnumWrapper*>(self)->value,
  179. rhs = reinterpret_cast<EnumWrapper*>(other)->value;
  180. if (op == Py_EQ || op == Py_NE) {
  181. RETURN_RICHCOMPARE(lhs, rhs, op);
  182. }
  183. Py_RETURN_NOTIMPLEMENTED;
  184. }
  185. };
  186. template<typename T>
  187. struct pyobj_convert_generic<T,
  188. std::enable_if_t<std::is_enum_v<std::decay_t<T>>>> {
  189. using Wrapper = EnumWrapper<T>;
  190. static T from(PyObject* obj) {
  191. if (PyObject_TypeCheck(obj, &Wrapper::type)) {
  192. return reinterpret_cast<Wrapper*>(obj)->value;
  193. }
  194. // try as string
  195. // TODO: type checkcd
  196. return Wrapper(pyobj_convert_generic<std::string>::from(obj)).value;
  197. }
  198. static PyObject* to(T t) {
  199. PyTypeObject* pytype = &Wrapper::type;
  200. PyObject* obj = pytype->tp_alloc(pytype, 0);
  201. reinterpret_cast<Wrapper*>(obj)->value = t;
  202. return obj;
  203. }
  204. };
  205. void _init_py_op_def(py::module m) {
  206. auto& py_type = PyOpType(OpDef);
  207. py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
  208. py_type.tp_name = "megengine.core._imperative_rt.OpDef";
  209. py_type.tp_basicsize = sizeof(PyOp(OpDef));
  210. py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  211. py_type.tp_doc = "OpDef";
  212. py_type.tp_base = &PyBaseObject_Type;
  213. py_type.tp_hash = PyOp(OpDef)::tp_hash;
  214. py_type.tp_richcompare = PyOp(OpDef)::tp_richcompare;
  215. mgb_assert(PyType_Ready(&py_type) >= 0);
  216. m.add_object("OpDef", reinterpret_cast<PyObject*>(&py_type));
  217. }
  218. /*********** begin of hand-write opdefs **************/
  219. PyOpDefBegin(BackwardGraph) // {{
  220. // };
  221. PyOpDefEnd(BackwardGraph)
  222. void _init_py_backward_graph(py::module m) {
  223. using py_op = PyOp(BackwardGraph);
  224. auto& py_type = PyOpType(BackwardGraph);
  225. py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
  226. py_type.tp_name = "megengine.core._imperative_rt.ops.BackwardGraph";
  227. py_type.tp_basicsize = sizeof(PyOp(BackwardGraph));
  228. py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  229. py_type.tp_doc = "BackwardGraph";
  230. py_type.tp_base = &PyOpType(OpDef);
  231. py_type.tp_dealloc = py_dealloc_generic<py_op>;
  232. py_type.tp_new = py_new_generic<py_op>;
  233. mgb_assert(PyType_Ready(&py_type) >= 0);
  234. // FIXME: rewrite interpret function in cpython instead wrap directly by pybind11::cppfunction
  235. auto interpret = py::cpp_function(
  236. [](OpDef& self, py::object pyf, py::object pyc,
  237. const mgb::SmallVector<py::object>& inputs) {
  238. auto f = [pyf](OpDef& op, const mgb::SmallVector<py::object>& inputs) {
  239. return py::cast<mgb::SmallVector<py::object>>(pyf(op.shared_from_this(), inputs));
  240. };
  241. auto c = [pyc](const TensorPtr& tensor) {
  242. return pyc(tensor->dev_tensor());
  243. };
  244. return self.cast_final_safe<BackwardGraph>().graph().interpret<py::object>(f, c, inputs);
  245. });
  246. mgb_assert(PyDict_SetItemString(
  247. py_type.tp_dict, "interpret", interpret.release().ptr()) >= 0);
  248. PyType_Modified(&py_type);
  249. m.add_object("BackwardGraph", reinterpret_cast<PyObject*>(&py_type));
  250. mgb_assert(PyOp(OpDef)::ctype2pytype.emplace(BackwardGraph::typeinfo(), &py_type).second);
  251. }
  252. struct PyOpBase : PyOpDef {
  253. static PyTypeObject py_type;
  254. static PyObject* tp_new(PyTypeObject* type, PyObject*, PyObject*) {
  255. auto* obj = type->tp_alloc(type, 0);
  256. if (obj) {
  257. auto* self = reinterpret_cast<PyOpBase*>(obj);
  258. new(&self->op) decltype(self->op);
  259. }
  260. return obj;
  261. }
  262. };
  263. PyTypeObject PyOpBase::py_type;
  264. void _init_py_op_base(py::module m) {
  265. using py_op = PyOpBase;
  266. auto& py_type = PyOpBase::py_type;
  267. py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
  268. py_type.tp_name = "megengine.core._imperative_rt.ops.PyOpBase";
  269. py_type.tp_basicsize = sizeof(py_op);
  270. py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  271. py_type.tp_doc = "PyOpBase";
  272. py_type.tp_base = &PyOpType(OpDef);
  273. py_type.tp_dealloc = py_dealloc_generic<py_op>;
  274. py_type.tp_new = py_op::tp_new;
  275. mgb_assert(PyType_Ready(&py_type) >= 0);
  276. m.add_object("PyOpBase", reinterpret_cast<PyObject*>(&py_type));
  277. }
  278. /*********** end of hand-write opdefs **************/
  279. // auto generated opdefs
  280. #include "opdef.cpy.inl"
  281. } // anonymous namespace
  282. namespace PYBIND11_NAMESPACE {
  283. namespace detail {
  284. bool type_caster<OpDef>::load(handle src, bool convert) {
  285. PyObject* obj = src.ptr();
  286. if (!PyObject_TypeCheck(obj, &PyOpType(OpDef))) {
  287. return false;
  288. }
  289. value = reinterpret_cast<PyOp(OpDef)*>(obj)->op;
  290. if (!value) {
  291. // opdef only defined in Python
  292. value = std::make_shared<GenericPyOp>(reinterpret_borrow<object>(src));
  293. }
  294. return true;
  295. }
  296. handle type_caster<OpDef>::cast(const OpDef& op, return_value_policy, handle) {
  297. if (auto* pyop = op.try_cast_final<GenericPyOp>()) {
  298. return object(pyop->obj).release();
  299. }
  300. PyTypeObject* pytype;
  301. auto& c2p = PyOp(OpDef)::ctype2pytype;
  302. auto&& iter = c2p.find(op.dyn_typeinfo());
  303. if (iter != c2p.end()) { // FIXME: should always meet this condition
  304. pytype = iter->second;
  305. } else { // which means unregistered op type, jsut make it as an opaque op type
  306. // currently, only OprAttr goes into this branch
  307. pytype = &PyOpType(OpDef);
  308. }
  309. PyObject* obj = pytype->tp_alloc(pytype, 0);
  310. mgb_assert(PyObject_TypeCheck(obj, &PyOpType(OpDef)));
  311. reinterpret_cast<PyOp(OpDef)*>(obj)->op = const_cast<OpDef&>(op).shared_from_this();
  312. return py::handle(obj);
  313. }
  314. } // detail
  315. } // PYBIND11_NAMESPACE
  316. void init_ops(py::module m) {
  317. _init_py_op_def(m);
  318. _init_py_backward_graph(m);
  319. _init_py_op_base(m);
  320. INIT_ALL_OP(m)
  321. }

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