_backend
Array-module backend selection for CPU (numpy) and GPU (cupy).
Kept intentionally minimal: a single resolver returns the appropriate
array module, a PRNG factory, and a host-transfer helper. All hot-loop
code paths receive an xp module and call xp.linalg.* /
xp.asarray directly, so backend selection adds zero per-op overhead.
Functions#
default_rng #
default_rng(seed: int | None) -> np.random.Generator
Return a seeded NumPy Generator.
Always uses NumPy so that the same seed produces identical draws
regardless of the device, and avoids CuPy Generator API
differences (e.g. missing .normal()).
Source code in src/src_method/utils/_backend.py
44 45 46 47 48 49 50 51 52 53 | |
get_xp #
get_xp(device: str) -> ModuleType
Return the array module for the requested device.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModuleType
|
The numpy or cupy module. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
ImportError
|
If |
Source code in src/src_method/utils/_backend.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
to_numpy #
to_numpy(arr: NDArray) -> np.ndarray
Bring an array onto the host as a numpy array (no-op for numpy).
Source code in src/src_method/utils/_backend.py
56 57 58 59 60 61 62 | |