11# SPDX-License-Identifier: LGPL-3.0-or-later
2- import tempfile
32import json
43import os
54import shutil
5+ import tempfile
66import unittest
77from copy import (
88 deepcopy ,
1414 NoReturn ,
1515)
1616
17- import torch
1817import h5py
1918import numpy as np
19+ import torch
2020
2121from deepmd .pt .entrypoints .main import (
2222 get_trainer ,
3030from deepmd .pt .utils .multi_task import (
3131 preprocess_shared_params ,
3232)
33+ from deepmd .pt .utils .utils import (
34+ to_numpy_array ,
35+ to_torch_tensor ,
36+ )
3337from deepmd .utils .argcheck import (
3438 normalize ,
3539)
3640from deepmd .utils .compat import (
3741 update_deepmd_input ,
3842)
39- from deepmd .pt .utils .utils import (
40- to_numpy_array ,
41- to_torch_tensor ,
42- )
4343from deepmd .utils .path import (
4444 DPPath ,
4545)
46+
4647from .model .test_permutation import (
4748 model_se_e2_a ,
4849)
@@ -204,38 +205,41 @@ def raise_error() -> NoReturn:
204205 arefs_inv , to_numpy_array (fitting .aparam_inv_std )
205206 )
206207
208+
207209def get_weighted_fitting_stat (model_prob : list , * stat_arrays , protection : float ):
208210 n_arrays = len (stat_arrays )
209211 assert len (model_prob ) == n_arrays
210212
211213 nframes = [stat .shape [0 ] for stat in stat_arrays ]
212214 sums = [stat .sum (axis = 0 ) for stat in stat_arrays ]
213- squared_sums = [(stat ** 2 ).sum (axis = 0 ) for stat in stat_arrays ]
215+ squared_sums = [(stat ** 2 ).sum (axis = 0 ) for stat in stat_arrays ]
214216
215217 weighted_sum = sum (model_prob [i ] * sums [i ] for i in range (n_arrays ))
216218 total_weighted_frames = sum (model_prob [i ] * nframes [i ] for i in range (n_arrays ))
217219 weighted_avg = weighted_sum / total_weighted_frames
218220
219221 weighted_square_sum = sum (model_prob [i ] * squared_sums [i ] for i in range (n_arrays ))
220222 weighted_square_avg = weighted_square_sum / total_weighted_frames
221- weighted_std = np .sqrt (weighted_square_avg - weighted_avg ** 2 )
223+ weighted_std = np .sqrt (weighted_square_avg - weighted_avg ** 2 )
222224 weighted_std = np .where (weighted_std < protection , protection , weighted_std )
223-
225+
224226 return weighted_avg , weighted_std
225227
226- class TestMultiTaskFittingStat (unittest .TestCase ):
227228
229+ class TestMultiTaskFittingStat (unittest .TestCase ):
228230 def setUp (self ) -> None :
229231 multitask_sharefit_template_json = str (
230232 Path (__file__ ).parent / "water/multitask_sharefit.json"
231233 )
232234 with open (multitask_sharefit_template_json ) as f :
233- multitask_se_e2_a = json .load (f )
235+ multitask_se_e2_a = json .load (f )
234236 multitask_se_e2_a ["model" ]["shared_dict" ]["my_descriptor" ] = model_se_e2_a [
235237 "descriptor"
236238 ]
237239 self .data_file = [str (Path (__file__ ).parent / "water/data/data_0" )]
238- self .data_file_without_fparam = [str (Path (__file__ ).parent / "water/data/data_1" )]
240+ self .data_file_without_fparam = [
241+ str (Path (__file__ ).parent / "water/data/data_1" )
242+ ]
239243 self .data_file_single = [str (Path (__file__ ).parent / "water/data/single" )]
240244 self .stat_files = "se_e2_a_share_fit"
241245 os .makedirs (self .stat_files , exist_ok = True )
@@ -249,12 +253,14 @@ def setUp(self) -> None:
249253 )
250254 self .config ["model" ]["shared_dict" ]["my_fitting" ]["numb_fparam" ] = 2
251255 self .default_fparam = [1.0 , 0.0 ]
252- self .config ["model" ]["shared_dict" ]["my_fitting" ]["default_fparam" ] = self .default_fparam
256+ self .config ["model" ]["shared_dict" ]["my_fitting" ]["default_fparam" ] = (
257+ self .default_fparam
258+ )
253259 self .config ["training" ]["numb_steps" ] = 1
254260 self .config ["training" ]["save_freq" ] = 1
255261
256262 self .origin_config = deepcopy (self .config )
257-
263+
258264 def test_sharefitting_with_fparam (self ):
259265 # test multitask training with fparam
260266 self .config = deepcopy (self .origin_config )
@@ -287,12 +293,12 @@ def test_sharefitting_with_fparam(self):
287293 # check fparam shared
288294 multi_state_dict = trainer .wrapper .model .state_dict ()
289295 torch .testing .assert_close (
290- multi_state_dict [' model_1.atomic_model.fitting_net.fparam_avg' ],
291- multi_state_dict [' model_2.atomic_model.fitting_net.fparam_avg' ]
296+ multi_state_dict [" model_1.atomic_model.fitting_net.fparam_avg" ],
297+ multi_state_dict [" model_2.atomic_model.fitting_net.fparam_avg" ],
292298 )
293299 torch .testing .assert_close (
294- multi_state_dict [' model_1.atomic_model.fitting_net.fparam_inv_std' ],
295- multi_state_dict [' model_2.atomic_model.fitting_net.fparam_inv_std' ]
300+ multi_state_dict [" model_1.atomic_model.fitting_net.fparam_inv_std" ],
301+ multi_state_dict [" model_2.atomic_model.fitting_net.fparam_inv_std" ],
296302 )
297303
298304 # check fitting stat in stat_file is correct
@@ -301,31 +307,39 @@ def test_sharefitting_with_fparam(self):
301307 fparam_data1 = np .load (f"{ self .data_file [0 ]} /set.000/fparam.npy" )
302308 fparam_data2 = np .load (f"{ self .data_file_single [0 ]} /set.000/fparam.npy" )
303309 np .testing .assert_almost_equal (
304- fparam_stat_model1 [:,0 ], [fparam_data1 .shape [0 ]] * 2
310+ fparam_stat_model1 [:, 0 ], [fparam_data1 .shape [0 ]] * 2
305311 )
306312 np .testing .assert_almost_equal (
307- fparam_stat_model1 [:,1 ], fparam_data1 .sum (axis = 0 )
313+ fparam_stat_model1 [:, 1 ], fparam_data1 .sum (axis = 0 )
308314 )
309315 np .testing .assert_almost_equal (
310- fparam_stat_model1 [:,2 ], (fparam_data1 ** 2 ).sum (axis = 0 )
316+ fparam_stat_model1 [:, 2 ], (fparam_data1 ** 2 ).sum (axis = 0 )
311317 )
312318 np .testing .assert_almost_equal (
313- fparam_stat_model2 [:,0 ], [fparam_data2 .shape [0 ]] * 2
319+ fparam_stat_model2 [:, 0 ], [fparam_data2 .shape [0 ]] * 2
314320 )
315321 np .testing .assert_almost_equal (
316- fparam_stat_model2 [:,1 ], fparam_data2 .sum (axis = 0 )
322+ fparam_stat_model2 [:, 1 ], fparam_data2 .sum (axis = 0 )
317323 )
318324 np .testing .assert_almost_equal (
319- fparam_stat_model2 [:,2 ], (fparam_data2 ** 2 ).sum (axis = 0 )
325+ fparam_stat_model2 [:, 2 ], (fparam_data2 ** 2 ).sum (axis = 0 )
320326 )
321327
322328 # check shared fitting stat is computed correctly
323- weighted_avg , weighted_std = get_weighted_fitting_stat (model_prob , fparam_data1 , fparam_data2 , protection = 1e-2 )
329+ weighted_avg , weighted_std = get_weighted_fitting_stat (
330+ model_prob , fparam_data1 , fparam_data2 , protection = 1e-2
331+ )
324332 np .testing .assert_almost_equal (
325- weighted_avg , to_numpy_array (multi_state_dict ['model_1.atomic_model.fitting_net.fparam_avg' ])
333+ weighted_avg ,
334+ to_numpy_array (
335+ multi_state_dict ["model_1.atomic_model.fitting_net.fparam_avg" ]
336+ ),
326337 )
327338 np .testing .assert_almost_equal (
328- 1 / weighted_std , to_numpy_array (multi_state_dict ['model_1.atomic_model.fitting_net.fparam_inv_std' ])
339+ 1 / weighted_std ,
340+ to_numpy_array (
341+ multi_state_dict ["model_1.atomic_model.fitting_net.fparam_inv_std" ]
342+ ),
329343 )
330344
331345 def test_sharefitting_using_default_fparam (self ):
@@ -344,11 +358,9 @@ def test_sharefitting_using_default_fparam(self):
344358 self .config ["training" ]["data_dict" ]["model_3" ] = deepcopy (
345359 self .config ["training" ]["data_dict" ]["model_2" ]
346360 )
347- self .config ["training" ]["data_dict" ]["model_3" ]["stat_file" ] = (
348- self .config ["training" ]["data_dict" ]["model_3" ]["stat_file" ].replace (
349- "model_2" , "model_3"
350- )
351- )
361+ self .config ["training" ]["data_dict" ]["model_3" ]["stat_file" ] = self .config [
362+ "training"
363+ ]["data_dict" ]["model_3" ]["stat_file" ].replace ("model_2" , "model_3" )
352364 self .config ["model" ]["shared_dict" ]["my_fitting" ]["dim_case_embd" ] = 3
353365
354366 model_prob = [0.1 , 0.3 , 0.6 ]
@@ -380,9 +392,15 @@ def test_sharefitting_using_default_fparam(self):
380392 data_stat_protect = 5e-3
381393 self .config ["model" ]["model_dict" ]["model_1" ]["data_stat_nbatch" ] = 3
382394 self .config ["model" ]["model_dict" ]["model_3" ]["data_stat_nbatch" ] = 100
383- self .config ["model" ]["model_dict" ]["model_1" ]["data_stat_protect" ] = data_stat_protect
384- self .config ["model" ]["model_dict" ]["model_2" ]["data_stat_protect" ] = data_stat_protect
385- self .config ["model" ]["model_dict" ]["model_3" ]["data_stat_protect" ] = data_stat_protect
395+ self .config ["model" ]["model_dict" ]["model_1" ]["data_stat_protect" ] = (
396+ data_stat_protect
397+ )
398+ self .config ["model" ]["model_dict" ]["model_2" ]["data_stat_protect" ] = (
399+ data_stat_protect
400+ )
401+ self .config ["model" ]["model_dict" ]["model_3" ]["data_stat_protect" ] = (
402+ data_stat_protect
403+ )
386404
387405 self .config ["model" ], self .shared_links = preprocess_shared_params (
388406 self .config ["model" ]
@@ -395,20 +413,20 @@ def test_sharefitting_using_default_fparam(self):
395413 # check fparam shared
396414 multi_state_dict = trainer .wrapper .model .state_dict ()
397415 torch .testing .assert_close (
398- multi_state_dict [' model_1.atomic_model.fitting_net.fparam_avg' ],
399- multi_state_dict [' model_2.atomic_model.fitting_net.fparam_avg' ]
416+ multi_state_dict [" model_1.atomic_model.fitting_net.fparam_avg" ],
417+ multi_state_dict [" model_2.atomic_model.fitting_net.fparam_avg" ],
400418 )
401419 torch .testing .assert_close (
402- multi_state_dict [' model_1.atomic_model.fitting_net.fparam_avg' ],
403- multi_state_dict [' model_3.atomic_model.fitting_net.fparam_avg' ]
420+ multi_state_dict [" model_1.atomic_model.fitting_net.fparam_avg" ],
421+ multi_state_dict [" model_3.atomic_model.fitting_net.fparam_avg" ],
404422 )
405423 torch .testing .assert_close (
406- multi_state_dict [' model_1.atomic_model.fitting_net.fparam_inv_std' ],
407- multi_state_dict [' model_2.atomic_model.fitting_net.fparam_inv_std' ]
424+ multi_state_dict [" model_1.atomic_model.fitting_net.fparam_inv_std" ],
425+ multi_state_dict [" model_2.atomic_model.fitting_net.fparam_inv_std" ],
408426 )
409427 torch .testing .assert_close (
410- multi_state_dict [' model_1.atomic_model.fitting_net.fparam_inv_std' ],
411- multi_state_dict [' model_3.atomic_model.fitting_net.fparam_inv_std' ]
428+ multi_state_dict [" model_1.atomic_model.fitting_net.fparam_inv_std" ],
429+ multi_state_dict [" model_3.atomic_model.fitting_net.fparam_inv_std" ],
412430 )
413431
414432 # check fitting stat in stat_file is correct
@@ -419,40 +437,52 @@ def test_sharefitting_using_default_fparam(self):
419437 fparam_data2 = np .load (f"{ self .data_file_single [0 ]} /set.000/fparam.npy" )
420438 fparam_data3 = np .load (f"{ self .data_file [0 ]} /set.000/fparam.npy" )
421439 np .testing .assert_almost_equal (
422- fparam_stat_model1 [:,0 ], [fparam_data1 .shape [0 ]] * 2
440+ fparam_stat_model1 [:, 0 ], [fparam_data1 .shape [0 ]] * 2
423441 )
424442 np .testing .assert_almost_equal (
425- fparam_stat_model1 [:,1 ], fparam_data1 .sum (axis = 0 )
443+ fparam_stat_model1 [:, 1 ], fparam_data1 .sum (axis = 0 )
426444 )
427445 np .testing .assert_almost_equal (
428- fparam_stat_model1 [:,2 ], (fparam_data1 ** 2 ).sum (axis = 0 )
446+ fparam_stat_model1 [:, 2 ], (fparam_data1 ** 2 ).sum (axis = 0 )
429447 )
430448 np .testing .assert_almost_equal (
431- fparam_stat_model2 [:,0 ], [fparam_data2 .shape [0 ]] * 2
449+ fparam_stat_model2 [:, 0 ], [fparam_data2 .shape [0 ]] * 2
432450 )
433451 np .testing .assert_almost_equal (
434- fparam_stat_model2 [:,1 ], fparam_data2 .sum (axis = 0 )
452+ fparam_stat_model2 [:, 1 ], fparam_data2 .sum (axis = 0 )
435453 )
436454 np .testing .assert_almost_equal (
437- fparam_stat_model2 [:,2 ], (fparam_data2 ** 2 ).sum (axis = 0 )
455+ fparam_stat_model2 [:, 2 ], (fparam_data2 ** 2 ).sum (axis = 0 )
438456 )
439457 np .testing .assert_almost_equal (
440- fparam_stat_model3 [:,0 ], [fparam_data3 .shape [0 ]] * 2
458+ fparam_stat_model3 [:, 0 ], [fparam_data3 .shape [0 ]] * 2
441459 )
442460 np .testing .assert_almost_equal (
443- fparam_stat_model3 [:,1 ], fparam_data3 .sum (axis = 0 )
461+ fparam_stat_model3 [:, 1 ], fparam_data3 .sum (axis = 0 )
444462 )
445463 np .testing .assert_almost_equal (
446- fparam_stat_model3 [:,2 ], (fparam_data3 ** 2 ).sum (axis = 0 )
464+ fparam_stat_model3 [:, 2 ], (fparam_data3 ** 2 ).sum (axis = 0 )
447465 )
448466
449467 # check shared fitting stat is computed correctly
450- weighted_avg , weighted_std = get_weighted_fitting_stat (model_prob , fparam_data1 , fparam_data2 , fparam_data3 , protection = data_stat_protect )
468+ weighted_avg , weighted_std = get_weighted_fitting_stat (
469+ model_prob ,
470+ fparam_data1 ,
471+ fparam_data2 ,
472+ fparam_data3 ,
473+ protection = data_stat_protect ,
474+ )
451475 np .testing .assert_almost_equal (
452- weighted_avg , to_numpy_array (multi_state_dict ['model_1.atomic_model.fitting_net.fparam_avg' ])
476+ weighted_avg ,
477+ to_numpy_array (
478+ multi_state_dict ["model_1.atomic_model.fitting_net.fparam_avg" ]
479+ ),
453480 )
454481 np .testing .assert_almost_equal (
455- 1 / weighted_std , to_numpy_array (multi_state_dict ['model_1.atomic_model.fitting_net.fparam_inv_std' ])
482+ 1 / weighted_std ,
483+ to_numpy_array (
484+ multi_state_dict ["model_1.atomic_model.fitting_net.fparam_inv_std" ]
485+ ),
456486 )
457487
458488 def tearDown (self ) -> None :
@@ -462,4 +492,4 @@ def tearDown(self) -> None:
462492 if f in ["lcurve.out" , "checkpoint" ]:
463493 os .remove (f )
464494 if f in [self .stat_files ]:
465- shutil .rmtree (f )
495+ shutil .rmtree (f )
0 commit comments