StorageView

class ctranslate2.StorageView

An allocated buffer with shape information.

The object implements the Array Interface and the CUDA Array Interface so that it can be passed to Numpy or PyTorch without copy.

Example

>>> x = np.ones((2, 4), dtype=np.int32)
>>> y = ctranslate2.StorageView.from_array(x)
>>> print(y)
 1 1 1 ... 1 1 1
[cpu:0 int32 storage viewed as 2x4]
>>> z = np.array(y)
...
>>> x = torch.ones((2, 4), dtype=torch.int32, device="cuda")
>>> y = ctranslate2.StorageView.from_array(x)
>>> print(y)
 1 1 1 ... 1 1 1
[cuda:0 int32 storage viewed as 2x4]
>>> z = torch.as_tensor(y, device="cuda")

Inherits from: pybind11_builtins.pybind11_object

Attributes:

Methods:

static from_array(array: object) StorageView

Creates a StorageView from an object implementing the array interface.

Parameters

array – An object implementing the array interface (e.g. a Numpy array or a PyTorch Tensor).

Returns

A new StorageView instance sharing the same data as the input array.

Raises

ValueError – if the object does not implement the array interface or uses an unsupported array specification.

to(dtype: DataType) StorageView

Converts the storage to another type.

Parameters

dtype – The data type to convert to.

Returns

A new StorageView instance.

to_device(device: Device) StorageView

Converts the storage to another device.

Parameters

device – The device to copy the data to.

Returns

A new StorageView instance.

property device

Device where the storage is allocated (“cpu” or “cuda”).

property device_index

Device index.

property dtype

Data type used by the storage.

property shape

Shape of the storage view.