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

Function _transpose_cpp

imperative/python/src/tensor_utils.cpp:1621–1663  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1619}
1620
1621py::object _transpose_cpp(py::handle inp_hdl, py::handle args) {
1622 py::object obj = _expand_args(args);
1623 py::list lis;
1624 if (!is_tensor(obj.ptr()) && PySequence_Check(obj.ptr())) {
1625 lis = py::reinterpret_steal<py::list>(PySequence_List(obj.ptr()));
1626 } else {
1627 py::object np = getattr(obj, "numpy")();
1628 PyArrayObject* arr = (PyArrayObject*)np.ptr();
1629 PyObject* maybe_list = PyArray_ToList(arr);
1630 if (PyList_Check(maybe_list)) {
1631 lis = py::reinterpret_steal<py::list>(maybe_list);
1632 }
1633 }
1634 if (get_ndim_safe(inp_hdl).first == 0) {
1635 if (lis.size() != 0) {
1636 throw py::index_error(
1637 "transpose for scalar does not accept additional args");
1638 }
1639 return getattr(inp_hdl, "to")(getattr(inp_hdl, "device"));
1640 }
1641 std::vector<int32_t> pattern;
1642 if (!lis.size()) {
1643 size_t ndim = getattr(inp_hdl, "ndim").cast<size_t>();
1644 for (size_t i = 0; i < ndim; ++i) {
1645 pattern.push_back(ndim - i - 1);
1646 }
1647 } else {
1648 for (size_t i = 0; i < lis.size(); ++i) {
1649 if (PyLong_Check(lis[i].ptr())) {
1650 pattern.push_back(lis[i].cast<int32_t>());
1651 } else {
1652 if (lis[i].cast<std::string>() == "x") {
1653 pattern.push_back(-1);
1654 }
1655 }
1656 }
1657 }
1658 std::shared_ptr<OpDef> op = Dimshuffle::make(pattern);
1659 py::object Op = py::cast(op);
1660 PyObject* p[2] = {Op.ptr(), inp_hdl.ptr()};
1661 py::tuple ret = py::reinterpret_steal<py::object>(py_apply(NULL, p, 2));
1662 return ret[0];
1663}
1664
1665py::object _matmul_cpp(
1666 py::handle inp1, py::handle inp2, py::handle dim1, py::handle dim2,

Callers 1

tensor_utils.cppFile · 0.85

Calls 9

is_tensorFunction · 0.85
get_ndim_safeFunction · 0.85
castFunction · 0.85
py_applyFunction · 0.85
_expand_argsFunction · 0.70
makeFunction · 0.50
ptrMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected