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 __exit__(self, exc_type, exc_value, exc_traceback):
      # Dump out all model gnorms to enable better debugging
      norm = {}
      gradients = {}
      for name, param in self.named_parameters:
          if param.grad is not None:
              grad_norm = torch.norm(param.grad.data.float(), p=2)
              norm[name] = param.norm().item()
              if (
                  torch.isnan(grad_norm).any()
                  or torch.isinf(grad_norm).any()
              ):
                  gradients[name] = param.grad.data
      if len(gradients) > 0:
          logger.info("Detected nan/inf grad norm, dumping norms...")
          logger.info(f"norms: {norm}")
          logger.info(f"gradients: {gradients}")
  
      self.close()
  
file_path: fairseq/fairseq/nan_detector.py
imports: import logging
  import torch
label_desc: Use image node_id 3 for calling the NanDetector obj's underlying member method code with example usage: obj.__exit__(exc_type, exc_value, exc_traceback) without return types