code: def warn_if_nan(value, msg="", *, filename=None, lineno=None):
      """
      A convenient function to warn if a Tensor or its grad contains any nan,
      also works with numbers.
      """
      if filename is None:
          try:
              frame = sys._getframe(1)
          except ValueError:
              filename = "sys"
              lineno = 1
          else:
              filename = frame.f_code.co_filename
              lineno = frame.f_lineno
  
      if torch.is_tensor(value) and value.requires_grad:
          value.register_hook(
              lambda x: warn_if_nan(
                  x, "backward " + msg, filename=filename, lineno=lineno
              )
          )
  
      if torch_isnan(value):
          warnings.warn_explicit(
              "Encountered NaN{}".format(": " + msg if msg else "."),
              UserWarning,
              filename,
              lineno,
          )
  
      return value
  
imports: import functools
  import math
  import numbers
  import random
  import sys
  import timeit
  import warnings
  from collections import defaultdict
  from contextlib import contextmanager
  from itertools import zip_longest
  import numpy
  import torch
  from pyro.poutine.util import site_is_subsample
label_desc: Use image node_id 6 for calling a global function with example usage: warn_if_nan(value, msg) and returns: value