gt4py._core package

Submodules

gt4py._core.definitions module

class gt4py._core.definitions.BoolDType(tensor_shape: 'TensorShape' = ())[source]

Bases: DType[bool]

scalar_type

alias of bool

class gt4py._core.definitions.BooleanIntegral[source]

Bases: Integral

Abstract base class for boolean integral types.

_abc_impl = <_abc._abc_data object>
gt4py._core.definitions.CUPY_DEVICE_TYPE: Literal[None, DeviceType.CUDA, DeviceType.ROCM] = None

Type of the GPU accelerator device, if present.

class gt4py._core.definitions.DType(scalar_type: type[ScalarT], tensor_shape: Sequence[int] = ())[source]

Bases: Generic[ScalarT]

Descriptor of data type for Field elements.

This definition is based on DLPack and Array API standards. The data type should be interpreted as packed lanes repetitions of elements from kind data-category of bit_width width.

The Array API standard only requires DTypes to be comparable with __eq__.

Additionally, instances of this class can also be used as valid NumPy dtype`s definitions due to the `.dtype attribute.

property bit_width: int
property byte_size: int[source]
property dtype: dtype[source]

The NumPy dtype corresponding to this DType.

property kind: DTypeKind[source]
property lanes: int
scalar_type: type[ScalarT]
property subndim: int
tensor_shape: Sequence[int] = ()
class gt4py._core.definitions.DTypeKind(*values)[source]

Bases: StrEnum

Kind of a specific data type.

