From 61e05e2e67c6fc1eb2e960c2111b6b412030a251 Mon Sep 17 00:00:00 2001 From: HydrogenSulfate <490868991@qq.com> Date: Sun, 22 Dec 2024 12:46:59 +0800 Subject: [PATCH 1/4] make dtype same as input x to avoid auto floating promotion --- deepmd/pd/utils/region.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepmd/pd/utils/region.py b/deepmd/pd/utils/region.py index f3e3eaa52d..d2600ef16e 100644 --- a/deepmd/pd/utils/region.py +++ b/deepmd/pd/utils/region.py @@ -108,5 +108,5 @@ def normalize_coord( """ icoord = phys2inter(coord, cell) - icoord = paddle.remainder(icoord, paddle.full([], 1.0)) + icoord = paddle.remainder(icoord, paddle.full([], 1.0, dtype=icoord.dtype)) return inter2phys(icoord, cell) From c601367fad90bf8823d30ac8cc379a0025af93ae Mon Sep 17 00:00:00 2001 From: HydrogenSulfate <490868991@qq.com> Date: Sun, 22 Dec 2024 12:48:17 +0800 Subject: [PATCH 2/4] switch to eval mode to avoid create_graph in 'fit_output_to_model_output' --- deepmd/pd/infer/deep_eval.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/deepmd/pd/infer/deep_eval.py b/deepmd/pd/infer/deep_eval.py index a2f8510f28..8887ee03f7 100644 --- a/deepmd/pd/infer/deep_eval.py +++ b/deepmd/pd/infer/deep_eval.py @@ -354,6 +354,12 @@ def _eval_model( request_defs: list[OutputVariableDef], ): model = self.dp.to(DEVICE) + + # switch to eval mode + is_training = model.training + if is_training: + model.eval() + prec = NP_PRECISION_DICT[RESERVED_PRECISON_DICT[GLOBAL_PD_FLOAT_PRECISION]] nframes = coords.shape[0] @@ -419,6 +425,11 @@ def _eval_model( results.append( np.full(np.abs(shape), np.nan, dtype=prec) ) # this is kinda hacky + + # switch back to training mode if previously enabled + if is_training: + model.train() + return tuple(results) def _eval_model_spin( From e781fd47834fbc2c10d19e33d9f90c47943a39e8 Mon Sep 17 00:00:00 2001 From: HydrogenSulfate <490868991@qq.com> Date: Mon, 23 Dec 2024 16:46:30 +0800 Subject: [PATCH 3/4] move to DeepEval.eval --- deepmd/pd/infer/deep_eval.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/deepmd/pd/infer/deep_eval.py b/deepmd/pd/infer/deep_eval.py index 8887ee03f7..2714e23a3e 100644 --- a/deepmd/pd/infer/deep_eval.py +++ b/deepmd/pd/infer/deep_eval.py @@ -237,6 +237,11 @@ def eval( The output of the evaluation. The keys are the names of the output variables, and the values are the corresponding output arrays. """ + # switch to eval mode + is_training = self.dp.training + if is_training: + self.dp.eval() + # convert all of the input to numpy array atom_types = np.array(atom_types, dtype=np.int32) coords = np.array(coords) @@ -260,6 +265,11 @@ def eval( aparam, request_defs, ) + + # switch back to training mode if previously enabled + if is_training: + self.dp.train() + return dict( zip( [x.name for x in request_defs], @@ -354,12 +364,6 @@ def _eval_model( request_defs: list[OutputVariableDef], ): model = self.dp.to(DEVICE) - - # switch to eval mode - is_training = model.training - if is_training: - model.eval() - prec = NP_PRECISION_DICT[RESERVED_PRECISON_DICT[GLOBAL_PD_FLOAT_PRECISION]] nframes = coords.shape[0] @@ -425,11 +429,6 @@ def _eval_model( results.append( np.full(np.abs(shape), np.nan, dtype=prec) ) # this is kinda hacky - - # switch back to training mode if previously enabled - if is_training: - model.train() - return tuple(results) def _eval_model_spin( From 3105ce2fdaad713483fd3196057de38c9f03bc83 Mon Sep 17 00:00:00 2001 From: HydrogenSulfate <490868991@qq.com> Date: Mon, 23 Dec 2024 17:07:40 +0800 Subject: [PATCH 4/4] add dp.eval() --- deepmd/pd/infer/deep_eval.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/deepmd/pd/infer/deep_eval.py b/deepmd/pd/infer/deep_eval.py index 2714e23a3e..c31170ad71 100644 --- a/deepmd/pd/infer/deep_eval.py +++ b/deepmd/pd/infer/deep_eval.py @@ -113,6 +113,7 @@ def __init__( else: # self.dp = paddle.jit.load(self.model_path.split(".json")[0]) raise ValueError(f"Unknown model file format: {self.model_path}!") + self.dp.eval() self.rcut = self.dp.model["Default"].get_rcut() self.type_map = self.dp.model["Default"].get_type_map() if isinstance(auto_batch_size, bool): @@ -237,11 +238,6 @@ def eval( The output of the evaluation. The keys are the names of the output variables, and the values are the corresponding output arrays. """ - # switch to eval mode - is_training = self.dp.training - if is_training: - self.dp.eval() - # convert all of the input to numpy array atom_types = np.array(atom_types, dtype=np.int32) coords = np.array(coords) @@ -265,11 +261,6 @@ def eval( aparam, request_defs, ) - - # switch back to training mode if previously enabled - if is_training: - self.dp.train() - return dict( zip( [x.name for x in request_defs],