Skip to content

Commit 9ba1b96

Browse files
committed
(tests) Overhaul modelExporters tests to compensate for reduced SSD of GitHub action runners; minimum complete set of model is processed and the temporary model files are deleted on the fly to free disk before the next one is processed
1 parent 4768e3c commit 9ba1b96

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

pysipfenn/core/modelExporters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def __init__(self, calculator: Calculator):
7777
model: onnx.load(temp)
7878
})
7979
del temp
80+
# Force garbage collection to free memory immediately
81+
gc.collect()
8082
print(f'Initialized ONNXExporter with models: {list(self.calculator.loadedModels.keys())}')
8183

8284
def simplify(self, model: str) -> None:

pysipfenn/tests/test_ModelExporters.py

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import unittest
22
import pytest
33
import os
4+
import gc
5+
import shutil
6+
from time import sleep
47
import pysipfenn
58

69
# Skip the tests if we're in GitHub Actions and the models haven't been fetched yet
@@ -105,9 +108,29 @@ def testONNXExport(self):
105108
assert self.onnxexp.calculator == self.c
106109

107110
self.onnxexp.simplify('SIPFENN_Krajewski2020_NN9')
108-
self.onnxexp.toFP16('SIPFENN_Krajewski2020_NN24')
109-
110-
self.onnxexp.exportAll()
111+
self.onnxexp.toFP16('SIPFENN_Krajewski2022_NN30')
112+
113+
# Export the simplified SIPFENN_Krajewski2020_NN9 model, assert the file was created, and then clean up
114+
self.onnxexp.export('SIPFENN_Krajewski2020_NN9')
115+
sleep(0.1) # Ensure file system has time to register the new file
116+
assert os.path.exists('SIPFENN_Krajewski2020_NN9_simplified.onnx'), \
117+
'ONNX Exporter did not create the expected simplified file for NN9.'
118+
assert os.path.getsize('SIPFENN_Krajewski2020_NN9_simplified.onnx') > 0, \
119+
'ONNX Exporter created an empty simplified file for NN9.'
120+
os.remove('SIPFENN_Krajewski2020_NN9_simplified.onnx')
121+
122+
# Export the FP16 SIPFENN_Krajewski2022_NN30 model, assert the file was created, and then clean up
123+
self.onnxexp.export('SIPFENN_Krajewski2022_NN30')
124+
sleep(0.1) # Ensure file system has time to register the new file
125+
assert os.path.exists('SIPFENN_Krajewski2022_NN30_fp16.onnx'), \
126+
'ONNX Exporter did not create the expected FP16 file for NN30.'
127+
assert os.path.getsize('SIPFENN_Krajewski2022_NN30_fp16.onnx') > 0, \
128+
'ONNX Exporter created an empty FP16 file for NN30.'
129+
os.remove('SIPFENN_Krajewski2022_NN30_fp16.onnx')
130+
131+
# Explicityly free up the memory used by the ONNX exporter
132+
del self.onnxexp
133+
gc.collect()
111134

112135
@pytest.mark.skipif(IN_GITHUB_ACTIONS, reason="Test depends on the ONNX network files")
113136
def testTorchExport(self):
@@ -116,8 +139,28 @@ def testTorchExport(self):
116139
'''
117140
self.torchexp = pysipfenn.TorchExporter(self.c)
118141
assert self.torchexp.calculator == self.c
119-
120-
self.torchexp.exportAll()
142+
143+
# Export the older NN9 model first to check backward compatibility and then clean up
144+
self.torchexp.export('SIPFENN_Krajewski2020_NN9')
145+
sleep(0.1) # Ensure file system has time to register the new file
146+
assert os.path.exists('SIPFENN_Krajewski2020_NN9.pt'), \
147+
'Torch Exporter did not create the expected file for NN9.'
148+
assert os.path.getsize('SIPFENN_Krajewski2020_NN9.pt') > 0, \
149+
'Torch Exporter created an empty file for NN9.'
150+
os.remove('SIPFENN_Krajewski2020_NN9.pt')
151+
152+
# Export the newer NN30 model and then clean up
153+
self.torchexp.export('SIPFENN_Krajewski2022_NN30')
154+
sleep(0.1) # Ensure file system has time to register the new file
155+
assert os.path.exists('SIPFENN_Krajewski2022_NN30.pt'), \
156+
'Torch Exporter did not create the expected file for NN30.'
157+
assert os.path.getsize('SIPFENN_Krajewski2022_NN30.pt') > 0, \
158+
'Torch Exporter created an empty file for NN30.'
159+
os.remove('SIPFENN_Krajewski2022_NN30.pt')
160+
161+
# Explicityly free up the memory used by the Torch exporter
162+
del self.torchexp
163+
gc.collect()
121164

122165
@pytest.mark.skipif(IN_GITHUB_ACTIONS, reason="Test depends on the ONNX network files")
123166
def testCoreMLExport(self):
@@ -127,4 +170,24 @@ def testCoreMLExport(self):
127170
self.coremlexp = pysipfenn.CoreMLExporter(self.c)
128171
assert self.coremlexp.calculator == self.c
129172

130-
self.coremlexp.exportAll()
173+
# Export the older NN24 model first to check backward compatibility, and then clean up
174+
self.coremlexp.export('SIPFENN_Krajewski2020_NN24')
175+
sleep(0.1) # Ensure file system has time to register the new file
176+
assert os.path.exists('SIPFENN_Krajewski2020_NN24.mlpackage'), \
177+
'CoreML Exporter did not create the expected file for NN24.'
178+
assert os.path.getsize('SIPFENN_Krajewski2020_NN24.mlpackage') > 0, \
179+
'CoreML Exporter created an empty file for NN24.'
180+
shutil.rmtree('SIPFENN_Krajewski2020_NN24.mlpackage')
181+
182+
# Export the newer NN30 model, and then clean up
183+
self.coremlexp.export('SIPFENN_Krajewski2022_NN30')
184+
sleep(0.1) # Ensure file system has time to register the new file
185+
assert os.path.exists('SIPFENN_Krajewski2022_NN30.mlpackage'), \
186+
'CoreML Exporter did not create the expected file for NN30.'
187+
assert os.path.getsize('SIPFENN_Krajewski2022_NN30.mlpackage') > 0, \
188+
'CoreML Exporter created an empty file for NN30.'
189+
shutil.rmtree('SIPFENN_Krajewski2022_NN30.mlpackage')
190+
191+
# Explicityly free up the memory used by the CoreML exporter
192+
del self.coremlexp
193+
gc.collect()

0 commit comments

Comments
 (0)