Character codes match the type code for the corresponding kind in the array interface protocol (https://numpy.org/doc/stable/reference/arrays.interface.html).

BOOL = 'b'
COMPLEX = 'c'
FLOAT = 'f'
INT = 'i'
OPAQUE_POINTER = 'V'
UINT = 'u'
class gt4py._core.definitions.Device(device_type: DeviceTypeT, device_id: int)[source]

Bases: Generic[DeviceTypeT]

Representation of a computing device.

This definition is based on the DLPack device definition. A device is described by a pair of DeviceType and device_id. The device_id is an integer that is interpreted differently depending on the DeviceType. For example, for DeviceType.CPU it could be the CPU core number, for DeviceType.CUDA it could be the CUDA device number, etc.

device_id: int
device_type: DeviceTypeT
classmethod from_dlpack_device(dlpack_device: tuple[DeviceTypeT, int]) → Self[source]
class gt4py._core.definitions.DeviceType(*values)[source]

Bases: IntEnum

The type of the device where a memory buffer is allocated.

Enum values taken from DLPack reference implementation at: https://github.com/dmlc/dlpack/blob/main/include/dlpack/dlpack.h

CPU = 1
CUDA = 2
ROCM = 10
class gt4py._core.definitions.Float32DType(tensor_shape: 'TensorShape' = ())[source]

Bases: FloatingDType[float32]

scalar_type

alias of float32

class gt4py._core.definitions.Float64DType(tensor_shape: 'TensorShape' = ())[source]

Bases: FloatingDType[float64]

scalar_type

alias of float64

class gt4py._core.definitions.FloatingDType(scalar_type: 'type[ScalarT]', tensor_shape: 'TensorShape' = ())[source]

Bases: DType[FloatingT]

class gt4py._core.definitions.GTDimsInterface(*args, **kwargs)[source]

Bases: Protocol

A GTDimsInterface is an object providing the __gt_dims__ property, naming the buffer dimensions.

In gt4py.cartesian the allowed values are “I”, “J” and “K” with the established semantics.

See Dimension Mapping for details.

_abc_impl = <_abc._abc_data object>
_is_protocol = True
class gt4py._core.definitions.GTOriginInterface(*args, **kwargs)[source]

Bases: Protocol

A GTOriginInterface is an object providing __gt_origin__, describing the origin of a buffer.

See Default Origin for details.

_abc_impl = <_abc._abc_data object>
_is_protocol = True
class gt4py._core.definitions.Int16DType(tensor_shape: 'TensorShape' = ())[source]

Bases: SignedIntDType[int16]

scalar_type

alias of int16

class gt4py._core.definitions.Int32DType(tensor_shape: 'TensorShape' = ())[source]

Bases: SignedIntDType[int32]

scalar_type

alias of int32

class gt4py._core.definitions.Int64DType(tensor_shape: 'TensorShape' = ())[source]

Bases: SignedIntDType[int64]

scalar_type

alias of int64

class gt4py._core.definitions.Int8DType(tensor_shape: 'TensorShape' = ())[source]

Bases: SignedIntDType[int8]

scalar_type

alias of int8

class gt4py._core.definitions.IntegerDType(scalar_type: 'type[ScalarT]', tensor_shape: 'TensorShape' = ())[source]

Bases: DType[IntegralT]

class gt4py._core.definitions.NDArrayObject(*args, **kwargs)[source]

Bases: Protocol

_abc_impl = <_abc._abc_data object>
_is_protocol = True
any() → bool[source]
astype(dtype: DTypeLike) → NDArrayObject[source]
property dtype: Any
item() → Any[source]
property itemsize: int
property ndim: int
property shape: tuple[int, ...]
property strides: tuple[int, ...]
class gt4py._core.definitions.PositiveIntegral[source]

Bases: Integral

Abstract base class representing positive integral numbers..

_abc_impl = <_abc._abc_data object>
gt4py._core.definitions.Scalar: TypeAlias = numpy.bool | bool | numpy.int8 | numpy.int16 | numpy.int32 | numpy.int64 | int | numpy.uint8 | numpy.uint16 | numpy.uint32 | numpy.uint64 | numpy.float32 | numpy.float64 | float

Type alias for all scalar types supported by GT4Py

class gt4py._core.definitions.SignedIntDType(scalar_type: 'type[ScalarT]', tensor_shape: 'TensorShape' = ())[source]

Bases: DType[IntT]

class gt4py._core.definitions.UInt16DType(tensor_shape: 'TensorShape' = ())[source]

Bases: UnsignedIntDType[uint16]

scalar_type

alias of uint16

class gt4py._core.definitions.UInt32DType(tensor_shape: 'TensorShape' = ())[source]

Bases: UnsignedIntDType[uint32]

scalar_type

alias of uint32

class gt4py._core.definitions.UInt64DType(tensor_shape: 'TensorShape' = ())[source]

Bases: UnsignedIntDType[uint64]

scalar_type

alias of uint64

class gt4py._core.definitions.UInt8DType(tensor_shape: 'TensorShape' = ())[source]

Bases: UnsignedIntDType[uint8]

scalar_type

alias of uint8

class gt4py._core.definitions.UnsignedIntDType(scalar_type: 'type[ScalarT]', tensor_shape: 'TensorShape' = ())[source]

Bases: DType[UnsignedIntT]

gt4py._core.definitions.dtype(dtype_like: DType | DTypeLike) → DType[source]

Return the DType corresponding to the given dtype-like object.

gt4py._core.definitions.dtype_kind(sc_type: type[IntT] | type[BoolT]) → Literal[DTypeKind.INT, DTypeKind.BOOL][source]
gt4py._core.definitions.dtype_kind(sc_type: type[UnsignedIntT]) → Literal[DTypeKind.UINT]
gt4py._core.definitions.dtype_kind(sc_type: type[FloatingT]) → Literal[DTypeKind.FLOAT]
gt4py._core.definitions.dtype_kind(sc_type: type[ScalarT]) → DTypeKind

Return the data type kind of the given scalar type.

gt4py._core.definitions.gpu_device_count() → int[source]

Return how many GPU devices are usable, or 0 if there are none.

A cupy install alone does not imply a usable GPU: the wheels install cleanly on machines with no driver and no visible device.

Returns:

The number of visible GPU devices, or 0 when cupy is missing or the driver cannot be queried.

Note

Enumerating is what makes the “installed but no visible device” case answerable at all: it reports 0. A cupy.cuda.Device() probe only resolves the current device and says nothing about how many exist.

gt4py._core.definitions.is_boolean_integral_type(integral_type: type) → TypeGuard[type[BooleanIntegral]][source]
gt4py._core.definitions.is_positive_integral_type(integral_type: type) → TypeGuard[type[PositiveIntegral]][source]
gt4py._core.definitions.is_scalar_type(t: Any) → TypeGuard[bool | bool | int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64 | float32 | float64 | float][source]
gt4py._core.definitions.is_valid_tensor_shape(value: Sequence[int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64]) → TypeGuard[Sequence[int]][source]

gt4py._core.dlpack module

The DLTensor of any array that exports DLPack, read from its capsule with ctypes.

The binary layout follows dlpack.h of DLPack v1.4 (https://github.com/dmlc/dlpack/blob/v1.4/include/dlpack/dlpack.h, documented at https://dmlc.github.io/dlpack/latest/c_api.html); the exchange protocol is the Python specification of DLPack (https://dmlc.github.io/dlpack/latest/python_spec.html) and the Array API __dlpack__ method (https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html).

Supported capsules: - “dltensor_versioned” (DLManagedTensorVersioned, DLPack >= 1.0) of ABI major version 1,

any minor version. It is requested with max_version=(1, 0).

  • “dltensor” (legacy DLManagedTensor, DLPack < 1.0), which producers without max_version support, and JAX, return.

Rejected: - BufferError: versioned capsules of major version 2 or later, and exports that need a copy. - TypeError: arrays without __dlpack__/__dlpack_device__.

class gt4py._core.dlpack.DataTypeCode(*values)[source]

Bases: IntEnum

DLDataTypeCode values up to kDLBool.

BFLOAT = 4
BOOL = 6
COMPLEX = 5
FLOAT = 2
INT = 0
OPAQUE_HANDLE = 3
UINT = 1
class gt4py._core.dlpack.DeviceType(*values)[source]

Bases: IntEnum

The DLDeviceType values this module needs to name.

CPU = 1
CUDA = 2
ROCM = 10
class gt4py._core.dlpack._DLDataType[source]

Bases: Structure

_fields_ = (('code', <class 'ctypes.c_ubyte'>), ('bits', <class 'ctypes.c_ubyte'>), ('lanes', <class 'ctypes.c_ushort'>))
bits

Structure/Union member

code

Structure/Union member

lanes

Structure/Union member

class gt4py._core.dlpack._DLDevice[source]

Bases: Structure

_fields_ = (('device_type', <class 'ctypes.c_int'>), ('device_id', <class 'ctypes.c_int'>))
device_id

Structure/Union member

device_type

Structure/Union member

class gt4py._core.dlpack._DLManagedTensorVersioned[source]

Bases: Structure

_fields_ = (('version', <class 'gt4py._core.dlpack._DLPackVersion'>), ('manager_ctx', <class 'ctypes.c_void_p'>), ('deleter', <class 'ctypes.c_void_p'>), ('flags', <class 'ctypes.c_ulong'>), ('dl_tensor', <class 'gt4py._core.dlpack._DLTensor'>))
deleter

Structure/Union member

dl_tensor

Structure/Union member

flags

Structure/Union member

manager_ctx

Structure/Union member

version

Structure/Union member

class gt4py._core.dlpack._DLPackVersion[source]

Bases: Structure

_fields_ = (('major', <class 'ctypes.c_uint'>), ('minor', <class 'ctypes.c_uint'>))
major

Structure/Union member

minor

Structure/Union member

class gt4py._core.dlpack._DLTensor[source]

Bases: Structure

_fields_ = (('data', <class 'ctypes.c_void_p'>), ('device', <class 'gt4py._core.dlpack._DLDevice'>), ('ndim', <class 'ctypes.c_int'>), ('dtype', <class 'gt4py._core.dlpack._DLDataType'>), ('shape', <class 'ctypes.LP_c_long'>), ('strides', <class 'ctypes.LP_c_long'>), ('byte_offset', <class 'ctypes.c_ulong'>))
byte_offset

Structure/Union member

data

Structure/Union member

device

Structure/Union member

dtype

Structure/Union member

ndim

Structure/Union member

shape

Structure/Union member

strides

Structure/Union member

gt4py._core.dlpack.tensor_fields(array: Any) → dict[str, Any][source]

The DLTensor fields of the DLPack export of array, read without consuming the capsule.

The keys are the DLTensor field names, with the values as exported: data (address, 0 for NULL), device as (device_type, device_id), ndim, dtype as (code, bits, lanes), shape, strides in elements (None for NULL, which DLPack before 1.2 allows for C-contiguous tensors) and byte_offset.

On CUDA and ROCm the capsule is requested without stream synchronization (stream=-1), the only devices for which the Array API standard defines that request; all other devices get stream=None, which the standard requires on CPU and on devices without streams. The result describes where the data lives, not when it is ready.

Raises:
  • TypeError – array does not export DLPack.

  • BufferError – array cannot be exported without a copy, or the capsule has an unsupported DLPack major version.

gt4py._core.file_utils module

Helpers for atomic file writes.

gt4py._core.file_utils.atomic_write_bytes(target: str | PathLike, data: bytes) → None[source]

Write data to target atomically.

The bytes are written to a temporary file in target’s directory and then renamed into place with os.replace. A reader (or the next process after a crash) therefore sees either the previous contents or the complete new contents, never a half-written file. The temporary file is a sibling of target so the rename stays on a single filesystem, and is created with a plain open so the result gets the usual umask-derived permissions (tempfile.mkstemp would pin it to 0600).

This protects against process kill / Ctrl-C / OOM during the write. It does not fsync, so a power loss may still lose the entry (callers tolerate this by treating an unreadable entry as a cache miss).

gt4py._core.file_utils.atomic_write_text(target: str | PathLike, text: str, *, encoding: str = 'utf-8') → None[source]

Write text to target atomically (see atomic_write_bytes).

gt4py._core.filecache module

class gt4py._core.filecache.FileCache(path: str | PathLike)[source]

Bases: object

Dictionary-like object for persistently caching objects on disk.

_get_path(key: Hashable) → Path[source]

Return the path where an item with key is stored.

path: Path

gt4py._core.locking module

class gt4py._core.locking._LockConstructor(*args, **kwargs)[source]

Bases: Protocol

__call__(path: str | PathLike, /) → ContextManager[source]

Call self as a function.

_abc_impl = <_abc._abc_data object>
_is_protocol = True
gt4py._core.locking.lock(directory: Path | str, suffix: str = '.lock') → ContextManager[source]

Create a lock for the given path.

If the path is a directory, it must already exist.

gt4py._core.ndarray_utils module

class gt4py._core.ndarray_utils.ArrayNamespace(*args, **kwargs)[source]

Bases: Hashable, Protocol

Currently only a subset of the Array API standard namespace with functions relevant for array creation.

TODO(havogt): replace by … or make it more complete and put next to NDArrayObject definition.

_abc_impl = <_abc._abc_data object>
_is_protocol = True
asarray(obj: Any, /, *, dtype: Any | None = None, device: Any | None = None, copy: bool | None = None) → NDArrayObject[source]
bool: _ArrayDType
empty(shape: tuple[int, ...], *, dtype: Any | None = None, device: Any | None = None) → NDArrayObject[source]
float32: _ArrayDType
float64: _ArrayDType
full(shape: tuple[int, ...], fill_value: Any, *, dtype: Any | None = None, device: Any | None = None) → NDArrayObject[source]
int16: _ArrayDType
int32: _ArrayDType
int64: _ArrayDType
int8: _ArrayDType
ones(shape: tuple[int, ...], *, dtype: Any | None = None, device: Any | None = None) → NDArrayObject[source]
uint16: _ArrayDType
uint32: _ArrayDType
uint64: _ArrayDType
uint8: _ArrayDType
zeros(shape: tuple[int, ...], *, dtype: Any | None = None, device: Any | None = None) → NDArrayObject[source]
class gt4py._core.ndarray_utils.BufferInfo(data_ptr: int, shape: tuple[int, ...], elem_strides: tuple[int, ...], dtype: DType, device: Device)[source]

Bases: object

Holds information about a buffer in memory.

Equal `BufferInfo`s (and equal `hash_key`s) describe buffers that behave the same apart from their contents.

Parameters:
  • data_ptr – Address of the element at index (0, …, 0).

  • shape – Number of elements along each dimension.

  • elem_strides – Distance between consecutive elements along each dimension, in elements.

  • dtype – Data type of the elements.

  • device – Device the buffer is allocated on.

data_ptr: int
device: Device
dtype: DType
elem_strides: tuple[int, ...]
classmethod from_dlpack(array: Any) → Self[source]

Describe the buffer of array from its DLPack export.

Raises:
  • TypeError – array does not export DLPack, or its dtype is not a gt4py scalar type.

  • BufferError – array cannot be exported without a copy, or its DLPack export is not supported (see gt4py._core.dlpack.tensor_fields), or it reports a stride of 2**63 bytes or more.

property hash_key: int[source]
shape: tuple[int, ...]
class gt4py._core.ndarray_utils._ArrayDType(*args, **kwargs)[source]

Bases: Protocol

_abc_impl = <_abc._abc_data object>
_is_protocol = True
gt4py._core.ndarray_utils._c_contiguous_strides(shape: tuple[int, ...]) → tuple[int, ...][source]
gt4py._core.ndarray_utils._canonicalize_numpy_dtype(dtype: dtype) → dtype[source]
gt4py._core.ndarray_utils._device_translation_registry: dict[ArrayNamespace, Callable[[Device | None], Any]] = {<module 'numpy' from '/home/runner/work/gt4py/gt4py/.nox/docs/lib/python3.14/site-packages/numpy/__init__.py'>: <function _numpy_device_translator>}

Registry for mappings from array namespaces to device_translators.

Device translators are functions mapping GT4Py ‘Device’ objects to the corresponding device objects for the given array namespace. It’s responsibility of the translator to check if ‘Device’ is valid for the given array namespace and raise a ‘ValueError’ if not.

gt4py._core.ndarray_utils._dtype_tables(array_ns: ArrayNamespace) → tuple[dict[type, _ArrayDType], dict[_ArrayDType, type]][source]
gt4py._core.ndarray_utils._numpy_device_translator(device: Device | None) → Any[source]
gt4py._core.ndarray_utils.array_namespace(array: NDArrayObject) → ArrayNamespace[source]
gt4py._core.ndarray_utils.device_from_dlpack(device_type: int, device_id: int) → Device[source]

The Device of a DLPack device pair; device types gt4py does not name are kept as integers.

gt4py._core.ndarray_utils.dtype_of(array: NDArrayObject) → DType[source]

The gt4py dtype of array.

gt4py._core.ndarray_utils.get_device_translator(array_ns: ArrayNamespace) → Callable[[Device | None], Any][source]

Returns a mapping from a GT4Py ‘Device’ to the corresponding device object for the given array namespace.

gt4py._core.ndarray_utils.is_array_namespace(obj: Any) → TypeGuard[ArrayNamespace][source]

Check whether obj (structurally) is a namespace of the array API.

See description in ‘ArrayNamespace’.

gt4py._core.ndarray_utils.namespace_dtype(array_ns: ArrayNamespace, scalar_type: type[bool | bool | int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64 | float32 | float64 | float]) → _ArrayDType[source]

The dtype object of array_ns for the gt4py scalar type scalar_type.

gt4py._core.ndarray_utils.scalar_type(array_ns: ArrayNamespace, dtype: _ArrayDType) → type[bool | bool | int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64 | float32 | float64 | float][source]

The gt4py scalar type of the arrays of array_ns with dtype dtype.

gt4py._core.types module

Module contents

GridTools core definitions and utilities.