deepmd.pt_expt.train.gradient#
Numerical safeguards around the gradient of a training step.
Two hazards are handled here, both of which have to be addressed without slowing the step down.
A gradient that is large but still representable can overflow the sum of squares of a naive L2 reduction, so a finite gradient would be reported as infinite and clipped as if training had diverged. Scaling the gradients by their largest magnitude before the reduction removes that failure mode.
A gradient that is genuinely non-finite must stop the run before it reaches a checkpoint, but reading that condition back on every step would serialize the host against the device. The condition is therefore accumulated on device and read only at a checkpoint boundary.
Classes#
Detect non-finite gradient norms without a per-step host synchronization. |
Functions#
| Clip gradients in place so their global L2 norm does not exceed |
| Raise a |
Module Contents#
- deepmd.pt_expt.train.gradient.clip_grad_norm_(parameters: collections.abc.Iterable[torch.nn.Parameter], max_norm: float, stable: bool = True) torch.Tensor[source]#
Clip gradients in place so their global L2 norm does not exceed
max_norm.The norm is computed and applied on device and returned without a host synchronization. Non-finite gradients are not reported here; see
NonFiniteGradGuard.- Parameters:
- parameters
Iterable[torch.nn.Parameter] Parameters whose gradients are clipped in place.
- max_norm
float Maximum allowed global L2 norm of the gradients.
- stablebool,
optional Norm reduction strategy.
Truescales the gradients by their largest magnitude before the float64 reduction, keeping the norm finite for an arbitrarily large but finite gradient of any dtype.Falseuses the native reduction, which propagatesDTensorsharding under FSDP2 but is not overflow-safe.
- parameters
- Returns:
torch.TensorThe global gradient norm before clipping, or a CPU zero when no gradient is present.
- class deepmd.pt_expt.train.gradient.NonFiniteGradGuard[source]#
Detect non-finite gradient norms without a per-step host synchronization.
update()accumulates the non-finite condition on device; the result is read back only byraise_if_nonfinite(). Calling the check before each checkpoint keeps a diverged interval from being written while leaving the training step free of host reads.- _nonfinite: torch.Tensor | None = None[source]#
- update(total_norm: torch.Tensor) None[source]#
Accumulate whether
total_normis non-finite.- Parameters:
- total_norm
torch.Tensor The gradient norm returned by
clip_grad_norm_().
- total_norm
- raise_if_nonfinite(named_parameters: collections.abc.Callable[[], collections.abc.Iterable[tuple[str, torch.nn.Parameter]]]) None[source]#
Raise if any norm accumulated since the previous call was non-finite.
On failure the current gradient state is reported via
raise_nonfinite_gradient_norm().- Parameters:
- Raises:
RuntimeErrorIf a non-finite gradient norm was recorded.
- deepmd.pt_expt.train.gradient.raise_nonfinite_gradient_norm(named_parameters: collections.abc.Iterable[tuple[str, torch.nn.Parameter]]) None[source]#
Raise a
RuntimeErrorreporting the current non-finite gradients.Parameters whose current gradient is non-finite are listed by name and shape. When every current individual gradient is finite, the accumulated guard flag came from an earlier step in the checkpoint interval or from the norm reduction.
- Parameters:
- Raises:
RuntimeErrorAlways; this is the divergence-reporting path.