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

Function _squeeze_cpp

imperative/python/src/tensor_utils.cpp:1575–1619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1573}
1574
1575py::object _squeeze_cpp(py::handle inp_hdl, py::handle axis_hdl) {
1576 std::vector<int32_t> axis;
1577 size_t ndim;
1578 if (axis_hdl.ptr() != Py_None) {
1579 axis = list2vector(axis_hdl);
1580 }
1581 if (auto p = TensorWrapper::try_cast(inp_hdl.ptr())) {
1582 auto&& shape = p->m_tensor->shape();
1583 if (shape) {
1584 ndim = shape->ndim;
1585 if (axis_hdl.ptr() == Py_None) {
1586 for (size_t i = 0; i < shape->ndim; ++i) {
1587 if (shape->shape[i] == 1) {
1588 axis.push_back(i);
1589 }
1590 }
1591 }
1592 }
1593 } else {
1594 py::tuple shape =
1595 py::reinterpret_borrow<py::tuple>(getattr(inp_hdl, "_tuple_shape"));
1596 ndim = shape.size();
1597 if (axis_hdl.ptr() == Py_None) {
1598 for (size_t i = 0; i < shape.size(); ++i) {
1599 if (shape[i].cast<size_t>() == 1) {
1600 axis.push_back(i);
1601 }
1602 }
1603 }
1604 }
1605 for (size_t i = 0; i < axis.size(); ++i) {
1606 if (axis[i] < 0) {
1607 axis[i] += static_cast<int32_t>(ndim);
1608 }
1609 }
1610 std::sort(axis.begin(), axis.end());
1611 for (size_t i = 0; i < axis.size(); ++i) {
1612 axis[i] -= static_cast<int32_t>(i);
1613 }
1614 std::shared_ptr<OpDef> op = RemoveAxis::make(axis = axis);
1615 py::object Op = py::cast(op);
1616 PyObject* p[2] = {Op.ptr(), inp_hdl.ptr()};
1617 py::tuple ret = py::reinterpret_steal<py::object>(py_apply(NULL, p, 2));
1618 return ret[0];
1619}
1620
1621py::object _transpose_cpp(py::handle inp_hdl, py::handle args) {
1622 py::object obj = _expand_args(args);

Callers 1

tensor_utils.cppFile · 0.85

Calls 12

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

Tested by

no test coverage detected