Implementation:Pyro ppl Pyro Settings
| Property | Value |
|---|---|
| Module | pyro.settings
|
| Source | pyro/settings.py |
| Lines | 164 |
| Functions | get, set, context, register
|
| Dependencies | (none -- pure Python) |
Overview
This module provides a global settings registry for Pyro. It allows any Pyro module to register a configurable setting, and provides a uniform interface for users to get, set, and temporarily override settings. The registry stores settings by alias (a user-friendly name) and maps them to their actual location in the codebase (module + attribute path).
Key features:
- Get/set individual or all settings.
- Context manager for temporary overrides (also works as a decorator).
- Registration with optional validator functions.
- Auto-documentation of defaults in the module docstring.
The most commonly used setting is cholesky_relative_jitter, which controls the amount of jitter added during Cholesky decomposition in pyro.ops.tensor_utils.safe_cholesky.
Code Reference
Function: get(alias=None)
Gets one or all settings.
- If
aliasis None, returns a dict of all registered settings and their current values. - If
aliasis a string, looks up the registered module and deep attribute path, imports the module, and traverses the attribute path to get the current value.
Function: set(**kwargs)
Sets one or more settings by alias.
- Looks up the module and deep attribute path.
- Runs the optional validator (if registered).
- Imports the module and sets the attribute.
Function: context(**kwargs)
Context manager (and decorator) for temporarily overriding settings.
- Saves old values on enter.
- Applies new values.
- Restores old values on exit (even if an exception occurs).
Function: register(alias, modulename, deepname, validator=None)
Registers a new setting in the global registry.
alias: User-friendly name (snake_case).modulename: Python module path (typically__name__).deepname: Dot-separated attribute path within the module (e.g.,"MyClass.my_attr").validator: Optional callable that validates values and raises on error.
Can be used as a decorator on a validator function:
@pyro.settings.register("my_setting", __name__, "MY_SETTING")
def _validate(value):
assert isinstance(value, float)
Updates the module docstring with the new default value after registration.
Internal Registry
The _REGISTRY dictionary maps each alias to a tuple (modulename, deepname, validator).
I/O Contract
| Function | Input | Output |
|---|---|---|
get(alias=None) |
Optional str |
Single value or Dict[str, Any]
|
set(**kwargs) |
alias=value keyword arguments |
None |
context(**kwargs) |
alias=value keyword arguments |
Context manager / decorator |
register(alias, modulename, deepname, validator) |
Strings, optional callable | Callable (decorator or validator) |
Usage Examples
import pyro
# View all settings
all_settings = pyro.settings.get()
print(all_settings)
# Get a specific setting
jitter = pyro.settings.get("cholesky_relative_jitter")
print(f"Current jitter: {jitter}")
# Set a setting
pyro.settings.set(cholesky_relative_jitter=0.5)
# Temporarily override (context manager)
with pyro.settings.context(cholesky_relative_jitter=1e-3):
# Inside this block, jitter is 1e-3
result = run_model()
# jitter is restored to previous value
# Temporarily override (decorator)
@pyro.settings.context(cholesky_relative_jitter=1e-6)
def my_precise_computation():
return run_model()
# Register a custom setting in a Pyro module
# In your_module.py:
MY_THRESHOLD = 0.1
pyro.settings.register(
"my_threshold", # alias
__name__, # module name
"MY_THRESHOLD", # attribute name
)
# Register with a validator
@pyro.settings.register("my_threshold", __name__, "MY_THRESHOLD")
def _validate_threshold(value):
assert isinstance(value, float) and value > 0
Related Pages
- Pyro_ppl_Pyro_TensorUtils -- Uses
cholesky_relative_jitterinsafe_cholesky - Pyro_ppl_Pyro_Util -- General Pyro utility functions