sgnts.base.backend
¶
Namespace-based backend resolution — "the array is the backend."
The historical approach in sgn-ts tracked the array backend as configuration:
a backend field stored on buffers and elements, threaded through call after
call, kept in sync by hand, and reconciled with the data via ad-hoc
isinstance(data, NumpyArray/TorchArray) ladders scattered across the code.
This module replaces that with a single idea: the array already knows what it
is. We derive the backend from the data through the Python Array API standard
(https://data-apis.org/array-api), accessed via array-api-compat so it works
uniformly across numpy (<2 and >=2), torch, cupy, jax and dask.
Primitives:
- :func:
array_namespace— the standardxpnamespace for writing backend-blind code (xp.zeros_like(x)etc.). - :func:
backend_name— the backend name ("numpy"/"torch") for an array, used by the capability system. - :func:
new_zeros— create a zeros array matching a reference array's namespace, dtype and device.
Two related vocabularies, kept deliberately distinct: a namespace is the
xp object you compute with (the Array API standard term, hence
:func:array_namespace); a backend is its string name
("numpy"/"torch") — the comparable label the capability layer declares
and checks (backends, ANY_BACKEND, BackendError). One is the dispatch
object; the other is a name you can put in a frozenset or a class attribute.
BackendError
¶
Bases: Exception
flowchart TD
sgnts.base.backend.BackendError[BackendError]
click sgnts.base.backend.BackendError href "" "sgnts.base.backend.BackendError"
Raised when an element receives data in a backend it does not declare.
The capability system (a transform's backends attribute) is validated at
runtime against the actual data; a mismatch raises this with a message naming
the element, the declared backends, the received backend, and the fix
(insert a Converter).
Source code in src/sgnts/base/backend.py
array_namespace(*xs)
¶
Return the Array API namespace (xp) for the given array(s).
Thin, forgiving wrapper over :func:array_api_compat.array_namespace.
None (gap data), Python scalars, and unrecognized objects yield None
rather than raising, so callers can fall back to a default backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*xs
|
Any
|
One or more candidate arrays. Non-array arguments are ignored. |
()
|
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
The shared |
Source code in src/sgnts/base/backend.py
backend_name(data)
¶
Return the backend name ("numpy" / "torch") for an array.
Yields the string name that capability sets are written in (used by the
runtime capability check). Returns None for None, scalars, and
unrecognized types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The array (or non-array) to classify. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
|
Source code in src/sgnts/base/backend.py
device(x)
¶
The device of an array, portably ("cpu" for numpy, the device for torch).
Thin wrapper over :func:array_api_compat.device — numpy 1.x arrays have no
.device attribute, so this is the safe way to read it.
Source code in src/sgnts/base/backend.py
new_zeros(reference, shape)
¶
Zeros of shape matching reference's namespace, dtype, and device.
The end-state replacement for backend.zeros(shape): rather than a
configured backend, the new array follows a reference array (e.g. the
non-gap data a gap is being filled alongside).
Source code in src/sgnts/base/backend.py
normalize_dtype(namespace, dtype)
¶
Resolve a dtype spec to namespace's own dtype object.
dtype may be a string name ("float32") or a dtype object already in
the namespace (e.g. torch.float16); returns the namespace's matching
dtype. Lets a caller accept a friendly "float32" and hand the result
straight to xp ops, with no per-backend if ladder.
Raises:
| Type | Description |
|---|---|
ValueError
|
if |