Skip to content

xbarray.backends.base

Classes:

Name Description
RNGBackend
ComputeBackend

Attributes:

Name Type Description
ArrayAPISetIndex
ArrayAPIGetIndex
BArrayType
BDeviceType
BDtypeType
BRNGType

ArrayAPISetIndex module-attribute

ArrayAPISetIndex = SetIndex

ArrayAPIGetIndex module-attribute

ArrayAPIGetIndex = GetIndex

BArrayType module-attribute

BArrayType = TypeVar('BArrayType', covariant=True, bound=ArrayAPIArray)

BDeviceType module-attribute

BDeviceType = TypeVar('BDeviceType', covariant=True, bound=ArrayAPIDevice)

BDtypeType module-attribute

BDtypeType = TypeVar('BDtypeType', covariant=True, bound=ArrayAPIDType)

BRNGType module-attribute

BRNGType = TypeVar('BRNGType', covariant=True)

RNGBackend

Bases: Protocol[BArrayType, BDeviceType, BDtypeType, BRNGType]

Methods:

Name Description
random_number_generator
random_discrete_uniform

Sample from a discrete uniform distribution [from_num, to_num) with shape shape.

random_uniform
random_exponential
random_normal
random_geometric
random_permutation

random_number_generator abstractmethod

random_number_generator(
    seed: Optional[int] = None, *, device: Optional[BDeviceType] = None
) -> BRNGType

random_discrete_uniform abstractmethod

random_discrete_uniform(
    shape: Union[int, Tuple[int, ...]],
    /,
    from_num: int,
    to_num: int,
    *,
    rng: BRNGType,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> Tuple[BRNGType, BArrayType]

Sample from a discrete uniform distribution [from_num, to_num) with shape shape.

random_uniform abstractmethod

random_uniform(
    shape: Union[int, Tuple[int, ...]],
    /,
    *,
    rng: BRNGType,
    low: float = 0.0,
    high: float = 1.0,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> Tuple[BRNGType, BArrayType]

random_exponential abstractmethod

random_exponential(
    shape: Union[int, Tuple[int, ...]],
    /,
    *,
    rng: BRNGType,
    lambd: float = 1.0,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> Tuple[BRNGType, BArrayType]

random_normal abstractmethod

random_normal(
    shape: Union[int, Tuple[int, ...]],
    /,
    *,
    rng: BRNGType,
    mean: float = 0.0,
    std: float = 1.0,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> Tuple[BRNGType, BArrayType]

random_geometric abstractmethod

random_geometric(
    shape: Union[int, Tuple[int, ...]],
    /,
    *,
    p: float,
    rng: BRNGType,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> Tuple[BRNGType, BArrayType]

random_permutation abstractmethod

random_permutation(
    n: int,
    /,
    *,
    rng: BRNGType,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> Tuple[BRNGType, BArrayType]

ComputeBackend

Bases: ArrayAPINamespace[BArrayType, BDeviceType, BDtypeType], Protocol[BArrayType, BDeviceType, BDtypeType, BRNGType]

Methods:

Name Description
serialize_device
deserialize_device
is_backendarray
from_numpy
from_other_backend

Convert an array from another backend to this backend.

to_numpy
to_dlpack
dtype_is_real_integer
dtype_is_real_floating
dtype_is_boolean
abbreviate_array

Abbreviates an array to a single element if possible.

map_fn_over_arrays

Map a function over arrays in a data structure and produce a new data structure with the same shape.

map_fn_over_arrays_ex

Map a function over arrays in a data structure with the support of another data structure and produce a new data structure with the same shape.

pad_dim

Pad a dimension of an array to a target size with a given value.

Attributes:

Name Type Description
simplified_name Literal['numpy', 'jax', 'pytorch']
ARRAY_TYPE Type[BArrayType]
DEVICE_TYPE Type[BDeviceType]
DTYPE_TYPE Type[BDtypeType]
RNG_TYPE Type[BRNGType]
default_integer_dtype BDtypeType
default_index_dtype BDtypeType
default_floating_dtype BDtypeType
default_boolean_dtype BDtypeType
random RNGBackend[BArrayType, BDeviceType, BDtypeType, BRNGType]

simplified_name instance-attribute

simplified_name: Literal['numpy', 'jax', 'pytorch']

ARRAY_TYPE instance-attribute

ARRAY_TYPE: Type[BArrayType]

DEVICE_TYPE instance-attribute

DEVICE_TYPE: Type[BDeviceType]

DTYPE_TYPE instance-attribute

DTYPE_TYPE: Type[BDtypeType]

RNG_TYPE instance-attribute

RNG_TYPE: Type[BRNGType]

default_integer_dtype instance-attribute

default_integer_dtype: BDtypeType

default_index_dtype instance-attribute

default_index_dtype: BDtypeType

default_floating_dtype instance-attribute

default_floating_dtype: BDtypeType

default_boolean_dtype instance-attribute

default_boolean_dtype: BDtypeType

random instance-attribute

serialize_device abstractmethod

serialize_device(device: Optional[BDeviceType]) -> Optional[str]

deserialize_device abstractmethod

deserialize_device(device_str: Optional[str]) -> Optional[BDeviceType]

is_backendarray abstractmethod

is_backendarray(data: Any) -> bool

from_numpy abstractmethod

from_numpy(
    data: ndarray,
    /,
    *,
    dtype: Optional[BDtypeType] = None,
    device: Optional[BDeviceType] = None,
) -> BArrayType

from_other_backend abstractmethod

from_other_backend(other_backend: ComputeBackend, data: ArrayAPIArray) -> BArrayType

Convert an array from another backend to this backend. The other backend must be compatible with the ArrayAPI.

to_numpy abstractmethod

to_numpy(data: BArrayType) -> ndarray

to_dlpack abstractmethod

to_dlpack(data: BArrayType) -> SupportsDLPack

dtype_is_real_integer abstractmethod

dtype_is_real_integer(dtype: BDtypeType) -> bool

dtype_is_real_floating abstractmethod

dtype_is_real_floating(dtype: BDtypeType) -> bool

dtype_is_boolean abstractmethod

dtype_is_boolean(dtype: BDtypeType) -> bool

abbreviate_array abstractmethod

abbreviate_array(
    x: BArrayType, try_cast_scalar: bool = True
) -> Union[float, int, BArrayType]

Abbreviates an array to a single element if possible. Or, if some dimensions are the same, abbreviates to a smaller array (but with the same number of dimensions).

map_fn_over_arrays abstractmethod

map_fn_over_arrays(data: Any, func: Callable[[BArrayType], BArrayType]) -> Any

Map a function over arrays in a data structure and produce a new data structure with the same shape. This is useful for applying a function to all arrays in a nested structure.

map_fn_over_arrays_ex abstractmethod

map_fn_over_arrays_ex(
    data: Any,
    *supp_data: Optional[Any],
    func: Callable[[BArrayType, Optional[Any]], BArrayType],
) -> Any

Map a function over arrays in a data structure with the support of another data structure and produce a new data structure with the same shape. This is useful for applying a function to all arrays in a nested structure.

pad_dim abstractmethod

pad_dim(
    x: BArrayType, dim: int, target_size: int, value: Union[float, int] = 0
) -> BArrayType

Pad a dimension of an array to a target size with a given value. If the dimension is already the target size, return the original array. If the dimension is larger than the target size, raise an error.