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

Add trainable settings for pt #3371

Merged
merged 7 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 3 additions & 2 deletions deepmd/pt/model/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
raise NotImplementedError("type_one_side is not supported.")
if precision != "default" and precision != "float64":
raise NotImplementedError("precison is not supported.")
if not trainable:
raise NotImplementedError("trainable == False is not supported.")
if exclude_types is not None and exclude_types != []:
raise NotImplementedError("exclude_types is not supported.")
if stripped_type_embedding:
Expand Down Expand Up @@ -106,6 +104,9 @@
self.type_embedding = TypeEmbedNet(ntypes, tebd_dim)
self.tebd_dim = tebd_dim
self.concat_output_tebd = concat_output_tebd
# set trainable
for param in self.parameters():
param.requires_grad = trainable

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa1.py#L108-L109

Added lines #L108 - L109 were not covered by tests

def get_rcut(self) -> float:
"""Returns the cut-off radius."""
Expand Down
6 changes: 6 additions & 0 deletions deepmd/pt/model/descriptor/dpa2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
repformer_update_style: str = "res_avg",
repformer_set_davg_zero: bool = True, # TODO
repformer_add_type_ebd_to_seq: bool = False,
trainable: bool = True,
type: Optional[
str
] = None, # work around the bad design in get_trainer and DpLoaderSet!
Expand Down Expand Up @@ -170,6 +171,8 @@
repformers block: set the avg to zero in statistics
repformer_add_type_ebd_to_seq : bool
repformers block: concatenate the type embedding at the output.
trainable : bool
If the parameters in the descriptor are trainable.

Returns
-------
Expand Down Expand Up @@ -249,6 +252,9 @@
self.rcut = self.repinit.get_rcut()
self.ntypes = ntypes
self.sel = self.repinit.sel
# set trainable
for param in self.parameters():
param.requires_grad = trainable

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa2.py#L256-L257

Added lines #L256 - L257 were not covered by tests

def get_rcut(self) -> float:
"""Returns the cut-off radius."""
Expand Down
4 changes: 4 additions & 0 deletions deepmd/pt/model/descriptor/se_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
exclude_types: List[Tuple[int, int]] = [],
old_impl: bool = False,
type_one_side: bool = True,
trainable: bool = True,
**kwargs,
):
"""Construct an embedding net of type `se_a`.
Expand Down Expand Up @@ -344,6 +345,9 @@
)
self.filter_layers = filter_layers
self.stats = None
# set trainable
for param in self.parameters():
param.requires_grad = trainable

Check warning on line 350 in deepmd/pt/model/descriptor/se_a.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_a.py#L349-L350

Added lines #L349 - L350 were not covered by tests

def get_rcut(self) -> float:
"""Returns the cut-off radius."""
Expand Down
4 changes: 4 additions & 0 deletions deepmd/pt/model/descriptor/se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
resnet_dt: bool = False,
exclude_types: List[Tuple[int, int]] = [],
old_impl: bool = False,
trainable: bool = True,
**kwargs,
):
super().__init__()
Expand Down Expand Up @@ -110,6 +111,9 @@
)
self.filter_layers = filter_layers
self.stats = None
# set trainable
for param in self.parameters():
param.requires_grad = trainable

Check warning on line 116 in deepmd/pt/model/descriptor/se_r.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_r.py#L115-L116

Added lines #L115 - L116 were not covered by tests

def get_rcut(self) -> float:
"""Returns the cut-off radius."""
Expand Down
13 changes: 12 additions & 1 deletion deepmd/pt/model/task/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
Random seed.
exclude_types: List[int]
Atomic contributions of the excluded atom types are set zero.
trainable : bool
If the parameters in the fitting net are trainable.

"""

Expand All @@ -265,6 +267,7 @@
rcond: Optional[float] = None,
seed: Optional[int] = None,
exclude_types: List[int] = [],
trainable: bool = True,
**kwargs,
):
super().__init__()
Expand All @@ -282,6 +285,11 @@
self.rcond = rcond
# order matters, should be place after the assignment of ntypes
self.reinit_exclude(exclude_types)
self.trainable = trainable

Check warning on line 288 in deepmd/pt/model/task/fitting.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/task/fitting.py#L288

Added line #L288 was not covered by tests
# need support for each layer settings
self.trainable = (

Check warning on line 290 in deepmd/pt/model/task/fitting.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/task/fitting.py#L290

Added line #L290 was not covered by tests
all(self.trainable) if isinstance(self.trainable, list) else self.trainable
iProzd marked this conversation as resolved.
Show resolved Hide resolved
)

net_dim_out = self._net_out_dim()
# init constants
Expand Down Expand Up @@ -356,6 +364,9 @@
if seed is not None:
log.info("Set seed to %d in fitting net.", seed)
torch.manual_seed(seed)
# set trainable
for param in self.parameters():
param.requires_grad = self.trainable

Check warning on line 369 in deepmd/pt/model/task/fitting.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/task/fitting.py#L368-L369

Added lines #L368 - L369 were not covered by tests

def reinit_exclude(
self,
Expand Down Expand Up @@ -397,7 +408,7 @@
# "spin": self.spin ,
## NOTICE: not supported by far
"tot_ener_zero": False,
"trainable": [True] * (len(self.neuron) + 1),
"trainable": [self.trainable] * (len(self.neuron) + 1),
"layer_name": None,
"use_aparam_as_mask": False,
"spin": None,
Expand Down
3 changes: 0 additions & 3 deletions deepmd/pt/train/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,6 @@ def get_loss(loss_params, start_lr, _ntypes):
frz_model = torch.jit.load(init_frz_model, map_location=DEVICE)
self.model.load_state_dict(frz_model.state_dict())

# Set trainable params
self.wrapper.set_trainable_params()

# Multi-task share params
if shared_links is not None:
self.wrapper.share_params(shared_links, resume=model_params["resuming"])
Expand Down
22 changes: 0 additions & 22 deletions deepmd/pt/train/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,6 @@ def __init__(
self.loss[task_key] = loss[task_key]
self.inference_only = self.loss is None

def set_trainable_params(self):
supported_types = ["type_embedding", "descriptor", "fitting_net"]
for model_item in self.model:
for net_type in supported_types:
trainable = True
if not self.multi_task:
if net_type in self.model_params:
trainable = self.model_params[net_type].get("trainable", True)
else:
if net_type in self.model_params["model_dict"][model_item]:
trainable = self.model_params["model_dict"][model_item][
net_type
].get("trainable", True)
if (
hasattr(self.model[model_item], net_type)
and getattr(self.model[model_item], net_type) is not None
):
for param in (
self.model[model_item].__getattr__(net_type).parameters()
):
param.requires_grad = trainable

def share_params(self, shared_links, resume=False):
supported_types = ["type_embedding", "descriptor", "fitting_net"]
for shared_item in shared_links:
Expand Down