| 525 | } // namespace |
| 526 | |
| 527 | TensorWrapper::TensorWrapper(PyObject* args, PyObject* kwargs) { |
| 528 | static PyArgDescs descs = { |
| 529 | { |
| 530 | {"data", []() -> py::object { return py::none(); }}, |
| 531 | {"dtype", []() -> py::object { return py::none(); }}, |
| 532 | {"device", []() -> py::object { return py::none(); }}, |
| 533 | {"is_const", []() -> py::object { return py::bool_(false); }}, |
| 534 | {"no_cache", []() -> py::object { return py::bool_(false); }}, |
| 535 | {"name", []() -> py::object { return py::none(); }}, |
| 536 | {"format", []() -> py::object { return py::none(); }}, |
| 537 | }, |
| 538 | name2idx}; |
| 539 | py::detail::loader_life_support life_sup; // FIXME!!!required to cast DType |
| 540 | auto tup = py::reinterpret_borrow<py::tuple>(args); |
| 541 | if (kwargs) { |
| 542 | tup = parse_args_and_kwargs( |
| 543 | tup, py::reinterpret_borrow<py::dict>(kwargs), descs); |
| 544 | } else { |
| 545 | tup = parse_args(tup, descs); |
| 546 | } |
| 547 | mgb_assert(tup.size() == 7); |
| 548 | if (auto* t = try_cast(tup[0].ptr())) { |
| 549 | m_tensor = t->m_tensor; |
| 550 | // TODO: merge two path in arg parse |
| 551 | if (!tup[1].is_none()) { |
| 552 | auto dtype = tup[1].cast<DType>(); |
| 553 | mgb_assert( |
| 554 | dtype == m_tensor->dtype(), "dtype mismatch: %s vs %s", |
| 555 | dtype.name(), m_tensor->dtype().name()); |
| 556 | } |
| 557 | if (!tup[2].is_none()) { |
| 558 | auto device = as_comp_node(tup[2]); |
| 559 | mgb_assert( |
| 560 | device == m_tensor->comp_node(), "device mismatch: %s vs %s", |
| 561 | device.to_string().c_str(), |
| 562 | m_tensor->comp_node().to_string().c_str()); |
| 563 | } |
| 564 | mgb_assert(!tup[3].cast<bool>(), "expect is_const == False, got True"); |
| 565 | bool no_cache = tup[4].cast<bool>(); |
| 566 | if (no_cache) { |
| 567 | // always copy because it's hard to tell whether this tensor is cached |
| 568 | m_tensor = m_tensor->copy(); |
| 569 | } |
| 570 | // ignore name |
| 571 | if (!tup[6].is_none()) { |
| 572 | Format format = tup[6].cast<std::string>(); |
| 573 | mgb_assert( |
| 574 | format == m_tensor->format(), "format mismatch: %s vs %s", |
| 575 | format.to_string().c_str(), m_tensor->format().to_string().c_str()); |
| 576 | } |
| 577 | } else { |
| 578 | auto data = tup[0]; |
| 579 | DType dtype = tup[1].cast<DType>(); |
| 580 | CompNode cn = as_comp_node(tup[2]); |
| 581 | bool is_const = tup[3].cast<bool>(); |
| 582 | bool no_cache = tup[4].cast<bool>(); |
| 583 | std::string name; |
| 584 | if (!tup[5].is_none()) { |
nothing calls this directly
no test coverage detected