diff --git a/deepmd/dpmodel/atomic_model/dp_atomic_model.py b/deepmd/dpmodel/atomic_model/dp_atomic_model.py index 3db0bb0c68..27363123df 100644 --- a/deepmd/dpmodel/atomic_model/dp_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/dp_atomic_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Optional, ) @@ -15,6 +14,9 @@ from deepmd.dpmodel.output_def import ( FittingOutputDef, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -208,7 +210,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 2) data.pop("@class") data.pop("type") diff --git a/deepmd/dpmodel/atomic_model/linear_atomic_model.py b/deepmd/dpmodel/atomic_model/linear_atomic_model.py index 2c7e029d53..1afecf0cc3 100644 --- a/deepmd/dpmodel/atomic_model/linear_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/linear_atomic_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Optional, Union, @@ -16,6 +15,9 @@ from deepmd.env import ( GLOBAL_NP_FLOAT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -305,7 +307,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "LinearEnergyAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 2), 2, 2) data.pop("@class", None) data.pop("type", None) @@ -418,7 +420,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPZBLLinearEnergyAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 2) models = [ BaseAtomicModel.get_class_by_type(model["type"]).deserialize(model) diff --git a/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py b/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py index c927089daf..500c374373 100644 --- a/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Optional, Union, @@ -18,6 +17,9 @@ from deepmd.dpmodel.utils.safe_gradient import ( safe_for_sqrt, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.pair_tab import ( PairTab, ) @@ -174,7 +176,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "PairTabAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 2) data.pop("@class") data.pop("type") diff --git a/deepmd/dpmodel/descriptor/se_e2_a.py b/deepmd/dpmodel/descriptor/se_e2_a.py index 6c0efb94d4..eac49edb66 100644 --- a/deepmd/dpmodel/descriptor/se_e2_a.py +++ b/deepmd/dpmodel/descriptor/se_e2_a.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import itertools from typing import ( Any, @@ -33,6 +32,9 @@ from deepmd.env import ( GLOBAL_NP_FLOAT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data_system import ( DeepmdDataSystem, ) @@ -460,7 +462,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "DescrptSeA": """Deserialize from dict.""" - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("@class", None) data.pop("type", None) diff --git a/deepmd/dpmodel/descriptor/se_r.py b/deepmd/dpmodel/descriptor/se_r.py index 0f646e143c..0a547516e9 100644 --- a/deepmd/dpmodel/descriptor/se_r.py +++ b/deepmd/dpmodel/descriptor/se_r.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Any, Optional, @@ -30,6 +29,9 @@ from deepmd.dpmodel.utils.update_sel import ( UpdateSel, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data_system import ( DeepmdDataSystem, ) @@ -388,7 +390,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "DescrptSeR": """Deserialize from dict.""" - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("@class", None) data.pop("type", None) diff --git a/deepmd/dpmodel/descriptor/se_t.py b/deepmd/dpmodel/descriptor/se_t.py index 5bc5970a87..3756cbd765 100644 --- a/deepmd/dpmodel/descriptor/se_t.py +++ b/deepmd/dpmodel/descriptor/se_t.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import itertools from typing import ( Optional, @@ -30,6 +29,9 @@ from deepmd.dpmodel.utils.update_sel import ( UpdateSel, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data_system import ( DeepmdDataSystem, ) @@ -388,7 +390,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "DescrptSeT": """Deserialize from dict.""" - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("@class", None) data.pop("type", None) diff --git a/deepmd/dpmodel/fitting/dipole_fitting.py b/deepmd/dpmodel/fitting/dipole_fitting.py index cecba865d0..0cc72cdda2 100644 --- a/deepmd/dpmodel/fitting/dipole_fitting.py +++ b/deepmd/dpmodel/fitting/dipole_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Any, Optional, @@ -20,6 +19,9 @@ OutputVariableDef, fitting_check_output, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -156,7 +158,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) var_name = data.pop("var_name", None) assert var_name == "dipole" diff --git a/deepmd/dpmodel/fitting/dos_fitting.py b/deepmd/dpmodel/fitting/dos_fitting.py index 32225ac6c0..7eef3b80c1 100644 --- a/deepmd/dpmodel/fitting/dos_fitting.py +++ b/deepmd/dpmodel/fitting/dos_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( TYPE_CHECKING, Optional, @@ -15,6 +14,9 @@ from deepmd.dpmodel.fitting.invar_fitting import ( InvarFitting, ) +from deepmd.utils.copy import ( + deepcopy, +) if TYPE_CHECKING: from deepmd.dpmodel.fitting.general_fitting import ( @@ -73,7 +75,7 @@ def __init__( @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data["numb_dos"] = data.pop("dim_out") data.pop("tot_ener_zero", None) diff --git a/deepmd/dpmodel/fitting/ener_fitting.py b/deepmd/dpmodel/fitting/ener_fitting.py index 9a1eae0156..3ed5cae6d0 100644 --- a/deepmd/dpmodel/fitting/ener_fitting.py +++ b/deepmd/dpmodel/fitting/ener_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( TYPE_CHECKING, Any, @@ -13,11 +12,15 @@ from deepmd.dpmodel.fitting.invar_fitting import ( InvarFitting, ) +from deepmd.utils.copy import ( + deepcopy, +) if TYPE_CHECKING: from deepmd.dpmodel.fitting.general_fitting import ( GeneralFitting, ) + from deepmd.utils.version import ( check_version_compatibility, ) @@ -73,7 +76,7 @@ def __init__( @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("var_name") data.pop("dim_out") diff --git a/deepmd/dpmodel/fitting/general_fitting.py b/deepmd/dpmodel/fitting/general_fitting.py index a027e1e59d..4d6abac15b 100644 --- a/deepmd/dpmodel/fitting/general_fitting.py +++ b/deepmd/dpmodel/fitting/general_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from abc import ( abstractmethod, ) @@ -32,6 +31,9 @@ from deepmd.env import ( GLOBAL_NP_FLOAT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( get_index_between_two_maps, map_atom_exclude_types, @@ -320,7 +322,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) data.pop("@class") data.pop("type") variables = data.pop("@variables") diff --git a/deepmd/dpmodel/fitting/invar_fitting.py b/deepmd/dpmodel/fitting/invar_fitting.py index 3f8607109b..58980dea80 100644 --- a/deepmd/dpmodel/fitting/invar_fitting.py +++ b/deepmd/dpmodel/fitting/invar_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Any, Optional, @@ -16,6 +15,9 @@ OutputVariableDef, fitting_check_output, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -179,7 +181,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) return super().deserialize(data) diff --git a/deepmd/dpmodel/fitting/polarizability_fitting.py b/deepmd/dpmodel/fitting/polarizability_fitting.py index b972b45971..747a395e87 100644 --- a/deepmd/dpmodel/fitting/polarizability_fitting.py +++ b/deepmd/dpmodel/fitting/polarizability_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Any, Optional, @@ -26,6 +25,9 @@ OutputVariableDef, fitting_check_output, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( get_index_between_two_maps, ) @@ -197,7 +199,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 3, 1) var_name = data.pop("var_name", None) assert var_name == "polar" diff --git a/deepmd/dpmodel/fitting/property_fitting.py b/deepmd/dpmodel/fitting/property_fitting.py index a1b6fe7638..e9a146a09a 100644 --- a/deepmd/dpmodel/fitting/property_fitting.py +++ b/deepmd/dpmodel/fitting/property_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Optional, Union, @@ -13,6 +12,9 @@ from deepmd.dpmodel.fitting.invar_fitting import ( InvarFitting, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -111,7 +113,7 @@ def __init__( @classmethod def deserialize(cls, data: dict) -> "PropertyFittingNet": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version"), 2, 1) data.pop("dim_out") data.pop("var_name") diff --git a/deepmd/dpmodel/utils/network.py b/deepmd/dpmodel/utils/network.py index a81ddb69a6..690e714fa6 100644 --- a/deepmd/dpmodel/utils/network.py +++ b/deepmd/dpmodel/utils/network.py @@ -4,7 +4,6 @@ See issue #2982 for more information. """ -import copy import itertools from typing import ( Callable, @@ -30,6 +29,9 @@ from deepmd.dpmodel.utils.seed import ( child_seed, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -135,7 +137,7 @@ def deserialize(cls, data: dict) -> "NativeLayer": data : dict The dict to deserialize from. """ - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) variables = data.pop("@variables") @@ -404,7 +406,7 @@ def deserialize(cls, data: dict) -> "LayerNorm": data : dict The dict to deserialize from. """ - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) variables = data.pop("@variables") @@ -673,7 +675,7 @@ def deserialize(cls, data: dict) -> "EmbeddingNet": data : dict The dict to deserialize from. """ - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("@class", None) layers = data.pop("layers") @@ -778,7 +780,7 @@ def deserialize(cls, data: dict) -> "FittingNet": data : dict The dict to deserialize from. """ - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) layers = data.pop("layers") diff --git a/deepmd/jax/model/model.py b/deepmd/jax/model/model.py index e636eba4c6..779e2a414f 100644 --- a/deepmd/jax/model/model.py +++ b/deepmd/jax/model/model.py @@ -1,8 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) - from deepmd.jax.atomic_model.dp_atomic_model import ( DPAtomicModel, ) @@ -24,6 +20,9 @@ from deepmd.jax.model.dp_zbl_model import ( DPZBLModel, ) +from deepmd.utils.copy import ( + deepcopy, +) def get_standard_model(data: dict): diff --git a/deepmd/pt/entrypoints/main.py b/deepmd/pt/entrypoints/main.py index c56e7f0731..5c41237a20 100644 --- a/deepmd/pt/entrypoints/main.py +++ b/deepmd/pt/entrypoints/main.py @@ -1,6 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import argparse -import copy import json import logging import os @@ -77,6 +76,9 @@ from deepmd.utils.compat import ( update_deepmd_input, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data_system import ( get_data, process_systems, @@ -384,7 +386,7 @@ def change_bias( old_state_dict = torch.load( input_file, map_location=env.DEVICE, weights_only=True ) - model_state_dict = copy.deepcopy(old_state_dict.get("model", old_state_dict)) + model_state_dict = deepcopy(old_state_dict.get("model", old_state_dict)) model_params = model_state_dict["_extra_state"]["model_params"] elif input_file.endswith(".pth"): old_model = torch.jit.load(input_file, map_location=env.DEVICE) diff --git a/deepmd/pt/infer/inference.py b/deepmd/pt/infer/inference.py index b3d120cbc4..ca69a7afe3 100644 --- a/deepmd/pt/infer/inference.py +++ b/deepmd/pt/infer/inference.py @@ -1,8 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import logging -from copy import ( - deepcopy, -) import torch @@ -16,6 +13,9 @@ DEVICE, JIT, ) +from deepmd.utils.copy import ( + deepcopy, +) if torch.__version__.startswith("2"): import torch._dynamo diff --git a/deepmd/pt/model/atomic_model/base_atomic_model.py b/deepmd/pt/model/atomic_model/base_atomic_model.py index e26549581e..dae0387ae7 100644 --- a/deepmd/pt/model/atomic_model/base_atomic_model.py +++ b/deepmd/pt/model/atomic_model/base_atomic_model.py @@ -1,6 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Callable, @@ -33,6 +32,9 @@ to_numpy_array, to_torch_tensor, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( get_index_between_two_maps, map_atom_exclude_types, @@ -331,7 +333,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "BaseAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) variables = data.pop("@variables", None) variables = ( {"out_bias": None, "out_std": None} if variables is None else variables diff --git a/deepmd/pt/model/atomic_model/dp_atomic_model.py b/deepmd/pt/model/atomic_model/dp_atomic_model.py index 4f53fb69f9..7ef0e8eb35 100644 --- a/deepmd/pt/model/atomic_model/dp_atomic_model.py +++ b/deepmd/pt/model/atomic_model/dp_atomic_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import functools import logging from typing import ( @@ -17,6 +16,9 @@ from deepmd.pt.model.task.base_fitting import ( BaseFitting, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.path import ( DPPath, ) @@ -149,7 +151,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("@class", None) data.pop("type", None) diff --git a/deepmd/pt/model/atomic_model/linear_atomic_model.py b/deepmd/pt/model/atomic_model/linear_atomic_model.py index 33c2eb6c59..eab27ae5cf 100644 --- a/deepmd/pt/model/atomic_model/linear_atomic_model.py +++ b/deepmd/pt/model/atomic_model/linear_atomic_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Callable, Optional, @@ -20,6 +19,9 @@ get_multiple_nlist_key, nlist_distinguish_types, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.path import ( DPPath, ) @@ -369,7 +371,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "LinearEnergyAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 2), 2, 1) data.pop("@class", None) data.pop("type", None) @@ -563,7 +565,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPZBLLinearEnergyAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) models = [ BaseAtomicModel.get_class_by_type(model["type"]).deserialize(model) diff --git a/deepmd/pt/model/atomic_model/pairtab_atomic_model.py b/deepmd/pt/model/atomic_model/pairtab_atomic_model.py index 87e3027bc8..0af7aafca8 100644 --- a/deepmd/pt/model/atomic_model/pairtab_atomic_model.py +++ b/deepmd/pt/model/atomic_model/pairtab_atomic_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Callable, Optional, @@ -15,6 +14,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.pair_tab import ( PairTab, ) @@ -195,7 +197,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "PairTabAtomicModel": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) tab = PairTab.deserialize(data.pop("tab")) data.pop("@class", None) diff --git a/deepmd/pt/model/model/__init__.py b/deepmd/pt/model/model/__init__.py index f3896ec853..abb193a8d4 100644 --- a/deepmd/pt/model/model/__init__.py +++ b/deepmd/pt/model/model/__init__.py @@ -11,7 +11,6 @@ Models generated by `make_model` have already done it. """ -import copy import json import numpy as np @@ -26,6 +25,9 @@ from deepmd.pt.model.task import ( BaseFitting, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.spin import ( Spin, ) @@ -75,13 +77,13 @@ def _get_standard_model_components(model_params, ntypes): # descriptor model_params["descriptor"]["ntypes"] = ntypes - model_params["descriptor"]["type_map"] = copy.deepcopy(model_params["type_map"]) + model_params["descriptor"]["type_map"] = deepcopy(model_params["type_map"]) descriptor = BaseDescriptor(**model_params["descriptor"]) # fitting fitting_net = model_params.get("fitting_net", {}) fitting_net["type"] = fitting_net.get("type", "ener") fitting_net["ntypes"] = descriptor.get_ntypes() - fitting_net["type_map"] = copy.deepcopy(model_params["type_map"]) + fitting_net["type_map"] = deepcopy(model_params["type_map"]) fitting_net["mixed_types"] = descriptor.mixed_types() if fitting_net["type"] in ["dipole", "polar"]: fitting_net["embedding_width"] = descriptor.get_dim_emb() @@ -96,7 +98,7 @@ def _get_standard_model_components(model_params, ntypes): def get_spin_model(model_params): - model_params = copy.deepcopy(model_params) + model_params = deepcopy(model_params) if not model_params["spin"]["use_spin"] or isinstance( model_params["spin"]["use_spin"][0], int ): @@ -132,7 +134,7 @@ def get_spin_model(model_params): def get_linear_model(model_params): - model_params = copy.deepcopy(model_params) + model_params = deepcopy(model_params) weights = model_params.get("weights", "mean") list_of_models = [] ntypes = len(model_params["type_map"]) @@ -172,7 +174,7 @@ def get_linear_model(model_params): def get_zbl_model(model_params): - model_params = copy.deepcopy(model_params) + model_params = deepcopy(model_params) ntypes = len(model_params["type_map"]) descriptor, fitting, _ = _get_standard_model_components(model_params, ntypes) dp_model = DPAtomicModel(descriptor, fitting, type_map=model_params["type_map"]) @@ -234,7 +236,7 @@ def _convert_preset_out_bias_to_array(preset_out_bias, type_map): def get_standard_model(model_params): model_params_old = model_params - model_params = copy.deepcopy(model_params) + model_params = deepcopy(model_params) ntypes = len(model_params["type_map"]) descriptor, fitting, fitting_net_type = _get_standard_model_components( model_params, ntypes diff --git a/deepmd/pt/model/model/dipole_model.py b/deepmd/pt/model/model/dipole_model.py index c83d1f0bf7..b858f9e6ed 100644 --- a/deepmd/pt/model/model/dipole_model.py +++ b/deepmd/pt/model/model/dipole_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from .dp_model import ( DPModelCommon, diff --git a/deepmd/pt/model/model/dos_model.py b/deepmd/pt/model/model/dos_model.py index abfcd4a2b4..26581e255d 100644 --- a/deepmd/pt/model/model/dos_model.py +++ b/deepmd/pt/model/model/dos_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from .dp_model import ( DPModelCommon, diff --git a/deepmd/pt/model/model/dp_linear_model.py b/deepmd/pt/model/model/dp_linear_model.py index 4028d77228..67dab2c31c 100644 --- a/deepmd/pt/model/model/dp_linear_model.py +++ b/deepmd/pt/model/model/dp_linear_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data_system import ( DeepmdDataSystem, ) diff --git a/deepmd/pt/model/model/dp_zbl_model.py b/deepmd/pt/model/model/dp_zbl_model.py index 0f05e3e56d..7b6ea58961 100644 --- a/deepmd/pt/model/model/dp_zbl_model.py +++ b/deepmd/pt/model/model/dp_zbl_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data_system import ( DeepmdDataSystem, ) diff --git a/deepmd/pt/model/model/ener_model.py b/deepmd/pt/model/model/ener_model.py index 82f429c4ab..3544550ae4 100644 --- a/deepmd/pt/model/model/ener_model.py +++ b/deepmd/pt/model/model/ener_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from .dp_model import ( DPModelCommon, diff --git a/deepmd/pt/model/model/make_hessian_model.py b/deepmd/pt/model/model/make_hessian_model.py index d2541a815e..6decbdd144 100644 --- a/deepmd/pt/model/model/make_hessian_model.py +++ b/deepmd/pt/model/model/make_hessian_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import math from typing import ( Optional, @@ -11,6 +10,9 @@ from deepmd.dpmodel import ( get_hessian_name, ) +from deepmd.utils.copy import ( + deepcopy, +) def make_hessian_model(T_Model): @@ -41,7 +43,7 @@ def __init__( *args, **kwargs, ) - self.hess_fitting_def = copy.deepcopy(super().atomic_output_def()) + self.hess_fitting_def = deepcopy(super().atomic_output_def()) def requires_hessian( self, diff --git a/deepmd/pt/model/model/polar_model.py b/deepmd/pt/model/model/polar_model.py index 57379ba372..22fdcfa6a9 100644 --- a/deepmd/pt/model/model/polar_model.py +++ b/deepmd/pt/model/model/polar_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from .dp_model import ( DPModelCommon, diff --git a/deepmd/pt/model/model/property_model.py b/deepmd/pt/model/model/property_model.py index 164331f44c..9ae45f95b1 100644 --- a/deepmd/pt/model/model/property_model.py +++ b/deepmd/pt/model/model/property_model.py @@ -1,7 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -14,6 +11,9 @@ from deepmd.pt.model.model.model import ( BaseModel, ) +from deepmd.utils.copy import ( + deepcopy, +) from .dp_model import ( DPModelCommon, diff --git a/deepmd/pt/model/model/spin_model.py b/deepmd/pt/model/model/spin_model.py index bc1bc81a74..54a98d9952 100644 --- a/deepmd/pt/model/model/spin_model.py +++ b/deepmd/pt/model/model/spin_model.py @@ -1,8 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import functools -from copy import ( - deepcopy, -) from typing import ( Optional, ) @@ -18,6 +15,9 @@ from deepmd.pt.utils.utils import ( to_torch_tensor, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.path import ( DPPath, ) diff --git a/deepmd/pt/model/task/dipole.py b/deepmd/pt/model/task/dipole.py index 79f9a0a86c..7a10bcf3e8 100644 --- a/deepmd/pt/model/task/dipole.py +++ b/deepmd/pt/model/task/dipole.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Callable, @@ -22,6 +21,9 @@ from deepmd.pt.utils.env import ( DEFAULT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.path import ( DPPath, ) @@ -128,7 +130,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("var_name", None) return super().deserialize(data) diff --git a/deepmd/pt/model/task/dos.py b/deepmd/pt/model/task/dos.py index 4f69094b0d..3512099bd1 100644 --- a/deepmd/pt/model/task/dos.py +++ b/deepmd/pt/model/task/dos.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Optional, @@ -27,6 +26,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -99,7 +101,7 @@ def output_def(self) -> FittingOutputDef: @classmethod def deserialize(cls, data: dict) -> "DOSFittingNet": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("@class", None) data.pop("var_name", None) diff --git a/deepmd/pt/model/task/ener.py b/deepmd/pt/model/task/ener.py index ee8372c3ac..d00a2adc94 100644 --- a/deepmd/pt/model/task/ener.py +++ b/deepmd/pt/model/task/ener.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Optional, @@ -30,6 +29,9 @@ from deepmd.pt.utils.env import ( DEFAULT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -78,7 +80,7 @@ def __init__( @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("var_name") data.pop("dim_out") diff --git a/deepmd/pt/model/task/fitting.py b/deepmd/pt/model/task/fitting.py index bae46c2adb..8b601ee061 100644 --- a/deepmd/pt/model/task/fitting.py +++ b/deepmd/pt/model/task/fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from abc import ( abstractmethod, @@ -36,6 +35,9 @@ to_numpy_array, to_torch_tensor, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( get_index_between_two_maps, map_atom_exclude_types, @@ -312,7 +314,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) variables = data.pop("@variables") nets = data.pop("nets") obj = cls(**data) diff --git a/deepmd/pt/model/task/invar_fitting.py b/deepmd/pt/model/task/invar_fitting.py index b9be26cfdc..7679beb43b 100644 --- a/deepmd/pt/model/task/invar_fitting.py +++ b/deepmd/pt/model/task/invar_fitting.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Optional, @@ -22,6 +21,9 @@ from deepmd.pt.utils.env import ( DEFAULT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -141,7 +143,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) return super().deserialize(data) diff --git a/deepmd/pt/model/task/polarizability.py b/deepmd/pt/model/task/polarizability.py index 512044efbd..412c34774c 100644 --- a/deepmd/pt/model/task/polarizability.py +++ b/deepmd/pt/model/task/polarizability.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Optional, @@ -24,6 +23,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( get_index_between_two_maps, ) @@ -202,7 +204,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 3, 1) data.pop("var_name", None) return super().deserialize(data) diff --git a/deepmd/pt/model/task/property.py b/deepmd/pt/model/task/property.py index 4017f51468..66e9cd68b5 100644 --- a/deepmd/pt/model/task/property.py +++ b/deepmd/pt/model/task/property.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import logging from typing import ( Optional, @@ -23,6 +22,9 @@ from deepmd.pt.utils.env import ( DEFAULT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.version import ( check_version_compatibility, ) @@ -129,7 +131,7 @@ def output_def(self) -> FittingOutputDef: @classmethod def deserialize(cls, data: dict) -> "PropertyFittingNet": - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 1), 2, 1) data.pop("dim_out") data.pop("var_name") diff --git a/deepmd/pt/train/training.py b/deepmd/pt/train/training.py index 466080d34c..0eeee7f04e 100644 --- a/deepmd/pt/train/training.py +++ b/deepmd/pt/train/training.py @@ -2,9 +2,6 @@ import functools import logging import time -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -65,6 +62,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data import ( DataRequirementItem, ) diff --git a/deepmd/pt/utils/cache.py b/deepmd/pt/utils/cache.py index c40c4050b7..c6e13d91d2 100644 --- a/deepmd/pt/utils/cache.py +++ b/deepmd/pt/utils/cache.py @@ -1,7 +1,8 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy as copy_lib import functools +from deepmd.utils.copy import deepcopy as copy_lib + def lru_cache(maxsize=16, typed=False, copy=False, deepcopy=False): if deepcopy: diff --git a/deepmd/pt/utils/finetune.py b/deepmd/pt/utils/finetune.py index 96a420bf6a..1d0a0fba8a 100644 --- a/deepmd/pt/utils/finetune.py +++ b/deepmd/pt/utils/finetune.py @@ -1,14 +1,14 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import logging -from copy import ( - deepcopy, -) import torch from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( FinetuneRuleItem, ) diff --git a/deepmd/pt/utils/multi_task.py b/deepmd/pt/utils/multi_task.py index e2076b3b2b..4524847895 100644 --- a/deepmd/pt/utils/multi_task.py +++ b/deepmd/pt/utils/multi_task.py @@ -1,14 +1,13 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -from copy import ( - deepcopy, -) - from deepmd.pt.model.descriptor import ( BaseDescriptor, ) from deepmd.pt.model.task import ( BaseFitting, ) +from deepmd.utils.copy import ( + deepcopy, +) def preprocess_shared_params(model_config): diff --git a/deepmd/tf/model/model.py b/deepmd/tf/model/model.py index 03211d49d5..71acf0cd01 100644 --- a/deepmd/tf/model/model.py +++ b/deepmd/tf/model/model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from abc import ( ABC, abstractmethod, @@ -57,6 +56,9 @@ from deepmd.tf.utils.type_embed import ( TypeEmbedNet, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data import ( DataRequirementItem, ) @@ -804,7 +806,7 @@ def deserialize(cls, data: dict, suffix: str = "") -> "Descriptor": Descriptor The deserialized descriptor """ - data = copy.deepcopy(data) + data = deepcopy(data) check_version_compatibility(data.pop("@version", 2), 2, 1) descriptor = Descriptor.deserialize(data.pop("descriptor"), suffix=suffix) fitting = Fitting.deserialize(data.pop("fitting"), suffix=suffix) diff --git a/deepmd/utils/copy.py b/deepmd/utils/copy.py new file mode 100644 index 0000000000..7e304309ec --- /dev/null +++ b/deepmd/utils/copy.py @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: LGPL-3.0-or-later +import pickle +from typing import ( + Any, +) + + +def deepcopy(obj: Any) -> Any: + """Deep copy an object using pickle. + + Deep copy is a performance killer. Use this function only when necessary. + + Parameters + ---------- + obj + The object to be copied. + + Returns + ------- + object + The copied object. + """ + # pickle is faster than copy.deepcopy + # https://stackoverflow.com/a/29385667/9567349 + return pickle.loads(pickle.dumps(obj)) diff --git a/deepmd/utils/spin.py b/deepmd/utils/spin.py index 9ea5fc0713..027bd310ae 100644 --- a/deepmd/utils/spin.py +++ b/deepmd/utils/spin.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy from typing import ( Union, ) @@ -9,6 +8,9 @@ from deepmd.env import ( GLOBAL_NP_FLOAT_PRECISION, ) +from deepmd.utils.copy import ( + deepcopy, +) class Spin: @@ -143,9 +145,7 @@ def get_pair_exclude_types(self, exclude_types=None) -> list[tuple[int, int]]: if exclude_types is None: return self.pair_exclude_types else: - _exclude_types: list[tuple[int, int]] = copy.deepcopy( - self.pair_exclude_types - ) + _exclude_types: list[tuple[int, int]] = deepcopy(self.pair_exclude_types) for tt in exclude_types: assert len(tt) == 2 _exclude_types.append((tt[0], tt[1])) @@ -159,7 +159,7 @@ def get_atom_exclude_types(self, exclude_types=None) -> list[int]: if exclude_types is None: return self.atom_exclude_types_ps else: - _exclude_types: list[int] = copy.deepcopy(self.atom_exclude_types_ps) + _exclude_types: list[int] = deepcopy(self.atom_exclude_types_ps) _exclude_types += exclude_types _exclude_types = list(set(_exclude_types)) return _exclude_types @@ -172,7 +172,7 @@ def get_atom_exclude_types_placeholder(self, exclude_types=None) -> list[int]: if exclude_types is None: return self.atom_exclude_types_p else: - _exclude_types: list[int] = copy.deepcopy(self.atom_exclude_types_p) + _exclude_types: list[int] = deepcopy(self.atom_exclude_types_p) _exclude_types += exclude_types _exclude_types = list(set(_exclude_types)) return _exclude_types diff --git a/source/tests/common/dpmodel/test_network.py b/source/tests/common/dpmodel/test_network.py index 381c542272..28bf503947 100644 --- a/source/tests/common/dpmodel/test_network.py +++ b/source/tests/common/dpmodel/test_network.py @@ -2,9 +2,6 @@ import itertools import os import unittest -from copy import ( - deepcopy, -) import numpy as np @@ -17,6 +14,9 @@ load_dp_model, save_dp_model, ) +from deepmd.utils.copy import ( + deepcopy, +) class TestNativeLayer(unittest.TestCase): diff --git a/source/tests/consistent/io/test_io.py b/source/tests/consistent/io/test_io.py index ca213da13c..2648d35c80 100644 --- a/source/tests/consistent/io/test_io.py +++ b/source/tests/consistent/io/test_io.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import shutil import unittest from pathlib import ( @@ -20,6 +19,9 @@ from deepmd.infer.deep_eval import ( DeepEval, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...utils import ( CI, @@ -86,13 +88,13 @@ def test_data_equal(self): backend = Backend.get_backend(backend_name)() if not backend.is_available(): continue - reference_data = copy.deepcopy(self.data) + reference_data = deepcopy(self.data) self.save_data_to_model( prefix + backend.suffixes[suffix_idx], reference_data ) data = self.get_data_from_model(prefix + backend.suffixes[suffix_idx]) - data = copy.deepcopy(data) - reference_data = copy.deepcopy(self.data) + data = deepcopy(data) + reference_data = deepcopy(self.data) # some keys are not expected to be not the same for kk in [ "backend", @@ -152,7 +154,7 @@ def test_deep_eval(self): backend = Backend.get_backend(backend_name)() if not backend.is_available(): continue - reference_data = copy.deepcopy(self.data) + reference_data = deepcopy(self.data) self.save_data_to_model( prefix + backend.suffixes[suffix_idx], reference_data ) @@ -221,7 +223,7 @@ def setUp(self): "seed": 1, }, } - model = get_model(copy.deepcopy(model_def_script)) + model = get_model(deepcopy(model_def_script)) self.data = { "model": model.serialize(), "backend": "test", @@ -265,7 +267,7 @@ def setUp(self): "numb_aparam": 2, }, } - model = get_model(copy.deepcopy(model_def_script)) + model = get_model(deepcopy(model_def_script)) self.data = { "model": model.serialize(), "backend": "test", diff --git a/source/tests/pt/model/test_autodiff.py b/source/tests/pt/model/test_autodiff.py index 1adcff55fc..b495973665 100644 --- a/source/tests/pt/model/test_autodiff.py +++ b/source/tests/pt/model/test_autodiff.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import numpy as np @@ -14,6 +13,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -177,77 +179,77 @@ def ff(bb): class TestEnergyModelSeAForce(unittest.TestCase, ForceTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelSeAVirial(unittest.TestCase, VirialTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1Force(unittest.TestCase, ForceTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1Virial(unittest.TestCase, VirialTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA2Force(unittest.TestCase, ForceTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPAUniVirial(unittest.TestCase, VirialTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelHybridForce(unittest.TestCase, ForceTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelHybridVirial(unittest.TestCase, VirialTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelZBLForce(unittest.TestCase, ForceTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelZBLVirial(unittest.TestCase, VirialTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelSpinSeAForce(unittest.TestCase, ForceTest): def setUp(self): - model_params = copy.deepcopy(model_spin) + model_params = deepcopy(model_spin) self.type_split = False self.test_spin = True self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_deeppot.py b/source/tests/pt/model/test_deeppot.py index 7f530b0a5e..6be3abaefe 100644 --- a/source/tests/pt/model/test_deeppot.py +++ b/source/tests/pt/model/test_deeppot.py @@ -2,9 +2,6 @@ import json import os import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -20,6 +17,9 @@ from deepmd.pt.infer.deep_eval import ( DeepPot, ) +from deepmd.utils.copy import ( + deepcopy, +) class TestDeepPot(unittest.TestCase): diff --git a/source/tests/pt/model/test_ener_spin_model.py b/source/tests/pt/model/test_ener_spin_model.py index a0cc621c9b..f5dec9e933 100644 --- a/source/tests/pt/model/test_ener_spin_model.py +++ b/source/tests/pt/model/test_ener_spin_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import numpy as np @@ -19,6 +18,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -386,8 +388,8 @@ def test_dp_consistency(self): class TestEnergyModelSpinSeA(unittest.TestCase, SpinTest): def setUp(self): SpinTest.setUp(self) - model_params = copy.deepcopy(model_spin) - model_params["descriptor"] = copy.deepcopy(model_se_e2_a["descriptor"]) + model_params = deepcopy(model_spin) + model_params["descriptor"] = deepcopy(model_se_e2_a["descriptor"]) self.rcut = model_params["descriptor"]["rcut"] self.nsel = sum(model_params["descriptor"]["sel"]) self.type_map = model_params["type_map"] @@ -397,8 +399,8 @@ def setUp(self): class TestEnergyModelSpinDPA1(unittest.TestCase, SpinTest): def setUp(self): SpinTest.setUp(self) - model_params = copy.deepcopy(model_spin) - model_params["descriptor"] = copy.deepcopy(model_dpa1["descriptor"]) + model_params = deepcopy(model_spin) + model_params["descriptor"] = deepcopy(model_dpa1["descriptor"]) self.rcut = model_params["descriptor"]["rcut"] self.nsel = model_params["descriptor"]["sel"] self.type_map = model_params["type_map"] @@ -410,8 +412,8 @@ def setUp(self): class TestEnergyModelSpinDPA2(unittest.TestCase, SpinTest): def setUp(self): SpinTest.setUp(self) - model_params = copy.deepcopy(model_spin) - model_params["descriptor"] = copy.deepcopy(model_dpa2["descriptor"]) + model_params = deepcopy(model_spin) + model_params["descriptor"] = deepcopy(model_dpa2["descriptor"]) self.rcut = model_params["descriptor"]["repinit"]["rcut"] self.nsel = model_params["descriptor"]["repinit"]["nsel"] self.type_map = model_params["type_map"] diff --git a/source/tests/pt/model/test_force_grad.py b/source/tests/pt/model/test_force_grad.py index d3cd11f71d..d87c7a1f38 100644 --- a/source/tests/pt/model/test_force_grad.py +++ b/source/tests/pt/model/test_force_grad.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import json import unittest from pathlib import ( @@ -18,6 +17,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data import ( DeepmdData, ) @@ -45,7 +47,7 @@ def get_disturb(self, index, atom_index, axis_index, delta): if index < self.prefix_sum[i]: break frames = self._load_set(self.dirs[i - 1]) - tmp = copy.deepcopy(frames["coord"].reshape(self.nframes, -1, 3)) + tmp = deepcopy(frames["coord"].reshape(self.nframes, -1, 3)) tmp[:, atom_index, axis_index] += delta frames["coord"] = tmp frame = self._get_subdata(frames, index - self.prefix_sum[i - 1]) diff --git a/source/tests/pt/model/test_forward_lower.py b/source/tests/pt/model/test_forward_lower.py index 87a3f5b06e..d02def59d4 100644 --- a/source/tests/pt/model/test_forward_lower.py +++ b/source/tests/pt/model/test_forward_lower.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -13,6 +12,9 @@ from deepmd.pt.utils.nlist import ( extend_input_and_build_neighbor_list, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -137,35 +139,35 @@ def test( class TestEnergyModelSeA(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA2(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelZBL(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelSpinSeA(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_spin) + model_params = deepcopy(model_spin) self.test_spin = True self.model = get_model(model_params).to(env.DEVICE) @@ -173,8 +175,8 @@ def setUp(self): class TestEnergyModelSpinDPA1(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_spin) - model_params["descriptor"] = copy.deepcopy(model_dpa1)["descriptor"] + model_params = deepcopy(model_spin) + model_params["descriptor"] = deepcopy(model_dpa1)["descriptor"] # double sel for virtual atoms to avoid large error model_params["descriptor"]["sel"] *= 2 self.test_spin = True @@ -184,8 +186,8 @@ def setUp(self): class TestEnergyModelSpinDPA2(unittest.TestCase, ForwardLowerTest): def setUp(self): self.prec = 1e-10 - model_params = copy.deepcopy(model_spin) - model_params["descriptor"] = copy.deepcopy(model_dpa2)["descriptor"] + model_params = deepcopy(model_spin) + model_params["descriptor"] = deepcopy(model_dpa2)["descriptor"] # double sel for virtual atoms to avoid large error model_params["descriptor"]["repinit"]["nsel"] *= 2 model_params["descriptor"]["repformer"]["nsel"] *= 2 diff --git a/source/tests/pt/model/test_get_model.py b/source/tests/pt/model/test_get_model.py index 4774582f57..8b3d0714fd 100644 --- a/source/tests/pt/model/test_get_model.py +++ b/source/tests/pt/model/test_get_model.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import numpy as np @@ -11,6 +10,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) dtype = torch.float64 @@ -46,7 +48,7 @@ class TestGetModel(unittest.TestCase): def test_model_attr(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.model = get_model(model_params).to(env.DEVICE) atomic_model = self.model.atomic_model self.assertEqual(atomic_model.type_map, ["O", "H", "B"]) @@ -64,7 +66,7 @@ def test_model_attr(self): self.assertEqual(atomic_model.pair_exclude_types, [[1, 2]]) def test_model_attr_energy_float(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) model_params["preset_out_bias"] = {"energy": ["1.", 3, None]} self.model = get_model(model_params).to(env.DEVICE) atomic_model = self.model.atomic_model @@ -83,19 +85,19 @@ def test_model_attr_energy_float(self): self.assertEqual(atomic_model.pair_exclude_types, [[1, 2]]) def test_model_attr_energy_unsupported_type(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) model_params["preset_out_bias"] = {"energy": [1.0 + 2.0j, 3, None]} with self.assertRaises(ValueError): self.model = get_model(model_params).to(env.DEVICE) def test_model_attr_energy_unsupported_value(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) model_params["preset_out_bias"] = {"energy": ["1.0 + 2.0j", 3, None]} with self.assertRaises(ValueError): self.model = get_model(model_params).to(env.DEVICE) def test_notset_model_attr(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) model_params.pop("atom_exclude_types") model_params.pop("pair_exclude_types") model_params.pop("preset_out_bias") @@ -107,7 +109,7 @@ def test_notset_model_attr(self): self.assertEqual(atomic_model.pair_exclude_types, []) def test_preset_wrong_len(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) model_params["preset_out_bias"] = {"energy": [None]} with self.assertRaises(ValueError): self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_jit.py b/source/tests/pt/model/test_jit.py index 1f1034c330..34147eb7d3 100644 --- a/source/tests/pt/model/test_jit.py +++ b/source/tests/pt/model/test_jit.py @@ -3,9 +3,6 @@ import os import shutil import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -18,6 +15,9 @@ from deepmd.pt.infer import ( inference, ) +from deepmd.utils.copy import ( + deepcopy, +) from .test_permutation import ( model_dos, diff --git a/source/tests/pt/model/test_null_input.py b/source/tests/pt/model/test_null_input.py index a2e0fa66db..440ed3fb8f 100644 --- a/source/tests/pt/model/test_null_input.py +++ b/source/tests/pt/model/test_null_input.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import numpy as np @@ -15,6 +14,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -88,28 +90,28 @@ def test_nloc_2_far( class TestEnergyModelSeA(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA2(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelDPA2(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -118,14 +120,14 @@ def setUp(self): class TestEnergyModelHybrid(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelHybrid(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -134,6 +136,6 @@ def setUp(self): class TestEnergyModelZBL(unittest.TestCase, NullTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_zbl_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_permutation.py b/source/tests/pt/model/test_permutation.py index 2d391c7115..64aefdfd8a 100644 --- a/source/tests/pt/model/test_permutation.py +++ b/source/tests/pt/model/test_permutation.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import os import unittest @@ -11,6 +10,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -397,35 +399,35 @@ def test( class TestEnergyModelSeA(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestDOSModelSeA(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_dos) + model_params = deepcopy(model_dos) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA2(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelDPA2(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -434,14 +436,14 @@ def setUp(self): class TestEnergyModelHybrid(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelHybrid(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -450,14 +452,14 @@ def setUp(self): class TestEnergyModelZBL(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelSpinSeA(unittest.TestCase, PermutationTest): def setUp(self): - model_params = copy.deepcopy(model_spin) + model_params = deepcopy(model_spin) self.type_split = False self.test_spin = True self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_permutation_denoise.py b/source/tests/pt/model/test_permutation_denoise.py index 53bf55fb0f..ff856ff6e0 100644 --- a/source/tests/pt/model/test_permutation_denoise.py +++ b/source/tests/pt/model/test_permutation_denoise.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -25,9 +27,9 @@ dtype = torch.float64 -model_dpa1 = copy.deepcopy(model_dpa1) -model_dpa2 = copy.deepcopy(model_dpa2) -model_hybrid = copy.deepcopy(model_hybrid) +model_dpa1 = deepcopy(model_dpa1) +model_dpa2 = deepcopy(model_dpa2) +model_hybrid = deepcopy(model_hybrid) model_dpa1["type_map"] = ["O", "H", "B", "MASKED_TOKEN"] model_dpa1.pop("fitting_net") model_dpa2["type_map"] = ["O", "H", "B", "MASKED_TOKEN"] @@ -72,7 +74,7 @@ def test( @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA1(unittest.TestCase, PermutationDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -80,7 +82,7 @@ def setUp(self): @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA2(unittest.TestCase, PermutationDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model( model_params, @@ -90,7 +92,7 @@ def setUp(self): # @unittest.skip("hybrid not supported at the moment") # class TestDenoiseModelHybrid(unittest.TestCase, TestPermutationDenoise): # def setUp(self): -# model_params = copy.deepcopy(model_hybrid_denoise) +# model_params = deepcopy(model_hybrid_denoise) # self.type_split = True # self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_rot.py b/source/tests/pt/model/test_rot.py index ca6a6375c8..4452f91ca5 100644 --- a/source/tests/pt/model/test_rot.py +++ b/source/tests/pt/model/test_rot.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -147,35 +149,35 @@ def test( class TestEnergyModelSeA(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestDOSModelSeA(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_dos) + model_params = deepcopy(model_dos) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA2(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelDPA2(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -184,14 +186,14 @@ def setUp(self): class TestEnergyModelHybrid(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelHybrid(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -200,14 +202,14 @@ def setUp(self): class TestEnergyModelZBL(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelSpinSeA(unittest.TestCase, RotTest): def setUp(self): - model_params = copy.deepcopy(model_spin) + model_params = deepcopy(model_spin) self.type_split = False self.test_spin = True self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_rot_denoise.py b/source/tests/pt/model/test_rot_denoise.py index 9828ba5225..323d034ced 100644 --- a/source/tests/pt/model/test_rot_denoise.py +++ b/source/tests/pt/model/test_rot_denoise.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -105,7 +107,7 @@ def test( @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA1(unittest.TestCase, RotDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -113,7 +115,7 @@ def setUp(self): @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA2(unittest.TestCase, RotDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -121,7 +123,7 @@ def setUp(self): # @unittest.skip("hybrid not supported at the moment") # class TestEnergyModelHybrid(unittest.TestCase, TestRotDenoise): # def setUp(self): -# model_params = copy.deepcopy(model_hybrid_denoise) +# model_params = deepcopy(model_hybrid_denoise) # self.type_split = True # self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_saveload_dpa1.py b/source/tests/pt/model/test_saveload_dpa1.py index 5b2b6cd583..04a23c2e96 100644 --- a/source/tests/pt/model/test_saveload_dpa1.py +++ b/source/tests/pt/model/test_saveload_dpa1.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import json import os import unittest @@ -34,6 +33,9 @@ from deepmd.tf.common import ( expand_sys_str, ) +from deepmd.utils.copy import ( + deepcopy, +) def get_dataset(config): @@ -100,7 +102,7 @@ def get_model_result(self, read=False, model_file="tmp_model.pt"): return result def create_wrapper(self, read: bool): - model_config = copy.deepcopy(self.config["model"]) + model_config = deepcopy(self.config["model"]) model_config["resuming"] = read model_config["stat_file_dir"] = "stat_files" model_config["stat_file"] = "stat.hdf5" diff --git a/source/tests/pt/model/test_saveload_se_e2_a.py b/source/tests/pt/model/test_saveload_se_e2_a.py index d226f628bc..84d05c6d90 100644 --- a/source/tests/pt/model/test_saveload_se_e2_a.py +++ b/source/tests/pt/model/test_saveload_se_e2_a.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import json import os import unittest @@ -34,6 +33,9 @@ from deepmd.tf.common import ( expand_sys_str, ) +from deepmd.utils.copy import ( + deepcopy, +) def get_dataset(config): @@ -100,7 +102,7 @@ def get_model_result(self, read=False, model_file="tmp_model.pt"): return result def create_wrapper(self): - model_config = copy.deepcopy(self.config["model"]) + model_config = deepcopy(self.config["model"]) model = get_model(model_config).to(env.DEVICE) return ModelWrapper(model, self.loss) diff --git a/source/tests/pt/model/test_smooth.py b/source/tests/pt/model/test_smooth.py index 9a7040f9cc..7ffba43645 100644 --- a/source/tests/pt/model/test_smooth.py +++ b/source/tests/pt/model/test_smooth.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -147,7 +149,7 @@ def compare(ret0, ret1): class TestEnergyModelSeA(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) self.epsilon, self.aprec = None, None @@ -155,7 +157,7 @@ def setUp(self): class TestDOSModelSeA(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dos) + model_params = deepcopy(model_dos) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) self.epsilon, self.aprec = None, None @@ -163,7 +165,7 @@ def setUp(self): class TestEnergyModelDPA1(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) # less degree of smoothness, @@ -174,7 +176,7 @@ def setUp(self): class TestEnergyModelDPA1Excl1(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) model_params["pair_exclude_types"] = [[0, 1]] self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -186,7 +188,7 @@ def setUp(self): class TestEnergyModelDPA1Excl12(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) model_params["pair_exclude_types"] = [[0, 1], [0, 2]] self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -198,7 +200,7 @@ def setUp(self): class TestEnergyModelDPA2(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["descriptor"]["repinit"]["rcut"] = 8 model_params["descriptor"]["repinit"]["rcut_smth"] = 3.5 self.type_split = True @@ -208,7 +210,7 @@ def setUp(self): class TestEnergyModelDPA2_1(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["fitting_net"]["type"] = "ener" self.type_split = True self.test_virial = False @@ -218,7 +220,7 @@ def setUp(self): class TestEnergyModelDPA2_2(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["fitting_net"]["type"] = "ener" self.type_split = True self.test_virial = False @@ -228,7 +230,7 @@ def setUp(self): class TestEnergyModelHybrid(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) self.epsilon, self.aprec = None, None @@ -236,7 +238,7 @@ def setUp(self): class TestEnergyModelZBL(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) self.epsilon, self.aprec = None, 5e-2 @@ -244,7 +246,7 @@ def setUp(self): class TestEnergyModelSpinSeA(unittest.TestCase, SmoothTest): def setUp(self): - model_params = copy.deepcopy(model_spin) + model_params = deepcopy(model_spin) self.type_split = False self.test_spin = True self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_smooth_denoise.py b/source/tests/pt/model/test_smooth_denoise.py index faa892c5d0..c527163152 100644 --- a/source/tests/pt/model/test_smooth_denoise.py +++ b/source/tests/pt/model/test_smooth_denoise.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -104,7 +106,7 @@ def compare(ret0, ret1): @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA2(unittest.TestCase, SmoothDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["descriptor"]["sel"] = 8 model_params["descriptor"]["rcut_smth"] = 3.5 self.type_split = True @@ -117,7 +119,7 @@ def setUp(self): @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA2_1(unittest.TestCase, SmoothDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) # model_params["descriptor"]["combine_grrg"] = True self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -129,7 +131,7 @@ def setUp(self): # @unittest.skip("hybrid not supported at the moment") # class TestDenoiseModelHybrid(unittest.TestCase, TestSmoothDenoise): # def setUp(self): -# model_params = copy.deepcopy(model_hybrid_denoise) +# model_params = deepcopy(model_hybrid_denoise) # self.type_split = True # self.model = get_model(model_params).to(env.DEVICE) # self.epsilon, self.aprec = None, None diff --git a/source/tests/pt/model/test_trans.py b/source/tests/pt/model/test_trans.py index b62fac1312..88e9d43354 100644 --- a/source/tests/pt/model/test_trans.py +++ b/source/tests/pt/model/test_trans.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -89,35 +91,35 @@ def test( class TestEnergyModelSeA(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_se_e2_a) + model_params = deepcopy(model_se_e2_a) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestDOSModelSeA(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_dos) + model_params = deepcopy(model_dos) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA1(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelDPA2(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelDPA2(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -126,14 +128,14 @@ def setUp(self): class TestEnergyModelHybrid(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) class TestForceModelHybrid(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) model_params["fitting_net"]["type"] = "direct_force_ener" self.type_split = True self.test_virial = False @@ -142,14 +144,14 @@ def setUp(self): class TestEnergyModelZBL(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_zbl) + model_params = deepcopy(model_zbl) self.type_split = False self.model = get_model(model_params).to(env.DEVICE) class TestEnergyModelSpinSeA(unittest.TestCase, TransTest): def setUp(self): - model_params = copy.deepcopy(model_spin) + model_params = deepcopy(model_spin) self.type_split = False self.test_spin = True self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_trans_denoise.py b/source/tests/pt/model/test_trans_denoise.py index 84ec21929c..eb85ba76f3 100644 --- a/source/tests/pt/model/test_trans_denoise.py +++ b/source/tests/pt/model/test_trans_denoise.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -64,7 +66,7 @@ def test( @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA1(unittest.TestCase, TransDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa1) + model_params = deepcopy(model_dpa1) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -72,7 +74,7 @@ def setUp(self): @unittest.skip("support of the denoise is temporally disabled") class TestDenoiseModelDPA2(unittest.TestCase, TransDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_dpa2) + model_params = deepcopy(model_dpa2) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) @@ -80,7 +82,7 @@ def setUp(self): @unittest.skip("hybrid not supported at the moment") class TestDenoiseModelHybrid(unittest.TestCase, TransDenoiseTest): def setUp(self): - model_params = copy.deepcopy(model_hybrid) + model_params = deepcopy(model_hybrid) self.type_split = True self.model = get_model(model_params).to(env.DEVICE) diff --git a/source/tests/pt/model/test_unused_params.py b/source/tests/pt/model/test_unused_params.py index 98bbe7040e..454445f6fe 100644 --- a/source/tests/pt/model/test_unused_params.py +++ b/source/tests/pt/model/test_unused_params.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import unittest import torch @@ -10,6 +9,9 @@ from deepmd.pt.utils import ( env, ) +from deepmd.utils.copy import ( + deepcopy, +) from ...seed import ( GLOBAL_SEED, @@ -43,7 +45,7 @@ def test_unused(self): if (not grrg) and (not conv): # skip the case g2 is not involved continue - model = copy.deepcopy(model_dpa2) + model = deepcopy(model_dpa2) model["descriptor"]["repformer"]["nlayers"] = 2 # model["descriptor"]["combine_grrg"] = cmbg2 model["descriptor"]["repformer"]["update_g1_has_conv"] = conv diff --git a/source/tests/pt/test_calculator.py b/source/tests/pt/test_calculator.py index 5f5470c611..73a06f2d23 100644 --- a/source/tests/pt/test_calculator.py +++ b/source/tests/pt/test_calculator.py @@ -1,9 +1,6 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import json import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -17,6 +14,9 @@ from deepmd.pt.utils.ase_calc import ( DPCalculator, ) +from deepmd.utils.copy import ( + deepcopy, +) from ..seed import ( GLOBAL_SEED, diff --git a/source/tests/pt/test_change_bias.py b/source/tests/pt/test_change_bias.py index febc439f50..87bddb638e 100644 --- a/source/tests/pt/test_change_bias.py +++ b/source/tests/pt/test_change_bias.py @@ -4,9 +4,6 @@ import shutil import tempfile import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -36,6 +33,9 @@ from deepmd.pt.utils.utils import ( to_torch_tensor, ) +from deepmd.utils.copy import ( + deepcopy, +) from .common import ( run_dp, diff --git a/source/tests/pt/test_dp_show.py b/source/tests/pt/test_dp_show.py index 5d6cb9bd36..9552340671 100644 --- a/source/tests/pt/test_dp_show.py +++ b/source/tests/pt/test_dp_show.py @@ -7,9 +7,6 @@ from contextlib import ( redirect_stderr, ) -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -20,6 +17,9 @@ from deepmd.pt.utils.multi_task import ( preprocess_shared_params, ) +from deepmd.utils.copy import ( + deepcopy, +) from .common import ( run_dp, diff --git a/source/tests/pt/test_dp_test.py b/source/tests/pt/test_dp_test.py index 0427f2b14a..09c2e02296 100644 --- a/source/tests/pt/test_dp_test.py +++ b/source/tests/pt/test_dp_test.py @@ -4,9 +4,6 @@ import shutil import tempfile import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -21,6 +18,9 @@ from deepmd.pt.utils.utils import ( to_numpy_array, ) +from deepmd.utils.copy import ( + deepcopy, +) from .model.test_permutation import ( model_property, diff --git a/source/tests/pt/test_finetune.py b/source/tests/pt/test_finetune.py index dff3a4c8a4..bfe9c38695 100644 --- a/source/tests/pt/test_finetune.py +++ b/source/tests/pt/test_finetune.py @@ -4,9 +4,6 @@ import shutil import tempfile import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -39,6 +36,9 @@ to_numpy_array, to_torch_tensor, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.data import ( DataRequirementItem, ) diff --git a/source/tests/pt/test_init_frz_model.py b/source/tests/pt/test_init_frz_model.py index 69c738d6bd..e4bebdfc8d 100644 --- a/source/tests/pt/test_init_frz_model.py +++ b/source/tests/pt/test_init_frz_model.py @@ -4,9 +4,6 @@ import shutil import tempfile import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -20,6 +17,9 @@ from deepmd.pt.infer.deep_eval import ( DeepPot, ) +from deepmd.utils.copy import ( + deepcopy, +) from .common import ( run_dp, diff --git a/source/tests/pt/test_init_model.py b/source/tests/pt/test_init_model.py index dd264fbe89..39a36f08cd 100644 --- a/source/tests/pt/test_init_model.py +++ b/source/tests/pt/test_init_model.py @@ -4,9 +4,6 @@ import shutil import tempfile import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -19,6 +16,9 @@ from deepmd.pt.infer.deep_eval import ( DeepPot, ) +from deepmd.utils.copy import ( + deepcopy, +) from .common import ( run_dp, diff --git a/source/tests/pt/test_multitask.py b/source/tests/pt/test_multitask.py index 0f0224006d..d3c2f9be0b 100644 --- a/source/tests/pt/test_multitask.py +++ b/source/tests/pt/test_multitask.py @@ -3,9 +3,6 @@ import os import shutil import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -27,6 +24,9 @@ from deepmd.utils.compat import ( update_deepmd_input, ) +from deepmd.utils.copy import ( + deepcopy, +) from .model.test_permutation import ( model_dpa1, diff --git a/source/tests/pt/test_training.py b/source/tests/pt/test_training.py index a7fcedcede..f0fb12ec74 100644 --- a/source/tests/pt/test_training.py +++ b/source/tests/pt/test_training.py @@ -3,9 +3,6 @@ import os import shutil import unittest -from copy import ( - deepcopy, -) from pathlib import ( Path, ) @@ -18,6 +15,9 @@ from deepmd.pt.utils.finetune import ( get_finetune_rules, ) +from deepmd.utils.copy import ( + deepcopy, +) from .model.test_permutation import ( model_dos, diff --git a/source/tests/tf/test_deepmd_data.py b/source/tests/tf/test_deepmd_data.py index 0969a9baf1..8ac0c5acb8 100644 --- a/source/tests/tf/test_deepmd_data.py +++ b/source/tests/tf/test_deepmd_data.py @@ -1,5 +1,4 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import copy import os import shutil import unittest @@ -12,6 +11,9 @@ from deepmd.tf.utils.data import ( DeepmdData, ) +from deepmd.utils.copy import ( + deepcopy, +) from ..seed import ( GLOBAL_SEED, @@ -209,7 +211,7 @@ def test_shuffle(self): .add("test_frame", 5, atomic=False, must=True) ) data = dd._load_set(os.path.join(self.data_name, "set.foo")) - data_bk = copy.deepcopy(data) + data_bk = deepcopy(data) data, idx = dd._shuffle_data(data) self._comp_np_mat2(data_bk["coord"][idx, :], data["coord"]) self._comp_np_mat2(data_bk["test_atom"][idx, :], data["test_atom"]) @@ -225,7 +227,7 @@ def test_shuffle_with_numb_copy(self): .add("test_frame", 5, atomic=False, must=True) ) data = dd._load_set(os.path.join(self.data_name, "set.foo")) - data_bk = copy.deepcopy(data) + data_bk = deepcopy(data) data, idx = dd._shuffle_data(data) assert idx.size == np.sum(prob) self._comp_np_mat2(data_bk["coord"][idx, :], data["coord"]) diff --git a/source/tests/universal/common/cases/descriptor/utils.py b/source/tests/universal/common/cases/descriptor/utils.py index 4c8219d347..032425b5ef 100644 --- a/source/tests/universal/common/cases/descriptor/utils.py +++ b/source/tests/universal/common/cases/descriptor/utils.py @@ -1,14 +1,14 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import itertools -from copy import ( - deepcopy, -) import numpy as np from deepmd.dpmodel.utils import ( PairExcludeMask, ) +from deepmd.utils.copy import ( + deepcopy, +) from deepmd.utils.finetune import ( get_index_between_two_maps, ) diff --git a/source/tests/universal/common/cases/fitting/utils.py b/source/tests/universal/common/cases/fitting/utils.py index 2ab5fd911b..e68ec0dc53 100644 --- a/source/tests/universal/common/cases/fitting/utils.py +++ b/source/tests/universal/common/cases/fitting/utils.py @@ -1,14 +1,14 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import itertools -from copy import ( - deepcopy, -) import numpy as np from deepmd.dpmodel.utils import ( AtomExcludeMask, ) +from deepmd.utils.copy import ( + deepcopy, +) from .....seed import ( GLOBAL_SEED, diff --git a/source/tests/universal/common/cases/model/utils.py b/source/tests/universal/common/cases/model/utils.py index 628c415eb2..fc005808ea 100644 --- a/source/tests/universal/common/cases/model/utils.py +++ b/source/tests/universal/common/cases/model/utils.py @@ -1,8 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import unittest -from copy import ( - deepcopy, -) from typing import ( Any, Callable, @@ -17,6 +14,9 @@ from deepmd.dpmodel.utils.nlist import ( extend_input_and_build_neighbor_list, ) +from deepmd.utils.copy import ( + deepcopy, +) from .....seed import ( GLOBAL_SEED, diff --git a/source/tests/universal/common/cases/utils/utils.py b/source/tests/universal/common/cases/utils/utils.py index 9f86ca1feb..a8d1538aa8 100644 --- a/source/tests/universal/common/cases/utils/utils.py +++ b/source/tests/universal/common/cases/utils/utils.py @@ -1,11 +1,12 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import itertools -from copy import ( - deepcopy, -) import numpy as np +from deepmd.utils.copy import ( + deepcopy, +) + from .....seed import ( GLOBAL_SEED, )