code: def test_copy_grad(self):
      def set_random_grad(link):
          link.zero_grad()
          x = np.random.normal(size=(1, 1)).astype(np.float32)
          y = link(torch.from_numpy(x)) * np.random.normal()
          torch.sum(y).backward()
  
      # When source is not None and target is None
      a = nn.Linear(1, 5)
      b = nn.Linear(1, 5)
      set_random_grad(a)
      b.zero_grad()
      assert a.weight.grad is not None
      assert a.bias.grad is not None
      assert b.weight.grad is None
      assert b.bias.grad is None
      copy_param.copy_grad(target_link=b, source_link=a)
      torch_assert_allclose(a.weight.grad, b.weight.grad)
      torch_assert_allclose(a.bias.grad, b.bias.grad)
      assert a.weight.grad is not b.weight.grad
      assert a.bias.grad is not b.bias.grad
  
      # When both are not None
      a = nn.Linear(1, 5)
      b = nn.Linear(1, 5)
      set_random_grad(a)
      set_random_grad(b)
      assert a.weight.grad is not None
      assert a.bias.grad is not None
      assert b.weight.grad is not None
      assert b.bias.grad is not None
      copy_param.copy_grad(target_link=b, source_link=a)
      torch_assert_allclose(a.weight.grad, b.weight.grad)
      torch_assert_allclose(a.bias.grad, b.bias.grad)
      assert a.weight.grad is not b.weight.grad
      assert a.bias.grad is not b.bias.grad
  
      # When source is None and target is not None
      a = nn.Linear(1, 5)
      b = nn.Linear(1, 5)
      a.zero_grad()
      set_random_grad(b)
      assert a.weight.grad is None
      assert a.bias.grad is None
      assert b.weight.grad is not None
      assert b.bias.grad is not None
      copy_param.copy_grad(target_link=b, source_link=a)
      assert a.weight.grad is None
      assert a.bias.grad is None
      assert b.weight.grad is None
      assert b.bias.grad is None
  
      # When both are None
      a = nn.Linear(1, 5)
      b = nn.Linear(1, 5)
      a.zero_grad()
      b.zero_grad()
      assert a.weight.grad is None
      assert a.bias.grad is None
      assert b.weight.grad is None
      assert b.bias.grad is None
      copy_param.copy_grad(target_link=b, source_link=a)
      assert a.weight.grad is None
      assert a.bias.grad is None
      assert b.weight.grad is None
      assert b.bias.grad is None
  
file_path: pfrl/tests/utils_tests/test_copy_param.py
imports: import unittest
  import numpy
  import torch
  import torch.nn
  from pfrl.testing import torch_assert_allclose
  from pfrl.utils import copy_param
label_desc: Use image node_id 7 for calling the TestCopyParam obj's underlying member method code with example usage: obj.test_copy_grad() without return types