code: def test_gradient_clipping(self):
      apex_available = True
      try:
          import apex  # noqa F401
      except ImportError:
          apex_available = False
  
      def train_with_clipped_gradients(amp_args=None):
          task = build_task(get_fast_test_task_config())
          task.set_num_epochs(1)
          task.set_model(SimpleModel())
          task.set_loss(SimpleLoss())
          task.set_meters([])
          task.set_use_gpu(torch.cuda.is_available())
          task.set_clip_grad_norm(0.5)
          task.set_amp_args(amp_args)
  
          task.set_optimizer(SGD(lr=1))
  
          trainer = LocalTrainer()
          trainer.train(task)
  
          return task.model.param.grad.norm()
  
      grad_norm = train_with_clipped_gradients(None)
      self.assertAlmostEqual(grad_norm, 0.5, delta=1e-2)
  
      if apex_available and torch.cuda.is_available():
          grad_norm = train_with_clipped_gradients({"opt_level": "O2"})
          self.assertAlmostEqual(grad_norm, 0.5, delta=1e-2)
  
imports: import copy
  import itertools
  import shutil
  import tempfile
  import unittest
  import torch
  import torch.nn
  from classy_vision.dataset import build_dataset
  from classy_vision.generic.distributed_util import is_distributed_training_run
  from classy_vision.generic.util import get_checkpoint_dict, get_torch_version
  from classy_vision.hooks import CheckpointHook, LossLrMeterLoggingHook
  from classy_vision.losses import build_loss, ClassyLoss, register_loss
  from classy_vision.models import build_model, ClassyModel
  from classy_vision.optim import build_optimizer, SGD
  from classy_vision.tasks import build_task, ClassificationTask
  from classy_vision.trainer import LocalTrainer
  from test.generic.config_utils import get_fast_test_task_config, get_test_task_config
  from test.generic.utils import compare_model_state, compare_samples, compare_states, LimitedPhaseTrainer
label_desc: Use image node_id 18 for calling the TestClassificationTask obj's underlying member method code with example usage: obj.test_gradient_clipping() and returns: task