forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_extension_utils.py
88 lines (64 loc) · 2.8 KB
/
test_extension_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Owner(s): ["module: PrivateUse1"]
import sys
import torch
from torch.testing._internal.common_utils import run_tests, TestCase
class DummyPrivateUse1Module:
@staticmethod
def is_available():
return True
@staticmethod
def is_autocast_enabled():
return True
@staticmethod
def get_autocast_dtype():
return torch.float16
@staticmethod
def set_autocast_enabled(enable):
pass
@staticmethod
def set_autocast_dtype(dtype):
pass
@staticmethod
def get_amp_supported_dtype():
return [torch.float16]
class TestExtensionUtils(TestCase):
def tearDown(self):
# Clean up
backend_name = torch._C._get_privateuse1_backend_name()
if hasattr(torch, backend_name):
delattr(torch, backend_name)
if f"torch.{backend_name}" in sys.modules:
del sys.modules[f"torch.{backend_name}"]
def test_external_module_register(self):
# Built-in module
with self.assertRaisesRegex(RuntimeError, "The runtime module of"):
torch._register_device_module("cuda", torch.cuda)
# Wrong device type
with self.assertRaisesRegex(RuntimeError, "Expected one of cpu"):
torch._register_device_module("dummmy", DummyPrivateUse1Module)
with self.assertRaises(AttributeError):
torch.privateuseone.is_available() # type: ignore[attr-defined]
torch._register_device_module("privateuseone", DummyPrivateUse1Module)
torch.privateuseone.is_available() # type: ignore[attr-defined]
# No supporting for override
with self.assertRaisesRegex(RuntimeError, "The runtime module of"):
torch._register_device_module("privateuseone", DummyPrivateUse1Module)
def test_external_module_register_with_renamed_backend(self):
torch.utils.rename_privateuse1_backend("foo")
with self.assertRaisesRegex(RuntimeError, "has already been set"):
torch.utils.rename_privateuse1_backend("dummmy")
custom_backend_name = torch._C._get_privateuse1_backend_name()
self.assertEqual(custom_backend_name, "foo")
with self.assertRaises(AttributeError):
torch.foo.is_available() # type: ignore[attr-defined]
with self.assertRaisesRegex(AssertionError, "Tried to use AMP with the"):
with torch.autocast(device_type=custom_backend_name):
pass
torch._register_device_module("foo", DummyPrivateUse1Module)
torch.foo.is_available() # type: ignore[attr-defined]
with torch.autocast(device_type=custom_backend_name):
pass
self.assertEqual(torch._utils._get_device_index("foo:1"), 1)
self.assertEqual(torch._utils._get_device_index(torch.device("foo:2")), 2)
if __name__ == "__main__":
run_tests()