code: def test_pin_memory(self):
      x = torch.randn(1, device="cuda:1")
  
      # We should have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      self.assertFalse(x.is_pinned())
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      x = torch.randn(3, device="cpu").pin_memory()
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      self.assertTrue(x.is_pinned())
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      x = torch.randn(3, device="cpu", pin_memory=True)
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      x = torch.zeros(3, device="cpu", pin_memory=True)
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      x = torch.empty(3, device="cpu", pin_memory=True)
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
      x = x.pin_memory()
  
      # We should still have only created context on 'cuda:1'
      self.assertFalse(torch._C._cuda_hasPrimaryContext(0))
      self.assertTrue(torch._C._cuda_hasPrimaryContext(1))
  
file_path: pytorch/test/test_cuda_primary_ctx.py
imports: import torch
  from torch.testing._internal.common_utils import TestCase, run_tests, skipIfRocmVersionLessThan, NoTest
  from torch.testing._internal.common_cuda import TEST_CUDA, TEST_MULTIGPU
  import sys
  import unittest
label_desc: Use image node_id 4 for calling the TestCudaPrimaryCtx obj's underlying member method code with example usage: obj.test_pin_memory() without return types