| 694 | } |
| 695 | |
| 696 | PyObject* TensorWrapper::numpy() { |
| 697 | auto hv = m_tensor->numpy(); |
| 698 | if (!hv) { |
| 699 | if (TransformationManager::get_instance() |
| 700 | .segments[TransformationManager::Segment::Eval] |
| 701 | .size() > 1) { |
| 702 | PyErr_SetString( |
| 703 | PyExc_ValueError, |
| 704 | "tensor invalid, can not infer value of this tensor under " |
| 705 | "trace(symbolic=True). You can try to use trace(symbolic=False) to " |
| 706 | "avoid this issue."); |
| 707 | } else { |
| 708 | PyErr_SetString(PyExc_ValueError, "tensor invalid"); |
| 709 | } |
| 710 | return nullptr; |
| 711 | } |
| 712 | auto arr = py::reinterpret_steal<py::array>( |
| 713 | npy::ndarray_from_tensor(hv->as_nd(true), npy::ShareType::TRY_SHARE)); |
| 714 | if (hv->shape().is_scalar()) { |
| 715 | mgb_assert(PyArray_Check(arr.ptr())); |
| 716 | return PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(arr.ptr())); |
| 717 | } |
| 718 | return arr.release().ptr(); |
| 719 | } |
| 720 | |
| 721 | void TensorWrapper::reset(PyObject* tensor) { |
| 722 | TensorWrapper* t = TensorWrapper::try_cast(tensor); |