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

Fix: LinearModel Stat #3575

Merged
merged 35 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fdb6a3d
feat: jit export in linear model
anyangml Mar 21, 2024
a4201fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 21, 2024
c0c1dba
fix: revert atomic change
anyangml Mar 22, 2024
21dc19f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
a5cc8b1
Merge branch 'devel' into feat/linear-jit
anyangml Mar 22, 2024
cba5823
fix: revert change
anyangml Mar 22, 2024
83686cf
Merge branch 'devel' into feat/linear-jit
anyangml Mar 23, 2024
08e6053
fix: linear bias
anyangml Mar 24, 2024
83e8a5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2024
e57ac56
fix: index
anyangml Mar 24, 2024
abb2455
fix: UTs
anyangml Mar 25, 2024
7d8afcb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2024
6307ad5
chore: revert changes
anyangml Mar 25, 2024
82f1c39
fix: placeholder
anyangml Mar 25, 2024
798cb3c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2024
0f6f0f2
fix: Jit
anyangml Mar 25, 2024
eb3919c
Merge branch 'devel' into feat/linear-jit
anyangml Mar 25, 2024
8d59871
Merge branch 'devel' into feat/linear-jit
anyangml Apr 7, 2024
6fda3d6
fix: remove get/set_out_bias
anyangml Apr 7, 2024
a5c6f2d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2024
cbffc2b
fix: log
anyangml Apr 7, 2024
a3ca59e
Merge branch 'devel' into feat/linear-jit
anyangml Apr 7, 2024
33b33be
fix:UTs
anyangml Apr 7, 2024
3b6f65c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2024
9f0979d
fix: UTs
anyangml Apr 7, 2024
919dad1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2024
89fe042
chore: remove bias_atom_e
anyangml Apr 7, 2024
90b8d56
chore:use forward_common_atomic
anyangml Apr 7, 2024
1ef891e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2024
f32614f
feat: add UTs
anyangml Apr 7, 2024
1b233e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2024
97bb808
fix: precommit
anyangml Apr 7, 2024
2dec4cd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 7, 2024
6669939
fix: cuda
anyangml Apr 8, 2024
c8010bc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 8, 2024
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
21 changes: 0 additions & 21 deletions deepmd/dpmodel/atomic_model/dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,6 @@ def mixed_types(self) -> bool:
"""
return self.descriptor.mixed_types()

def set_out_bias(self, out_bias: np.ndarray, add=False) -> None:
"""
Modify the output bias for the atomic model.

Parameters
----------
out_bias : np.ndarray
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""
self.fitting["bias_atom_e"] = (
out_bias + self.fitting["bias_atom_e"] if add else out_bias
)

def get_out_bias(self) -> np.ndarray:
"""Return the output bias of the atomic model."""
return self.fitting["bias_atom_e"]

def forward_atomic(
self,
extended_coord: np.ndarray,
Expand Down
33 changes: 4 additions & 29 deletions deepmd/dpmodel/atomic_model/linear_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
)
]
ener_list = []

for i, model in enumerate(self.models):
mapping = self.mapping_list[i]
ener_list.append(
Expand All @@ -176,13 +175,10 @@
)["energy"]
)
self.weights = self._compute_weight(extended_coord, extended_atype, nlists_)
self.atomic_bias = None
if self.atomic_bias is not None:
raise NotImplementedError("Need to add bias in a future PR.")
else:
fit_ret = {
"energy": np.sum(np.stack(ener_list) * np.stack(self.weights), axis=0),
} # (nframes, nloc, 1)

fit_ret = {

Check warning on line 179 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#L179

Added line #L179 was not covered by tests
"energy": np.sum(np.stack(ener_list) * np.stack(self.weights), axis=0),
} # (nframes, nloc, 1)
return fit_ret

@staticmethod
Expand Down Expand Up @@ -275,27 +271,6 @@
# join all the selected types
return list(set().union(*[model.get_sel_type() for model in self.models]))

