forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hub.py
256 lines (212 loc) · 10.8 KB
/
test_hub.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Owner(s): ["module: hub"]
import unittest
from unittest.mock import patch
import os
import tempfile
import warnings
import torch
import torch.hub as hub
from torch.testing._internal.common_utils import retry, IS_SANDCASTLE, TestCase
def sum_of_state_dict(state_dict):
s = 0
for _, v in state_dict.items():
s += v.sum()
return s
SUM_OF_HUB_EXAMPLE = 431080
TORCHHUB_EXAMPLE_RELEASE_URL = 'https://github.com/ailzhang/torchhub_example/releases/download/0.1/mnist_init_ones'
@unittest.skipIf(IS_SANDCASTLE, 'Sandcastle cannot ping external')
class TestHub(TestCase):
def setUp(self):
super().setUp()
self.previous_hub_dir = torch.hub.get_dir()
self.tmpdir = tempfile.TemporaryDirectory('hub_dir')
torch.hub.set_dir(self.tmpdir.name)
self.trusted_list_path = os.path.join(torch.hub.get_dir(), "trusted_list")
def tearDown(self):
super().tearDown()
torch.hub.set_dir(self.previous_hub_dir) # probably not needed, but can't hurt
self.tmpdir.cleanup()
def _assert_trusted_list_is_empty(self):
with open(self.trusted_list_path) as f:
assert not f.readlines()
def _assert_in_trusted_list(self, line):
with open(self.trusted_list_path) as f:
assert line in (l.strip() for l in f.readlines())
@retry(Exception, tries=3)
def test_load_from_github(self):
hub_model = hub.load('ailzhang/torchhub_example', 'mnist', source='github', pretrained=True, verbose=False)
self.assertEqual(sum_of_state_dict(hub_model.state_dict()), SUM_OF_HUB_EXAMPLE)
@retry(Exception, tries=3)
def test_load_from_local_dir(self):
local_dir = hub._get_cache_or_reload(
'ailzhang/torchhub_example',
force_reload=False,
trust_repo=True,
calling_fn=None
)
hub_model = hub.load(local_dir, 'mnist', source='local', pretrained=True, verbose=False)
self.assertEqual(sum_of_state_dict(hub_model.state_dict()), SUM_OF_HUB_EXAMPLE)
@retry(Exception, tries=3)
def test_load_from_branch(self):
hub_model = hub.load('ailzhang/torchhub_example:ci/test_slash', 'mnist', pretrained=True, verbose=False)
self.assertEqual(sum_of_state_dict(hub_model.state_dict()), SUM_OF_HUB_EXAMPLE)
@retry(Exception, tries=3)
def test_get_set_dir(self):
previous_hub_dir = torch.hub.get_dir()
with tempfile.TemporaryDirectory('hub_dir') as tmpdir:
torch.hub.set_dir(tmpdir)
self.assertEqual(torch.hub.get_dir(), tmpdir)
self.assertNotEqual(previous_hub_dir, tmpdir)
hub_model = hub.load('ailzhang/torchhub_example', 'mnist', pretrained=True, verbose=False)
self.assertEqual(sum_of_state_dict(hub_model.state_dict()), SUM_OF_HUB_EXAMPLE)
assert os.path.exists(os.path.join(tmpdir, 'ailzhang_torchhub_example_master'))
# Test that set_dir properly calls expanduser()
# non-regression test for https://github.com/pytorch/pytorch/issues/69761
new_dir = os.path.join("~", "hub")
torch.hub.set_dir(new_dir)
self.assertEqual(torch.hub.get_dir(), os.path.expanduser(new_dir))
@retry(Exception, tries=3)
def test_list_entrypoints(self):
entry_lists = hub.list('ailzhang/torchhub_example', trust_repo=True)
self.assertObjectIn('mnist', entry_lists)
@retry(Exception, tries=3)
def test_download_url_to_file(self):
with tempfile.TemporaryDirectory() as tmpdir:
f = os.path.join(tmpdir, 'temp')
hub.download_url_to_file(TORCHHUB_EXAMPLE_RELEASE_URL, f, progress=False)
loaded_state = torch.load(f)
self.assertEqual(sum_of_state_dict(loaded_state), SUM_OF_HUB_EXAMPLE)
@retry(Exception, tries=3)
def test_load_state_dict_from_url(self):
loaded_state = hub.load_state_dict_from_url(TORCHHUB_EXAMPLE_RELEASE_URL)
self.assertEqual(sum_of_state_dict(loaded_state), SUM_OF_HUB_EXAMPLE)
# with name
file_name = "the_file_name"
loaded_state = hub.load_state_dict_from_url(TORCHHUB_EXAMPLE_RELEASE_URL, file_name=file_name)
expected_file_path = os.path.join(torch.hub.get_dir(), 'checkpoints', file_name)
self.assertTrue(os.path.exists(expected_file_path))
self.assertEqual(sum_of_state_dict(loaded_state), SUM_OF_HUB_EXAMPLE)
@retry(Exception, tries=3)
def test_load_legacy_zip_checkpoint(self):
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
hub_model = hub.load('ailzhang/torchhub_example', 'mnist_zip', pretrained=True, verbose=False)
self.assertEqual(sum_of_state_dict(hub_model.state_dict()), SUM_OF_HUB_EXAMPLE)
assert any("will be deprecated in favor of default zipfile" in str(w) for w in ws)
# Test the default zipfile serialization format produced by >=1.6 release.
@retry(Exception, tries=3)
def test_load_zip_1_6_checkpoint(self):
hub_model = hub.load(
'ailzhang/torchhub_example',
'mnist_zip_1_6',
pretrained=True,
verbose=False,
trust_repo=True
)
self.assertEqual(sum_of_state_dict(hub_model.state_dict()), SUM_OF_HUB_EXAMPLE)
@retry(Exception, tries=3)
def test_hub_parse_repo_info(self):
# If the branch is specified we just parse the input and return
self.assertEqual(
torch.hub._parse_repo_info('a/b:c'),
('a', 'b', 'c')
)
# For torchvision, the default branch is main
self.assertEqual(
torch.hub._parse_repo_info('pytorch/vision'),
('pytorch', 'vision', 'main')
)
# For the torchhub_example repo, the default branch is still master
self.assertEqual(
torch.hub._parse_repo_info('ailzhang/torchhub_example'),
('ailzhang', 'torchhub_example', 'master')
)
@retry(Exception, tries=3)
def test_load_commit_from_forked_repo(self):
with self.assertRaisesRegex(ValueError, 'If it\'s a commit from a forked repo'):
torch.hub.load('pytorch/vision:4e2c216', 'resnet18')
@retry(Exception, tries=3)
@patch('builtins.input', return_value='')
def test_trust_repo_false_emptystring(self, patched_input):
with self.assertRaisesRegex(Exception, 'Untrusted repository.'):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=False)
self._assert_trusted_list_is_empty()
patched_input.assert_called_once()
patched_input.reset_mock()
with self.assertRaisesRegex(Exception, 'Untrusted repository.'):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=False)
self._assert_trusted_list_is_empty()
patched_input.assert_called_once()
@retry(Exception, tries=3)
@patch('builtins.input', return_value='no')
def test_trust_repo_false_no(self, patched_input):
with self.assertRaisesRegex(Exception, 'Untrusted repository.'):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=False)
self._assert_trusted_list_is_empty()
patched_input.assert_called_once()
patched_input.reset_mock()
with self.assertRaisesRegex(Exception, 'Untrusted repository.'):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=False)
self._assert_trusted_list_is_empty()
patched_input.assert_called_once()
@retry(Exception, tries=3)
@patch('builtins.input', return_value='y')
def test_trusted_repo_false_yes(self, patched_input):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=False)
self._assert_in_trusted_list("ailzhang_torchhub_example")
patched_input.assert_called_once()
# Loading a second time with "check", we don't ask for user input
patched_input.reset_mock()
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo="check")
patched_input.assert_not_called()
# Loading again with False, we still ask for user input
patched_input.reset_mock()
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=False)
patched_input.assert_called_once()
@retry(Exception, tries=3)
@patch('builtins.input', return_value='no')
def test_trust_repo_check_no(self, patched_input):
with self.assertRaisesRegex(Exception, 'Untrusted repository.'):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo="check")
self._assert_trusted_list_is_empty()
patched_input.assert_called_once()
patched_input.reset_mock()
with self.assertRaisesRegex(Exception, 'Untrusted repository.'):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo="check")
patched_input.assert_called_once()
@retry(Exception, tries=3)
@patch('builtins.input', return_value='y')
def test_trust_repo_check_yes(self, patched_input):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo="check")
self._assert_in_trusted_list("ailzhang_torchhub_example")
patched_input.assert_called_once()
# Loading a second time with "check", we don't ask for user input
patched_input.reset_mock()
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo="check")
patched_input.assert_not_called()
@retry(Exception, tries=3)
def test_trust_repo_true(self):
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=True)
self._assert_in_trusted_list("ailzhang_torchhub_example")
@retry(Exception, tries=3)
def test_trust_repo_builtin_trusted_owners(self):
torch.hub.load('pytorch/vision', 'resnet18', trust_repo="check")
self._assert_trusted_list_is_empty()
@retry(Exception, tries=3)
def test_trust_repo_none(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=None)
assert len(w) == 1
assert issubclass(w[-1].category, UserWarning)
assert "You are about to download and run code from an untrusted repository" in str(w[-1].message)
self._assert_trusted_list_is_empty()
@retry(Exception, tries=3)
def test_trust_repo_legacy(self):
# We first download a repo and then delete the allowlist file
# Then we check that the repo is indeed trusted without a prompt,
# because it was already downloaded in the past.
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo=True)
os.remove(self.trusted_list_path)
torch.hub.load('ailzhang/torchhub_example', 'mnist_zip_1_6', trust_repo="check")
self._assert_trusted_list_is_empty()