Skip to content

Commit 4ab8794

Browse files
authored
Merge branch 'master' into devel_pt_dplr
Signed-off-by: Jia-Xin Zhu <jiaxinzhu@stu.xmu.edu.cn>
2 parents be3b838 + 8787b45 commit 4ab8794

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+674
-442
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ repos:
2929
exclude: ^source/3rdparty
3030
- repo: https://github.com/astral-sh/ruff-pre-commit
3131
# Ruff version.
32-
rev: v0.14.13
32+
rev: v0.14.14
3333
hooks:
3434
- id: ruff
3535
args: ["--fix"]

deepmd/dpmodel/modifier/base_modifier.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
Any,
99
)
1010

11+
from typing_extensions import (
12+
Self,
13+
)
14+
1115
from deepmd.utils.plugin import (
1216
PluginVariant,
1317
make_plugin_registry,
@@ -18,7 +22,7 @@ def make_base_modifier() -> type[object]:
1822
class BaseModifier(ABC, PluginVariant, make_plugin_registry("modifier")):
1923
"""Base class for data modifier."""
2024

21-
def __new__(cls, *args: Any, **kwargs: Any) -> "BaseModifier":
25+
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
2226
if cls is BaseModifier:
2327
cls = cls.get_class_by_type(kwargs["type"])
2428
return super().__new__(cls)

deepmd/entrypoints/eval_desc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def eval_desc(
128128
atype,
129129
fparam=fparam,
130130
aparam=aparam,
131+
mixed_type=mixed_type,
131132
)
132133

133134
# descriptors are kept in 3D format (nframes, natoms, ndesc)

deepmd/infer/deep_eval.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
)
1212

1313
import numpy as np
14+
from typing_extensions import (
15+
Self,
16+
)
1417

1518
from deepmd.backend.backend import (
1619
Backend,
@@ -89,9 +92,7 @@ def __init__(
8992
) -> None:
9093
pass
9194

92-
def __new__(
93-
cls, model_file: str, *args: object, **kwargs: object
94-
) -> "DeepEvalBackend":
95+
def __new__(cls, model_file: str, *args: object, **kwargs: object) -> Self:
9596
if cls is DeepEvalBackend:
9697
backend = Backend.detect_backend_by_model(model_file)
9798
return super().__new__(backend().deep_eval)
@@ -384,7 +385,7 @@ class DeepEval(ABC):
384385
Keyword arguments.
385386
"""
386387

387-
def __new__(cls, model_file: str, *args: object, **kwargs: object) -> "DeepEval":
388+
def __new__(cls, model_file: str, *args: object, **kwargs: object) -> Self:
388389
if cls is DeepEval:
389390
deep_eval = DeepEvalBackend(
390391
model_file,

deepmd/pd/entrypoints/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def change_bias(
574574
log.info(f"Saved model to {output_path}")
575575

576576

577-
def main(args: list[str] | argparse.Namespace | None = None):
577+
def main(args: list[str] | argparse.Namespace | None = None) -> None:
578578
if not isinstance(args, argparse.Namespace):
579579
FLAGS = parse_args(args=args)
580580
else:

deepmd/pd/infer/deep_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def _eval_model(
474474
fparam: np.ndarray | None,
475475
aparam: np.ndarray | None,
476476
request_defs: list[OutputVariableDef],
477-
):
477+
) -> tuple[np.ndarray, ...]:
478478
if not self.static_model:
479479
model = self.dp.to(DEVICE)
480480
prec = NP_PRECISION_DICT[RESERVED_PRECISION_DICT[GLOBAL_PD_FLOAT_PRECISION]]

deepmd/pd/infer/inference.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
class Tester:
2424
def __init__(
2525
self,
26-
model_ckpt,
27-
head=None,
28-
):
26+
model_ckpt: str,
27+
head: str | None = None,
28+
) -> None:
2929
"""Construct a DeePMD tester.
3030
3131
Args:

deepmd/pd/loss/ener.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
from typing import (
3+
Any,
4+
)
25

36
import paddle
47
import paddle.nn.functional as F
@@ -20,7 +23,9 @@
2023
)
2124

2225

