brainevent.config.set_backend

Contents

brainevent.config.set_backend#

brainevent.config.set_backend(platform, backend)[source]#

Set the global default backend for a platform across all primitives.

After this call, every primitive that has a kernel registered for backend on platform will use it by default, unless overridden by an explicit backend= keyword argument at call time.

Parameters:
  • platform (str) – The platform name: 'cpu', 'gpu' or 'tpu'.

  • backend (str | None) – The backend name (e.g., 'numba', 'jax_raw', 'cuda_raw', 'pallas'). Pass None to clear the global default for this platform, reverting to per-primitive defaults.

Raises:

ValueError – If platform is not 'cpu', 'gpu' or 'tpu', or if backend is an empty string.

See also

get_backend

Query the current global backend for a platform.

clear_backends

Clear all global backend defaults.

Notes

Backend selection is resolved inside each primitive’s MLIR lowering function, and JAX caches lowered/compiled executables (both the eager dispatch cache and the jit compilation cache) keyed on primitive + abstract values, not on the global backend setting. If this call actually changes the effective value for platform, it therefore calls jax.clear_caches() so that already-compiled call sites pick up the new backend on their next invocation (at the cost of forcing recompilation everywhere on next use). Calling set_backend with the value it already has is a no-op and does not clear caches.

This makes set_backend a setup-time, single-threaded control: it is intended to be called before the hot loop / before other threads start compiling, not toggled concurrently from multiple threads while other work is in flight (jax.clear_caches() is process-global). For per-call, thread-safe backend selection, pass backend=<name> directly to the primitive call instead – it is a bind parameter and therefore part of the cache key, so it composes safely with concurrent dispatch.

Determinism on GPU. The default GPU kernels (cuda_raw) accumulate with float atomics, so their results can differ in the last bits from one call to the next. When XLA_FLAGS sets --xla_gpu_deterministic_ops (read when a primitive is lowered), every primitive without an explicit choice lowers with 'jax_raw' on GPU, whose XLA scatters and reductions follow the flag; a primitive without a 'jax_raw' GPU kernel warns once. An explicit choice (backend= on the call, or this function) is kept.

Examples

>>> import brainevent
>>> brainevent.config.set_backend('gpu', 'cuda_raw')
>>> brainevent.config.get_backend('gpu')
'cuda_raw'
>>> brainevent.config.set_backend('gpu', None)  # clear
>>> brainevent.config.get_backend('gpu') is None
True