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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. #define CATCH_ALL(RETVAL) \
  31. catch(py::error_already_set& e) { \
  32. e.restore(); \
  33. return RETVAL; \
  34. } catch(py::builtin_exception& e) { \
  35. e.set_error(); \
  36. return RETVAL; \
  37. } catch(std::exception& e) { \
  38. PyErr_SetString(PyExc_RuntimeError, e.what()); \
  39. return RETVAL; \
  40. } \
  41. namespace {
  42. #define PyOp(name) Py##name
  43. #define PyOpType(name) PyOp(name)::py_type
  44. #define PyOpDefBegin(name) \
  45. struct PyOp(name) : PyOpDef { \
  46. using Ty = name; \
  47. Ty& inst() { return op->cast_final_safe<Ty>(); } \
  48. static PyTypeObject py_type;
  49. #define PyOpDefEnd(name) \
  50. }; \
  51. PyTypeObject PyOpType(name);
  52. #define RETURN_RICHCOMPARE(val1, val2, op) \
  53. do { \
  54. switch (op) { \
  55. case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  56. case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  57. case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  58. case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  59. case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  60. case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  61. default: \
  62. Py_FatalError("Unreachable C code path reached"); \
  63. } \
  64. } while (0)
  65. template <typename T>
  66. PyObject* py_new_generic(PyTypeObject* type, PyObject*, PyObject*) {
  67. PyObject* obj = type->tp_alloc(type, 0);
  68. T* self = reinterpret_cast<T*>(obj);
  69. if (self != NULL) {
  70. self->op = T::Ty::make();
  71. }
  72. return obj;
  73. }
  74. template<typename T>
  75. void py_dealloc_generic(PyObject* obj) {
  76. reinterpret_cast<T*>(obj)->op.reset();
  77. Py_TYPE(obj)->tp_free(obj);
  78. }
  79. template<typename T, typename U, U T::Ty::*attr>
  80. PyObject* py_get_generic_impl(PyObject* obj, void* /* closure */) {
  81. auto& op = reinterpret_cast<T*>(obj)->inst();
  82. return py::cast(op.*attr).release().ptr();
  83. }
  84. #define py_get_generic(name, attr) \
  85. py_get_generic_impl<PyOp(name), decltype(std::declval<name>().attr), &name::attr>
  86. template<typename T, typename U, U T::Ty::*attr>
  87. int py_set_generic_impl(PyObject* obj, PyObject* value, void* /* closure */) {
  88. if (value == NULL) {
  89. PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
  90. return -1;
  91. }
  92. auto& op = reinterpret_cast<T*>(obj)->inst();
  93. try {
  94. // TODO: remove this guard which is used for pybind11 implicit conversion
  95. py::detail::loader_life_support guard{};
  96. op.*attr = py::cast<U>(py::handle(value));
  97. } CATCH_ALL(-1)
  98. return 0;
  99. }
  100. #define py_set_generic(name, attr) \
  101. py_set_generic_impl<PyOp(name), decltype(std::declval<name>().attr), &name::attr>
  102. struct PyOpDef {
  103. PyObject_HEAD
  104. std::shared_ptr<OpDef> op;
  105. static PyTypeObject py_type;
  106. static std::unordered_map<mgb::Typeinfo*, PyTypeObject*> ctype2pytype;
  107. static PyGetSetDef py_getsetters[];
  108. static Py_hash_t tp_hash(PyObject *obj);
  109. static PyObject* tp_richcompare(PyObject *self, PyObject *other, int op);
  110. };
  111. PyTypeObject PyOpType(OpDef);
  112. std::unordered_map<mgb::Typeinfo*, PyTypeObject*> PyOp(OpDef)::ctype2pytype;
  113. PyObject* py_get_scope(PyObject* obj, void* /* closure */) {
  114. return py::cast(
  115. reinterpret_cast<PyOp(OpDef)*>(obj)->op->scope()).release().ptr();
  116. }
  117. int py_set_scope(PyObject* obj, PyObject* value, void* /* closure */) {
  118. if (value == NULL) {
  119. PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
  120. return -1;
  121. }
  122. try {
  123. reinterpret_cast<PyOp(OpDef)*>(obj)->op
  124. ->set_scope(py::cast<std::string>(py::handle(value)));
  125. } CATCH_ALL(-1)
  126. return 0;
  127. }
  128. PyGetSetDef PyOp(OpDef)::py_getsetters[] = {
  129. {const_cast<char*>("scope"), py_get_scope, py_set_scope, "scope", NULL},
  130. {NULL}
  131. };
  132. Py_hash_t PyOp(OpDef)::tp_hash(PyObject *obj) {
  133. return static_cast<Py_hash_t>(
  134. reinterpret_cast<PyOp(OpDef)*>(obj)->op->hash());
  135. }
  136. PyObject* PyOp(OpDef)::tp_richcompare(PyObject *self, PyObject *other, int op) {
  137. bool same = reinterpret_cast<PyOp(OpDef)*>(self)->op->is_same(
  138. *reinterpret_cast<PyOp(OpDef)*>(other)->op);
  139. if (op == Py_EQ || op == Py_NE) {
  140. RETURN_RICHCOMPARE(same, true, op);
  141. }
  142. Py_RETURN_NOTIMPLEMENTED;
  143. }
  144. template<typename T>
  145. struct EnumTrait;
  146. #define PyEnumHead \
  147. static_assert(std::is_enum_v<T>); \
  148. PyObject_HEAD \
  149. T value; \
  150. constexpr static const char *name = EnumTrait<T>::name; \
  151. static PyTypeObject* type; \
  152. static const char* members[]; \
  153. static std::unordered_map<std::string, T> mem2value; \
  154. static PyObject* pyobj_insts[];
  155. template<typename T>
  156. struct EnumWrapper {
  157. PyEnumHead
  158. std::string to_string() const {
  159. return members[static_cast<size_t>(value)];
  160. }
  161. static PyObject* py_repr(PyObject* self) {
  162. return py::cast(
  163. std::string(name) + "." + reinterpret_cast<EnumWrapper*>(self)->to_string())
  164. .release().ptr();
  165. }
  166. static PyObject* tp_richcompare(PyObject *self, PyObject *other, int op) {
  167. T lhs = reinterpret_cast<EnumWrapper*>(self)->value,
  168. rhs = reinterpret_cast<EnumWrapper*>(other)->value;
  169. if (op == Py_EQ || op == Py_NE) {
  170. RETURN_RICHCOMPARE(lhs, rhs, op);
  171. }
  172. Py_RETURN_NOTIMPLEMENTED;
  173. }
  174. static bool load(py::handle src, T& value) {
  175. PyObject* obj = src.ptr();
  176. if (PyObject_TypeCheck(obj, type)) {
  177. value = reinterpret_cast<EnumWrapper*>(obj)->value;
  178. return true;
  179. }
  180. if (py::isinstance<py::str>(src)) {
  181. auto&& iter = mem2value.find(
  182. normalize_enum(py::cast<std::string>(src)));
  183. if (iter != mem2value.end()) {
  184. value = iter->second;
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. return false;
  191. }
  192. static PyObject* cast(const T& value) {
  193. auto v = static_cast<std::underlying_type_t<T>>(value);
  194. mgb_assert(v <= EnumTrait<T>::max);
  195. PyObject* obj = pyobj_insts[v];
  196. Py_INCREF(obj);
  197. return obj;
  198. }
  199. };
  200. template<typename T>
  201. struct BitCombinedEnumWrapper {
  202. PyEnumHead
  203. std::string to_string() const {
  204. uint32_t value_int = static_cast<uint32_t>(value);
  205. if (value_int == 0) {
  206. return "None";
  207. } else {
  208. std::string ret;
  209. bool first = true;
  210. for (uint32_t i = 0; i < 32; i++) {
  211. if (value_int >> i & 1) {
  212. if (!first) {
  213. ret += " + ";
  214. } else {
  215. first = false;
  216. }
  217. ret += (std::string(name) + "." + members[i]);
  218. }
  219. }
  220. return ret;
  221. }
  222. }
  223. static PyObject* py_new_combined_enum(PyTypeObject* type, PyObject* args, PyObject*) {
  224. if (!PyTuple_Size(args)) {
  225. PyObject* obj = type->tp_alloc(type, 0);
  226. reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value = T();
  227. return obj;
  228. }
  229. else {
  230. PyObject* input;
  231. if (!PyArg_ParseTuple(args, "|O", &input)) {
  232. return nullptr;
  233. }
  234. T value;
  235. if (load(input, value)) {
  236. return cast(value);
  237. } else {
  238. PyErr_SetString(PyExc_RuntimeError,
  239. mgb::ssprintf("Cannot convert type %s to type %s\n",
  240. input->ob_type->tp_name, name).c_str());
  241. return nullptr;
  242. }
  243. }
  244. }
  245. static PyObject* py_repr(PyObject* self) {
  246. return py::cast(
  247. reinterpret_cast<BitCombinedEnumWrapper*>(self)->to_string())
  248. .release().ptr();
  249. }
  250. static PyObject* py_or(PyObject* self, PyObject* other) {
  251. if(!(self->ob_type == other->ob_type)){
  252. return PyErr_Format(
  253. PyExc_RuntimeError,
  254. "Operand in or operator must be the same type.");
  255. }
  256. T lhs = reinterpret_cast<BitCombinedEnumWrapper*>(self)->value,
  257. rhs = reinterpret_cast<BitCombinedEnumWrapper*>(other)->value;
  258. return cast(lhs | rhs);
  259. }
  260. static PyObject* py_and(PyObject* self, PyObject* other) {
  261. if (!(self->ob_type == other->ob_type)) {
  262. return PyErr_Format(
  263. PyExc_RuntimeError,
  264. "Operand in and operator must be the same type.");
  265. }
  266. T lhs = reinterpret_cast<BitCombinedEnumWrapper*>(self)->value,
  267. rhs = reinterpret_cast<BitCombinedEnumWrapper*>(other)->value;
  268. return cast(lhs & rhs);
  269. }
  270. static PyObject* tp_richcompare(PyObject* self, PyObject* other, int op) {
  271. T lhs = reinterpret_cast<BitCombinedEnumWrapper*>(self)->value,
  272. rhs = reinterpret_cast<BitCombinedEnumWrapper*>(other)->value;
  273. if (op == Py_EQ || op == Py_NE) {
  274. RETURN_RICHCOMPARE(lhs, rhs, op);
  275. }
  276. Py_RETURN_NOTIMPLEMENTED;
  277. }
  278. static bool load(py::handle src, T& value) {
  279. PyObject* obj = src.ptr();
  280. if (PyObject_TypeCheck(obj, type)) {
  281. value = reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value;
  282. return true;
  283. }
  284. if (py::isinstance<py::str>(src)) {
  285. auto&& iter = mem2value.find(
  286. normalize_enum(py::cast<std::string>(src)));
  287. if (iter != mem2value.end()) {
  288. value = iter->second;
  289. return true;
  290. } else {
  291. return false;
  292. }
  293. }
  294. if (py::isinstance<py::int_>(obj)) {
  295. auto v = py::cast<std::underlying_type_t<T>>(src);
  296. if(v > EnumTrait<T>::max) {
  297. return false;
  298. }
  299. value = static_cast<T>(v);
  300. return true;
  301. }
  302. return false;
  303. }
  304. static PyObject* cast(const T& value) {
  305. auto v = static_cast<std::underlying_type_t<T>>(value);
  306. mgb_assert(v <= EnumTrait<T>::max);
  307. if ((!v) || (v & (v - 1))) {
  308. PyObject* obj = type->tp_alloc(type, 0);
  309. reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value = value;
  310. return obj;
  311. } else {
  312. PyObject* obj = pyobj_insts[__builtin_ctz(v)];
  313. Py_INCREF(obj);
  314. return obj;
  315. }
  316. }
  317. };
  318. void _init_py_op_def(py::module m) {
  319. using py_op = PyOp(OpDef);
  320. auto& py_type = PyOpType(OpDef);
  321. py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
  322. py_type.tp_name = "megengine.core._imperative_rt.OpDef";
  323. py_type.tp_basicsize = sizeof(PyOp(OpDef));
  324. py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  325. py_type.tp_doc = "OpDef";
  326. py_type.tp_base = &PyBaseObject_Type;
  327. py_type.tp_hash = PyOp(OpDef)::tp_hash;
  328. py_type.tp_richcompare = PyOp(OpDef)::tp_richcompare;
  329. py_type.tp_getset = py_op::py_getsetters;
  330. mgb_assert(PyType_Ready(&py_type) >= 0);
  331. m.add_object("OpDef", reinterpret_cast<PyObject*>(&py_type));
  332. }
  333. /*********** begin of hand-write opdefs **************/
  334. PyOpDefBegin(BackwardGraph) // {{
  335. // };
  336. PyOpDefEnd(BackwardGraph)
  337. void _init_py_backward_graph(py::module m) {
  338. using py_op = PyOp(BackwardGraph);
  339. auto& py_type = PyOpType(BackwardGraph);
  340. py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
  341. py_type.tp_name = "megengine.core._imperative_rt.ops.BackwardGraph";
  342. py_type.tp_basicsize = sizeof(PyOp(BackwardGraph));
  343. py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  344. py_type.tp_doc = "BackwardGraph";
  345. py_type.tp_base = &PyOpType(OpDef);
  346. py_type.tp_dealloc = py_dealloc_generic<py_op>;
  347. py_type.tp_new = py_new_generic<py_op>;
  348. mgb_assert(PyType_Ready(&py_type) >= 0);
  349. // FIXME: rewrite interpret function in cpython instead wrap directly by pybind11::cppfunction
  350. auto interpret = py::cpp_function(
  351. [](OpDef& self, py::object pyf, py::object pyc,
  352. const mgb::SmallVector<py::object>& inputs) {
  353. auto f = [pyf](OpDef& op, const mgb::SmallVector<py::object>& inputs) {
  354. return py::cast<mgb::SmallVector<py::object>>(pyf(op.shared_from_this(), inputs));
  355. };
  356. auto c = [pyc](const TensorPtr& tensor) {
  357. return pyc(tensor->dev_tensor());
  358. };
  359. return self.cast_final_safe<BackwardGraph>().graph().interpret<py::object>(f, c, inputs);
  360. });
  361. mgb_assert(PyDict_SetItemString(
  362. py_type.tp_dict, "interpret", interpret.release().ptr()) >= 0);
  363. PyType_Modified(&py_type);
  364. m.add_object("BackwardGraph", reinterpret_cast<PyObject*>(&py_type));
  365. mgb_assert(PyOp(OpDef)::ctype2pytype.emplace(BackwardGraph::typeinfo(), &py_type).second);
  366. }
  367. struct PyOpBase : PyOpDef {
  368. static PyTypeObject py_type;
  369. static PyObject* tp_new(PyTypeObject* type, PyObject*, PyObject*) {
  370. auto* obj = type->tp_alloc(type, 0);
  371. if (obj) {
  372. auto* self = reinterpret_cast<PyOpBase*>(obj);
  373. new(&self->op) decltype(self->op);
  374. }
  375. return obj;
  376. }
  377. };
  378. PyTypeObject PyOpBase::py_type;
  379. void _init_py_op_base(py::module m) {
  380. using py_op = PyOpBase;
  381. auto& py_type = PyOpBase::py_type;
  382. py_type = {PyVarObject_HEAD_INIT(NULL, 0)};
  383. py_type.tp_name = "megengine.core._imperative_rt.ops.PyOpBase";
  384. py_type.tp_basicsize = sizeof(py_op);
  385. py_type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
  386. py_type.tp_doc = "PyOpBase";
  387. py_type.tp_base = &PyOpType(OpDef);
  388. py_type.tp_dealloc = py_dealloc_generic<py_op>;
  389. py_type.tp_new = py_op::tp_new;
  390. mgb_assert(PyType_Ready(&py_type) >= 0);
  391. m.add_object("PyOpBase", reinterpret_cast<PyObject*>(&py_type));
  392. }
  393. /*********** end of hand-write opdefs **************/
  394. // auto generated opdefs
  395. #include "opdef.cpy.inl"
  396. #undef CATCH_ALL
  397. } // anonymous namespace
  398. namespace PYBIND11_NAMESPACE {
  399. namespace detail {
  400. bool type_caster<OpDef>::load(handle src, bool convert) {
  401. PyObject* obj = src.ptr();
  402. if (!PyObject_TypeCheck(obj, &PyOpType(OpDef))) {
  403. return false;
  404. }
  405. value = reinterpret_cast<PyOp(OpDef)*>(obj)->op;
  406. if (!value) {
  407. // opdef only defined in Python
  408. value = std::make_shared<GenericPyOp>(reinterpret_borrow<object>(src));
  409. }
  410. return true;
  411. }
  412. handle type_caster<OpDef>::cast(const OpDef& op, return_value_policy, handle) {
  413. if (auto* pyop = op.try_cast_final<GenericPyOp>()) {
  414. return object(pyop->obj).release();
  415. }
  416. PyTypeObject* pytype;
  417. auto& c2p = PyOp(OpDef)::ctype2pytype;
  418. auto&& iter = c2p.find(op.dyn_typeinfo());
  419. if (iter != c2p.end()) { // FIXME: should always meet this condition
  420. pytype = iter->second;
  421. } else { // which means unregistered op type, jsut make it as an opaque op type
  422. // currently, only OprAttr goes into this branch
  423. pytype = &PyOpType(OpDef);
  424. }
  425. PyObject* obj = pytype->tp_alloc(pytype, 0);
  426. mgb_assert(PyObject_TypeCheck(obj, &PyOpType(OpDef)));
  427. reinterpret_cast<PyOp(OpDef)*>(obj)->op = const_cast<OpDef&>(op).shared_from_this();
  428. return py::handle(obj);
  429. }
  430. #define ENUM_CASTER_IMPL(T) \
  431. bool type_caster<T>::load(handle src, bool) { \
  432. return EnumWrapper<T>::load(src, value); \
  433. } \
  434. handle type_caster<T>::cast(const T& value, return_value_policy, handle) { \
  435. return EnumWrapper<T>::cast(value); \
  436. }
  437. FOR_EACH_ENUM_PARAM(ENUM_CASTER_IMPL)
  438. #define BIT_COMBINED_ENUM_CASTER_IMPL(T) \
  439. bool type_caster<T>::load(handle src, bool) { \
  440. return BitCombinedEnumWrapper<T>::load(src, value); \
  441. } \
  442. handle type_caster<T>::cast(const T& value, return_value_policy, handle) { \
  443. return BitCombinedEnumWrapper<T>::cast(value); \
  444. }
  445. FOR_EACH_BIT_COMBINED_ENUM_PARAM(BIT_COMBINED_ENUM_CASTER_IMPL)
  446. } // detail
  447. } // PYBIND11_NAMESPACE
  448. void init_ops(py::module m) {
  449. _init_py_op_def(m);
  450. _init_py_backward_graph(m);
  451. _init_py_op_base(m);
  452. INIT_ALL_OP(m)
  453. }

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