Skip to content

Commit e80e7d9

Browse files
Copilotnjzjz
andcommitted
fix(paddle): Address additional code review feedback
- Changed zip strict=False to strict=True in nlist.py - Changed ctx type from object to paddle.autograd.PyLayerContext in utils.py backward methods - Changed TaskLoss.__init__ to accept **kwargs: Any and forward to return paddle.Tensor - Added mae: bool | None = None parameter to TaskLoss.forward signature - Changed all remaining object types to Any in frozen.py, model.py, ener.py, stat.py - Fixed _can_be_converted_to_float return type from bool | None to bool - Added Any imports where needed (ener.py, loss.py, frozen.py, model.py) All code review feedback has been addressed. Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com>
1 parent b9ab901 commit e80e7d9

8 files changed

Lines changed: 17 additions & 13 deletions

File tree

deepmd/pd/loss/ener.py

Lines changed: 5 additions & 2 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
@@ -54,7 +57,7 @@ def __init__(
5457
inference: bool = False,
5558
use_huber: bool = False,
5659
huber_delta: float = 0.01,
57-
**kwargs: object,
60+
**kwargs: Any,
5861
) -> None:
5962
r"""Construct a layer to compute loss on energy, force and virial.
6063
@@ -547,7 +550,7 @@ def __init__(
547550
self,
548551
start_pref_h: float = 0.0,
549552
limit_pref_h: float = 0.0,
550-
**kwargs: object,
553+
**kwargs: Any,
551554
) -> None:
552555
r"""Enable the layer to compute loss on hessian.
553556

deepmd/pd/loss/loss.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
abstractmethod,
55
)
66
from typing import (
7-
NoReturn,
7+
Any,
88
)
99

1010
import paddle
@@ -18,7 +18,7 @@
1818

1919

2020
class TaskLoss(paddle.nn.Layer, ABC, make_plugin_registry("loss")):
21-
def __init__(self, **kwargs) -> None:
21+
def __init__(self, **kwargs: Any) -> None:
2222
"""Construct loss."""
2323
super().__init__()
2424

@@ -29,7 +29,8 @@ def forward(
2929
label: dict[str, paddle.Tensor],
3030
natoms: int,
3131
learning_rate: float,
32-
) -> NoReturn:
32+
mae: bool | None = None,
33+
) -> paddle.Tensor:
3334
"""Return loss ."""
3435
raise NotImplementedError
3536

deepmd/pd/model/model/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _get_standard_model_components(
6565
return descriptor, fitting, fitting_net["type"]
6666

6767

68-
def _can_be_converted_to_float(value: object) -> bool | None:
68+
def _can_be_converted_to_float(value: object) -> bool:
6969
try:
7070
float(value)
7171
return True

deepmd/pd/model/model/frozen.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
22
import json
33
from typing import (
4+
Any,
45
NoReturn,
56
)
67

@@ -27,7 +28,7 @@ class FrozenModel(BaseModel):
2728
The path to the frozen model
2829
"""
2930

30-
def __init__(self, model_file: str, **kwargs: object) -> None:
31+
def __init__(self, model_file: str, **kwargs: Any) -> None:
3132
super().__init__(**kwargs)
3233
self.model_file = model_file
3334
if model_file.endswith(".json"):

deepmd/pd/model/model/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Callable,
55
)
66
from typing import (
7+
Any,
78
NoReturn,
89
)
910

@@ -18,7 +19,7 @@
1819

1920

2021
class BaseModel(paddle.nn.Layer, make_base_model()):
21-
def __init__(self, *args: object, **kwargs: object) -> None:
22+
def __init__(self, *args: Any, **kwargs: Any) -> None:
2223
"""Construct a basic model for different tasks."""
2324
paddle.nn.Layer.__init__(self)
2425
self.model_def_script = ""

deepmd/pd/utils/nlist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def build_multiple_neighbor_list(
404404
rr.masked_fill(nlist_mask, float("inf"))
405405
nlist0 = nlist
406406
ret = {}
407-
for rc, ns in zip(rcuts[::-1], nsels[::-1], strict=False):
407+
for rc, ns in zip(rcuts[::-1], nsels[::-1], strict=True):
408408
nlist0 = nlist0[:, :, :ns].masked_fill(rr[:, :, :ns] > rc, -1)
409409
ret[get_multiple_nlist_key(rc, ns)] = nlist0
410410
return ret

deepmd/pd/utils/stat.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ def _compute_model_predict(
176176
fparam = system.get("fparam", None)
177177
aparam = system.get("aparam", None)
178178

179-
def model_forward_auto_batch_size(
180-
*args: object, **kwargs: object
181-
) -> paddle.Tensor:
179+
def model_forward_auto_batch_size(*args: Any, **kwargs: Any) -> paddle.Tensor:
182180
return auto_batch_size.execute_all(
183181
model_forward,
184182
nframes,

deepmd/pd/utils/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def forward(
148148

149149
@staticmethod
150150
def backward(
151-
ctx: object, grad_grad_output: paddle.Tensor
151+
ctx: paddle.autograd.PyLayerContext, grad_grad_output: paddle.Tensor
152152
) -> tuple[paddle.Tensor, paddle.Tensor]:
153153
(x, grad_output) = ctx.saved_tensor()
154154
threshold = ctx.threshold

0 commit comments

Comments
 (0)