brainstate.environ module#
Environment configuration and context management for BrainState.
This module provides comprehensive functionality for managing computational environments, including platform selection, precision control, mode setting, and context-based configuration management. It enables flexible configuration of JAX-based computations with thread-safe context switching.
The module supports: - Platform configuration (CPU, GPU, TPU) - Precision control (8, 16, 32, 64 bit and bfloat16) - Computation mode management - Context-based temporary settings - Default data type management - Custom behavior registration
Examples
Global environment configuration:
>>> import brainstate as bs
>>> import brainstate.environ as env
>>>
>>> # Set global precision to 32-bit
>>> env.set(precision=32, dt=0.01, mode=bs.mixin.Training())
>>>
>>> # Get current settings
>>> print(env.get('precision')) # 32
>>> print(env.get('dt')) # 0.01
Context-based temporary settings:
>>> import brainstate.environ as env
>>>
>>> # Temporarily change precision
>>> with env.context(precision=64, dt=0.001):
... high_precision_result = compute_something()
... print(env.get('precision')) # 64
>>> print(env.get('precision')) # Back to 32
The brainstate.environ module provides a comprehensive environment management system for
configuring computational settings, platform preferences, numerical precision, and runtime
behaviors. It offers thread-safe configuration management with support for both global
settings and context-specific overrides.
Environment Management#
Functions for managing environment configuration and settings. These utilities allow you to set, retrieve, and manage environment variables that control the behavior of brainstate computations.
Environment State#
Thread-local environment container used for custom or isolated configurations.
Thread-local storage for environment configuration. |
Platform and Device Settings#
Configure the computing platform (CPU, GPU, TPU) and control device allocation. These functions help optimize performance by allowing you to select appropriate hardware backends and manage device resources.
Get the current computing platform. |
|
Set the computing platform. |
|
Get the number of host devices. |
|
Set the number of host (CPU) devices. |
Precision and Data Types#
Control numerical precision and retrieve appropriate data types for computations. These functions ensure consistent precision across your calculations and provide easy access to the correct NumPy/JAX data types based on the current precision setting.
Get the current numerical precision as an integer. |
|
Set the global numerical precision. |
|
Get the default floating-point data type. |
|
Get the default integer data type. |
|
Get the default unsigned integer data type. |
|
Get the default complex data type. |
|
Get numerical tolerance based on current precision. |
Mode and Timing#
Access computation mode settings and numerical integration parameters. These functions provide information about the current execution mode and time step settings for simulations.
Get the current numerical integration time step. |
Environment Keys and Flags#
Common environment key constants used with get/set/context.
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
|
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
Defaults and Supported Values#
Default values and supported platform/precision settings.
int([x]) -> integer int(x, base=10) -> integer |
|
Built-in immutable sequence. |
|
Built-in immutable sequence. |
Behavior Registration#
Register custom callbacks that respond to environment parameter changes. This system allows you to define automatic behaviors when specific environment settings are modified, enabling reactive configuration management.
Register a callback for environment parameter changes. |
|
Remove a registered callback for an environment parameter. |
|
List all keys with registered callbacks. |