-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathtest_utils.py
54 lines (42 loc) · 1.81 KB
/
test_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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD 3-Clause license found in the
# LICENSE file in the root directory of this source tree.
import unittest
from unittest.mock import patch
import torch
from torchao.utils import TorchAOBaseTensor, torch_version_at_least
class TestTorchVersionAtLeast(unittest.TestCase):
def test_torch_version_at_least(self):
test_cases = [
("2.5.0a0+git9f17037", "2.5.0", True),
("2.5.0a0+git9f17037", "2.4.0", True),
("2.5.0.dev20240708+cu121", "2.5.0", True),
("2.5.0.dev20240708+cu121", "2.4.0", True),
("2.5.0", "2.4.0", True),
("2.5.0", "2.5.0", True),
("2.4.0", "2.4.0", True),
("2.4.0", "2.5.0", False),
]
for torch_version, compare_version, expected_result in test_cases:
with patch("torch.__version__", torch_version):
result = torch_version_at_least(compare_version)
self.assertEqual(
result,
expected_result,
f"Failed for torch.__version__={torch_version}, comparing with {compare_version}",
)
class TestTorchAOBaseTensor(unittest.TestCase):
def test_print_arg_types(self):
class MyTensor(TorchAOBaseTensor):
def __new__(cls, data):
shape = data.shape
return torch.Tensor._make_wrapper_subclass(cls, shape) # type: ignore[attr-defined]
def __init__(self, data):
self.data = data
l = torch.nn.Linear(10, 10)
with self.assertRaisesRegex(NotImplementedError, "arg_types"):
l.weight = torch.nn.Parameter(MyTensor(l.weight))
if __name__ == "__main__":
unittest.main()