dpnp.frombuffer

dpnp.frombuffer(buffer, dtype=<class 'float'>, count=-1, offset=0, *, like=None, device=None, usm_type='device', sycl_queue=None)[source]

Interpret a buffer as a 1-dimensional array.

For full documentation refer to numpy.frombuffer.

Parameters:
bufferbuffer_like

An object that exposes the buffer interface.

dtype{None, str, dtype object}, optional

Data-type of the returned array. If None, uses a default floating point data type for the device on which the returned array is allocated.

Default: float.

countint, optional

Number of items to read. -1 means all data in the buffer.

Default: -1.

offsetint, optional

Start reading the buffer from this offset (in bytes).

Default: 0.

device{None, string, SyclDevice, SyclQueue, Device}, optional

An array API concept of device where the output array is created. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a dpnp.tensor.Device object returned by dpnp.ndarray.device.

Default: None.

usm_type{None, "device", "shared", "host"}, optional

The type of SYCL USM allocation for the output array.

Default: "device".

sycl_queue{None, SyclQueue}, optional

A SYCL queue to use for output array allocation and copying. The sycl_queue can be passed as None (the default), which means to get the SYCL queue from device keyword if present or to use a default queue.

Default: None.

Returns:
outdpnp.ndarray

A 1-dimensional array created from input buffer object.

Limitations

Parameter like is supported only with default value None. Otherwise, the function raises NotImplementedError exception.

See also

dpnp.fromfile

Construct array from data in a text or binary file.

dpnp.fromiter

Construct array from an iterable object.

dpnp.fromstring

Construct array from the text data in a string.

ndarray.tobytes

Construct Python bytes from the raw data bytes in the array.

Notes

This uses numpy.frombuffer and coerces the result to a DPNP array.

Examples

>>> import dpnp as np
>>> s = b'\x01\x02\x03\x04'
>>> np.frombuffer(s, dtype=np.int32)
array([67305985], dtype=int32)
>>> np.frombuffer(b'\x01\x02\x03\x04\x05', dtype='u1', count=3)
array([1, 2, 3], dtype=uint8)

Creating an array on a different device or with a specified usm_type

>>> x = np.frombuffer(s, dtype=np.int32) # default case
>>> x.device, x.usm_type
(Device(level_zero:gpu:0), 'device')
>>> y = np.frombuffer(s, dtype=np.int32, device='cpu')
>>> y.device, y.usm_type
(Device(opencl:cpu:0), 'device')
>>> z = np.frombuffer(s, dtype=np.int32, usm_type="host")
>>> z.device, z.usm_type
(Device(level_zero:gpu:0), 'host')