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

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

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