Backend๏ƒ

Backend abstraction layer for array/tensor operations.

This module provides backend inference and a unified API over NumPy, pandas, PyTorch, JAX and TensorFlow objects.

Note:๏ƒ

Public API functions should infer the backend from user-provided inputs, call BackendOps.asarray() once at the function boundary, and then pass the normalized backend-native values to the rest of the backend operations. Most backend methods intentionally stay close to their native library semantics and do not defensively normalize every argument. Use normalize_backend_inputs() when a function needs both backend inference and one-shot input normalization.

class backend.BackendOps(*args, **kwargs)๏ƒ

Bases: Protocol

Protocol describing backend operations used across the API.

Contract:

  • asarray() converts external user inputs to the backend-native type.

  • to_numpy() converts backend-native values back to numpy.ndarray.

  • All other methods are primarily defined over already-normalized backend-native values. Callers should normalize once at the function boundary instead of relying on every backend op to coerce inputs.

abs(x)๏ƒ
Return type:

Any

any(x)๏ƒ
Return type:

bool

arange(start, stop=None, step=1)๏ƒ
Return type:

Any

argmax(x, axis=None)๏ƒ
Return type:

Any

argsort(x, axis=-1, descending=False)๏ƒ
Return type:

Any

asarray(x, like=None)๏ƒ

Convert x to this backendโ€™s native array/tensor type.

If like is provided, the returned value should also follow backend-specific placement metadata from like when supported. In practice, PyTorch, JAX and TensorFlow implementations place the result on the same device as like. NumPy and pandas ignore like because they do not expose accelerator-device placement through this backend.

Return type:

Any

astype(x, dtype)๏ƒ
Return type:

Any

clip(x, a_min, a_max)๏ƒ
Return type:

Any

column_stack(xs)๏ƒ
Return type:

Any

concat(xs, axis=0)๏ƒ
Return type:

Any

cumsum(x, axis=0)๏ƒ
Return type:

Any

empty_like(x)๏ƒ
Return type:

Any

equal(a, b)๏ƒ
Return type:

Any

full(shape, fill_value, dtype=None)๏ƒ
Return type:

Any

max(x, axis=None, keepdims=False)๏ƒ
Return type:

Any

maximum(a, b)๏ƒ
Return type:

Any

mean(x, axis=None, keepdims=False)๏ƒ
Return type:

Any

min(x, axis=None, keepdims=False)๏ƒ
Return type:

Any

minimum(a, b)๏ƒ
Return type:

Any

name: str๏ƒ
ones_like(x)๏ƒ
Return type:

Any

random_uniform(shape)๏ƒ
Return type:

Any

reshape(x, shape)๏ƒ
Return type:

Any

scalar_at(x, index)๏ƒ
Return type:

Any

sqrt(x)๏ƒ
Return type:

Any

squeeze(x, axis=None)๏ƒ
Return type:

Any

sum(x, axis=None, keepdims=False)๏ƒ
Return type:

Any

take(x, indices, axis=0)๏ƒ
Return type:

Any

take_along_axis(arr, indices, axis)๏ƒ
Return type:

Any

to_numpy(x)๏ƒ
Return type:

ndarray

where(cond, x, y)๏ƒ
Return type:

Any

backend.concat_columns(cols, *, like)๏ƒ

Concatenate column objects along axis 1 using like backend.

Parameters:
  • cols (Sequence[Any]) โ€“ column-like objects to concatenate.

  • like (Any) โ€“ reference object used to infer backend.

Returns:

concatenated object in the backend of like.

Return type:

Any

backend.copy_model(model)๏ƒ

Copy a model-like object using the matching backend strategy.

Parameters:

model (Any) โ€“ model-like object to copy.

Returns:

copied model.

Return type:

Any

Raises:

RuntimeError โ€“ if the model cannot be copied.

backend.get_backend(*xs)๏ƒ

Instantiate backend operations from input objects.

This function selects the backend implementation. After calling get_backend(), callers should use BackendOps.asarray() or normalize_backend_inputs() to normalize user-provided inputs before applying backend operations.

Parameters:

xs (Any) โ€“ objects used to infer backend.

Returns:

backend operations instance implementing BackendOps.

Return type:

BackendOps

Raises:

RuntimeError โ€“ if inferred backend has no registered implementation.

backend.infer_backend(*xs)๏ƒ

Infer backend name from one or more objects.

Priority order is: torch > jax > tensorflow > pandas > numpy. Mixed explicit backends are rejected.

Parameters:

xs (Any) โ€“ objects used to infer the computational backend.

Returns:

inferred backend name.

Return type:

str

Raises:

TypeError โ€“ if multiple incompatible backends are mixed.

backend.infer_model_backend(model)๏ƒ

Infer backend name from a model-like object.

Supported model backends are sklearn, torch, tensorflow and jax. Everything else falls back to generic.

Parameters:

model (Any) โ€“ model-like object to inspect.

Returns:

inferred model backend name.

Return type:

str

backend.normalize_backend_inputs(*xs)๏ƒ

Infer a backend and normalize user-provided inputs once.

This helper is intended for public API boundaries. It keeps backend selection explicit while avoiding repeated b.asarray(...) calls spread across the rest of the implementation. None values are preserved.

Parameters:

xs (Any) โ€“ external inputs to normalize.

Returns:

tuple (backend, normalized_inputs) where normalized_inputs contains backend-native values in the same order as xs.

Return type:

Tuple[BackendOps, Tuple[Any, โ€ฆ]]

backend.shape2(x)๏ƒ

Return dimensionality and shape for backend arrays/tensors.

Parameters:

x (Any) โ€“ input array-like.

Returns:

(ndim, shape_tuple).

Return type:

Tuple[int, Tuple[int, โ€ฆ]]

backend.split_columns(x, columns, *, keepdims=False)๏ƒ

Extract selected columns from a 2D backend object.

Parameters:
  • x (Any) โ€“ backend array/tensor/dataframe with shape (n, d).

  • columns (Sequence[int]) โ€“ column indices to extract.

  • keepdims (bool) โ€“ if True, keep extracted columns as 2D objects. Defaults to False.

Returns:

extracted columns in the same backend.

Return type:

Tuple[Any, โ€ฆ]