23-
def custom_huber_loss(predictions, targets, delta=1.0):
26+
def custom_huber_loss(
27+
predictions: paddle.Tensor, targets: paddle.Tensor, delta: float = 1.0
28+
) -> paddle.Tensor:
2429
error = targets - predictions
2530
abs_error = paddle.abs(error)
2631
quadratic_loss = 0.5 * paddle.pow(error, 2)
@@ -32,13 +37,13 @@ def custom_huber_loss(predictions, targets, delta=1.0):
3237
class EnergyStdLoss(TaskLoss):
3338
def __init__(
3439
self,
35-
starter_learning_rate=1.0,
36-
start_pref_e=0.0,
37-
limit_pref_e=0.0,
38-
start_pref_f=0.0,
39-
limit_pref_f=0.0,
40-
start_pref_v=0.0,
41-
limit_pref_v=0.0,
40+
starter_learning_rate: float = 1.0,
41+
start_pref_e: float = 0.0,
42+
limit_pref_e: float = 0.0,
43+
start_pref_f: float = 0.0,
44+
limit_pref_f: float = 0.0,
45+
start_pref_v: float = 0.0,
46+
limit_pref_v: float = 0.0,
4247
start_pref_ae: float = 0.0,
4348
limit_pref_ae: float = 0.0,
4449
start_pref_pf: float = 0.0,
@@ -49,10 +54,10 @@ def __init__(
4954
limit_pref_gf: float = 0.0,
5055
numb_generalized_coord: int = 0,
5156
use_l1_all: bool = False,
52-
inference=False,
53-
use_huber=False,
54-
huber_delta=0.01,
55-
**kwargs,
57+
inference: bool = False,
58+
use_huber: bool = False,
59+
huber_delta: float = 0.01,
60+
**kwargs: Any,
5661
) -> None:
5762
r"""Construct a layer to compute loss on energy, force and virial.
5863
@@ -146,7 +151,15 @@ def __init__(
146151
"Huber loss is not implemented for force with atom_pref, generalized force and relative force. "
147152
)
148153

149-
def forward(self, input_dict, model, label, natoms, learning_rate, mae=False):
154+
def forward(
155+
self,
156+
input_dict: dict[str, paddle.Tensor],
157+
model: paddle.nn.Layer,
158+
label: dict[str, paddle.Tensor],
159+
natoms: int,
160+
learning_rate: float,
161+
mae: bool = False,
162+
) -> tuple[dict[str, paddle.Tensor], paddle.Tensor, dict[str, paddle.Tensor]]:
150163
"""Return loss on energy and force.
151164
152165
Parameters
@@ -535,10 +548,10 @@ def deserialize(cls, data: dict) -> "TaskLoss":
535548
class EnergyHessianStdLoss(EnergyStdLoss):
536549
def __init__(
537550
self,
538-
start_pref_h=0.0,
539-
limit_pref_h=0.0,
540-
**kwargs,
541-
):
551+
start_pref_h: float = 0.0,
552+
limit_pref_h: float = 0.0,
553+
**kwargs: Any,
554+
) -> None:
542555
r"""Enable the layer to compute loss on hessian.
543556
544557
Parameters
@@ -556,7 +569,15 @@ def __init__(
556569
self.start_pref_h = start_pref_h
557570
self.limit_pref_h = limit_pref_h
558571

559-
def forward(self, input_dict, model, label, natoms, learning_rate, mae=False):
572+
def forward(
573+
self,
574+
input_dict: dict[str, paddle.Tensor],
575+
model: paddle.nn.Module,
576+
label: dict[str, paddle.Tensor],
577+
natoms: int,
578+
learning_rate: float,
579+
mae: bool = False,
580+
) -> tuple[dict[str, paddle.Tensor], paddle.Tensor, dict[str, paddle.Tensor]]:
560581
model_pred, loss, more_loss = super().forward(
561582
input_dict, model, label, natoms, learning_rate, mae=mae
562583
)

deepmd/pd/loss/loss.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
ABC,
44
abstractmethod,
55
)
6+
from typing import (
7+
Any,
8+
)
69

710
import paddle
811

@@ -15,11 +18,19 @@
1518

1619

1720
class TaskLoss(paddle.nn.Layer, ABC, make_plugin_registry("loss")):
18-
def __init__(self, **kwargs):
21+
def __init__(self, **kwargs: Any) -> None:
1922
"""Construct loss."""
2023
super().__init__()
2124

22-
def forward(self, input_dict, model, label, natoms, learning_rate):
25+
def forward(
26+
self,
27+
input_dict: dict[str, paddle.Tensor],
28+
model: paddle.nn.Module,
29+
label: dict[str, paddle.Tensor],
30+
natoms: int,
31+
learning_rate: float,
32+
mae: bool | None = None,
33+
) -> paddle.Tensor:
2334
"""Return loss ."""
2435
raise NotImplementedError
2536

deepmd/pd/model/atomic_model/base_atomic_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def _default_bias(self) -> paddle.Tensor:
578578
device=device
579579
)
580580

581-
def _default_std(self):
581+
def _default_std(self) -> paddle.Tensor:
582582
ntypes = self.get_ntypes()
583583
return paddle.ones([self.n_out, ntypes, self.max_out_size], dtype=dtype).to(
584584
device=device
@@ -626,7 +626,7 @@ def _store_out_stat(
626626
paddle.assign(out_bias_data, self.out_bias)
627627
paddle.assign(out_std_data, self.out_std)
628628

629-
def get_ntypes(self):
629+
def get_ntypes(self) -> int:
630630
return len(self.type_map)
631631

632632
def get_buffer_ntypes(self) -> paddle.Tensor:

0 commit comments

Comments
 (0)