Utilities
Utilities support reproducibility, rank-aware logging, configuration display, task cleanup, shape normalization, and conversion between JAX and OmegaConf values.
Reproducibility
resolve_seed(None) selects a random unsigned 32-bit seed. The training entrypoint writes that concrete value back to
the composed config and logs it before initialization. seed_everything seeds Python and NumPy and returns a JAX key
folded with the current process index.
resolve_seed
[source]
Resolve an optional root seed to a concrete unsigned 32-bit integer.
A configured integer is preserved for reproducibility. Passing None draws one seed from the operating system's
cryptographic random source so it does not depend on Python or NumPy RNG state that this seed will subsequently
initialize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Configured root seed, or |
required |
Returns:
| Type | Description |
|---|---|
int
|
Concrete unsigned 32-bit root seed. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If an integer seed lies outside the unsigned 32-bit range. |
seed_everything
[source]
Globally seed Python and NumPy and return an explicit process-local JAX key.
JAX has no implicit global random generator, so callers must still thread the returned key through model initialization, sampling, and functional training state. In distributed execution, host RNGs and the returned JAX key are deterministically separated by process index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Reproducible unsigned 32-bit root seed. |
required |
process_index
|
int | None
|
Optional global JAX process index. Defaults to :func: |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Process-local typed JAX PRNG key derived from |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Python logging
RankedLogger
[source]
RankedLogger(
name: str = __name__,
*,
rank_zero_only: bool = False,
extra: Mapping[str, object] | None = None,
)
Bases: LoggerAdapter
Standard-library logger adapter with optional rank-zero-only emission.
The adapter delegates to the logger registered under name, so handlers installed by Hydra remain responsible for
formatting and routing records. When rank_zero_only is enabled, records created on nonzero JAX processes are
discarded before they reach those handlers.
Initialize a JAX-aware logger adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the underlying standard-library logger. |
__name__
|
rank_zero_only
|
bool
|
Whether to emit records only from JAX process zero. |
False
|
extra
|
Mapping[str, object] | None
|
Optional contextual values added to each emitted log record. |
None
|
log
[source]
Emit a record when its level and configured process policy allow it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
Standard-library logging level for the record. |
required |
msg
|
object
|
Message object passed to the underlying logger. |
required |
*args
|
object
|
Positional arguments forwarded to :meth: |
()
|
**kwargs
|
Any
|
Keyword arguments forwarded to :meth: |
{}
|
get_colorlogger
[source]
Create or retrieve a colorized Hydra-style logger.
The logger owns one colorized stream handler and disables propagation so Hydra's root handler does not emit each message a second time. Repeated calls with the same name reuse the configured handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logger name displayed in each record. |
'app'
|
level
|
int
|
Minimum standard-library logging level to emit. |
INFO
|
Returns:
| Name | Type | Description |
|---|---|---|
Configured |
Logger
|
class: |
pad_keys
[source]
pad_keys(
mapping: Mapping[str, Any],
prefix: str | None = None,
postfix: str | None = None,
exclude: str | Sequence[str] | None = None,
) -> dict[str, Any]
Add a prefix and postfix to selected mapping keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[str, Any]
|
Mapping whose string keys will be transformed. |
required |
prefix
|
str | None
|
Text to prepend to keys, or |
None
|
postfix
|
str | None
|
Text to append to keys, or |
None
|
exclude
|
str | Sequence[str] | None
|
One key or a sequence of keys to leave unchanged. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
New dictionary containing the transformed keys and original values. |
Configuration and task lifecycle
pre_hydra_routine must run before a decorated Hydra entrypoint so project-root discovery, environment defaults, and
OmegaConf resolver registration happen before composition. task_wrapper logs failures and always runs registered
finalizers, including optional W&B shutdown.
pre_hydra_routine
[source]
Configure repository paths and environment variables before Hydra starts.
The repository root is discovered from this source file using pyproject.toml, exported as PROJECT_ROOT, and used
to load an optional repository-level .env file. PhiJAX's process-global OmegaConf resolvers are registered before
Hydra composes a config. The working directory and Python import path are unchanged.
extras
[source]
Apply optional warning and Rich-rendering utilities before a task starts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
DictConfig
|
Configuration containing an optional |
required |
print_config_tree
[source]
print_config_tree(
cfg: DictConfig,
print_order: Sequence[str] = (
"application",
"data",
"model",
"callbacks",
"logger",
"trainer",
"paths",
"extras",
),
resolve: bool = False,
save_to_file: bool = False,
) -> None
Print a composed Hydra configuration as a Rich tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
DictConfig
|
Configuration composed by Hydra. |
required |
print_order
|
Sequence[str]
|
Preferred order for top-level configuration fields. Present fields not listed here are appended in their configuration order. |
('application', 'data', 'model', 'callbacks', 'logger', 'trainer', 'paths', 'extras')
|
resolve
|
bool
|
Whether to resolve interpolations before rendering configuration groups. |
False
|
save_to_file
|
bool
|
Whether to save a plain-text rendering as |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
task_wrapper
[source]
task_wrapper(
task_func: Callable[[DictConfig], TaskReturn],
) -> Callable[[DictConfig], TaskReturn]
Decorate a Hydra task with assertion resolution and consistent lifecycle logging.
Lifecycle owners finalize stage-local resources normally. Longer-lived resources with idempotent cleanup, such as
trainer checkpoint services and remote experiment loggers, may register a failure-safe finalizer through
:func:register_task_finalizer; the wrapper invokes those finalizers in last-created-first-closed order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_func
|
Callable[[DictConfig], TaskReturn]
|
Task callable accepting the composed configuration as its only argument. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[DictConfig], TaskReturn]
|
Wrapped callable that preserves the task's return value and re-raises failures with their original traceback. |
register_task_finalizer
[source]
Register an idempotent resource finalizer with the active task wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
finalizer
|
TaskFinalizer
|
Callable accepting the terminal status |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether an active :func: |
Conversion and shapes
as_numpy
[source]
Convert an array-like value to a host NumPy array.
JAX is imported only when it is installed and conversion is requested. This keeps configuration and evaluation utilities importable without a JAX runtime while ensuring device arrays are explicitly transferred to the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Array-like value to convert. JAX arrays may reside on any supported device. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Host NumPy view or copy of |
ndarray
|
provide a native |
to_plain_container
[source]
Convert an OmegaConf container to ordinary Python containers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value that may be an OmegaConf configuration container. |
required |
resolve
|
bool
|
Whether to resolve OmegaConf interpolations during conversion. |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
Plain Python containers for OmegaConf inputs, otherwise |
as_tuple
[source]
Normalize a scalar or integer sequence shape to a tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
int | Sequence[int]
|
Scalar dimension or sequence of dimensions. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, ...]
|
Tuple containing all dimensions from |