| 386 | } |
| 387 | |
| 388 | py::object get_res_by_refhdl( |
| 389 | py::handle value, py::handle dtype, py::handle device, py::handle ref_hdl) { |
| 390 | py::object res = _Const(value, dtype, device); |
| 391 | py::object ref; |
| 392 | if (py::isinstance<py::tuple>(ref_hdl)) { |
| 393 | py::tuple tup = py::reinterpret_borrow<py::tuple>(ref_hdl); |
| 394 | if (tup.size()) { |
| 395 | ref = tup[0]; |
| 396 | } else { |
| 397 | ref = py::none(); |
| 398 | } |
| 399 | } else { |
| 400 | ref = py::reinterpret_borrow<py::object>(ref_hdl); |
| 401 | } |
| 402 | if (PyObject_TypeCheck(ref.ptr(), py_varnode_type)) { |
| 403 | ComputingGraph* graph = getattr(ref, "graph").cast<ComputingGraph*>(); |
| 404 | cg::VarNode* node = getattr(ref, "var").cast<cg::VarNode*>(); |
| 405 | CompNode cn; |
| 406 | if (device.ptr() == Py_None) { |
| 407 | cn = node->comp_node(); |
| 408 | } else { |
| 409 | cn = device2obj(device).cast<CompNode>(); |
| 410 | } |
| 411 | OperatorNodeConfig config(cn); |
| 412 | auto hv = npy::np2tensor( |
| 413 | value.ptr(), npy::Meth::borrow(cn), dtype.cast<mgb::DType>()); |
| 414 | auto typeobj = ref.get_type(); |
| 415 | return typeobj(opr::ImmutableTensor::make(*graph, hv, config).node()); |
| 416 | } |
| 417 | return res; |
| 418 | } |
| 419 | |
| 420 | mgb::DType _get_dtype(py::handle tensor) { |
| 421 | auto* tw = TensorWrapper::try_cast(tensor.ptr()); |
no test coverage detected