|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from collections import deque |
| 8 | +from typing import Callable, Iterator, List, Optional, Sequence, Union |
| 9 | + |
| 10 | +import torch |
| 11 | +from pyre_extensions import none_throws |
| 12 | +from torch.autograd.graph import GradientEdge, Node |
| 13 | +from torch.utils.hooks import RemovableHandle |
| 14 | + |
| 15 | + |
| 16 | +def _get_grad_fn_or_grad_acc(t: Union[torch.Tensor, GradientEdge]) -> Node: |
| 17 | + if isinstance(t, torch.Tensor): |
| 18 | + return none_throws(t.grad_fn) |
| 19 | + else: |
| 20 | + # pyre-ignore Undefined attribute [16]: `GradientEdge` has no attribute `function`. |
| 21 | + return t.function if t is not None else None |
| 22 | + |
| 23 | + |
| 24 | +def register_nan_hooks_on_whole_graph( # noqa: C901 |
| 25 | + t_outputs: Sequence[Union[torch.Tensor, GradientEdge]] |
| 26 | +) -> Callable[[], None]: |
| 27 | + """ |
| 28 | + Registers a nan hook on the whole graph of the given tensors. The hook will throw error if a nan is detected. |
| 29 | +
|
| 30 | + This is useful if you want training to halt when a nan is detected during autograd process (ie loss is inf or nan). |
| 31 | +
|
| 32 | + Usage: |
| 33 | +
|
| 34 | + >>> class NaNFunction(torch.autograd.Function): |
| 35 | + @staticmethod |
| 36 | + def forward(ctx, input): |
| 37 | + return input.clone() |
| 38 | +
|
| 39 | + @staticmethod |
| 40 | + def backward(ctx, grad_output): |
| 41 | + return torch.tensor([float("nan")], device="cpu") |
| 42 | + >>> x = torch.tensor([1.0], device="cpu", requires_grad=True) |
| 43 | + >>> out = NaNFunction.apply(x) |
| 44 | + >>> _ = register_nan_hooks_on_whole_graph([out]) |
| 45 | + >>> out.backward() |
| 46 | + RuntimeError: Detected NaN in 'grad_inputs[0]' after executing Node |
| 47 | +
|
| 48 | + """ |
| 49 | + |
| 50 | + grad_fns = list(map(_get_grad_fn_or_grad_acc, t_outputs)) |
| 51 | + |
| 52 | + def iter_graph(roots: List[torch.autograd.graph.Node]) -> Iterator[Node]: |
| 53 | + if not roots: |
| 54 | + return |
| 55 | + seen = set() |
| 56 | + q = deque() |
| 57 | + for node in roots: |
| 58 | + if node is not None and node not in seen: |
| 59 | + seen.add(node) |
| 60 | + q.append(node) |
| 61 | + while q: |
| 62 | + node = q.popleft() |
| 63 | + for fn, _ in node.next_functions: |
| 64 | + if fn is None or fn in seen: |
| 65 | + continue |
| 66 | + seen.add(fn) |
| 67 | + q.append(fn) |
| 68 | + yield node |
| 69 | + |
| 70 | + def _assert_no_nan_tensor(t: Optional[torch.Tensor], msg: str) -> None: |
| 71 | + if t is not None: |
| 72 | + torch._assert_async(torch.logical_not(torch.any(torch.isnan(t))), msg) |
| 73 | + |
| 74 | + def posthook( |
| 75 | + grad_inputs: Sequence[Optional[torch.Tensor]], |
| 76 | + grad_outputs: Sequence[Optional[torch.Tensor]], |
| 77 | + ) -> None: |
| 78 | + node = torch._C._current_autograd_node() |
| 79 | + for i, g_in in enumerate(grad_inputs): |
| 80 | + _assert_no_nan_tensor( |
| 81 | + g_in, f"Detected NaN in 'grad_inputs[{i}]' after executing Node: {node}" |
| 82 | + ) |
| 83 | + |
| 84 | + handles: List[RemovableHandle] = [] |
| 85 | + for node in iter_graph(grad_fns): |
| 86 | + posthandle = node.register_hook(posthook) |
| 87 | + handles.append(posthandle) |
| 88 | + |
| 89 | + def unregister_hooks() -> None: |
| 90 | + for handle in handles: |
| 91 | + handle.remove() |
| 92 | + |
| 93 | + return unregister_hooks |
| 94 | + |
| 95 | + |
| 96 | +def check_for_nan_or_inf( |
| 97 | + tensor: torch.Tensor, msg: str = "Detected NaN or Inf in tensor" |
| 98 | +) -> None: |
| 99 | + """ |
| 100 | + Asynchronously assert that the tensor is neither NaN nor infinity. This will |
| 101 | + produce a cuda device side assert error if tensor on gpu. |
| 102 | + """ |
| 103 | + |
| 104 | + torch._assert_async( |
| 105 | + torch.logical_not(torch.any(torch.isnan(tensor) | torch.isinf(tensor))), |
| 106 | + msg, |
| 107 | + ) |
0 commit comments