ptyrad.runtime.seed

ptyrad.runtime.seed#

Random seed resolution and initialization.

This module provides utilities to ensure reproducibility across PtyRAD reconstructions by managing random seeds for Python, NumPy, and PyTorch. It includes logic to prioritize user-provided seeds and automatically enforce seeding during distributed (multi-GPU) runs to prevent divergent model initializations.

Functions

resolve_seed_priority(args_seed, ...)

Determines the final random seed to use based on user inputs and runtime context.

set_random_seed(seed[, deterministic])

Sets the random seeds for Python, NumPy, and PyTorch operations.

ptyrad.runtime.seed.resolve_seed_priority(args_seed, params_seed, acc)[source]#

Determines the final random seed to use based on user inputs and runtime context.

This function resolves the seed using the following priority hierarchy:

  1. Command Line Argument (args_seed)

  2. Configuration File (params_seed)

  3. Automatic Fallback (forces seed to 42 if running in multi-GPU mode to ensure consistent probe/object initializations across processes).

  4. None (no fixed seed).

Parameters:
  • args_seed (int or None) – The seed provided via command-line arguments.

  • params_seed (int or None) – The seed provided in the parameters YAML file.

  • acc (accelerate.Accelerator or None) – The active HuggingFace Accelerator instance, used to check the number of active processes.

Returns:

The resolved integer seed, or None if no seeding is required.

Return type:

int or None

ptyrad.runtime.seed.set_random_seed(seed, deterministic=False)[source]#

Sets the random seeds for Python, NumPy, and PyTorch operations.

Ensures that pseudo-random number generators (PRNGs) across all relevant libraries and devices (CPU and all available CUDA GPUs) are synchronized for reproducible reconstructions.

Parameters:
  • seed (int, optional) – The integer seed to apply. If None, this function does nothing.

  • deterministic (bool, optional) – If True, forces PyTorch to use deterministic algorithms. Note that this can significantly impact performance and may cause crashes if certain operations do not have a deterministic implementation. Defaults to False.