Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 7, 2024
1 parent f32614f commit 1b233e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
9 changes: 5 additions & 4 deletions deepmd/pt/model/atomic_model/linear_atomic_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import copy
from typing import (
Callable,
Dict,
List,
Optional,
Tuple,
Union,
Callable,
)

import torch
Expand Down Expand Up @@ -297,7 +297,8 @@ def _compute_weight(
nmodels = len(self.models)
nframes, nloc, _ = nlists_[0].shape

Check warning on line 298 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#L298

Added line #L298 was not covered by tests
return [
torch.ones((nframes, nloc, 1), dtype=torch.float64, device=env.DEVICE) / nmodels
torch.ones((nframes, nloc, 1), dtype=torch.float64, device=env.DEVICE)
/ nmodels
for _ in range(nmodels)
]

Expand Down Expand Up @@ -336,7 +337,7 @@ def is_aparam_nall(self) -> bool:
If False, the shape is (nframes, nloc, ndim).
"""
return False

def compute_or_load_out_stat(

Check warning on line 341 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#L341

Added line #L341 was not covered by tests
self,
merged: Union[Callable[[], List[dict]], List[dict]],
Expand All @@ -360,7 +361,7 @@ def compute_or_load_out_stat(
"""
for md in self.models:
md.compute_or_load_out_stat(merged, stat_file_path)

Check warning on line 363 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#L362-L363

Added lines #L362 - L363 were not covered by tests

def compute_or_load_stat(

Check warning on line 365 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#L365

Added line #L365 was not covered by tests
self,
sampled_func,
Expand Down
22 changes: 11 additions & 11 deletions source/tests/pt/model/test_linear_atomic_model_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
OutputVariableDef,
)
from deepmd.pt.model.atomic_model import (
BaseAtomicModel,
DPAtomicModel,
LinearEnergyAtomicModel,
)
Expand Down Expand Up @@ -85,9 +84,10 @@ def forward(
.to(env.GLOBAL_PT_FLOAT_PRECISION)
.to(env.DEVICE)
)

return ret


class FooFittingB(torch.nn.Module, BaseFitting):
def output_def(self):
return FittingOutputDef(
Expand Down Expand Up @@ -128,9 +128,10 @@ def forward(
.to(env.GLOBAL_PT_FLOAT_PRECISION)
.to(env.DEVICE)
)

return ret


class TestAtomicModelStat(unittest.TestCase, TestCaseSingleFrameWithNlist):
def tearDown(self):
self.tempdir.cleanup()
Expand All @@ -153,7 +154,6 @@ def setUp(self):
),
# bias of foo: 1, 3
"energy": to_torch_tensor(np.array([5.0, 7.0]).reshape(2, 1)),

}
]
self.tempdir = tempfile.TemporaryDirectory()
Expand Down Expand Up @@ -183,9 +183,9 @@ def test_linear_atomic_model_stat_with_bias(self):
ft_b,
type_map=type_map,
).to(env.DEVICE)
linear_model = LinearEnergyAtomicModel(
[md0,md1],type_map=type_map
).to(env.DEVICE)
linear_model = LinearEnergyAtomicModel([md0, md1], type_map=type_map).to(
env.DEVICE
)

args = [
to_torch_tensor(ii) for ii in [self.coord_ext, self.atype_ext, self.nlist]
Expand All @@ -196,7 +196,7 @@ def test_linear_atomic_model_stat_with_bias(self):
# 1. test run without bias
# nf x na x odim
ret0 = linear_model.forward_common_atomic(*args)

ret0 = to_numpy_array(ret0["energy"])
ret_no_bias = []
for md in linear_model.models:
Expand All @@ -207,7 +207,7 @@ def test_linear_atomic_model_stat_with_bias(self):
[7.0, 8.0, 9.0],
]
).reshape([nf, nloc] + linear_model.fitting_output_def()["energy"].shape)

np.testing.assert_almost_equal(ret0, expected_ret0)

# 2. test bias is applied
Expand All @@ -222,8 +222,8 @@ def test_linear_atomic_model_stat_with_bias(self):
ret = to_numpy_array(ret["energy"])
linear_ret.append(ret_no_bias[idx] + ener_bias[at])
np.testing.assert_almost_equal((ret_no_bias[idx] + ener_bias[at]), ret)

# linear model not adding bias again
ret1 = linear_model.forward_common_atomic(*args)
ret1 = to_numpy_array(ret1["energy"])
np.testing.assert_almost_equal(torch.mean(torch.stack(linear_ret),dim=0), ret1)
np.testing.assert_almost_equal(torch.mean(torch.stack(linear_ret), dim=0), ret1)

0 comments on commit 1b233e7

Please sign in to comment.