Method:
np.from_dlpack(x, /, *, device=None, copy=None)
Creates an array from a DLPack tensor.
Returns:
np.ndarray
Parameters:
x: DLPack tensor-
This is the input object that implements the DLPack protocol, such as a PyTorch tensor.
import numpy as np
import torch
# Creating a 2x2 tensor of zeros
tensor = torch.zeros((2,2))
arr = np.from_dlpack(tensor)
print(arr)
'''
Output:
[[0. 0.]
[0. 0.]]
'''
device: None, Optional-
In NumPy 2.1, the device parameter in np.from_dlpack() is still in an early stage and currently only accepts "cpu" as a valid option. At this time, it is primarily there for future development, allowing the function to eventually support GPUs or other devices, but for now, it doesn't support GPU IDs directly.
copy: None, Optional-
Specifies whether to create a copy of the tensor data or share memory.
Similar functionality as copy in np.array().