Skip to content

Commit 8c5bede

Browse files
authored
perf(test): cache SwinUNETR SSL weights instead of re-downloading (#8987)
### Description Cache the SwinUNETR SSL pretrained weights (~750 MB) under `tests/testing_data/` instead of a per-run `tempfile.TemporaryDirectory()`, so `test_filter_swinunetr` reuses them across runs instead of re-downloading every time. `download_url` skips the download when the cached file's hash matches, so repeated runs replace a ~750 MB download with a local hash-check (~5x faster). The cache path is gitignored and behavior/coverage are unchanged. ### Changes - `tests/networks/nets/test_swin_unetr.py`: download the weights to `tests/testing_data/temp_ssl_pretrained_weights.pth` (new `TESTS_PATH` constant) instead of a temporary directory; drop the `tempfile` import and add `from pathlib import Path`. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests`. - [ ] In-line docstrings updated. - [ ] Documentation updated. Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent 5a61f40 commit 8c5bede

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

‎tests/networks/nets/test_swin_unetr.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from __future__ import annotations
1313

1414
import os
15-
import tempfile
1615
import unittest
16+
from pathlib import Path
1717
from unittest import skipUnless
1818

1919
import torch
@@ -45,6 +45,8 @@
4545
HAS_NVSUBQ = is_nvsubquadratic_available()
4646
HAS_CUDA = torch.cuda.is_available()
4747

48+
TESTS_PATH = Path(__file__).parents[2]
49+
4850
test_merging_mode = ["mergingv2", "merging", PatchMerging, PatchMergingV2]
4951
checkpoint_vals = [True, False]
5052

@@ -134,19 +136,18 @@ def test_patch_merging(self):
134136
@skip_if_no_cuda
135137
def test_filter_swinunetr(self, input_param, key, value):
136138
with skip_if_downloading_fails():
137-
with tempfile.TemporaryDirectory() as tempdir:
138-
file_name = "ssl_pretrained_weights.pth"
139-
data_spec = testing_data_config("models", f"{file_name.split('.', 1)[0]}")
140-
weight_path = os.path.join(tempdir, file_name)
141-
download_url(
142-
data_spec["url"], weight_path, hash_val=data_spec["hash_val"], hash_type=data_spec["hash_type"]
143-
)
144-
145-
ssl_weight = torch.load(weight_path, weights_only=True)["model"]
146-
net = SwinUNETR(**input_param)
147-
dst_dict, loaded, not_loaded = copy_model_state(net, ssl_weight, filter_func=filter_swinunetr)
148-
assert_allclose(dst_dict[key][:8], value, atol=1e-4, rtol=1e-4, type_test=False)
149-
self.assertTrue(len(loaded) == 157 and len(not_loaded) == 2)
139+
file_name = "ssl_pretrained_weights.pth"
140+
data_spec = testing_data_config("models", f"{file_name.split('.', 1)[0]}")
141+
weight_path = os.path.join(TESTS_PATH, "testing_data", f"temp_{file_name}")
142+
download_url(
143+
data_spec["url"], weight_path, hash_val=data_spec["hash_val"], hash_type=data_spec["hash_type"]
144+
)
145+
146+
ssl_weight = torch.load(weight_path, weights_only=True)["model"]
147+
net = SwinUNETR(**input_param)
148+
dst_dict, loaded, not_loaded = copy_model_state(net, ssl_weight, filter_func=filter_swinunetr)
149+
assert_allclose(dst_dict[key][:8], value, atol=1e-4, rtol=1e-4, type_test=False)
150+
self.assertTrue(len(loaded) == 157 and len(not_loaded) == 2)
150151

151152

152153
# Backward-compat reference for SwinUNETR(use_hyena=False), feature_size=12, img_size=64^3,

0 commit comments

Comments
 (0)