11import unittest
22import pytest
33import os
4+ import gc
5+ import shutil
6+ from time import sleep
47import 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