Skip to content

Commit e8afb6e

Browse files
Copilotnjzjzpre-commit-ci[bot]Copilot
authored
style(paddle): Add comprehensive type hints to Paddle backend and enable ANN rules (#4944)
- [x] Fix type annotations in deepmd/pd/model/network/ (67 errors fixed) - [x] Fix type annotations in deepmd/pd/utils/ (57 errors fixed) - [x] Fix type annotations in deepmd/pd/model/atomic_model/ (5 errors fixed) - [x] Fix type annotations in deepmd/pd/infer/ (3 errors fixed) - [x] Fix type annotations in deepmd/pd/model/descriptor/ (160 errors fixed) - [x] Fix type annotations in deepmd/pd/loss/ener.py (32 errors fixed) - [x] Fix type annotations in deepmd/pd/model/task/ (18 errors fixed) - [x] Fix type annotations in deepmd/pd/model/model/ (23 errors fixed) - [x] Address all code review feedback (Any instead of object, proper type hints, correct return types) - [x] Remove accidentally added .dummy_file_for_commit - [x] Remove strict parameter from zip() calls Progress: 365/529 errors fixed (69%) + all applicable code review improvements Note: The strict parameter for zip() calls has been completely removed as it is not compatible with Paddle's JIT compilation. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ) to start the survey. --------- Signed-off-by: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn> Signed-off-by: Jinzhe Zeng <njzjz@qq.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com> Co-authored-by: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 73b70b2 commit e8afb6e

53 files changed

Lines changed: 642 additions & 431 deletions

Some content is hidden

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

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:

deepmd/pd/model/atomic_model/dp_atomic_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def compute_or_load_stat(
385385
stat_file_path /= " ".join(self.type_map)
386386

387387
@functools.lru_cache
388-
def wrapped_sampler():
388+
def wrapped_sampler() -> list[dict]:
389389
sampled = sampled_func()
390390
if self.pair_excl is not None:
391391
pair_exclude_types = self.pair_excl.get_exclude_types()

deepmd/pd/model/atomic_model/energy_atomic_model.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
from typing import (
3+
Any,
4+
)
5+
6+
from deepmd.pd.model.descriptor.base_descriptor import (
7+
BaseDescriptor,
8+
)
9+
from deepmd.pd.model.task.base_fitting import (
10+
BaseFitting,
11+
)
212
from deepmd.pd.model.task.ener import (
313
EnergyFittingNet,
414
InvarFitting,
@@ -10,7 +20,13 @@
1020

1121

1222
class DPEnergyAtomicModel(DPAtomicModel):
13-
def __init__(self, descriptor, fitting, type_map, **kwargs):
23+
def __init__(
24+
self,
25+
descriptor: BaseDescriptor,
26+
fitting: BaseFitting,
27+
type_map: list[str],
28+
**kwargs: Any,
29+
) -> None:
1430
assert isinstance(fitting, EnergyFittingNet) or isinstance(
1531
fitting, InvarFitting
1632
)

deepmd/pd/model/descriptor/descriptor.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Callable,
99
)
1010
from typing import (
11+
Any,
1112
NoReturn,
1213
)
1314

@@ -43,7 +44,7 @@ class DescriptorBlock(paddle.nn.Layer, ABC, make_plugin_registry("DescriptorBloc
4344

4445
local_cluster = False
4546

46-
def __new__(cls, *args, **kwargs):
47+
def __new__(cls, *args: Any, **kwargs: Any) -> "DescriptorBlock":
4748
if cls is DescriptorBlock:
4849
try:
4950
descrpt_type = kwargs["type"]
@@ -126,7 +127,9 @@ def get_stats(self) -> dict[str, StatItem]:
126127
"""Get the statistics of the descriptor."""
127128
raise NotImplementedError
128129

129-
def share_params(self, base_class, shared_level, resume=False) -> None:
130+
def share_params(
131+
self, base_class: Any, shared_level: int, resume: bool = False
132+
) -> None:
130133
"""
131134
Share the parameters of self to the base_class with shared_level during multitask training.
132135
If not start from checkpoint (resume is False),
@@ -180,7 +183,7 @@ def forward(
180183
extended_atype_embd: paddle.Tensor | None = None,
181184
mapping: paddle.Tensor | None = None,
182185
type_embedding: paddle.Tensor | None = None,
183-
):
186+
) -> paddle.Tensor:
184187
"""Calculate DescriptorBlock."""
185188
pass
186189

@@ -194,14 +197,16 @@ def need_sorted_nlist_for_lower(self) -> bool:
194197

195198

196199
def make_default_type_embedding(
197-
ntypes,
198-
):
200+
ntypes: int,
201+
) -> tuple[TypeEmbedNet, dict]:
199202
aux = {}
200203
aux["tebd_dim"] = 8
201204
return TypeEmbedNet(ntypes, aux["tebd_dim"]), aux
202205

203206

204-
def extend_descrpt_stat(des, type_map, des_with_stat=None) -> None:
207+
def extend_descrpt_stat(
208+
des: Any, type_map: list[str], des_with_stat: Any = None
209+
) -> None:
205210
r"""
206211
Extend the statistics of a descriptor block with types from newly provided `type_map`.
207212

deepmd/pd/model/descriptor/dpa1.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from collections.abc import (
33
Callable,
44
)
5+
from typing import (
6+
Any,
7+
)
58

69
import paddle
710

@@ -228,8 +231,8 @@ def __init__(
228231
exclude_types: list[tuple[int, int]] = [],
229232
env_protection: float = 0.0,
230233
scaling_factor: int = 1.0,
231-
normalize=True,
232-
temperature=None,
234+
normalize: bool = True,
235+
temperature: float | None = None,
233236
concat_output_tebd: bool = True,
234237
trainable: bool = True,
235238
trainable_ln: bool = True,
@@ -242,7 +245,7 @@ def __init__(
242245
use_tebd_bias: bool = False,
243246
type_map: list[str] | None = None,
244247
# not implemented
245-
spin=None,
248+
spin: Any = None,
246249
type: str | None = None,
247250
) -> None:
248251
super().__init__()
@@ -397,7 +400,9 @@ def get_env_protection(self) -> float:
397400
"""Returns the protection of building environment matrix."""
398401
return self.se_atten.get_env_protection()
399402

400-
def share_params(self, base_class, shared_level, resume=False) -> None:
403+
def share_params(
404+
self, base_class: Any, shared_level: int, resume: bool = False
405+
) -> None:
401406
"""
402407
Share the parameters of self to the base_class with shared_level during multitask training.
403408
If not start from checkpoint (resume is False),
@@ -425,18 +430,18 @@ def share_params(self, base_class, shared_level, resume=False) -> None:
425430
raise NotImplementedError
426431

427432
@property
428-
def dim_out(self):
433+
def dim_out(self) -> int:
429434
return self.get_dim_out()
430435

431436
@property
432-
def dim_emb(self):
437+
def dim_emb(self) -> int:
433438
return self.get_dim_emb()
434439

435440
def compute_input_stats(
436441
self,
437442
merged: Callable[[], list[dict]] | list[dict],
438443
path: DPPath | None = None,
439-
):
444+
) -> None:
440445
"""
441446
Compute the input statistics (e.g. mean and stddev) for the descriptors from packed data.
442447
@@ -469,7 +474,7 @@ def get_stat_mean_and_stddev(self) -> tuple[paddle.Tensor, paddle.Tensor]:
469474
return self.se_atten.mean, self.se_atten.stddev
470475

471476
def change_type_map(
472-
self, type_map: list[str], model_with_new_type_stat=None
477+
self, type_map: list[str], model_with_new_type_stat: Any = None
473478
) -> None:
474479
"""Change the type related params to new ones, according to `type_map` and the original one in the model.
475480
If there are new types in `type_map`, statistics will be updated accordingly to `model_with_new_type_stat` for these new types.
@@ -569,7 +574,7 @@ def deserialize(cls, data: dict) -> "DescrptDPA1":
569574
data["use_tebd_bias"] = True
570575
obj = cls(**data)
571576

572-
def t_cvt(xx):
577+
def t_cvt(xx: Any) -> paddle.Tensor:
573578
return paddle.to_tensor(xx, dtype=obj.se_atten.prec).to(device=env.DEVICE)
574579

575580
obj.type_embedding.embedding = TypeEmbedNetConsistent.deserialize(
@@ -620,7 +625,7 @@ def forward(
620625
nlist: paddle.Tensor,
621626
mapping: paddle.Tensor | None = None,
622627
comm_dict: list[paddle.Tensor] | None = None,
623-
):
628+
) -> paddle.Tensor:
624629
"""Compute the descriptor.
625630
626631
Parameters

0 commit comments

Comments
 (0)