code: def clip_gradients(batch_result, model, max_grad_norm):
      
      if max_grad_norm is not None:
          grad_norm = torch.nn.utils.clip_grad_norm_(
              filter(lambda p: p.requires_grad, model.parameters()),
              max_norm=max_grad_norm
          )
      else:
          grad_norm = 0.0
  
      batch_result['grad_norm'] = grad_norm
docs: Clip gradients to a given maximum length