gt4py.storage package

Subpackages

Submodules

gt4py.storage.allocators module

class gt4py.storage.allocators.ArrayUtils(array_ns: 'types.ModuleType', empty: 'Callable[..., _NDBuffer]', byte_bounds: 'Callable[[_NDBuffer], tuple[int, int]]', as_strided: 'Callable[..., core_defs.NDArrayObject]')[source]

Bases: object

array_ns: ModuleType
as_strided: Callable[[...], NDArrayObject]
byte_bounds: Callable[[Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer], tuple[int, int]]
empty: Callable[[...], Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer]
class gt4py.storage.allocators.BufferAllocator(*args, **kwargs)[source]

Bases: Protocol[DeviceTypeT]

Protocol for buffer allocators.

_abc_impl = <_abc._abc_data object>
_is_protocol = True
allocate(shape: Sequence[int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64], dtype: DType[ScalarT], device_id: int, layout_map: BufferLayoutMap, byte_alignment: int, aligned_index: Sequence[int] | None = None) → TensorBuffer[DeviceTypeT, ScalarT][source]

Allocate a TensorBuffer with the given shape, layout and alignment settings.

Parameters:
  • shape – Tensor dimensions.

  • dtype – Data type descriptor.

  • layout_map – layout of the dimensions in a buffer with C-layout (contiguous dimension is last). layout_map[i] = j means that the i-th dimension of the tensor corresponds to the j-th dimension of the buffer.

  • device_id – Id of the device of device_type where the buffer is allocated.

  • byte_alignment – Alignment (in bytes) of the first valid element.

  • aligned_index – N-dimensional index of the first aligned element.

property device_type: DeviceTypeT
class gt4py.storage.allocators.BufferLayoutMap

Tuple of positive integers encoding a permutation of the dimensions, such that layout_map[i] = j means that the i-th dimension of the tensor corresponds to the j-th dimension in the (C-layout) buffer.

alias of Sequence[PositiveIntegral]

class gt4py.storage.allocators.NDArrayBufferAllocator(device_type: 'core_defs.DeviceTypeT', array_utils: 'ArrayUtils')[source]

Bases: _BaseNDArrayBufferAllocator[DeviceTypeT]

_abc_impl = <_abc._abc_data object>
_array_utils: ArrayUtils
_device_type: DeviceTypeT
property array_utils: ArrayUtils
property device_type: DeviceTypeT
malloc(length: int, device_id: int) → Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer[source]
tensorize(buffer: Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer, dtype: DType[ScalarT], shape: Sequence[int], allocated_shape: Sequence[int], item_size: int, strides: Sequence[int], byte_offset: int) → NDArrayObject[source]
class gt4py.storage.allocators.TensorBuffer(buffer: Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer, memory_address: int, device: Device[DeviceTypeT], dtype: DType[ScalarT], shape: Sequence[int], strides: tuple[int, ...], layout_map: BufferLayoutMap, byte_offset: int, byte_alignment: int, aligned_index: tuple[int, ...], ndarray: NDArrayObject)[source]

Bases: Generic[DeviceTypeT, ScalarT]

N-dimensional (tensor-like) memory buffer.

The actual class of the stored buffer and ndarray instances is represented in the NDBufferT parameter and might be any n-dimensional buffer-like class with a compatible buffer interface (e.g. NumPy or CuPy ndarray.)

buffer

Raw allocated buffer.

Type:

collections.abc.Buffer | gt4py.eve.xtyping.ArrayInterface | gt4py.eve.xtyping.CUDAArrayInterface | gt4py.eve.xtyping.MultiStreamDLPackBuffer | gt4py.eve.xtyping.SingleStreamDLPackBuffer

memory_address

Memory address of the buffer.

Type:

int

device

Device where the buffer is allocated.

Type:

gt4py._core.definitions.Device[gt4py._core.definitions.DeviceTypeT]

dtype

Data type descriptor.

Type:

gt4py._core.definitions.DType[gt4py._core.definitions.ScalarT]

shape

Tuple with lengths of the corresponding tensor dimensions.

Type:

collections.abc.Sequence[int]

strides

Tuple with sizes (in bytes) of the steps in each dimension.

Type:

tuple[int, …]

layout_map

