kempnerforge.resilience.health¶
GPU health monitoring and NaN detection.
- Provides utilities for detecting common failures during training:
NaN/Inf in loss, gradients or parameters
GPU availability and basic health
NCCL liveness via lightweight collectives
Functions
|
Run basic GPU health checks. |
|
Check NCCL communication health via a lightweight all-reduce. |
|
Check that no rank holds a NaN/Inf parameter of |
Classes
Detects and tracks NaN/Inf values in loss and gradients. |
|
Tracks NaN/Inf occurrences across training steps. |
- class kempnerforge.resilience.health.NaNState[source]¶
Bases:
objectTracks NaN/Inf occurrences across training steps.
- class kempnerforge.resilience.health.NaNDetector[source]¶
Bases:
objectDetects and tracks NaN/Inf values in loss and gradients.
- Supports three responses to NaN:
"warn": Log a warning and continue."skip": Skip the optimizer step (zero gradients)."raise": Raise aRuntimeError.
Once the consecutive NaN count reaches
max_consecutive, the detector signals that a checkpoint rollback is recommended.- Parameters:
action – What to do when NaN is detected.
max_consecutive – Consecutive NaN steps before recommending rollback.
max_history – Number of NaN step indices to retain.
- check_loss(loss, step, grad_norm=None)[source]¶
Check a step’s loss, and its gradient norm when given, for NaN/Inf.
When running distributed, all-reduces a NaN flag so ALL ranks agree on whether to skip. Prevents rank desync where one rank sees NaN and skips its optimizer step while others proceed normally.
A finite loss does not guarantee finite gradients: a micro-batch whose labels are all masked has an exact zero cross-entropy loss whatever its activations, so the caller passes the gradient norm as well. Each rank’s norm goes into the same reduced flag as the loss, so every rank skips the step together even when a non-finite gradient reaches only some ranks’ norms.
- Parameters:
- Returns:
True if the loss and the gradient norm are finite on ALL ranks, False if any rank has NaN/Inf in either.
- Raises:
RuntimeError – If action is “raise” and NaN is detected.
- Return type:
- check_gradients(model, step)[source]¶
Check model gradients for NaN/Inf before optimizer step.
- Parameters:
model (torch.nn.Module) – The model to check.
step (int) – Current training step.
- Returns:
True if all gradients are finite.
- Return type:
- kempnerforge.resilience.health.params_are_finite(model, device)[source]¶
Check that no rank holds a NaN/Inf parameter of
model.Each rank checks the parameters it holds (the local shard of a
DTensor). When running distributed, one all-reduce of a flag over the default process group gives every rank the same answer, so this is a collective that every rank must call.- Parameters:
model (torch.nn.Module) – The model, or this rank’s pipeline stage of it.
device (torch.device | str) – Device for the all-reduced flag; it must suit the default process group’s backend (CUDA under NCCL).
- Returns:
True if every parameter is finite on every rank.
- Return type:
- kempnerforge.resilience.health.check_gpu_health(device=0)[source]¶
Run basic GPU health checks.
- Performs:
CUDA availability check
Small test computation on the device
Memory allocation test
- kempnerforge.resilience.health.check_nccl_health(timeout_sec=10.0)[source]¶
Check NCCL communication health via a lightweight all-reduce.
The all-reduce runs with
async_op=Truesowork.wait(timeout=...)enforces the caller’s bound rather than falling back to the process-group default timeout (nccl_timeout_sec, 1800s). Without that, this function would sit for 30 minutes on a single stuck peer regardless of thetimeout_secargument.