Skip to content

Commit

Permalink
remove paddle.jit.export
Browse files Browse the repository at this point in the history
  • Loading branch information
HydrogenSulfate committed Oct 17, 2024
1 parent 13c7f55 commit e013860
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 157 deletions.
48 changes: 22 additions & 26 deletions deepmd/pd/model/atomic_model/base_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import logging
from typing import (

Check warning on line 5 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L3-L5

Added lines #L3 - L5 were not covered by tests
Callable,
Dict,
List,
Optional,
Tuple,
Union,
)

Expand Down Expand Up @@ -76,11 +73,11 @@ class BaseAtomicModel(paddle.nn.Layer, BaseAtomicModel_):

def __init__(

Check warning on line 74 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L74

Added line #L74 was not covered by tests
self,
type_map: List[str],
atom_exclude_types: List[int] = [],
pair_exclude_types: List[Tuple[int, int]] = [],
type_map: list[str],
atom_exclude_types: list[int] = [],
pair_exclude_types: list[tuple[int, int]] = [],
rcond: Optional[float] = None,
preset_out_bias: Optional[Dict[str, paddle.Tensor]] = None,
preset_out_bias: Optional[dict[str, paddle.Tensor]] = None,
):
paddle.nn.Layer.__init__(self)
BaseAtomicModel_.__init__(self)
Expand All @@ -93,7 +90,7 @@ def __init__(
def init_out_stat(self):

Check warning on line 90 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L90

Added line #L90 was not covered by tests
"""Initialize the output bias."""
ntypes = self.get_ntypes()

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable ntypes is not used.
self.bias_keys: List[str] = list(self.fitting_output_def().keys())
self.bias_keys: list[str] = list(self.fitting_output_def().keys())
self.max_out_size = max(

Check warning on line 94 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L92-L94

Added lines #L92 - L94 were not covered by tests
[self.atomic_output_def()[kk].size for kk in self.bias_keys]
)
Expand Down Expand Up @@ -122,14 +119,13 @@ def __getitem__(self, key):
else:
raise KeyError(key)

Check warning on line 120 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L120

Added line #L120 was not covered by tests

# @paddle.jit.export
def get_type_map(self) -> List[str]:
def get_type_map(self) -> list[str]:

Check warning on line 122 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L122

Added line #L122 was not covered by tests
"""Get the type map."""
return self.type_map

Check warning on line 124 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L124

Added line #L124 was not covered by tests

def reinit_atom_exclude(

Check warning on line 126 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L126

Added line #L126 was not covered by tests
self,
exclude_types: List[int] = [],
exclude_types: list[int] = [],
):
self.atom_exclude_types = exclude_types
if exclude_types == []:
Expand All @@ -139,7 +135,7 @@ def reinit_atom_exclude(

def reinit_pair_exclude(

Check warning on line 136 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L136

Added line #L136 was not covered by tests
self,
exclude_types: List[Tuple[int, int]] = [],
exclude_types: list[tuple[int, int]] = [],
):
self.pair_exclude_types = exclude_types
if exclude_types == []:
Expand Down Expand Up @@ -194,8 +190,8 @@ def forward_common_atomic(
mapping: Optional[paddle.Tensor] = None,
fparam: Optional[paddle.Tensor] = None,
aparam: Optional[paddle.Tensor] = None,
comm_dict: Optional[Dict[str, paddle.Tensor]] = None,
) -> Dict[str, paddle.Tensor]:
comm_dict: Optional[dict[str, paddle.Tensor]] = None,
) -> dict[str, paddle.Tensor]:
"""Common interface for atomic inference.
This method accept extended coordinates, extended atom typs, neighbor list,
Expand Down Expand Up @@ -277,8 +273,8 @@ def forward(
mapping: Optional[paddle.Tensor] = None,
fparam: Optional[paddle.Tensor] = None,
aparam: Optional[paddle.Tensor] = None,
comm_dict: Optional[Dict[str, paddle.Tensor]] = None,
) -> Dict[str, paddle.Tensor]:
comm_dict: Optional[dict[str, paddle.Tensor]] = None,
) -> dict[str, paddle.Tensor]:
return self.forward_common_atomic(

Check warning on line 278 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L278

Added line #L278 was not covered by tests
extended_coord,
extended_atype,
Expand All @@ -290,7 +286,7 @@ def forward(
)

def change_type_map(

Check warning on line 288 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L288

Added line #L288 was not covered by tests
self, type_map: List[str], model_with_new_type_stat=None
self, type_map: list[str], model_with_new_type_stat=None
) -> None:
"""Change the type related params to new ones, according to `type_map` and the original one in the model.
If there are new types in `type_map`, statistics will be updated accordingly to `model_with_new_type_stat` for these new types.
Expand Down Expand Up @@ -355,7 +351,7 @@ def deserialize(cls, data: dict) -> "BaseAtomicModel":

def compute_or_load_stat(

Check warning on line 352 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L352

Added line #L352 was not covered by tests
self,
merged: Union[Callable[[], List[dict]], List[dict]],
merged: Union[Callable[[], list[dict]], list[dict]],
stat_file_path: Optional[DPPath] = None,
):
"""
Expand All @@ -378,7 +374,7 @@ def compute_or_load_stat(

def compute_or_load_out_stat(

Check warning on line 375 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L375

Added line #L375 was not covered by tests
self,
merged: Union[Callable[[], List[dict]], List[dict]],
merged: Union[Callable[[], list[dict]], list[dict]],
stat_file_path: Optional[DPPath] = None,
):
"""
Expand All @@ -405,7 +401,7 @@ def compute_or_load_out_stat(

def apply_out_stat(

Check warning on line 402 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L402

Added line #L402 was not covered by tests
self,
ret: Dict[str, paddle.Tensor],
ret: dict[str, paddle.Tensor],
atype: paddle.Tensor,
):
"""Apply the stat to each atomic output.
Expand Down Expand Up @@ -521,7 +517,7 @@ def _default_std(self):

def _varsize(

Check warning on line 518 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L518

Added line #L518 was not covered by tests
self,
shape: List[int],
shape: list[int],
) -> int:
output_size = 1
len_shape = len(shape)
Expand All @@ -533,7 +529,7 @@ def _get_bias_index(
self,
kk: str,
) -> int:
res: List[int] = []
res: list[int] = []
for i, e in enumerate(self.bias_keys):
if e == kk:
res.append(i)
Expand All @@ -542,8 +538,8 @@ def _get_bias_index(

def _store_out_stat(

Check warning on line 539 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L539

Added line #L539 was not covered by tests
self,
out_bias: Dict[str, paddle.Tensor],
out_std: Dict[str, paddle.Tensor],
out_bias: dict[str, paddle.Tensor],
out_std: dict[str, paddle.Tensor],
add: bool = False,
):
ntypes = self.get_ntypes()
Expand All @@ -563,8 +559,8 @@ def _store_out_stat(

def _fetch_out_stat(

Check warning on line 560 in deepmd/pd/model/atomic_model/base_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/base_atomic_model.py#L560

Added line #L560 was not covered by tests
self,
keys: List[str],
) -> Tuple[Dict[str, paddle.Tensor], Dict[str, paddle.Tensor]]:
keys: list[str],
) -> tuple[dict[str, paddle.Tensor], dict[str, paddle.Tensor]]:
ret_bias = {}
ret_std = {}
ntypes = self.get_ntypes()
Expand Down
18 changes: 7 additions & 11 deletions deepmd/pd/model/atomic_model/dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import functools
import logging
from typing import (

Check warning on line 5 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L2-L5

Added lines #L2 - L5 were not covered by tests
Dict,
List,
Optional,
)

Expand Down Expand Up @@ -52,7 +50,7 @@ def __init__(
self,
descriptor,
fitting,
type_map: List[str],
type_map: list[str],
**kwargs,
):
super().__init__(type_map, **kwargs)
Expand All @@ -66,7 +64,7 @@ def __init__(
super().init_out_stat()

Check warning on line 64 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L56-L64

Added lines #L56 - L64 were not covered by tests

# register 'type_map' as buffer
def _string_to_array(s: str) -> List[int]:
def _string_to_array(s: str) -> list[int]:
return [ord(c) for c in s]

Check warning on line 68 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L67-L68

Added lines #L67 - L68 were not covered by tests

self.register_buffer(

Check warning on line 70 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L70

Added line #L70 was not covered by tests
Expand Down Expand Up @@ -107,7 +105,6 @@ def _string_to_array(s: str) -> List[int]:
)
self.buffer_aparam_nall.name = "buffer_aparam_nall"

Check warning on line 106 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L106

Added line #L106 was not covered by tests

# @paddle.jit.export
def fitting_output_def(self) -> FittingOutputDef:

Check warning on line 108 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L108

Added line #L108 was not covered by tests
"""Get the output def of the fitting net."""
return (

Check warning on line 110 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L110

Added line #L110 was not covered by tests
Expand All @@ -116,12 +113,11 @@ def fitting_output_def(self) -> FittingOutputDef:
else self.coord_denoise_net.output_def()
)

# @paddle.jit.export
def get_rcut(self) -> float:

Check warning on line 116 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L116

Added line #L116 was not covered by tests
"""Get the cut-off radius."""
return self.rcut

Check warning on line 118 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L118

Added line #L118 was not covered by tests

def get_sel(self) -> List[int]:
def get_sel(self) -> list[int]:

Check warning on line 120 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L120

Added line #L120 was not covered by tests
"""Get the neighbor selection."""
return self.sel

Check warning on line 122 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L122

Added line #L122 was not covered by tests

Expand All @@ -138,7 +134,7 @@ def mixed_types(self) -> bool:
return self.descriptor.mixed_types()

Check warning on line 134 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L134

Added line #L134 was not covered by tests

def change_type_map(

Check warning on line 136 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L136

Added line #L136 was not covered by tests
self, type_map: List[str], model_with_new_type_stat=None
self, type_map: list[str], model_with_new_type_stat=None
) -> None:
"""Change the type related params to new ones, according to `type_map` and the original one in the model.
If there are new types in `type_map`, statistics will be updated accordingly to `model_with_new_type_stat` for these new types.
Expand Down Expand Up @@ -199,8 +195,8 @@ def forward_atomic(
mapping: Optional[paddle.Tensor] = None,
fparam: Optional[paddle.Tensor] = None,
aparam: Optional[paddle.Tensor] = None,
comm_dict: Optional[Dict[str, paddle.Tensor]] = None,
) -> Dict[str, paddle.Tensor]:
comm_dict: Optional[dict[str, paddle.Tensor]] = None,
) -> dict[str, paddle.Tensor]:
"""Return atomic prediction.
Parameters
Expand Down Expand Up @@ -300,7 +296,7 @@ def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return self.fitting_net.get_dim_aparam()

Check warning on line 297 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L297

Added line #L297 was not covered by tests

def get_sel_type(self) -> List[int]:
def get_sel_type(self) -> list[int]:

Check warning on line 299 in deepmd/pd/model/atomic_model/dp_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/atomic_model/dp_atomic_model.py#L299

Added line #L299 was not covered by tests
"""Get the selected atom types of this model.
Only atoms with selected atom types have atomic contribution
Expand Down
33 changes: 14 additions & 19 deletions deepmd/pd/model/descriptor/repformer_layer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from typing import (
List,
Optional,
Union,
)
Expand Down Expand Up @@ -45,7 +44,7 @@ def get_residual(
_mode: str = "norm",
trainable: bool = True,
precision: str = "float64",
seed: Optional[Union[int, List[int]]] = None,
seed: Optional[Union[int, list[int]]] = None,
) -> paddle.Tensor:
r"""
Get residual tensor for one update vector.
Expand Down Expand Up @@ -163,7 +162,7 @@ def __init__(
smooth: bool = True,
attnw_shift: float = 20.0,
precision: str = "float64",
seed: Optional[Union[int, List[int]]] = None,
seed: Optional[Union[int, list[int]]] = None,
):
"""Return neighbor-wise multi-head self-attention maps, with gate mechanism."""
super().__init__()
Expand Down Expand Up @@ -288,7 +287,7 @@ def __init__(
input_dim: int,
head_num: int,
precision: str = "float64",
seed: Optional[Union[int, List[int]]] = None,
seed: Optional[Union[int, list[int]]] = None,
):
super().__init__()
self.input_dim = input_dim
Expand Down Expand Up @@ -375,7 +374,7 @@ def __init__(
input_dim: int,
head_num: int,
precision: str = "float64",
seed: Optional[Union[int, List[int]]] = None,
seed: Optional[Union[int, list[int]]] = None,
):
super().__init__()
self.input_dim = input_dim
Expand Down Expand Up @@ -448,7 +447,7 @@ def __init__(
smooth: bool = True,
attnw_shift: float = 20.0,
precision: str = "float64",
seed: Optional[Union[int, List[int]]] = None,
seed: Optional[Union[int, list[int]]] = None,
):
super().__init__()
self.input_dim = input_dim
Expand Down Expand Up @@ -609,7 +608,7 @@ def __init__(
precision: str = "float64",
trainable_ln: bool = True,
ln_eps: Optional[float] = 1e-5,
seed: Optional[Union[int, List[int]]] = None,
seed: Optional[Union[int, list[int]]] = None,
):
super().__init__()
self.epsilon = 1e-4 # protection of 1./nnei
Expand Down Expand Up @@ -1070,10 +1069,10 @@ def forward(
assert [nb, nloc] == g1.shape[:2]
assert [nb, nloc, nnei] == h2.shape[:3]

g2_update: List[paddle.Tensor] = [g2]
h2_update: List[paddle.Tensor] = [h2]
g1_update: List[paddle.Tensor] = [g1]
g1_mlp: List[paddle.Tensor] = [g1]
g2_update: list[paddle.Tensor] = [g2]
h2_update: list[paddle.Tensor] = [h2]
g1_update: list[paddle.Tensor] = [g1]
g1_mlp: list[paddle.Tensor] = [g1]

if cal_gg1:
gg1 = _make_nei_g1(g1_ext, nlist)
Expand Down Expand Up @@ -1163,29 +1162,26 @@ def forward(
g1_new = self.list_update(g1_update, "g1")
return g1_new, g2_new, h2_new

# @paddle.jit.export
def list_update_res_avg(
self,
update_list: List[paddle.Tensor],
update_list: list[paddle.Tensor],
) -> paddle.Tensor:
nitem = len(update_list)
uu = update_list[0]
for ii in range(1, nitem):
uu = uu + update_list[ii]
return uu / (float(nitem) ** 0.5)

# @paddle.jit.export
def list_update_res_incr(self, update_list: List[paddle.Tensor]) -> paddle.Tensor:
def list_update_res_incr(self, update_list: list[paddle.Tensor]) -> paddle.Tensor:
nitem = len(update_list)
uu = update_list[0]
scale = 1.0 / (float(nitem - 1) ** 0.5) if nitem > 1 else 0.0
for ii in range(1, nitem):
uu = uu + scale * update_list[ii]
return uu

# @paddle.jit.export
def list_update_res_residual(
self, update_list: List[paddle.Tensor], update_name: str = "g1"
self, update_list: list[paddle.Tensor], update_name: str = "g1"
) -> paddle.Tensor:
nitem = len(update_list)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable nitem is not used.
uu = update_list[0]
Expand All @@ -1203,9 +1199,8 @@ def list_update_res_residual(
raise NotImplementedError
return uu

# @paddle.jit.export
def list_update(
self, update_list: List[paddle.Tensor], update_name: str = "g1"
self, update_list: list[paddle.Tensor], update_name: str = "g1"
) -> paddle.Tensor:
if self.update_style == "res_avg":
return self.list_update_res_avg(update_list)
Expand Down
Loading

0 comments on commit e013860

Please sign in to comment.