Tuple with the order of the dimensions in the buffer layout_map[i] = j means that the i-th dimension of the tensor corresponds to the j-th dimension in the (C-layout) buffer.

Type:

gt4py.storage.allocators.BufferLayoutMap

byte_offset

Offset (in bytes) from the beginning of the buffer to the first valid element.

Type:

int

byte_alignment

Alignment (in bytes) of the first valid element.

Type:

int

aligned_index

N-dimensional index of the first aligned element.

Type:

tuple[int, …]

ndarray

N-dimensional tensor view of the allocated buffer.

Type:

gt4py._core.definitions.NDArrayObject

aligned_index: tuple[int, ...]
buffer: Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer
byte_alignment: int
byte_offset: int
device: Device[DeviceTypeT]
dtype: DType[ScalarT]
layout_map: BufferLayoutMap
memory_address: int
ndarray: NDArrayObject
property ndim

Order of the tensor (len(tensor_buffer.shape)).

shape: Sequence[int]
strides: tuple[int, ...]
class gt4py.storage.allocators._BaseNDArrayBufferAllocator[source]

Bases: ABC, Generic[DeviceTypeT]

Base class for buffer allocators using NumPy-like modules.

_abc_impl = <_abc._abc_data object>
allocate(shape: Sequence[int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64], dtype: DType[ScalarT], device_id: int, layout_map: BufferLayoutMap, byte_alignment: int, aligned_index: Sequence[int] | None = None) → TensorBuffer[DeviceTypeT, ScalarT][source]
abstract property array_utils: ArrayUtils
abstract property device_type: DeviceTypeT
abstractmethod malloc(length: int, device_id: int) → Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer[source]
abstractmethod tensorize(buffer: Buffer | ArrayInterface | CUDAArrayInterface | MultiStreamDLPackBuffer | SingleStreamDLPackBuffer, dtype: DType[ScalarT], shape: Sequence[int], allocated_shape: Sequence[int], item_size: int, strides: Sequence[int], byte_offset: int) → NDArrayObject[source]
gt4py.storage.allocators.is_valid_layout_map(value: Sequence[Any]) → TypeGuard[BufferLayoutMap][source]

Module contents

GridTools storages utilities.

gt4py.storage.empty(shape: Sequence[int], dtype: DTypeLike = <class 'numpy.float64'>, *, backend: str, aligned_index: Optional[Sequence[int]] = None, dimensions: Optional[Sequence[str]] = None) → np.ndarray | 'cp.ndarray'[source]

Allocate an array of uninitialized (undefined) values with performance-optimal strides and alignment.

Parameters:
  • shape (Sequence of int) – The shape of the resulting ndarray

  • dtype (DTypeLike, optional) – The dtype of the resulting ndarray

Keyword Arguments:
  • backend (str) – The target backend for which the allocation is optimized.

  • aligned_index (Sequence of int, optional) – Indicate the index of the resulting array that most commonly corresponds to the origin of the compute domain. If not passed, it is aligned at the data origin.

  • dimensions (Sequence of str, optional) – Indicate the semantic meaning of the dimensions in the provided array. Only used for determining optimal strides, the information is not stored.

Returns:

With uninitialized values, padded and aligned to provide optimal performance for the given backend and aligned_index

Return type:

NumPy or CuPy ndarray

Raises:
  • TypeError – If arguments of an unexpected type are specified.

  • ValueError – If illegal or inconsistent arguments are specified.

gt4py.storage.from_array(data: ArrayLike, dtype: DTypeLike = <class 'numpy.float64'>, *, backend: str, aligned_index: Sequence[int] | None = None, dimensions: Sequence[str] | None = None) → np.ndarray | 'cp.ndarray'[source]

Allocate an array with values initialized to those of data with performance-optimal strides and alignment.

This copies the values from data to the resulting buffer.

Parameters:
  • data (ArrayLike) – Source data to be copied, from which also the shape of the result is derived.

  • dtype (DTypeLike, optional) – The dtype of the resulting ndarray

Keyword Arguments:
  • backend (str) – The target backend for which the allocation is optimized.

  • aligned_index (Sequence of int, optional) – Indicate the index of the resulting array that most commonly corresponds to the origin of the compute domain. If not passed, it is aligned at the data origin.

  • dimensions (Sequence of str, optional) – Indicate the semantic meaning of the dimensions in the provided array. Only used for determining optimal strides, the information is not stored.

