Skip to content

breaking(pt/tf/dp): disable bias in type embedding #3958

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

Merged
merged 11 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions deepmd/dpmodel/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@
The default value is `None`, which means the `tebd_input_mode` setting will be used instead.
use_econf_tebd: bool, Optional
Whether to use electronic configuration type embedding.
use_tebd_bias : bool, Optional
Whether to use bias in the type embedding layer.
type_map: List[str], Optional
A list of strings. Give the name to each type of atoms.
spin
Expand Down Expand Up @@ -253,6 +255,7 @@
spin: Optional[Any] = None,
stripped_type_embedding: Optional[bool] = None,
use_econf_tebd: bool = False,
use_tebd_bias: bool = False,
type_map: Optional[List[str]] = None,
# consistent with argcheck, not used though
seed: Optional[Union[int, List[int]]] = None,
Expand Down Expand Up @@ -301,6 +304,7 @@
seed=child_seed(seed, 0),
)
self.use_econf_tebd = use_econf_tebd
self.use_tebd_bias = use_tebd_bias

Check warning on line 307 in deepmd/dpmodel/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa1.py#L307

Added line #L307 was not covered by tests
self.type_map = type_map
self.type_embedding = TypeEmbedNet(
ntypes=ntypes,
Expand All @@ -309,6 +313,7 @@
activation_function="Linear",
precision=precision,
use_econf_tebd=use_econf_tebd,
use_tebd_bias=use_tebd_bias,
type_map=type_map,
seed=child_seed(seed, 1),
)
Expand Down Expand Up @@ -491,7 +496,7 @@
data = {
"@class": "Descriptor",
"type": "dpa1",
"@version": 1,
"@version": 2,
"rcut": obj.rcut,
"rcut_smth": obj.rcut_smth,
"sel": obj.sel,
Expand All @@ -516,6 +521,7 @@
"type_one_side": obj.type_one_side,
"concat_output_tebd": self.concat_output_tebd,
"use_econf_tebd": self.use_econf_tebd,
"use_tebd_bias": self.use_tebd_bias,
"type_map": self.type_map,
# make deterministic
"precision": np.dtype(PRECISION_DICT[obj.precision]).name,
Expand All @@ -541,7 +547,7 @@
def deserialize(cls, data: dict) -> "DescrptDPA1":
"""Deserialize from dict."""
data = data.copy()
check_version_compatibility(data.pop("@version"), 1, 1)
check_version_compatibility(data.pop("@version"), 2, 2)

Check warning on line 550 in deepmd/dpmodel/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa1.py#L550

Added line #L550 was not covered by tests
data.pop("@class")
data.pop("type")
variables = data.pop("@variables")
Expand Down
10 changes: 8 additions & 2 deletions deepmd/dpmodel/descriptor/dpa2.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
seed: Optional[Union[int, List[int]]] = None,
add_tebd_to_repinit_out: bool = False,
use_econf_tebd: bool = False,
use_tebd_bias: bool = False,
type_map: Optional[List[str]] = None,
):
r"""The DPA-2 descriptor. see https://arxiv.org/abs/2312.15492.
Expand Down Expand Up @@ -361,6 +362,8 @@
Whether to add type embedding to the output representation from repinit before inputting it into repformer.
use_econf_tebd : bool, Optional
Whether to use electronic configuration type embedding.
use_tebd_bias : bool, Optional
Whether to use bias in the type embedding layer.
type_map : List[str], Optional
A list of strings. Give the name to each type of atoms.

Expand Down Expand Up @@ -449,6 +452,7 @@
seed=child_seed(seed, 1),
)
self.use_econf_tebd = use_econf_tebd
self.use_tebd_bias = use_tebd_bias

Check warning on line 455 in deepmd/dpmodel/descriptor/dpa2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa2.py#L455

Added line #L455 was not covered by tests
self.type_map = type_map
self.type_embedding = TypeEmbedNet(
ntypes=ntypes,
Expand All @@ -457,6 +461,7 @@
activation_function="Linear",
precision=precision,
use_econf_tebd=use_econf_tebd,
use_tebd_bias=use_tebd_bias,
type_map=type_map,
seed=child_seed(seed, 2),
)
Expand Down Expand Up @@ -718,7 +723,7 @@
data = {
"@class": "Descriptor",
"type": "dpa2",
"@version": 1,
"@version": 2,
"ntypes": self.ntypes,
"repinit_args": self.repinit_args.serialize(),
"repformer_args": self.repformer_args.serialize(),
Expand All @@ -730,6 +735,7 @@
"trainable": self.trainable,
"add_tebd_to_repinit_out": self.add_tebd_to_repinit_out,
"use_econf_tebd": self.use_econf_tebd,
"use_tebd_bias": self.use_tebd_bias,
"type_map": self.type_map,
"type_embedding": self.type_embedding.serialize(),
"g1_shape_tranform": self.g1_shape_tranform.serialize(),
Expand Down Expand Up @@ -772,7 +778,7 @@
@classmethod
def deserialize(cls, data: dict) -> "DescrptDPA2":
data = data.copy()
check_version_compatibility(data.pop("@version"), 1, 1)
check_version_compatibility(data.pop("@version"), 2, 2)

Check warning on line 781 in deepmd/dpmodel/descriptor/dpa2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa2.py#L781

Added line #L781 was not covered by tests
data.pop("@class")
data.pop("type")
repinit_variable = data.pop("repinit_variable").copy()
Expand Down
7 changes: 5 additions & 2 deletions deepmd/dpmodel/descriptor/se_atten_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
spin: Optional[Any] = None,
stripped_type_embedding: Optional[bool] = None,
use_econf_tebd: bool = False,
use_tebd_bias: bool = False,
type_map: Optional[List[str]] = None,
# consistent with argcheck, not used though
seed: Optional[Union[int, List[int]]] = None,
Expand Down Expand Up @@ -100,6 +101,7 @@
spin=spin,
stripped_type_embedding=stripped_type_embedding,
use_econf_tebd=use_econf_tebd,
use_tebd_bias=use_tebd_bias,
type_map=type_map,
# consistent with argcheck, not used though
seed=seed,
Expand All @@ -111,7 +113,7 @@
data = {
"@class": "Descriptor",
"type": "se_atten_v2",
"@version": 1,
"@version": 2,
"rcut": obj.rcut,
"rcut_smth": obj.rcut_smth,
"sel": obj.sel,
Expand All @@ -134,6 +136,7 @@
"type_one_side": obj.type_one_side,
"concat_output_tebd": self.concat_output_tebd,
"use_econf_tebd": self.use_econf_tebd,
"use_tebd_bias": self.use_tebd_bias,
"type_map": self.type_map,
# make deterministic
"precision": np.dtype(PRECISION_DICT[obj.precision]).name,
Expand All @@ -158,7 +161,7 @@
def deserialize(cls, data: dict) -> "DescrptSeAttenV2":
"""Deserialize from dict."""
data = data.copy()
check_version_compatibility(data.pop("@version"), 1, 1)
check_version_compatibility(data.pop("@version"), 2, 2)

Check warning on line 164 in deepmd/dpmodel/descriptor/se_atten_v2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/se_atten_v2.py#L164

Added line #L164 was not covered by tests
data.pop("@class")
data.pop("type")
variables = data.pop("@variables")
Expand Down
9 changes: 6 additions & 3 deletions deepmd/dpmodel/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@
resnet_dt: bool = False,
precision: str = DEFAULT_PRECISION,
seed: Optional[Union[int, List[int]]] = None,
bias: bool = True,
):
layers = []
i_in = in_dim
Expand All @@ -590,7 +591,7 @@
T_NetworkLayer(
i_in,
i_ot,
bias=True,
bias=bias,
use_timestep=resnet_dt,
activation_function=activation_function,
resnet=True,
Expand All @@ -605,6 +606,7 @@
self.activation_function = activation_function
self.resnet_dt = resnet_dt
self.precision = precision
self.bias = bias

Check warning on line 609 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L609

Added line #L609 was not covered by tests

def serialize(self) -> dict:
"""Serialize the network to a dict.
Expand All @@ -616,11 +618,12 @@
"""
return {
"@class": "EmbeddingNetwork",
"@version": 1,
"@version": 2,
"in_dim": self.in_dim,
"neuron": self.neuron.copy(),
"activation_function": self.activation_function,
"resnet_dt": self.resnet_dt,
"bias": self.bias,
# make deterministic
"precision": np.dtype(PRECISION_DICT[self.precision]).name,
"layers": [layer.serialize() for layer in self.layers],
Expand All @@ -636,7 +639,7 @@
The dict to deserialize from.
"""
data = copy.deepcopy(data)
check_version_compatibility(data.pop("@version", 1), 1, 1)
check_version_compatibility(data.pop("@version", 1), 2, 1)

Check warning on line 642 in deepmd/dpmodel/utils/network.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/network.py#L642

Added line #L642 was not covered by tests
data.pop("@class", None)
layers = data.pop("layers")
obj = cls(**data)
Expand Down
10 changes: 8 additions & 2 deletions deepmd/dpmodel/utils/type_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
Concat the zero padding to the output, as the default embedding of empty type.
use_econf_tebd: bool, Optional
Whether to use electronic configuration type embedding.
use_tebd_bias : bool, Optional
Whether to use bias in the type embedding layer.
type_map: List[str], Optional
A list of strings. Give the name to each type of atoms.
"""
Expand All @@ -61,6 +63,7 @@
seed: Optional[Union[int, List[int]]] = None,
padding: bool = False,
use_econf_tebd: bool = False,
use_tebd_bias: bool = False,
type_map: Optional[List[str]] = None,
) -> None:
self.ntypes = ntypes
Expand All @@ -72,6 +75,7 @@
self.trainable = trainable
self.padding = padding
self.use_econf_tebd = use_econf_tebd
self.use_tebd_bias = use_tebd_bias

Check warning on line 78 in deepmd/dpmodel/utils/type_embed.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/type_embed.py#L78

Added line #L78 was not covered by tests
self.type_map = type_map
embed_input_dim = ntypes
if self.use_econf_tebd:
Expand All @@ -85,6 +89,7 @@
self.resnet_dt,
self.precision,
seed=self.seed,
bias=self.use_tebd_bias,
)

def call(self) -> np.ndarray:
Expand Down Expand Up @@ -114,7 +119,7 @@
The deserialized model
"""
data = data.copy()
check_version_compatibility(data.pop("@version", 1), 1, 1)
check_version_compatibility(data.pop("@version", 1), 2, 2)

Check warning on line 122 in deepmd/dpmodel/utils/type_embed.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/type_embed.py#L122

Added line #L122 was not covered by tests
data_cls = data.pop("@class")
assert data_cls == "TypeEmbedNet", f"Invalid class {data_cls}"

Expand All @@ -133,7 +138,7 @@
"""
return {
"@class": "TypeEmbedNet",
"@version": 1,
"@version": 2,
"ntypes": self.ntypes,
"neuron": self.neuron,
"resnet_dt": self.resnet_dt,
Expand All @@ -142,6 +147,7 @@
"trainable": self.trainable,
"padding": self.padding,
"use_econf_tebd": self.use_econf_tebd,
"use_tebd_bias": self.use_tebd_bias,
"type_map": self.type_map,
"embedding": self.embedding_net.serialize(),
}
Expand Down
10 changes: 8 additions & 2 deletions deepmd/pt/model/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@
Random seed for parameter initialization.
use_econf_tebd: bool, Optional
Whether to use electronic configuration type embedding.
use_tebd_bias : bool, Optional
Whether to use bias in the type embedding layer.
type_map: List[str], Optional
A list of strings. Give the name to each type of atoms.
spin
Expand Down Expand Up @@ -241,6 +243,7 @@
stripped_type_embedding: Optional[bool] = None,
seed: Optional[Union[int, List[int]]] = None,
use_econf_tebd: bool = False,
use_tebd_bias: bool = False,
type_map: Optional[List[str]] = None,
# not implemented
spin=None,
Expand Down Expand Up @@ -293,13 +296,15 @@
old_impl=old_impl,
)
self.use_econf_tebd = use_econf_tebd
self.use_tebd_bias = use_tebd_bias

Check warning on line 299 in deepmd/pt/model/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa1.py#L299

Added line #L299 was not covered by tests
self.type_map = type_map
self.type_embedding = TypeEmbedNet(
ntypes,
tebd_dim,
precision=precision,
seed=child_seed(seed, 2),
use_econf_tebd=use_econf_tebd,
use_tebd_bias=use_tebd_bias,
type_map=type_map,
)
self.tebd_dim = tebd_dim
Expand Down Expand Up @@ -462,7 +467,7 @@
data = {
"@class": "Descriptor",
"type": "dpa1",
"@version": 1,
"@version": 2,
"rcut": obj.rcut,
"rcut_smth": obj.rcut_smth,
"sel": obj.sel,
Expand All @@ -487,6 +492,7 @@
"type_one_side": obj.type_one_side,
"concat_output_tebd": self.concat_output_tebd,
"use_econf_tebd": self.use_econf_tebd,
"use_tebd_bias": self.use_tebd_bias,
"type_map": self.type_map,
# make deterministic
"precision": RESERVED_PRECISON_DICT[obj.prec],
Expand All @@ -510,7 +516,7 @@
@classmethod
def deserialize(cls, data: dict) -> "DescrptDPA1":
data = data.copy()
check_version_compatibility(data.pop("@version"), 1, 1)
check_version_compatibility(data.pop("@version"), 2, 2)

Check warning on line 519 in deepmd/pt/model/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa1.py#L519

Added line #L519 was not covered by tests
data.pop("@class")
data.pop("type")
variables = data.pop("@variables")
Expand Down
9 changes: 7 additions & 2 deletions deepmd/pt/model/descriptor/dpa2.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
seed: Optional[Union[int, List[int]]] = None,
add_tebd_to_repinit_out: bool = False,
use_econf_tebd: bool = False,
use_tebd_bias: bool = False,
type_map: Optional[List[str]] = None,
old_impl: bool = False,
):
Expand Down Expand Up @@ -121,6 +122,8 @@
Whether to add type embedding to the output representation from repinit before inputting it into repformer.
use_econf_tebd : bool, Optional
Whether to use electronic configuration type embedding.
use_tebd_bias : bool, Optional
Whether to use bias in the type embedding layer.
type_map : List[str], Optional
A list of strings. Give the name to each type of atoms.

Expand Down Expand Up @@ -211,13 +214,15 @@
old_impl=old_impl,
)
self.use_econf_tebd = use_econf_tebd
self.use_tebd_bias = use_tebd_bias

Check warning on line 217 in deepmd/pt/model/descriptor/dpa2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa2.py#L217

Added line #L217 was not covered by tests
self.type_map = type_map
self.type_embedding = TypeEmbedNet(
ntypes,
self.repinit_args.tebd_dim,
precision=precision,
seed=child_seed(seed, 2),
use_econf_tebd=self.use_econf_tebd,
use_tebd_bias=use_tebd_bias,
type_map=type_map,
)
self.concat_output_tebd = concat_output_tebd
Expand Down Expand Up @@ -455,7 +460,7 @@
data = {
"@class": "Descriptor",
"type": "dpa2",
"@version": 1,
"@version": 2,
"ntypes": self.ntypes,
"repinit_args": self.repinit_args.serialize(),
"repformer_args": self.repformer_args.serialize(),
Expand Down Expand Up @@ -509,7 +514,7 @@
@classmethod
def deserialize(cls, data: dict) -> "DescrptDPA2":
data = data.copy()
check_version_compatibility(data.pop("@version"), 1, 1)
check_version_compatibility(data.pop("@version"), 2, 2)

Check warning on line 517 in deepmd/pt/model/descriptor/dpa2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa2.py#L517

Added line #L517 was not covered by tests
data.pop("@class")
data.pop("type")
repinit_variable = data.pop("repinit_variable").copy()
Expand Down
Loading
Loading