Skip to content

Commit a0cbfd2

Browse files
authored
Merge pull request numpy#30989 from crawfordxx/doc-fix-dlpack-example-30936
DOC: clarify DLPack GPU tensor example with device='cpu' workaround
2 parents 581d10f + f603c46 commit a0cbfd2

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

doc/source/user/basics.interoperability.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,19 @@ will mean duplicating the memory. Do not do this for very large arrays:
487487

488488
.. note::
489489

490-
Note that GPU tensors can't be converted to NumPy arrays since NumPy doesn't
491-
support GPU devices:
490+
GPU tensors cannot be directly zero-copy converted to NumPy arrays since
491+
NumPy does not support GPU devices. However, since DLPack v1, cross-device
492+
copy is supported via the ``device`` parameter:
492493

493494
>>> x_torch = torch.arange(5, device='cuda')
494-
>>> np.from_dlpack(x_torch)
495+
>>> np.from_dlpack(x_torch) # fails: implicit device=None means same device
495496
Traceback (most recent call last):
496497
File "<stdin>", line 1, in <module>
497498
RuntimeError: Unsupported device in DLTensor.
499+
>>> np.from_dlpack(x_torch, device='cpu') # works: explicit copy to CPU
500+
array([0, 1, 2, 3, 4])
498501

499-
But, if both libraries support the device the data buffer is on, it is
502+
If both libraries support the device the data buffer is on, it is
500503
possible to use the ``__dlpack__`` protocol (e.g. PyTorch_ and CuPy_):
501504

502505
>>> x_torch = torch.arange(5, device='cuda')

0 commit comments

Comments
 (0)