def test_clip_grad_norm_(self):
    params = torch.nn.Parameter(torch.zeros(5)).requires_grad_(False)
    grad_norm = utils.clip_grad_norm_(params, 1.0)
    self.assertTrue(torch.is_tensor(grad_norm))
    self.assertEqual(grad_norm, 0.0)
    params = [torch.nn.Parameter(torch.zeros(5)) for i in range(3)]
    for p in params:
        p.grad = torch.full((5,), fill_value=2.0)
    grad_norm = utils.clip_grad_norm_(params, 1.0)
    exp_grad_norm = torch.full((15,), fill_value=2.0).norm()
    self.assertTrue(torch.is_tensor(grad_norm))
    self.assertEqual(grad_norm, exp_grad_norm)
    grad_norm = utils.clip_grad_norm_(params, 1.0)
    self.assertAlmostEqual(grad_norm, torch.tensor(1.0))
def test_clip_grad_norm_(self):
    params = torch.nn.Parameter(torch.zeros(5)).requires_grad_(False)
    grad_norm = utils.clip_grad_norm_(params, 1.0)
    self.assertTrue(torch.is_tensor(grad_norm))
    self.assertEqual(grad_norm, 0.0)
    params = [torch.nn.Parameter(torch.zeros(5)) for i in range(3)]
    for p in params:
        p.grad = torch.full((5,), fill_value=2.0)
    grad_norm = utils.clip_grad_norm_(params, 1.0)
    exp_grad_norm = torch.full((15,), fill_value=2.0).norm()
    self.assertTrue(torch.is_tensor(grad_norm))
    self.assertEqual(grad_norm, exp_grad_norm)
    grad_norm = utils.clip_grad_norm_(params, 1.0)
    self.assertAlmostEqual(grad_norm, torch.tensor(1.0))