| 287 | } |
| 288 | |
| 289 | NPY_NO_EXPORT PyObject * |
| 290 | from_dlpack(PyObject *NPY_UNUSED(self), PyObject *obj) { |
| 291 | PyObject *capsule = PyObject_CallMethod((PyObject *)obj->ob_type, |
| 292 | "__dlpack__", "O", obj); |
| 293 | if (capsule == NULL) { |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | DLManagedTensor *managed = |
| 298 | (DLManagedTensor *)PyCapsule_GetPointer(capsule, |
| 299 | NPY_DLPACK_CAPSULE_NAME); |
| 300 | |
| 301 | if (managed == NULL) { |
| 302 | Py_DECREF(capsule); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | const int ndim = managed->dl_tensor.ndim; |
| 307 | if (ndim > NPY_MAXDIMS) { |
| 308 | PyErr_SetString(PyExc_RuntimeError, |
| 309 | "maxdims of DLPack tensor is higher than the supported " |
| 310 | "maxdims."); |
| 311 | Py_DECREF(capsule); |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | DLDeviceType device_type = managed->dl_tensor.device.device_type; |
| 316 | if (device_type != kDLCPU && |
| 317 | device_type != kDLCUDAHost && |
| 318 | device_type != kDLROCMHost && |
| 319 | device_type != kDLCUDAManaged) { |
| 320 | PyErr_SetString(PyExc_RuntimeError, |
| 321 | "Unsupported device in DLTensor."); |
| 322 | Py_DECREF(capsule); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | if (managed->dl_tensor.dtype.lanes != 1) { |
| 327 | PyErr_SetString(PyExc_RuntimeError, |
| 328 | "Unsupported lanes in DLTensor dtype."); |
| 329 | Py_DECREF(capsule); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | int typenum = -1; |
| 334 | const uint8_t bits = managed->dl_tensor.dtype.bits; |
| 335 | const npy_intp itemsize = bits / 8; |
| 336 | switch (managed->dl_tensor.dtype.code) { |
| 337 | case kDLBool: |
| 338 | if (bits == 8) { |
| 339 | typenum = NPY_BOOL; |
| 340 | } |
| 341 | break; |
| 342 | case kDLInt: |
| 343 | switch (bits) |
| 344 | { |
| 345 | case 8: typenum = NPY_INT8; break; |
| 346 | case 16: typenum = NPY_INT16; break; |
nothing calls this directly
no test coverage detected