flopscope.

flopscope.numpy.from_dlpack

fnp.from_dlpack(*args, **kwargs)[flopscope source]

Create a NumPy array from an object implementing the ``__dlpack__`` protocol. Generally, the returned NumPy array is a view of the input object. See [1]_ and [2]_ for more details.

Adapted from NumPy docs np.from_dlpack

Areacore
Typefree
Cost
0
Flopscope Context

Create ndarray from DLPack object (zero-copy). Cost: numel(output).

Create a NumPy array from an object implementing the __dlpack__ protocol. Generally, the returned NumPy array is a view of the input object. See [1]_ and [2]_ for more details.

Parameters

x:object

A Python object that implements the __dlpack__ and __dlpack_device__ methods.

device:device, optional

Device on which to place the created array. Default: None. Must be "cpu" if passed which may allow importing an array that is not already CPU available.

copy:bool, optional

Boolean indicating whether or not to copy the input. If True, the copy will be made. If False, the function will never copy, and will raise BufferError in case a copy is deemed necessary. Passing it requests a copy from the exporter who may or may not implement the capability. If None, the function will reuse the existing memory buffer if possible and copy otherwise. Default: None.

Returns

out:ndarray

References

footnote
1

Array API documentation,
https://data-apis.org/array-api/latest/design_topics/data_interchange.html#syntax-for-data-interchange-with-dlpack
footnote
2

Python specification for DLPack,
https://dmlc.github.io/dlpack/latest/python_spec.html

Examples

>>> import torch  # doctest: +SKIP
>>> x = torch.arange(10)  # doctest: +SKIP
>>> # create a view of the torch tensor "x" in NumPy
>>> y = flops.from_dlpack(x)  # doctest: +SKIP