2222# Local
2323from fms_mo .utils .qconfig_utils import qconfig_load , qconfig_save
2424from tests .models .test_model_utils import (
25- delete_config ,
25+ delete_file ,
2626 load_json ,
2727 save_json ,
2828 save_serialized_json ,
2929)
3030
31+
32+ @pytest .fixture (autouse = True )
33+ def delete_files ():
34+ """
35+ Delete any known files lingering before starting test
36+ """
37+ delete_file ("qcfg.json" )
38+ delete_file ("keys_to_save.json" )
39+
40+
3141#########
3242# Tests #
3343#########
@@ -45,7 +55,6 @@ def test_save_config_warn_bad_pair(
4555 bad_pair (tuple): A pair that can't be serialized for qconfig_save
4656 """
4757 key , val = bad_pair
48- delete_config ()
4958
5059 # Add bad key,val pair and save ; should generate UserWarning(s) for removing bad pair
5160 config_fp32 [key ] = val
@@ -56,8 +65,6 @@ def test_save_config_warn_bad_pair(
5665 loaded_config = load_json ("qcfg.json" ) # load json as is - do not modify
5766 assert key not in loaded_config
5867
59- delete_config ()
60-
6168
6269def test_save_config_wanted_pairs (
6370 config_fp32 : dict ,
@@ -71,7 +78,6 @@ def test_save_config_wanted_pairs(
7178 wanted_pair (tuple): A pair that needs to be re-init if not present in qconfig_save
7279 """
7380 key , default_val = wanted_pair
74- delete_config ()
7581
7682 # Delete wanted pair from config and save ; should be reset to default
7783 if key in config_fp32 :
@@ -82,8 +88,6 @@ def test_save_config_wanted_pairs(
8288 loaded_config = load_json ()
8389 assert loaded_config .get (key ) == default_val
8490
85- delete_config ()
86-
8791
8892def test_save_config_with_qcfg_save (
8993 config_fp32 : dict ,
@@ -96,7 +100,6 @@ def test_save_config_with_qcfg_save(
96100 config_fp32 (dict): Config for fp32 quantization
97101 save_list (list): List of variables to save in a quantized config.
98102 """
99- delete_config ()
100103 config_fp32 ["keys_to_save" ] = save_list
101104
102105 qconfig_save (config_fp32 , minimal = False )
@@ -114,7 +117,6 @@ def test_save_config_with_qcfg_save(
114117 assert key in loaded_config
115118 assert loaded_config .get (key ) == config_fp32 .get (key )
116119
117- delete_config ()
118120 del config_fp32 ["keys_to_save" ]
119121
120122
@@ -129,10 +131,6 @@ def test_save_config_with_recipe_save(
129131 config_fp32 (dict): Config for fp32 quantization
130132 save_list (list): List of variables to save in a quantized config.
131133 """
132- # Delete both qcfg and the save.json before starting
133- delete_config ()
134- delete_config ("keys_to_save.json" )
135-
136134 # Save new "save.json"
137135 save_path = "keys_to_save.json"
138136 save_json (save_list , file_path = save_path )
@@ -153,9 +151,6 @@ def test_save_config_with_recipe_save(
153151 assert key in loaded_config
154152 assert loaded_config .get (key ) == config_fp32 .get (key )
155153
156- delete_config ()
157- delete_config ("keys_to_save.json" )
158-
159154
160155def test_save_config_minimal (
161156 config_fp32 : dict ,
@@ -166,8 +161,6 @@ def test_save_config_minimal(
166161 Args:
167162 config_fp32 (dict): Config for fp32 quantization
168163 """
169- delete_config ()
170-
171164 qconfig_save (config_fp32 , minimal = True )
172165
173166 # Check that saved qcfg matches
@@ -180,8 +173,6 @@ def test_save_config_minimal(
180173 # No items should exist - default config should be completely removed
181174 assert len (loaded_config ) == 0
182175
183- delete_config ()
184-
185176
186177def test_double_qconfig_save (
187178 config_fp32 : dict ,
@@ -192,14 +183,8 @@ def test_double_qconfig_save(
192183 Args:
193184 config_fp32 (dict): Config for fp32 quantization
194185 """
195- delete_config ()
196-
197- # Creating a qcfg, then saving again will cause a warning -> ignore it
198- with pytest .warns (UserWarning , match = "qcfg.json already exist, will overwrite." ):
199- qconfig_save (config_fp32 , minimal = False )
200- qconfig_save (config_fp32 , minimal = False )
201-
202- delete_config ()
186+ qconfig_save (config_fp32 , minimal = False )
187+ qconfig_save (config_fp32 , minimal = False )
203188
204189
205190def test_qconfig_save_list_as_dict (
@@ -211,7 +196,7 @@ def test_qconfig_save_list_as_dict(
211196 Args:
212197 config_fp32 (dict): Config for fp32 quantization
213198 """
214- delete_config ()
199+ delete_file ()
215200
216201 # Fill in keys_to_save as dict with nonsense val
217202 config_fp32 ["keys_to_save" ] = {
@@ -226,8 +211,6 @@ def test_qconfig_save_list_as_dict(
226211 with pytest .raises (ValueError ):
227212 qconfig_save (config_fp32 , minimal = True )
228213
229- delete_config ()
230-
231214
232215def test_qconfig_save_recipe_as_dict (
233216 config_fp32 : dict ,
@@ -238,8 +221,6 @@ def test_qconfig_save_recipe_as_dict(
238221 Args:
239222 config_fp32 (dict): Config for fp32 quantization
240223 """
241- delete_config ()
242-
243224 # Fill in keys_to_save as dict with nonsense val
244225 save_dict = {
245226 "qa_mode" : None ,
@@ -254,8 +235,6 @@ def test_qconfig_save_recipe_as_dict(
254235 with pytest .raises (ValueError ):
255236 qconfig_save (config_fp32 , recipe = "keys_to_save.json" , minimal = True )
256237
257- delete_config ()
258-
259238
260239def test_qconfig_load_with_recipe_as_list (
261240 config_fp32 : dict ,
@@ -266,17 +245,13 @@ def test_qconfig_load_with_recipe_as_list(
266245 Args:
267246 config_fp32 (dict): Config for fp32 quantization
268247 """
269- delete_config ()
270-
271248 config_list = list (config_fp32 .keys ())
272249
273250 save_json (config_list , file_path = "qcfg.json" )
274251
275252 with pytest .raises (ValueError ):
276253 _ = qconfig_load (fname = "qcfg.json" )
277254
278- delete_config ()
279-
280255
281256def test_load_config_restored_pair (
282257 config_fp32 : dict ,
@@ -290,7 +265,6 @@ def test_load_config_restored_pair(
290265 wanted_pair (tuple): A pair that needs to be re-init if not present in qconfig_load
291266 """
292267 key , default_val = wanted_pair
293- delete_config ()
294268
295269 if key in config_fp32 :
296270 del config_fp32 [key ]
@@ -302,8 +276,6 @@ def test_load_config_restored_pair(
302276 loaded_config = qconfig_load ("qcfg.json" )
303277 assert loaded_config .get (key ) == default_val
304278
305- delete_config ()
306-
307279
308280def test_load_config_required_pair (
309281 config_fp32 : dict ,
@@ -317,7 +289,6 @@ def test_load_config_required_pair(
317289 required_pair (tuple): A pair that needs to be re-init if not present in qconfig_load
318290 """
319291 key , default_val = required_pair
320- delete_config ()
321292
322293 if key in config_fp32 :
323294 del config_fp32 [key ]
0 commit comments