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

dp: pt: rearrange folder structure of atomic_model and model. #3268

Merged
merged 9 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 0 additions & 2 deletions deepmd/dpmodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
NativeOP,
)
from .model import (
DPAtomicModel,
DPModel,
)
from .output_def import (
Expand All @@ -20,7 +19,6 @@

__all__ = [
"DPModel",
"DPAtomicModel",
"PRECISION_DICT",
"DEFAULT_PRECISION",
"NativeOP",
Expand Down
23 changes: 23 additions & 0 deletions deepmd/dpmodel/atomic_model/__init__.py
njzjz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from .dp_atomic_model import (
DPAtomicModel,
)
from .linear_atomic_model import (
DPZBLLinearAtomicModel,
LinearAtomicModel,
)
from .make_base_atomic_model import (
make_base_atomic_model,
)
from .pairtab_atomic_model import (
PairTabAtomicModel,
)

__all__ = [
"make_base_atomic_model",
"BaseAtomicModel",
Fixed Show fixed Hide fixed
"DPAtomicModel",
"PairTabAtomicModel",
"LinearAtomicModel",
"DPZBLLinearAtomicModel",
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@

import numpy as np

from deepmd.dpmodel import (
FittingOutputDef,
OutputVariableDef,
)
from deepmd.dpmodel.utils.nlist import (
build_multiple_neighbor_list,
get_multiple_nlist_key,
nlist_distinguish_types,
)

from ..output_def import (
FittingOutputDef,
OutputVariableDef,
)
from .base_atomic_model import (
BaseAtomicModel,
)
from .dp_atomic_model import (
DPAtomicModel,
)
from .pairtab_atomic_model import (
PairTabModel,
PairTabAtomicModel,
)


Expand All @@ -39,8 +39,8 @@

Parameters
----------
models : list[DPAtomicModel or PairTabModel]
A list of models to be combined. PairTabModel must be used together with a DPAtomicModel.
models : list[DPAtomicModel or PairTabAtomicModel]
A list of models to be combined. PairTabAtomicModel must be used together with a DPAtomicModel.
"""

def __init__(
Expand Down Expand Up @@ -212,7 +212,7 @@
def __init__(
self,
dp_model: DPAtomicModel,
zbl_model: PairTabModel,
zbl_model: PairTabAtomicModel,
sw_rmin: float,
sw_rmax: float,
smin_alpha: Optional[float] = 0.1,
Expand Down Expand Up @@ -277,7 +277,9 @@
# use the larger rr based on nlist
nlist_larger = zbl_nlist if zbl_nnei >= dp_nnei else dp_nlist
masked_nlist = np.clip(nlist_larger, 0, None)
pairwise_rr = PairTabModel._get_pairwise_dist(extended_coord, masked_nlist)
pairwise_rr = PairTabAtomicModel._get_pairwise_dist(

Check warning on line 280 in deepmd/dpmodel/atomic_model/linear_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/linear_atomic_model.py#L280

Added line #L280 was not covered by tests
extended_coord, masked_nlist
)

numerator = np.sum(
pairwise_rr * np.exp(-pairwise_rr / self.smin_alpha), axis=-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)


class PairTabModel(BaseAtomicModel):
class PairTabAtomicModel(BaseAtomicModel):
"""Pairwise tabulation energy model.

This model can be used to tabulate the pairwise energy between atoms for either
Expand Down Expand Up @@ -99,7 +99,7 @@ def serialize(self) -> dict:
return {"tab": self.tab.serialize(), "rcut": self.rcut, "sel": self.sel}

@classmethod
def deserialize(cls, data) -> "PairTabModel":
def deserialize(cls, data) -> "PairTabAtomicModel":
rcut = data["rcut"]
sel = data["sel"]
tab = PairTab.deserialize(data["tab"])
Expand Down
8 changes: 0 additions & 8 deletions deepmd/dpmodel/model/__init__.py
njzjz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from .dp_atomic_model import (
DPAtomicModel,
)
from .dp_model import (
DPModel,
)
from .make_base_atomic_model import (
make_base_atomic_model,
)

__all__ = [
"DPModel",
"DPAtomicModel",
"make_base_atomic_model",
]
3 changes: 2 additions & 1 deletion deepmd/dpmodel/model/dp_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from .dp_atomic_model import (
from deepmd.dpmodel.atomic_model import (
DPAtomicModel,
)

from .make_model import (
make_model,
)
Expand Down
27 changes: 27 additions & 0 deletions deepmd/pt/model/atomic_model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from deepmd.dpmodel.atomic_model import (

Check warning on line 2 in deepmd/pt/model/atomic_model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/__init__.py#L2

Added line #L2 was not covered by tests
make_base_atomic_model,
)

from .base_atomic_model import (

Check warning on line 6 in deepmd/pt/model/atomic_model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/__init__.py#L6

Added line #L6 was not covered by tests
BaseAtomicModel,
)
from .dp_atomic_model import (

Check warning on line 9 in deepmd/pt/model/atomic_model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/__init__.py#L9

Added line #L9 was not covered by tests
DPAtomicModel,
)
from .linear_atomic_model import (

Check warning on line 12 in deepmd/pt/model/atomic_model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/__init__.py#L12

Added line #L12 was not covered by tests
DPZBLLinearAtomicModel,
LinearAtomicModel,
)
from .pairtab_atomic_model import (

Check warning on line 16 in deepmd/pt/model/atomic_model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/__init__.py#L16

Added line #L16 was not covered by tests
PairTabAtomicModel,
)

__all__ = [

Check warning on line 20 in deepmd/pt/model/atomic_model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/__init__.py#L20

Added line #L20 was not covered by tests
"make_base_atomic_model",
"BaseAtomicModel",
"DPAtomicModel",
"PairTabAtomicModel",
"LinearAtomicModel",
"DPZBLLinearAtomicModel",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import torch

from deepmd.dpmodel.model import (
from deepmd.dpmodel.atomic_model import (

Check warning on line 5 in deepmd/pt/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/base_atomic_model.py#L5

Added line #L5 was not covered by tests
make_base_atomic_model,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@
from .base_atomic_model import (
BaseAtomicModel,
)
from .model import (
BaseModel,
)

log = logging.getLogger(__name__)


class DPAtomicModel(BaseModel, BaseAtomicModel):
class DPAtomicModel(torch.nn.Module, BaseAtomicModel):
"""Model give atomic prediction of some physical property.

Check warning on line 36 in deepmd/pt/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/dp_atomic_model.py#L35-L36

Added lines #L35 - L36 were not covered by tests

Parameters
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,18 @@
from .dp_atomic_model import (
DPAtomicModel,
)
from .model import (
BaseModel,
)
from .pairtab_atomic_model import (
PairTabModel,
PairTabAtomicModel,
)


class LinearAtomicModel(BaseModel, BaseAtomicModel):
class LinearAtomicModel(torch.nn.Module, BaseAtomicModel):
"""Linear model make linear combinations of several existing models.

Parameters
----------
models : list[DPAtomicModel or PairTabModel]
A list of models to be combined. PairTabModel must be used together with a DPAtomicModel.
models : list[DPAtomicModel or PairTabAtomicModel]
A list of models to be combined. PairTabAtomicModel must be used together with a DPAtomicModel.
"""

def __init__(
Expand Down Expand Up @@ -226,7 +223,7 @@
def __init__(
self,
dp_model: DPAtomicModel,
zbl_model: PairTabModel,
zbl_model: PairTabAtomicModel,
sw_rmin: float,
sw_rmax: float,
smin_alpha: Optional[float] = 0.1,
Expand Down Expand Up @@ -294,7 +291,9 @@
# use the larger rr based on nlist
nlist_larger = zbl_nlist if zbl_nnei >= dp_nnei else dp_nlist
masked_nlist = torch.clamp(nlist_larger, 0)
pairwise_rr = PairTabModel._get_pairwise_dist(extended_coord, masked_nlist)
pairwise_rr = PairTabAtomicModel._get_pairwise_dist(

Check warning on line 294 in deepmd/pt/model/atomic_model/linear_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/linear_atomic_model.py#L294

Added line #L294 was not covered by tests
extended_coord, masked_nlist
)
numerator = torch.sum(
pairwise_rr * torch.exp(-pairwise_rr / self.smin_alpha), dim=-1
) # masked nnei will be zero, no need to handle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
from .base_atomic_model import (
BaseAtomicModel,
)
from .model import (
BaseModel,
)


class PairTabModel(BaseModel, BaseAtomicModel):
class PairTabAtomicModel(torch.nn.Module, BaseAtomicModel):

Check warning on line 24 in deepmd/pt/model/atomic_model/pairtab_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/pairtab_atomic_model.py#L24

Added line #L24 was not covered by tests
wanghan-iapcm marked this conversation as resolved.
Show resolved Hide resolved
"""Pairwise tabulation energy model.

This model can be used to tabulate the pairwise energy between atoms for either
Expand Down Expand Up @@ -117,7 +114,7 @@
return {"tab": self.tab.serialize(), "rcut": self.rcut, "sel": self.sel}

@classmethod
def deserialize(cls, data) -> "PairTabModel":
def deserialize(cls, data) -> "PairTabAtomicModel":

Check warning on line 117 in deepmd/pt/model/atomic_model/pairtab_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/atomic_model/pairtab_atomic_model.py#L117

Added line #L117 was not covered by tests
rcut = data["rcut"]
sel = data["sel"]
tab = PairTab.deserialize(data["tab"])
Expand Down
24 changes: 14 additions & 10 deletions deepmd/pt/model/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import copy

from deepmd.pt.model.descriptor.descriptor import (
Descriptor,
)
from deepmd.pt.model.model.dp_atomic_model import (
from deepmd.pt.model.atomic_model import (

Check warning on line 4 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L4

Added line #L4 was not covered by tests
DPAtomicModel,
PairTabAtomicModel,
)
from deepmd.pt.model.model.pairtab_atomic_model import (
PairTabModel,
from deepmd.pt.model.descriptor.descriptor import (

Check warning on line 8 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L8

Added line #L8 was not covered by tests
Descriptor,
)
from deepmd.pt.model.task import (
Fitting,
)

from .ener import (
from .dp_model import (

Check warning on line 15 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L15

Added line #L15 was not covered by tests
DPModel,
)
from .dp_zbl_model import (

Check warning on line 18 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L18

Added line #L18 was not covered by tests
DPZBLModel,
)
from .ener_model import (

Check warning on line 21 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L21

Added line #L21 was not covered by tests
EnergyModel,
ZBLModel,
)
from .model import (
BaseModel,
Expand Down Expand Up @@ -44,13 +47,13 @@
dp_model = DPAtomicModel(descriptor, fitting, type_map=model_params["type_map"])
# pairtab
filepath = model_params["use_srtab"]
pt_model = PairTabModel(
pt_model = PairTabAtomicModel(

Check warning on line 50 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L50

Added line #L50 was not covered by tests
filepath, model_params["descriptor"]["rcut"], model_params["descriptor"]["sel"]
)

rmin = model_params["sw_rmin"]
rmax = model_params["sw_rmax"]
return ZBLModel(
return DPZBLModel(

Check warning on line 56 in deepmd/pt/model/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/__init__.py#L56

Added line #L56 was not covered by tests
dp_model,
pt_model,
rmin,
Expand Down Expand Up @@ -84,4 +87,5 @@
"BaseModel",
"EnergyModel",
"get_model",
"DPModel",
]
10 changes: 10 additions & 0 deletions deepmd/pt/model/model/dp_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from deepmd.pt.model.atomic_model import (

Check warning on line 2 in deepmd/pt/model/model/dp_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/dp_model.py#L2

Added line #L2 was not covered by tests
DPAtomicModel,
)

from .make_model import (

Check warning on line 6 in deepmd/pt/model/model/dp_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/dp_model.py#L6

Added line #L6 was not covered by tests
make_model,
)

DPModel = make_model(DPAtomicModel)

Check warning on line 10 in deepmd/pt/model/model/dp_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/model/dp_model.py#L10

Added line #L10 was not covered by tests
Loading
Loading