class_docstr: Detects the first NaN or Inf in forward and/or backward pass and logs, together with the module name
class_docstr_tok: ["Detects","the","first","NaN","or","Inf","in","forward","and\/or","backward","pass","and","logs",",","together","with","the","module","name"]
code: def _apply(self, module, inp, x, backward):
      if torch.is_tensor(x):
          if isinstance(inp, tuple) and len(inp) > 0:
              inp = inp[0]
          err = self._detect(x, module.__module_name, backward)
          if err is not None:
              if torch.is_tensor(inp) and not backward:
                  err += f" input max: {inp.max().item()}, input min: {inp.min().item()}"
  
              has_printed_attr = (
                  "has_printed_b" if backward else "has_printed_f"
              )
              logger.warning(err)
              setattr(self, has_printed_attr, True)
      elif isinstance(x, dict):
          for v in x.values():
              self._apply(module, inp, v, backward)
      elif isinstance(x, list) or isinstance(x, tuple):
          for v in x:
              self._apply(module, inp, v, backward)
  
file_path: fairseq/fairseq/nan_detector.py
imports: import logging
  import torch
label_desc: Use image node_id 7 for calling the NanDetector obj's underlying member method code with example usage: obj._apply(module, inp, x, backward) without return types