| 1537 | } |
| 1538 | |
| 1539 | py::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 | |
| 1575 | py::object _squeeze_cpp(py::handle inp_hdl, py::handle axis_hdl) { |
| 1576 | std::vector<int32_t> axis; |
no test coverage detected