| 1020 | } |
| 1021 | |
| 1022 | py::object _reshape_cpp(py::handle inp_hdl, py::handle args) { |
| 1023 | py::object shape_hdl = _expand_args(args); |
| 1024 | py::object shape_tuple; |
| 1025 | try { |
| 1026 | shape_tuple = _make_shape_tuple(shape_hdl); |
| 1027 | } catch (py::error_already_set& err) { |
| 1028 | shape_tuple = py::reinterpret_borrow<py::object>(shape_hdl); |
| 1029 | } |
| 1030 | int32_t unspec_axis = -1; |
| 1031 | if (PyTuple_Check(shape_tuple.ptr())) { |
| 1032 | py::tuple tup = py::reinterpret_borrow<py::tuple>(shape_tuple); |
| 1033 | for (size_t i = 0; i < tup.size(); ++i) { |
| 1034 | py::object obj = py::reinterpret_borrow<py::object>(tup[i]); |
| 1035 | if (obj < py::int_(0)) { |
| 1036 | if (obj.not_equal(py::int_(-1))) { |
| 1037 | throw py::value_error( |
| 1038 | "expect shape [" + std::to_string(i) + "] >= -1, got " + |
| 1039 | repr(obj).cast<std::string>()); |
| 1040 | } |
| 1041 | if (unspec_axis >= 0) { |
| 1042 | throw py::value_error( |
| 1043 | "multiple -1 in shape: " + std::to_string(unspec_axis) + |
| 1044 | " & " + std::to_string(i)); |
| 1045 | } |
| 1046 | unspec_axis = i; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | auto [shape, fastpath] = tuple2vector(shape_tuple); |
| 1051 | fastpath &= enable_fastpath(inp_hdl); |
| 1052 | std::shared_ptr<OpDef> op; |
| 1053 | std::vector<PyObject*> p; |
| 1054 | py::object shape_tensor; |
| 1055 | if (fastpath) { |
| 1056 | if (unspec_axis >= 0) { |
| 1057 | op = Reshape::make(unspec_axis, shape); |
| 1058 | } else { |
| 1059 | op = Reshape::make(::megdnn::param::OptionalAxisV1::INVALID_AXIS, shape); |
| 1060 | } |
| 1061 | p.resize(2); |
| 1062 | } else { |
| 1063 | shape.clear(); |
| 1064 | if (unspec_axis >= 0) { |
| 1065 | op = Reshape::make(unspec_axis, shape); |
| 1066 | } else { |
| 1067 | op = Reshape::make(); |
| 1068 | } |
| 1069 | shape_tensor = _astensor1d_cpp( |
| 1070 | shape_hdl, py::cast((mgb::DType)dtype::Int32()), |
| 1071 | getattr(inp_hdl, "device"), inp_hdl); |
| 1072 | p.resize(3); |
| 1073 | p[2] = shape_tensor.ptr(); |
| 1074 | } |
| 1075 | py::object Op = py::cast(op); |
| 1076 | p[0] = Op.ptr(); |
| 1077 | p[1] = inp_hdl.ptr(); |
| 1078 | py::tuple ret = |
| 1079 | py::reinterpret_steal<py::object>(py_apply(NULL, p.data(), p.size())); |
no test coverage detected