code: def test_gradient_clipping_by_norm(self):
      model = model_helper.ModelHelper(name="test")
      data = model.net.AddExternalInput("data")
      fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2)
  
      # no operator name set, will use default
      fc2 = brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1)
  
      sigm = model.net.Sigmoid(fc2, "sigm")
      sq = model.net.SquaredL2Distance([sigm, "label"], "sq")
      loss = model.net.SumElements(sq, "loss")
  
      grad_map = model.AddGradientOperators([loss])
  
      grad_map_for_param = {
          key: grad_map[key] for key in ["fc1_w", "fc2_w"]
      }
  
      net_modifier = GradientClipping(
          grad_clip_method="by_norm",
          clip_norm_type="l2_norm",
          clip_threshold=0.1,
      )
  
      net_modifier(model.net, grad_map=grad_map_for_param)
  
      workspace.FeedBlob(
          "data", np.random.rand(10, 4).astype(np.float32)
      )
      workspace.FeedBlob(
          "label", np.random.rand(10, 1).astype(np.float32)
      )
  
      workspace.RunNetOnce(model.param_init_net)
      workspace.RunNetOnce(model.net)
  
      # 5 forward ops + 6 backward ops + 2 * (3 gradient clipping ops)
      self.assertEqual(len(model.net.Proto().op), 17)
  
file_path: pytorch/caffe2/python/modeling/gradient_clipping_test.py
imports: import unittest
  from caffe2.python import workspace, brew, model_helper
  from caffe2.python.modeling.gradient_clipping import GradientClipping
  import numpy
label_desc: Use image node_id 1 for calling the GradientClippingTest obj's underlying member method code with example usage: obj.test_gradient_clipping_by_norm() without return types