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 31 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
36 changes: 6 additions & 30 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 @@ -252,7 +248,8 @@
) -> List[np.ndarray]:
"""This should be a list of user defined weights that matches the number of models to be combined."""
nmodels = len(self.models)
return [np.ones(1) / nmodels for _ in range(nmodels)]
nframes, nloc, _ = nlists_[0].shape
return [np.ones((nframes, nloc, 1)) / nmodels for _ in range(nmodels)]

Check warning on line 252 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#L251-L252

Added lines #L251 - L252 were not covered by tests

def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
Expand All @@ -275,27 +272,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
Loading
Loading