Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DPSpin for AtomicModel #3301

Merged
merged 69 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
c19f829
Support DPSpin for AtomicModel
iProzd Feb 19, 2024
cda2e60
Merge branch 'devel' into spin_rf
iProzd Feb 19, 2024
b740b2d
Improve the model inherit
iProzd Feb 20, 2024
85f8c12
delete abstractclassmethod
iProzd Feb 20, 2024
feb9907
resolve conversations
iProzd Feb 20, 2024
a8a6609
Merge branch 'devel' into spin_rf
iProzd Feb 20, 2024
cd23522
fix QL
iProzd Feb 21, 2024
3f4b46b
Merge branch 'devel' into spin_rf
iProzd Feb 21, 2024
5841e59
fix uts
iProzd Feb 21, 2024
96426c3
Merge branch 'devel' into spin_rf
iProzd Feb 21, 2024
c7b6c42
fix uts
iProzd Feb 21, 2024
2915121
reformat spin model
iProzd Feb 22, 2024
b4b0289
Merge branch 'devel' into spin_rf
iProzd Feb 22, 2024
65d6162
fix cuda error
iProzd Feb 22, 2024
07e3fca
Add uts and reformat SpinModel
iProzd Feb 25, 2024
2c44876
Merge branch 'devel' into spin_rf
iProzd Feb 25, 2024
412e98e
Update test_autodiff.py
iProzd Feb 25, 2024
6c8e57f
Merge branch 'spin_rf' of https://github.com/iProzd/deepmd-kit into s…
iProzd Feb 25, 2024
6179824
Add spin data and se_e2_a examples
iProzd Feb 25, 2024
4f67f78
Update test_ener_spin_model.py
iProzd Feb 25, 2024
5b5c085
Add dp SpinModel
iProzd Feb 26, 2024
4606ab0
Fix uts
iProzd Feb 26, 2024
51e5f49
use torch.logical_and
iProzd Feb 26, 2024
9bf51b4
Merge branch 'devel' into spin_rf
iProzd Feb 26, 2024
1185bc1
Fix uts; change force_real to force
iProzd Feb 26, 2024
e35d1ad
improve env_protection doc
iProzd Feb 27, 2024
503e8c0
Update test_smooth.py
iProzd Feb 27, 2024
b121140
update example
iProzd Feb 27, 2024
796d870
Merge branch 'devel' into spin_rf
iProzd Feb 27, 2024
c2f306c
Update __init__.py
iProzd Feb 27, 2024
847565a
fix uts
iProzd Feb 27, 2024
6529441
Update test_smooth.py
iProzd Feb 27, 2024
e7dab5d
Support spin dp_eval and test
iProzd Feb 27, 2024
98d8232
Update test_smooth.py
iProzd Feb 27, 2024
b8e1dab
move `has_spin` to `has_spin_pt` in pt deep_eval
iProzd Feb 27, 2024
2475dea
Merge branch 'devel' into spin_rf
iProzd Feb 27, 2024
872702e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2024
fd9ca77
Merge branch 'devel' into spin_rf
iProzd Feb 28, 2024
6c0ef31
Update test_permutation.py
iProzd Feb 28, 2024
6eddb89
Update deep_eval.py
iProzd Feb 28, 2024
4fede6a
megre devel into spin_rf
iProzd Mar 4, 2024
64410d0
Merge branch 'devel' into spin_rf
iProzd Mar 4, 2024
0cc0776
Merge branch 'devel' into spin_rf
iProzd Mar 5, 2024
449bbf5
Merge branch 'devel' into spin_rf
iProzd Mar 5, 2024
24f5fd4
Update spin
iProzd Mar 5, 2024
4f2a8fc
fix se_r
iProzd Mar 5, 2024
dcfcadb
Merge branch 'devel' into spin_rf
iProzd Mar 5, 2024
b7554b9
Update fitting.py
iProzd Mar 5, 2024
8a72e01
Remove virial support
iProzd Mar 5, 2024
9772fa4
Add exclude_types for data stat
iProzd Mar 5, 2024
f27ae51
Update outdef for mask_mag
iProzd Mar 5, 2024
7409e6b
Update test_output_def.py
iProzd Mar 5, 2024
62c30cb
Fix uts
iProzd Mar 5, 2024
7804da8
Fix uts
iProzd Mar 5, 2024
8376830
Merge branch 'devel' into spin_rf
iProzd Mar 5, 2024
37c6330
Update multi_task.py
iProzd Mar 5, 2024
93e253e
Update test_deeppot_a.py
iProzd Mar 5, 2024
9dccb1c
Merge branch 'devel' into spin_rf
iProzd Mar 6, 2024
3cb3316
use to_numpy_array
iProzd Mar 6, 2024
7cb8f9c
Update `has_spin`
iProzd Mar 6, 2024
84d1c70
add mask in stat data
iProzd Mar 6, 2024
fa9ff2c
Merge branch 'devel' into spin_rf
iProzd Mar 6, 2024
dcaf07c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 6, 2024
5b49c75
Fix uts
iProzd Mar 6, 2024
3c09148
replace expand_aparam from fitting to model
iProzd Mar 6, 2024
a73c314
Add dp test ut for spin
iProzd Mar 6, 2024
e9582c7
add ut for spin loss
iProzd Mar 7, 2024
09dfd10
add ut for spin
iProzd Mar 7, 2024
c0700c9
Update test_dp_test.py
iProzd Mar 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deepmd/backend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

