code: def get_forward_hook(name, trainer, rank, logger, dump_to_file=False):
      """
      A forward hook to dump all of the module input and output norms. It is called at every time after forward() has computed an output.
      Only float type input/output tensor norms are computed.
      For more details about the forward hook, check https://pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_hook.html
  
      Args:
          name: tensor name
          trainer: PTL trainer
          rank: worker rank
          logger: PTL log function
          dump_to_file:  wether dump the csv file to the disk
      """
      if dump_to_file:
          os.makedirs("debug_info", exist_ok=True)
          fp = open(f"debug_info/forward_{name}_rank{rank}.txt", "w")
          header = False
  
      def forward_hook(module, inputs, outputs):
          nonlocal header
          nonlocal fp
          if trainer.training:
              values = []
              headers = []
              for n, i in enumerate(inputs):
                  if isinstance(i, torch.Tensor) and (
                      i.dtype == torch.float
                      or i.dtype == torch.half
                      or i.dtype == torch.bfloat16
                  ):
                      if not header:
                          headers.append("input")
                      input_norm = i.data.norm()
                      values.append(f"{input_norm}")
                      logger(
                          f"debug_info_forward/{name}_rank{rank}_input{n}",
                          input_norm,
                      )
              if isinstance(outputs, tuple):
                  for n, i in enumerate(outputs):
                      if isinstance(i, torch.Tensor) and (
                          i.dtype == torch.float
                          or i.dtype == torch.half
                          or i.dtype == torch.bfloat16
                      ):
                          if not header:
                              headers.append("output")
                          output_norm = i.data.norm()
                          values.append(f"{output_norm}")
                          logger(
                              f"debug_info_forward/{name}_rank{rank}_output{n}",
                              output_norm,
                          )
              else:
                  headers.append("output")
                  values.append(f"{outputs.data.norm()}")
              values.append(f"{trainer.global_step}")
              if not header:
                  headers.append("step")
                  fp.write(",".join(headers) + "\n")
                  header = True
              fp.write(",".join(values) + "\n")
          fp.flush()
  
      return forward_hook
  
imports: import os
  import torch
label_desc: Use image node_id 1 for calling a global function with example usage: get_forward_hook(name, trainer, rank, logger, dump_to_file) and returns: forward_hook