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

Function _expand_dims_cpp

imperative/python/src/tensor_utils.cpp:1539–1573  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1537}
1538
1539py::object _expand_dims_cpp(py::handle inp_hdl, py::handle axis_hdl) {
1540 std::vector<int32_t> axis = list2vector(axis_hdl);
1541 bool unknown_ndim = true;
1542 size_t ndim = axis.size();
1543 if (auto p = TensorWrapper::try_cast(inp_hdl.ptr())) {
1544 auto&& shape = p->m_tensor->shape();
1545 if (shape) {
1546 unknown_ndim = false;
1547 ndim += shape->ndim;
1548 }
1549 } else {
1550 auto&& inp_ndim = get_ndim_safe(inp_hdl);
1551 ndim += inp_ndim.first;
1552 unknown_ndim &= !inp_ndim.second;
1553 }
1554 for (size_t i = 0; i < axis.size(); ++i) {
1555 if (axis[i] < 0) {
1556 if (unknown_ndim) {
1557 throw py::index_error(
1558 "Does not support negative index when tensor's ndim is "
1559 "unknown");
1560 }
1561 axis[i] += static_cast<int32_t>(ndim);
1562 }
1563 }
1564 if (!axis.size()) {
1565 throw py::index_error("axis could not be empty");
1566 }
1567 std::sort(axis.begin(), axis.end());
1568 std::shared_ptr<OpDef> op = AddAxis::make(axis = axis);
1569 py::object Op = py::cast(op);
1570 PyObject* p[2] = {Op.ptr(), inp_hdl.ptr()};
1571 py::tuple ret = py::reinterpret_steal<py::object>(py_apply(NULL, p, 2));
1572 return ret[0];
1573}
1574
1575py::object _squeeze_cpp(py::handle inp_hdl, py::handle axis_hdl) {
1576 std::vector<int32_t> axis;

Callers 1

tensor_utils.cppFile · 0.85

Calls 12

list2vectorFunction · 0.85
try_castFunction · 0.85
get_ndim_safeFunction · 0.85
sortFunction · 0.85
castFunction · 0.85
py_applyFunction · 0.85
makeFunction · 0.50
sizeMethod · 0.45
ptrMethod · 0.45
shapeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected