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

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

Merged
merged 11 commits into from
Jul 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
13 changes: 11 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
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, 1)
data.pop("@class")
data.pop("type")
variables = data.pop("@variables")
Expand All @@ -554,6 +560,9 @@
embeddings_strip = data.pop("embeddings_strip")
else:
embeddings_strip = None
# compat with version 1
if "use_tebd_bias" not in data:
data["use_tebd_bias"] = True

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

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa1.py#L565

Added line #L565 was not covered by tests
obj = cls(**data)

obj.se_atten["davg"] = variables["davg"]
Expand Down
13 changes: 11 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.
iProzd marked this conversation as resolved.
Show resolved Hide resolved
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
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 @@ -720,7 +725,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 @@ -732,6 +737,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 @@ -774,7 +780,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, 1)
data.pop("@class")
data.pop("type")
repinit_variable = data.pop("repinit_variable").copy()
Expand All @@ -785,6 +791,9 @@
add_tebd_to_repinit_out = data["add_tebd_to_repinit_out"]
data["repinit"] = RepinitArgs(**data.pop("repinit_args"))
data["repformer"] = RepformerArgs(**data.pop("repformer_args"))
# compat with version 1
if "use_tebd_bias" not in data:
data["use_tebd_bias"] = True

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

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa2.py#L796

Added line #L796 was not covered by tests
obj = cls(**data)
obj.type_embedding = TypeEmbedNet.deserialize(type_embedding)
if add_tebd_to_repinit_out:
Expand Down
10 changes: 8 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, 1)
data.pop("@class")
data.pop("type")
variables = data.pop("@variables")
Expand All @@ -167,6 +170,9 @@
attention_layers = data.pop("attention_layers")
data.pop("env_mat")
embeddings_strip = data.pop("embeddings_strip")
# compat with version 1
if "use_tebd_bias" not in data:
data["use_tebd_bias"] = True

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

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/se_atten_v2.py#L175

Added line #L175 was not covered by tests
obj = cls(**data)

obj.se_atten["davg"] = variables["davg"]
Expand Down
12 changes: 9 additions & 3 deletions deepmd/dpmodel/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ class EN(T_Network):
Floating point precision for the model paramters.
seed : int, optional
Random seed.
bias : bool, Optional
Whether to use bias in the embedding layer.
"""

def __init__(
Expand All @@ -581,6 +583,7 @@ def __init__(
resnet_dt: bool = False,
precision: str = DEFAULT_PRECISION,
seed: Optional[Union[int, List[int]]] = None,
bias: bool = True,
iProzd marked this conversation as resolved.
Show resolved Hide resolved
):
layers = []
i_in = in_dim
Expand All @@ -590,7 +593,7 @@ def __init__(
T_NetworkLayer(
i_in,
i_ot,
bias=True,
bias=bias,
use_timestep=resnet_dt,
activation_function=activation_function,
resnet=True,
Expand All @@ -605,6 +608,7 @@ def __init__(
self.activation_function = activation_function
self.resnet_dt = resnet_dt
self.precision = precision
self.bias = bias

def serialize(self) -> dict:
"""Serialize the network to a dict.
Expand All @@ -616,11 +620,12 @@ def serialize(self) -> dict:
"""
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 +641,7 @@ def deserialize(cls, data: dict) -> "EmbeddingNet":
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)
data.pop("@class", None)
layers = data.pop("layers")
obj = cls(**data)
Expand Down Expand Up @@ -691,6 +696,7 @@ def __init__(
activation_function=activation_function,
resnet_dt=resnet_dt,
precision=precision,
seed=seed,
)
i_in = neuron[-1] if len(neuron) > 0 else in_dim
i_ot = out_dim
Expand Down
13 changes: 11 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
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,11 +119,14 @@
The deserialized model
"""
data = data.copy()
check_version_compatibility(data.pop("@version", 1), 1, 1)
check_version_compatibility(data.pop("@version", 1), 2, 1)
data_cls = data.pop("@class")
assert data_cls == "TypeEmbedNet", f"Invalid class {data_cls}"

embedding_net = EmbeddingNet.deserialize(data.pop("embedding"))
# compat with version 1
if "use_tebd_bias" not in data:
data["use_tebd_bias"] = True

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

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/utils/type_embed.py#L129

Added line #L129 was not covered by tests
type_embedding_net = cls(**data)
type_embedding_net.embedding_net = embedding_net
return type_embedding_net
Expand All @@ -133,7 +141,7 @@
"""
return {
"@class": "TypeEmbedNet",
"@version": 1,
"@version": 2,
"ntypes": self.ntypes,
"neuron": self.neuron,
"resnet_dt": self.resnet_dt,
Expand All @@ -142,6 +150,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
13 changes: 11 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
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, 1)
data.pop("@class")
data.pop("type")
variables = data.pop("@variables")
Expand All @@ -523,6 +529,9 @@
embeddings_strip = data.pop("embeddings_strip")
else:
embeddings_strip = None
# compat with version 1
if "use_tebd_bias" not in data:
data["use_tebd_bias"] = True

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L534 was not covered by tests
obj = cls(**data)

def t_cvt(xx):
Expand Down
Loading