Transform input to TensorTuple
| 33 | |
| 34 | // Transform input to TensorTuple |
| 35 | Maybe<one::TensorTuple> UnpackTensorTuple(const py::object& input) { |
| 36 | one::TensorTuple tp; |
| 37 | if (one::PyTensor_Check(input.ptr())) { |
| 38 | tp.emplace_back(input.cast<std::shared_ptr<one::Tensor>>()); |
| 39 | } else if (py::isinstance<py::tuple>(input)) { |
| 40 | auto tuple = input.cast<py::tuple>(); |
| 41 | tp.resize(tuple.size()); |
| 42 | for (int i = 0; i < tuple.size(); ++i) { |
| 43 | PyObject* obj = tuple[i].ptr(); |
| 44 | if (obj == Py_None) { |
| 45 | // do nothing |
| 46 | } else if (one::PyTensor_Check(obj)) { |
| 47 | tp[i] = one::PyTensor_Unpack(obj); |
| 48 | } else { |
| 49 | return Error::RuntimeError() |
| 50 | << "expected Tensor or None as element " << i << ", but got " |
| 51 | << one::functional::PyStringAsString(PyObject_Str((PyObject*)Py_TYPE(obj))); |
| 52 | } |
| 53 | } |
| 54 | } else { |
| 55 | return Error::RuntimeError() |
| 56 | << "autograd.Function's output only support tensor or list of tensors"; |
| 57 | } |
| 58 | return tp; |
| 59 | } |
| 60 | |
| 61 | // Return single Tensor when TensorTuple's size is one, otherwise py::tuple |
| 62 | py::object PackTensorTuple(const one::TensorTuple& tp) { |
no test coverage detected