xbarray.backends¶
Modules:
| Name | Description |
|---|---|
base |
|
jax |
|
numpy |
|
pytorch |
|
utils |
|
Classes:
| Name | Description |
|---|---|
RNGBackend |
|
ComputeBackend |
|
Functions:
| Name | Description |
|---|---|
get_backend_from_tensor |
|
Attributes:
| Name | Type | Description |
|---|---|---|
ArrayAPISetIndex |
|
|
ArrayAPIGetIndex |
|
|
BArrayType |
|
|
BDeviceType |
|
|
BDtypeType |
|
|
BRNGType |
|
BArrayType
module-attribute
¶
BDeviceType
module-attribute
¶
BDtypeType
module-attribute
¶
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 |
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]
|
|
deserialize_device
abstractmethod
¶
deserialize_device(device_str: Optional[str]) -> Optional[BDeviceType]
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.
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.
get_backend_from_tensor
¶
get_backend_from_tensor(
tensor: BArrayType,
) -> ComputeBackend[BArrayType, BDeviceType, BDtypeType, BRNGType]