nvalchemi.dynamics.hooks.nonfinite_graph_mask#

nvalchemi.dynamics.hooks.nonfinite_graph_mask(batch, keys=('positions', 'forces'))[source]#

Flag the graphs of batch holding a NaN or infinite value under any of keys.

A key the batch does not carry is skipped. A non-finite value in a node-level tensor flags the graph its atom belongs to, and one in a graph-level tensor flags its own graph. The check runs on the concatenated tensors without a host synchronization, so it can run inside a compiled step. It only returns the mask; a caller that must act on the result inspects the mask itself.

Parameters:
  • batch (Batch) – Batch to inspect.

  • keys (Iterable[str], optional) – Batch fields to inspect. Default ("positions", "forces").

Returns:

One flag per graph of batch, True where any inspected value is not finite.

Return type:

Bool[torch.Tensor, “G”]

Raises:

ValueError – If a present key holds a zero-dimensional tensor, or one whose leading dimension is neither the batch’s atom count nor its graph count, so its values cannot be attributed to graphs.

Examples

>>> from nvalchemi.dynamics.hooks import nonfinite_graph_mask
>>> nonfinite_graph_mask(batch)
tensor([False,  True, False])
>>> nonfinite_graph_mask(batch, keys=("energy", "stress"))
tensor([False, False, False])