MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / dtype_mgb2np

Function dtype_mgb2np

imperative/python/src/helper.cpp:665–691  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

663}
664
665PyObject* dtype_mgb2np(mgb::DType dtype) {
666 PYTHON_GIL;
667 // According to
668 // https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_TypeObjectFromType
669 // the following is equivalent to PyArray_TypeObjectFromType for built-in
670 // types.
671 if (!dtype.valid()) {
672 Py_XINCREF(Py_None);
673 return Py_None;
674 }
675 auto descr = dtype_mgb2np_descr(dtype);
676 if (descr == nullptr) {
677 Py_XINCREF(Py_None);
678 return Py_None;
679 }
680 static bool use_typeobj_as_dtype = MGB_GETENV("MGE_USE_TYPEOBJ_AS_DTYPE");
681 if (use_typeobj_as_dtype) {
682 if (dtype.has_param()) {
683 return reinterpret_cast<PyObject*>(descr.release());
684 }
685 PyObject* typeobj = reinterpret_cast<PyObject*>(descr->typeobj);
686 Py_XINCREF(typeobj);
687 return typeobj;
688 } else {
689 return reinterpret_cast<PyObject*>(descr.release());
690 }
691}
692
693mgb::DType dtype_np2mgb(PyObject* obj) {
694 mgb_assert(obj && obj != Py_None, "can not convert null PyObject to numpy dtype");

Callers 1

castMethod · 0.85

Calls 4

dtype_mgb2np_descrFunction · 0.85
validMethod · 0.45
has_paramMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected