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

refactor: split Model and AtomicModel #3438

Merged
merged 9 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions deepmd/dpmodel/atomic_model/make_base_atomic_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from abc import (
ABC,
abstractclassmethod,
abstractmethod,
)
from typing import (
Expand All @@ -13,6 +12,10 @@
from deepmd.dpmodel.output_def import (
FittingOutputDef,
)
from deepmd.utils.plugin import (
PluginVariant,
make_plugin_registry,
)


def make_base_atomic_model(
Expand All @@ -31,7 +34,7 @@ def make_base_atomic_model(

"""

class BAM(ABC):
class BAM(ABC, PluginVariant, make_plugin_registry("atomic model")):
"""Base Atomic Model provides the interfaces of an atomic model."""

@abstractmethod
Expand Down Expand Up @@ -128,8 +131,9 @@ def fwd(
def serialize(self) -> dict:
pass

@abstractclassmethod
def deserialize(cls):
@classmethod
@abstractmethod
def deserialize(cls, data: dict):
pass

def do_grad_r(
Expand Down
2 changes: 2 additions & 0 deletions deepmd/dpmodel/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
according to output variable definition
`deepmd.dpmodel.OutputVariableDef`.

All models should be inherited from :class:`deepmd.dpmodel.model.base_model.BaseModel`.
Models generated by `make_model` have already done it.
"""

from .dp_model import (
Expand Down
3 changes: 2 additions & 1 deletion deepmd/dpmodel/model/dp_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: LGPL-3.0-or-later


from deepmd.dpmodel.atomic_model import (
DPAtomicModel,
)
Expand All @@ -17,7 +18,7 @@

# use "class" to resolve "Variable not allowed in type expression"
@BaseModel.register("standard")
class DPModel(make_model(DPAtomicModel), BaseModel):
class DPModel(make_model(DPAtomicModel)):
@classmethod
def update_sel(cls, global_jdata: dict, local_jdata: dict):
"""Update the selection and perform neighbor statistics.
Expand Down
113 changes: 106 additions & 7 deletions deepmd/dpmodel/model/make_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
List,
Optional,
Tuple,
Type,
)

import numpy as np

from deepmd.dpmodel.atomic_model.base_atomic_model import (
BaseAtomicModel,
)
from deepmd.dpmodel.common import (
GLOBAL_ENER_FLOAT_PRECISION,
GLOBAL_NP_FLOAT_PRECISION,
PRECISION_DICT,
RESERVED_PRECISON_DICT,
NativeOP,
)
from deepmd.dpmodel.model.base_model import (
BaseModel,
)
from deepmd.dpmodel.output_def import (
FittingOutputDef,
ModelOutputDef,
OutputVariableCategory,
OutputVariableOperation,
Expand All @@ -34,7 +42,7 @@
)


def make_model(T_AtomicModel):
def make_model(T_AtomicModel: Type[BaseAtomicModel]):
"""Make a model as a derived class of an atomic model.

The model provide two interfaces.
Expand All @@ -57,16 +65,18 @@

"""

class CM(T_AtomicModel, NativeOP):
class CM(NativeOP, BaseModel):
def __init__(
self,
*args,
# underscore to prevent conflict with normal inputs
atomic_model_: Optional[T_AtomicModel] = None,
**kwargs,
):
super().__init__(
*args,
**kwargs,
)
if atomic_model_ is not None:
self.atomic_model: T_AtomicModel = atomic_model_
else:
self.atomic_model: T_AtomicModel = T_AtomicModel(*args, **kwargs)
self.precision_dict = PRECISION_DICT
self.reverse_precision_dict = RESERVED_PRECISON_DICT
self.global_np_float_precision = GLOBAL_NP_FLOAT_PRECISION
Expand Down Expand Up @@ -208,7 +218,7 @@
extended_coord, fparam=fparam, aparam=aparam
)
del extended_coord, fparam, aparam
atomic_ret = self.forward_common_atomic(
atomic_ret = self.atomic_model.forward_common_atomic(
cc_ext,
extended_atype,
nlist,
Expand Down Expand Up @@ -377,4 +387,93 @@
assert ret.shape[-1] == nnei
return ret

def do_grad_r(
self,
var_name: Optional[str] = None,
) -> bool:
"""Tell if the output variable `var_name` is r_differentiable.
if var_name is None, returns if any of the variable is r_differentiable.
"""
return self.atomic_model.do_grad_r(var_name)

Check warning on line 397 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L397

Added line #L397 was not covered by tests

def do_grad_c(
self,
var_name: Optional[str] = None,
) -> bool:
"""Tell if the output variable `var_name` is c_differentiable.
if var_name is None, returns if any of the variable is c_differentiable.
"""
return self.atomic_model.do_grad_c(var_name)

Check warning on line 406 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L406

Added line #L406 was not covered by tests

def serialize(self) -> dict:
return self.atomic_model.serialize()

@classmethod
def deserialize(cls, data) -> "CM":
return cls(atomic_model_=T_AtomicModel.deserialize(data))

def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
return self.atomic_model.get_dim_fparam()

Check warning on line 417 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L417

Added line #L417 was not covered by tests

def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return self.atomic_model.get_dim_aparam()

Check warning on line 421 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L421

Added line #L421 was not covered by tests

def get_sel_type(self) -> List[int]:
"""Get the selected atom types of this model.

Only atoms with selected atom types have atomic contribution
to the result of the model.
If returning an empty list, all atom types are selected.
"""
return self.atomic_model.get_sel_type()

Check warning on line 430 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L430

Added line #L430 was not covered by tests

def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).

If False, the shape is (nframes, nloc, ndim).
"""
return self.atomic_model.is_aparam_nall()

Check warning on line 437 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L437

Added line #L437 was not covered by tests

def get_rcut(self) -> float:
"""Get the cut-off radius."""
return self.atomic_model.get_rcut()

def get_type_map(self) -> List[str]:
"""Get the type map."""
return self.atomic_model.get_type_map()

def get_nsel(self) -> int:
"""Returns the total number of selected neighboring atoms in the cut-off radius."""
return self.atomic_model.get_nsel()

Check warning on line 449 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L449

Added line #L449 was not covered by tests

def get_nnei(self) -> int:
"""Returns the total number of selected neighboring atoms in the cut-off radius."""
return self.atomic_model.get_nnei()

Check warning on line 453 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L453

Added line #L453 was not covered by tests

def get_model_def_script(self) -> str:
"""Get the model definition script."""
return self.atomic_model.get_model_def_script()

Check warning on line 457 in deepmd/dpmodel/model/make_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/make_model.py#L457

Added line #L457 was not covered by tests

def get_sel(self) -> List[int]:
"""Returns the number of selected atoms for each type."""
return self.atomic_model.get_sel()

def mixed_types(self) -> bool:
"""If true, the model
1. assumes total number of atoms aligned across frames;
2. uses a neighbor list that does not distinguish different atomic types.

If false, the model
1. assumes total number of atoms of each atom type aligned across frames;
2. uses a neighbor list that distinguishes different atomic types.

"""
return self.atomic_model.mixed_types()

def atomic_output_def(self) -> FittingOutputDef:
"""Get the output def of the atomic model."""
return self.atomic_model.atomic_output_def()

return CM
31 changes: 25 additions & 6 deletions deepmd/pt/model/atomic_model/base_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
AtomExcludeMask,
PairExcludeMask,
)
from deepmd.utils.path import (
DPPath,
)

BaseAtomicModel_ = make_base_atomic_model(torch.Tensor)

Expand Down Expand Up @@ -55,12 +58,6 @@
else:
self.pair_excl = PairExcludeMask(self.get_ntypes(), self.pair_exclude_types)

# export public methods that are not abstract
get_nsel = torch.jit.export(BaseAtomicModel_.get_nsel)
get_nnei = torch.jit.export(BaseAtomicModel_.get_nnei)
get_ntypes = torch.jit.export(BaseAtomicModel_.get_ntypes)

@torch.jit.export
def get_model_def_script(self) -> str:
return self.model_def_script

Expand Down Expand Up @@ -126,3 +123,25 @@
"atom_exclude_types": self.atom_exclude_types,
"pair_exclude_types": self.pair_exclude_types,
}

def compute_or_load_stat(
self,
sampled_func,
stat_file_path: Optional[DPPath] = None,
):
"""
Compute or load the statistics parameters of the model,
such as mean and standard deviation of descriptors or the energy bias of the fitting net.
When `sampled` is provided, all the statistics parameters will be calculated (or re-calculated for update),
and saved in the `stat_file_path`(s).
When `sampled` is not provided, it will check the existence of `stat_file_path`(s)
and load the calculated statistics parameters.

Parameters
----------
sampled_func
The sampled data frames from different data systems.
stat_file_path
The path to the statistics files.
"""
raise NotImplementedError

Check warning on line 147 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#L147

Added line #L147 was not covered by tests
4 changes: 0 additions & 4 deletions deepmd/pt/model/atomic_model/dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,14 @@ def wrapped_sampler():
if self.fitting_net is not None:
self.fitting_net.compute_output_stats(wrapped_sampler, stat_file_path)

@torch.jit.export
def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
return self.fitting_net.get_dim_fparam()

@torch.jit.export
def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return self.fitting_net.get_dim_aparam()

@torch.jit.export
def get_sel_type(self) -> List[int]:
"""Get the selected atom types of this model.

Expand All @@ -243,7 +240,6 @@ def get_sel_type(self) -> List[int]:
"""
return self.fitting_net.get_sel_type()

@torch.jit.export
def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).

Expand Down
6 changes: 0 additions & 6 deletions deepmd/pt/model/atomic_model/linear_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ def mixed_types(self) -> bool:
"""
return True

@torch.jit.export
def get_rcut(self) -> float:
"""Get the cut-off radius."""
return max(self.get_model_rcuts())

@torch.jit.export
def get_type_map(self) -> List[str]:
"""Get the type map."""
return self.type_map
Expand Down Expand Up @@ -292,18 +290,15 @@ def _compute_weight(
"""This should be a list of user defined weights that matches the number of models to be combined."""
raise NotImplementedError

@torch.jit.export
def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
# tricky...
return max([model.get_dim_fparam() for model in self.models])

@torch.jit.export
def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return max([model.get_dim_aparam() for model in self.models])

@torch.jit.export
def get_sel_type(self) -> List[int]:
"""Get the selected atom types of this model.

Expand All @@ -324,7 +319,6 @@ def get_sel_type(self) -> List[int]:
)
).tolist()

@torch.jit.export
def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).

Expand Down
6 changes: 0 additions & 6 deletions deepmd/pt/model/atomic_model/pairtab_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ def fitting_output_def(self) -> FittingOutputDef:
]
)

@torch.jit.export
def get_rcut(self) -> float:
return self.rcut

@torch.jit.export
def get_type_map(self) -> List[str]:
return self.type_map

Expand Down Expand Up @@ -454,17 +452,14 @@ def _calculate_ener(coef: torch.Tensor, uu: torch.Tensor) -> torch.Tensor:
ener = etmp * uu + a0 # this energy has the extrapolated value when rcut > rmax
return ener

@torch.jit.export
def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
return 0

@torch.jit.export
def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return 0

@torch.jit.export
def get_sel_type(self) -> List[int]:
"""Get the selected atom types of this model.

Expand All @@ -474,7 +469,6 @@ def get_sel_type(self) -> List[int]:
"""
return []

@torch.jit.export
def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).

Expand Down
Loading