def set_out_bias(self, out_bias: np.ndarray, add=False) -> None:
"""
Modify the output bias for all the models in the linear atomic model.

Parameters
----------
out_bias : torch.Tensor
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""
for model in self.models:
model.set_out_bias(out_bias, add=add)

def get_out_bias(self) -> np.ndarray:
"""Return the weighted output bias of the linear atomic model."""
# TODO add get_out_bias for linear atomic model
raise NotImplementedError

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

Expand Down
19 changes: 0 additions & 19 deletions deepmd/dpmodel/atomic_model/make_base_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,6 @@ def get_sel_type(self) -> List[int]:
If returning an empty list, all atom types are selected.
"""

@abstractmethod
def set_out_bias(self, out_bias: t_tensor, add=False) -> None:
"""
Modify the output bias for the atomic model.

Parameters
----------
out_bias : t_tensor
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""

@abstractmethod
def get_out_bias(self) -> t_tensor:
"""Return the output bias of the atomic model."""

@abstractmethod
def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).
Expand Down
19 changes: 0 additions & 19 deletions deepmd/dpmodel/atomic_model/pairtab_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,6 @@ def mixed_types(self) -> bool:
# to match DPA1 and DPA2.
return True

def set_out_bias(self, out_bias: np.ndarray, add=False) -> None:
"""
Modify the output bias for the atomic model.

Parameters
----------
out_bias : torch.Tensor
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""
self.bias_atom_e = out_bias + self.bias_atom_e if add else out_bias

def get_out_bias(self) -> np.ndarray:
"""Return the output bias of the atomic model."""
return self.bias_atom_e

def serialize(self) -> dict:
dd = BaseAtomicModel.serialize(self)
dd.update(
Expand Down
2 changes: 0 additions & 2 deletions deepmd/pt/model/atomic_model/base_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ def change_out_bias(
rcond=self.rcond,
preset_bias=self.preset_out_bias,
)
# self.set_out_bias(delta_bias, add=True)
self._store_out_stat(delta_bias, out_std, add=True)
elif bias_adjust_mode == "set-by-statistic":
bias_out, std_out = compute_output_stats(
Expand All @@ -377,7 +376,6 @@ def change_out_bias(
rcond=self.rcond,
preset_bias=self.preset_out_bias,
)
# self.set_out_bias(bias_out)
self._store_out_stat(bias_out, std_out)
else:
raise RuntimeError("Unknown bias_adjust_mode mode: " + bias_adjust_mode)
Expand Down
24 changes: 3 additions & 21 deletions deepmd/pt/model/atomic_model/dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@
)
return fit_ret

def get_out_bias(self) -> torch.Tensor:
return self.out_bias

Check warning on line 180 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#L179-L180

Added lines #L179 - L180 were not covered by tests

def compute_or_load_stat(
self,
sampled_func,
Expand Down Expand Up @@ -217,27 +220,6 @@
self.descriptor.compute_input_stats(wrapped_sampler, stat_file_path)
self.compute_or_load_out_stat(wrapped_sampler, stat_file_path)

def set_out_bias(self, out_bias: torch.Tensor, add=False) -> None:
"""
Modify the output bias for the atomic model.

Parameters
----------
out_bias : torch.Tensor
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""
self.fitting_net["bias_atom_e"] = (
out_bias + self.fitting_net["bias_atom_e"] if add else out_bias
)

def get_out_bias(self) -> torch.Tensor:
"""Return the output bias of the atomic model."""
return self.fitting_net["bias_atom_e"]

def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
return self.fitting_net.get_dim_fparam()
Expand Down
77 changes: 34 additions & 43 deletions deepmd/pt/model/atomic_model/linear_atomic_model.py
anyangml marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
self.mapping_list.append(self.remap_atype(tpmp, self.type_map))
assert len(err_msg) == 0, "\n".join(err_msg)

self.atomic_bias = None
self.mixed_types_list = [model.mixed_types() for model in self.models]
self.rcuts = torch.tensor(
self.get_model_rcuts(), dtype=torch.float64, device=env.DEVICE
Expand All @@ -92,6 +91,9 @@
"""
return True

def get_out_bias(self) -> torch.Tensor:
return self.out_bias

Check warning on line 95 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#L94-L95

Added lines #L94 - L95 were not covered by tests

def get_rcut(self) -> float:
"""Get the cut-off radius."""
return max(self.get_model_rcuts())
Expand Down Expand Up @@ -188,36 +190,46 @@

for i, model in enumerate(self.models):
mapping = self.mapping_list[i]
raw_ret = model.forward_atomic(

Check warning on line 193 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#L193

Added line #L193 was not covered by tests
anyangml marked this conversation as resolved.
Show resolved Hide resolved
extended_coord,
mapping[extended_atype],
nlists_[i],
mapping,
fparam,
aparam,
)
# apply bias to each individual model
ener_list.append(
model.forward_atomic(
extended_coord,
mapping[extended_atype],
nlists_[i],
mapping,
fparam,
aparam,
)["energy"]
model.apply_out_stat(raw_ret, mapping[extended_atype][:, :nloc])[
"energy"
]
)

weights = self._compute_weight(extended_coord, extended_atype, nlists_)

atype = extended_atype[:, :nloc]
for idx, model in enumerate(self.models):
# TODO: provide interfaces for atomic models to access bias_atom_e
if isinstance(model, DPAtomicModel):
bias_atom_e = model.fitting_net.bias_atom_e
elif isinstance(model, PairTabAtomicModel):
bias_atom_e = model.bias_atom_e
else:
bias_atom_e = None
if bias_atom_e is not None:
ener_list[idx] += bias_atom_e[atype]

fit_ret = {
"energy": torch.sum(torch.stack(ener_list) * torch.stack(weights), dim=0),
} # (nframes, nloc, 1)
return fit_ret

def apply_out_stat(

Check warning on line 214 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#L214

Added line #L214 was not covered by tests
self,
ret: Dict[str, torch.Tensor],
atype: torch.Tensor,
):
"""Apply the stat to each atomic output.
The developer may override the method to define how the bias is applied
to the atomic output of the model.

Parameters
----------
ret
The returned dict by the forward_atomic method
atype
The atom types. nf x nloc

"""
return ret

Check warning on line 231 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#L231

Added line #L231 was not covered by tests

@staticmethod
def remap_atype(ori_map: List[str], new_map: List[str]) -> torch.Tensor:
"""
Expand Down Expand Up @@ -289,27 +301,6 @@
for _ in range(nmodels)
]

def set_out_bias(self, out_bias: torch.Tensor, add=False) -> None:
"""
Modify the output bias for all the models in the linear atomic model.

Parameters
----------
out_bias : torch.Tensor
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""
for model in self.models:
model.set_out_bias(out_bias, add=add)

def get_out_bias(self) -> torch.Tensor:
"""Return the weighted output bias of the linear atomic model."""
# TODO add get_out_bias for linear atomic model
raise NotImplementedError

def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
# tricky...
Expand Down
22 changes: 3 additions & 19 deletions deepmd/pt/model/atomic_model/pairtab_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
]
)