Returns:

Copy of data, padded and aligned to provide optimal performance for the given backend and aligned_index

Return type:

NumPy or CuPy ndarray

Raises:
  • TypeError – If arguments of an unexpected type are specified.

  • ValueError – If illegal or inconsistent arguments are specified.

gt4py.storage.from_name(name: str) → LayoutInfo[source]
gt4py.storage.full(shape: Sequence[int], fill_value: numbers.Number, dtype: DTypeLike = <class 'numpy.float64'>, *, backend: str, aligned_index: Optional[Sequence[int]] = None, dimensions: Optional[Sequence[str]] = None) → np.ndarray | 'cp.ndarray'[source]

Allocate an array with values initialized to fill_value with performance-optimal strides and alignment.

Parameters:
  • shape (Sequence of int) – The shape of the resulting ndarray

  • fill_value (numbers.Number) – The value to which the array elements are initialized.

  • dtype (DTypeLike, optional) – The dtype of the resulting ndarray

Keyword Arguments:
  • backend (str) – The target backend for which the allocation is optimized.

  • aligned_index (Sequence of int, optional) – Indicate the index of the resulting array that most commonly corresponds to the origin of the compute domain. If not passed, it is aligned at the data origin.

  • dimensions (Sequence of str, optional) – Indicate the semantic meaning of the dimensions in the provided array. Only used for determining optimal strides, the information is not stored.

Returns:

Initialized to fill_value, padded and aligned to provide optimal performance for the given backend and aligned_index

Return type:

NumPy or CuPy ndarray

Raises:
  • TypeError – If arguments of an unexpected type are specified.

  • ValueError – If illegal or inconsistent arguments are specified.

gt4py.storage.ones(shape: Sequence[int], dtype: DTypeLike = <class 'numpy.float64'>, *, backend: str, aligned_index: Optional[Sequence[int]] = None, dimensions: Optional[Sequence[str]] = None) → np.ndarray | 'cp.ndarray'[source]

Allocate an array with values initialized to 1.0 with performance-optimal strides and alignment.

Parameters:
  • shape (Sequence of int) – The shape of the resulting ndarray

  • dtype (DTypeLike, optional) – The dtype of the resulting ndarray

Keyword Arguments:
  • backend (str) – The target backend for which the allocation is optimized.

  • aligned_index (Sequence of int, optional) – Indicate the index of the resulting array that most commonly corresponds to the origin of the compute domain. If not passed, it is aligned at the data origin.

  • dimensions (Sequence of str, optional) – Indicate the semantic meaning of the dimensions in the provided array. Only used for determining optimal strides, the information is not stored.

Returns:

Initialized to 1.0, padded and aligned to provide optimal performance for the given backend and aligned_index

Return type:

NumPy or CuPy ndarray

Raises:
  • TypeError – If arguments of an unexpected type are specified.

  • ValueError – If illegal or inconsistent arguments are specified.

gt4py.storage.register(name: str, info: LayoutInfo) → None[source]
gt4py.storage.zeros(shape: Sequence[int], dtype: DTypeLike = <class 'numpy.float64'>, *, backend: str, aligned_index: Optional[Sequence[int]] = None, dimensions: Optional[Sequence[str]] = None) → np.ndarray | 'cp.ndarray'[source]

Allocate an array with values initialized to 0.0 with performance-optimal strides and alignment.

Parameters:
  • shape (Sequence of int) – The shape of the resulting ndarray

  • dtype (DTypeLike, optional) – The dtype of the resulting ndarray

Keyword Arguments:
  • backend (str) – The target backend for which the allocation is optimized.

  • aligned_index (Sequence of int, optional) – Indicate the index of the resulting array that most commonly corresponds to the origin of the compute domain. If not passed, it is aligned at the data origin.

  • dimensions (Sequence of str, optional) – Indicate the semantic meaning of the dimensions in the provided array. Only used for determining optimal strides, the information is not stored.

Returns:

Initialized to 0.0, padded and aligned to provide optimal performance for the given backend and aligned_index

Return type:

NumPy or CuPy ndarray

Raises:
  • TypeError – If arguments of an unexpected type are specified.

  • ValueError – If illegal or inconsistent arguments are specified.