已合并
[sync] PR-36922: test(npu): add primary context isolation tests #37661
ascend-robot创建于 6月5日
[sync] PR-36922: test(npu): add primary context isolation tests #37661
已合并
ascend-robot创建于 6月5日
1 个文件变更+120-0
Atest/distributed/test_npu_primary_ctx.py+120-0
@@ -0,0 +1,120 @@
1+# Owner(s): ["module: npu"]
2+ 
3+# NOTE: this needs to be run in a brand new process
4+ 
5+import os
6+import unittest
7+ 
8+import torch
9+import torch_npu
10+from torch.testing._internal.common_distributed import MultiProcessTestCase
11+from torch.testing._internal.common_utils import run_tests
12+ 
13+TEST_MULTINPU = torch_npu.npu.device_count() > 1
14+ 
15+@unittest.skipIf(not TEST_MULTINPU, "only one NPU detected")
16+class TestNpuPrimaryCtx(MultiProcessTestCase):
17+ CTX_ALREADY_CREATED_ERR_MSG = (
18+ "Tests defined in test_npu_primary_ctx.py must be run in a process "
19+ "where NPU contexts are never created."
20+ )
21+ 
22+ @property
23+ def world_size(self):
24+ return 1
25+ 
26+ def setUp(self):
27+ super().setUp()
28+ self._spawn_processes()
29+ 
30+ def _check_no_ctx(self):
31+ for device in range(torch_npu.npu.device_count()):
32+ self.assertFalse(
33+ torch_npu._C._npu_hasPrimaryContext(device),
34+ TestNpuPrimaryCtx.CTX_ALREADY_CREATED_ERR_MSG,
35+ )
36+ 
37+ def test_str_repr(self):
38+ self._check_no_ctx()
39+ x = torch.randn(1, device="npu:1")
40+ 
41+ # We should have only created context on 'npu:1'
42+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
43+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
44+ 
45+ str(x)
46+ repr(x)
47+ 
48+ # We should still have only created context on 'npu:1'
49+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
50+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
51+ 
52+ def test_copy(self):
53+ self._check_no_ctx()
54+ x = torch.randn(1, device="npu:1")
55+ 
56+ # We should have only created context on 'npu:1'
57+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
58+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
59+ 
60+ y = torch.randn(1, device="cpu")
61+ y.copy_(x)
62+ 
63+ # We should still have only created context on 'npu:1'
64+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
65+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
66+ 
67+ def test_pin_memory(self):
68+ self._check_no_ctx()
69+ x = torch.randn(1, device="npu:1")
70+ 
71+ # We should have only created context on 'npu:1'
72+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
73+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
74+ 
75+ self.assertFalse(x.is_pinned())
76+ 
77+ # We should still have only created context on 'npu:1'
78+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
79+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
80+ 
81+ x = torch.randn(3, device="cpu").pin_memory()
82+ 
83+ # We should still have only created context on 'npu:1'
84+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
85+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
86+ 
87+ self.assertTrue(x.is_pinned())
88+ 
89+ # We should still have only created context on 'npu:1'
90+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
91+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
92+ 
93+ x = torch.randn(3, device="cpu", pin_memory=True)
94+ 
95+ # We should still have only created context on 'npu:1'
96+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
97+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
98+ 
99+ x = torch.zeros(3, device="cpu", pin_memory=True)
100+ 
101+ # We should still have only created context on 'npu:1'
102+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
103+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
104+ 
105+ x = torch.empty(3, device="cpu", pin_memory=True)
106+ 
107+ # We should still have only created context on 'npu:1'
108+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
109+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
110+ 
111+ x = x.pin_memory()
112+ 
113+ # We should still have only created context on 'npu:1'
114+ self.assertFalse(torch_npu._C._npu_hasPrimaryContext(0))
115+ self.assertTrue(torch_npu._C._npu_hasPrimaryContext(1))
116+ 
117+ 
118+if __name__ == "__main__":
119+ os.environ["ACL_OP_INIT_MODE"] = "1"
120+ run_tests()