def get_out_bias(self) -> torch.Tensor:
return self.out_bias

Check warning on line 140 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#L139-L140

Added lines #L139 - L140 were not covered by tests

def get_rcut(self) -> float:
return self.rcut

Expand Down Expand Up @@ -226,25 +229,6 @@
"""
self.compute_or_load_out_stat(merged, stat_file_path)

def set_out_bias(self, out_bias: torch.Tensor, add=False) -> None:
"""
Modify the output bias for the atomic model.

Parameters
----------
out_bias : torch.Tensor
The new bias to be applied.
add : bool, optional
Whether to add the new bias to the existing one.
If False, the output bias will be directly replaced by the new bias.
If True, the new bias will be added to the existing one.
"""
self.bias_atom_e = out_bias + self.bias_atom_e if add else out_bias

def get_out_bias(self) -> torch.Tensor:
"""Return the output bias of the atomic model."""
return self.bias_atom_e

def forward_atomic(
self,
extended_coord: torch.Tensor,
Expand Down
4 changes: 2 additions & 2 deletions deepmd/pt/train/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ def _model_change_out_bias(
idx_type_map = sorter[np.searchsorted(model_type_map, new_type_map, sorter=sorter)]
log.info(
f"Change output bias of {new_type_map!s} "
f"from {to_numpy_array(old_bias[idx_type_map]).reshape(-1)!s} "
f"to {to_numpy_array(new_bias[idx_type_map]).reshape(-1)!s}."
f"from {to_numpy_array(old_bias[:,idx_type_map]).reshape(-1)!s} "
f"to {to_numpy_array(new_bias[:,idx_type_map]).reshape(-1)!s}."
)
return _model
Loading