@Backend.register("pt")
@Backend.register("pytorch")
class TensorFlowBackend(Backend):
"""TensorFlow backend."""
class PyTorchBackend(Backend):
"""PyTorch backend."""

name = "PyTorch"
"""The formal name of the backend."""
Expand Down
8 changes: 6 additions & 2 deletions deepmd/dpmodel/descriptor/se_e2_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class DescrptSeA(NativeOP, BaseDescriptor):
exclude_types : List[List[int]]
The excluded pairs of types which have no interaction with each other.
For example, `[[0, 1]]` means no interaction between type 0 and type 1.
env_protection: float
Protection parameter to prevent division by zero errors during environment matrix calculations.
set_davg_zero
Set the shift of embedding net input to zero.
activation_function
Expand Down Expand Up @@ -149,6 +151,7 @@ def __init__(
trainable: bool = True,
type_one_side: bool = True,
exclude_types: List[List[int]] = [],
env_protection: float = 0.0,
set_davg_zero: bool = False,
activation_function: str = "tanh",
precision: str = DEFAULT_PRECISION,
Expand All @@ -169,6 +172,7 @@ def __init__(
self.resnet_dt = resnet_dt
self.trainable = trainable
self.type_one_side = type_one_side
self.env_protection = env_protection
self.set_davg_zero = set_davg_zero
self.activation_function = activation_function
self.precision = precision
Expand All @@ -192,7 +196,7 @@ def __init__(
self.resnet_dt,
self.precision,
)
self.env_mat = EnvMat(self.rcut, self.rcut_smth)
self.env_mat = EnvMat(self.rcut, self.rcut_smth, protection=self.env_protection)
self.nnei = np.sum(self.sel)
self.davg = np.zeros(
[self.ntypes, self.nnei, 4], dtype=PRECISION_DICT[self.precision]
Expand Down Expand Up @@ -378,6 +382,7 @@ def serialize(self) -> dict:
"trainable": self.trainable,
"type_one_side": self.type_one_side,
"exclude_types": self.exclude_types,
"env_protection": self.env_protection,
"set_davg_zero": self.set_davg_zero,
"activation_function": self.activation_function,
# make deterministic
Expand Down Expand Up @@ -406,7 +411,6 @@ def deserialize(cls, data: dict) -> "DescrptSeA":
obj["davg"] = variables["davg"]
obj["dstd"] = variables["dstd"]
obj.embeddings = NetworkCollection.deserialize(embeddings)
obj.env_mat = EnvMat.deserialize(env_mat)
return obj

@classmethod
Expand Down
6 changes: 4 additions & 2 deletions deepmd/dpmodel/descriptor/se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(
trainable: bool = True,
type_one_side: bool = True,
exclude_types: List[List[int]] = [],
env_protection: float = 0.0,
set_davg_zero: bool = False,
activation_function: str = "tanh",
precision: str = DEFAULT_PRECISION,
Expand Down Expand Up @@ -133,6 +134,7 @@ def __init__(
self.precision = precision
self.spin = spin
self.emask = PairExcludeMask(self.ntypes, self.exclude_types)
self.env_protection = env_protection

in_dim = 1 # not considiering type embedding
self.embeddings = NetworkCollection(
Expand All @@ -150,7 +152,7 @@ def __init__(
self.resnet_dt,
self.precision,
)
self.env_mat = EnvMat(self.rcut, self.rcut_smth)
self.env_mat = EnvMat(self.rcut, self.rcut_smth, protection=self.env_protection)
self.nnei = np.sum(self.sel)
self.davg = np.zeros(
[self.ntypes, self.nnei, 1], dtype=PRECISION_DICT[self.precision]
Expand Down Expand Up @@ -305,6 +307,7 @@ def serialize(self) -> dict:
"trainable": self.trainable,
"type_one_side": self.type_one_side,
"exclude_types": self.exclude_types,
"env_protection": self.env_protection,
"set_davg_zero": self.set_davg_zero,
"activation_function": self.activation_function,
# make deterministic
Expand Down Expand Up @@ -333,7 +336,6 @@ def deserialize(cls, data: dict) -> "DescrptSeR":
obj["davg"] = variables["davg"]
obj["dstd"] = variables["dstd"]
obj.embeddings = NetworkCollection.deserialize(embeddings)
obj.env_mat = EnvMat.deserialize(env_mat)
return obj

@classmethod
Expand Down
1 change: 1 addition & 0 deletions deepmd/dpmodel/fitting/ener_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(
use_aparam_as_mask=use_aparam_as_mask,
spin=spin,
mixed_types=mixed_types,
exclude_types=exclude_types,
)

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions deepmd/dpmodel/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
from .make_model import (
make_model,
)
from .spin_model import (
SpinModel,
)

__all__ = [
"DPModel",
"SpinModel",
"make_model",
]
58 changes: 56 additions & 2 deletions deepmd/dpmodel/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
from deepmd.dpmodel.model.dp_model import (
DPModel,
)
from deepmd.dpmodel.model.spin_model import (
SpinModel,
)
from deepmd.utils.spin import (
Spin,
)


def get_model(data: dict) -> DPModel:
"""Get a DPModel from a dictionary.
def get_standard_model(data: dict) -> DPModel:
"""Get a standard DPModel from a dictionary.

Parameters
----------
Expand All @@ -30,6 +36,7 @@
fitting = EnergyFittingNet(
ntypes=descriptor.get_ntypes(),
dim_descrpt=descriptor.get_dim_out(),
mixed_types=descriptor.mixed_types(),
**data["fitting_net"],
)
else:
Expand All @@ -41,3 +48,50 @@
atom_exclude_types=data.get("atom_exclude_types", []),
pair_exclude_types=data.get("pair_exclude_types", []),
)


def get_spin_model(data: dict) -> SpinModel:
"""Get a spin model from a dictionary.

Parameters
----------
data : dict
The data to construct the model.
"""
# include virtual spin and placeholder types
data["type_map"] += [item + "_spin" for item in data["type_map"]]
spin = Spin(

Check warning on line 63 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L62-L63

Added lines #L62 - L63 were not covered by tests
use_spin=data["spin"]["use_spin"],
virtual_scale=data["spin"]["virtual_scale"],
)
pair_exclude_types = spin.get_pair_exclude_types(

Check warning on line 67 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L67

Added line #L67 was not covered by tests
exclude_types=data.get("pair_exclude_types", None)
)
data["pair_exclude_types"] = pair_exclude_types

Check warning on line 70 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L70

Added line #L70 was not covered by tests
# for descriptor data stat
data["descriptor"]["exclude_types"] = pair_exclude_types
atom_exclude_types = spin.get_atom_exclude_types(

Check warning on line 73 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L72-L73

Added lines #L72 - L73 were not covered by tests
exclude_types=data.get("atom_exclude_types", None)
)
data["atom_exclude_types"] = atom_exclude_types
if "env_protection" not in data["descriptor"]:
data["descriptor"]["env_protection"] = 1e-6
if data["descriptor"]["type"] in ["se_e2_a"]:

Check warning on line 79 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L76-L79

Added lines #L76 - L79 were not covered by tests
# only expand sel for se_e2_a
data["descriptor"]["sel"] += data["descriptor"]["sel"]
backbone_model = get_standard_model(data)
return SpinModel(backbone_model=backbone_model, spin=spin)

Check warning on line 83 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L81-L83

Added lines #L81 - L83 were not covered by tests


def get_model(data: dict):
"""Get a model from a dictionary.

Parameters
----------
data : dict
The data to construct the model.
"""
if "spin" in data:
return get_spin_model(data)

Check warning on line 95 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L95

Added line #L95 was not covered by tests
else:
return get_standard_model(data)
Loading