fn

set_detect_anomaly

None
set_detect_anomaly(mode: bool, check_nan: bool = True)
source

Programmatic global toggle for autograd anomaly detection.

Equivalent to entering detect_anomaly once and never exiting — flips the process-wide flag that backward consults at the end of each pass. Prefer the with-block form when the scope is bounded; use this free function when the flag is controlled by a CLI argument or an environment variable read once at start-up.

Parameters

modebool
True enables NaN / Inf checking on every backward; False disables it.
check_nanbool= True
Forwarded to the underlying flag. Currently the only supported check is for non-finite values; the parameter exists for API symmetry and forward compatibility.

Returns

None

The function mutates a module-level flag; it has no return value.

Notes

The effective flag is modecheck_nan\text{mode} \land \text{check\_nan}: setting either to False disables checking. There is no stack of saved states — once changed, the flag stays until the next call.

Examples

Enable checks at start-up:
>>> import lucid
>>> from lucid.autograd import set_detect_anomaly, is_anomaly_enabled
>>> set_detect_anomaly(True)
>>> is_anomaly_enabled()
True
>>> set_detect_anomaly(False)