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:
ProtocolProtocol 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 tonumpy.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
xto this backendโs native array/tensor type.If
likeis provided, the returned value should also follow backend-specific placement metadata fromlikewhen supported. In practice, PyTorch, JAX and TensorFlow implementations place the result on the same device aslike. NumPy and pandas ignorelikebecause 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
likebackend.- 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 useBackendOps.asarray()ornormalize_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:
- 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,tensorflowandjax. Everything else falls back togeneric.- 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.Nonevalues are preserved.- Parameters:
xs (Any) โ external inputs to normalize.
- Returns:
tuple
(backend, normalized_inputs)wherenormalized_inputscontains backend-native values in the same order asxs.- 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 toFalse.
- Returns:
extracted columns in the same backend.
- Return type:
Tuple[Any, โฆ]