Skip to content

Commit 8c29799

Browse files
add deep_eval_test
1 parent b370799 commit 8c29799

File tree

8 files changed

+148
-76
lines changed

8 files changed

+148
-76
lines changed

deepmd/pd/entrypoints/main.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Path,
88
)
99
from typing import (
10+
Any,
1011
Optional,
1112
Union,
1213
)
@@ -80,15 +81,15 @@
8081

8182

8283
def get_trainer(
83-
config,
84-
init_model=None,
85-
restart_model=None,
86-
finetune_model=None,
87-
force_load=False,
88-
init_frz_model=None,
89-
shared_links=None,
90-
finetune_links=None,
91-
):
84+
config: dict[str, Any],
85+
init_model: Optional[str] = None,
86+
restart_model: Optional[str] = None,
87+
finetune_model: Optional[str] = None,
88+
force_load: bool = False,
89+
init_frz_model: Optional[str] = None,
90+
shared_links: Optional[dict[str, Any]] = None,
91+
finetune_links: Optional[dict[str, Any]] = None,
92+
) -> training.Trainer:
9293
multi_task = "model_dict" in config.get("model", {})
9394

9495
# Initialize DDP
@@ -98,17 +99,22 @@ def get_trainer(
9899
fleet.init(is_collective=True)
99100

100101
def prepare_trainer_input_single(
101-
model_params_single, data_dict_single, rank=0, seed=None
102-
):
102+
model_params_single: dict[str, Any],
103+
data_dict_single: dict[str, Any],
104+
rank: int = 0,
105+
seed: Optional[int] = None,
106+
) -> tuple[DpLoaderSet, Optional[DpLoaderSet], Optional[DPPath]]:
103107
training_dataset_params = data_dict_single["training_data"]
104108
validation_dataset_params = data_dict_single.get("validation_data", None)
105109
validation_systems = (
106110
validation_dataset_params["systems"] if validation_dataset_params else None
107111
)
108112
training_systems = training_dataset_params["systems"]
109-
training_systems = process_systems(training_systems)
113+
trn_patterns = training_dataset_params.get("rglob_patterns", None)
114+
training_systems = process_systems(training_systems, patterns=trn_patterns)
110115
if validation_systems is not None:
111-
validation_systems = process_systems(validation_systems)
116+
val_patterns = validation_dataset_params.get("rglob_patterns", None)
117+
validation_systems = process_systems(validation_systems, val_patterns)
112118

113119
# stat files
114120
stat_file_path_single = data_dict_single.get("stat_file", None)
@@ -353,12 +359,6 @@ def freeze(
353359
)
354360
model = inference.Tester(model, head=head).model
355361
model.eval()
356-
# print(model.get_buffer_rcut.__func__.__qualname__)
357-
# print(model.get_buffer_rcut.__func__.__module__)
358-
# print(model.get_buffer_rcut.__func__.__code__.co_filename)
359-
# print(model.get_buffer_rcut.__func__.__code__.co_firstlineno)
360-
# print(model.get_buffer_type_map())
361-
# exit()
362362
from paddle.static import (
363363
InputSpec,
364364
)

deepmd/pd/infer/deep_eval.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def __init__(
161161
["add_shadow_output_after_dead_parameter_pass"], True
162162
)
163163
config.enable_use_gpu(4096, 0)
164+
config.disable_glog_info()
164165

165166
self.predictor = paddle_inference.create_predictor(config)
166167
self.coord_handle = self.predictor.get_input_handle("coord")
@@ -195,8 +196,8 @@ def __init__(
195196
if isinstance(self.dp, ModelWrapper)
196197
else False
197198
)
198-
if callable(self._has_spin) and not isinstance(self._has_spin, bool):
199-
setattr(self, "_has_spin", self._has_spin())
199+
if callable(self._has_spin):
200+
self._has_spin = False
200201
self._has_hessian = False
201202

202203
def get_rcut(self) -> float:
@@ -246,6 +247,8 @@ def get_var_name(self) -> str:
246247
@property
247248
def model_type(self) -> type["DeepEvalWrapper"]:
248249
"""The the evaluator of the model type."""
250+
if self.static_model:
251+
return DeepPot
249252
model_output_type = self.dp.model["Default"].model_output_type()
250253
if "energy" in model_output_type:
251254
return DeepPot

deepmd/pd/model/atomic_model/base_atomic_model.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class BaseAtomicModel(paddle.nn.Layer, BaseAtomicModel_):
6464
of the atomic model. Implemented by removing the pairs from the nlist.
6565
rcond : float, optional
6666
The condition number for the regression of atomic energy.
67-
preset_out_bias : Dict[str, list[Optional[paddle.Tensor]]], optional
67+
preset_out_bias : dict[str, list[Optional[np.ndarray]]], optional
6868
Specifying atomic energy contribution in vacuum. Given by key:value pairs.
69-
The value is a list specifying the bias. the elements can be None or np.array of output shape.
69+
The value is a list specifying the bias. the elements can be None or np.ndarray of output shape.
7070
For example: [None, [2.]] means type 0 is not set, type 1 is set to [2.]
7171
The `set_davg_zero` key in the descriptor should be set.
7272
@@ -114,15 +114,15 @@ def init_out_stat(self) -> None:
114114
def set_out_bias(self, out_bias: paddle.Tensor) -> None:
115115
self.out_bias = out_bias
116116

117-
def __setitem__(self, key, value) -> None:
117+
def __setitem__(self, key: str, value: paddle.Tensor) -> None:
118118
if key in ["out_bias"]:
119119
self.out_bias = value
120120
elif key in ["out_std"]:
121121
self.out_std = value
122122
else:
123123
raise KeyError(key)
124124

125-
def __getitem__(self, key):
125+
def __getitem__(self, key: str) -> paddle.Tensor:
126126
if key in ["out_bias"]:
127127
return self.out_bias
128128
elif key in ["out_std"]:
@@ -146,6 +146,10 @@ def get_intensive(self) -> bool:
146146
"""Whether the fitting property is intensive."""
147147
return False
148148

149+
def has_default_fparam(self) -> bool:
150+
"""Check if the model has default frame parameters."""
151+
return False
152+
149153
def reinit_atom_exclude(
150154
self,
151155
exclude_types: Optional[list[int]] = None,
@@ -271,7 +275,6 @@ def forward_common_atomic(
271275
comm_dict=comm_dict,
272276
)
273277
ret_dict = self.apply_out_stat(ret_dict, atype)
274-
275278
# nf x nloc
276279
atom_mask = ext_atom_mask[:, :nloc].astype(paddle.int32)
277280
if self.atom_excl is not None:
@@ -284,10 +287,10 @@ def forward_common_atomic(
284287
out_shape2 *= ss
285288
ret_dict[kk] = (
286289
ret_dict[kk].reshape([out_shape[0], out_shape[1], out_shape2])
287-
* atom_mask.unsqueeze(2).astype(ret_dict[kk].dtype)
290+
* atom_mask[:, :, None].astype(ret_dict[kk].dtype)
288291
).reshape(out_shape)
289292
ret_dict["mask"] = atom_mask
290-
293+
# raise
291294
return ret_dict
292295

293296
def forward(
@@ -311,7 +314,9 @@ def forward(
311314
)
312315

313316
def change_type_map(
314-
self, type_map: list[str], model_with_new_type_stat=None
317+
self,
318+
type_map: list[str],
319+
model_with_new_type_stat: Optional["BaseAtomicModel"] = None,
315320
) -> None:
316321
"""Change the type related params to new ones, according to `type_map` and the original one in the model.
317322
If there are new types in `type_map`, statistics will be updated accordingly to `model_with_new_type_stat` for these new types.
@@ -378,21 +383,25 @@ def compute_or_load_stat(
378383
self,
379384
merged: Union[Callable[[], list[dict]], list[dict]],
380385
stat_file_path: Optional[DPPath] = None,
386+
compute_or_load_out_stat: bool = True,
381387
) -> NoReturn:
382388
"""
383-
Compute the output statistics (e.g. energy bias) for the fitting net from packed data.
389+
Compute or load the statistics parameters of the model,
390+
such as mean and standard deviation of descriptors or the energy bias of the fitting net.
391+
When `sampled` is provided, all the statistics parameters will be calculated (or re-calculated for update),
392+
and saved in the `stat_file_path`(s).
393+
When `sampled` is not provided, it will check the existence of `stat_file_path`(s)
394+
and load the calculated statistics parameters.
384395
385396
Parameters
386397
----------
387-
merged : Union[Callable[[], list[dict]], list[dict]]
388-
- list[dict]: A list of data samples from various data systems.
389-
Each element, `merged[i]`, is a data dictionary containing `keys`: `paddle.Tensor`
390-
originating from the `i`-th data system.
391-
- Callable[[], list[dict]]: A lazy function that returns data samples in the above format
392-
only when needed. Since the sampling process can be slow and memory-intensive,
393-
the lazy function helps by only sampling once.
394-
stat_file_path : Optional[DPPath]
395-
The path to the stat file.
398+
merged
399+
The lazy sampled function to get data frames from different data systems.
400+
stat_file_path
401+
The dictionary of paths to the statistics files.
402+
compute_or_load_out_stat : bool
403+
Whether to compute the output statistics.
404+
If False, it will only compute the input statistics (e.g. mean and standard deviation of descriptors).
396405
397406
"""
398407
raise NotImplementedError
@@ -428,7 +437,7 @@ def apply_out_stat(
428437
self,
429438
ret: dict[str, paddle.Tensor],
430439
atype: paddle.Tensor,
431-
):
440+
) -> dict[str, paddle.Tensor]:
432441
"""Apply the stat to each atomic output.
433442
The developer may override the method to define how the bias is applied
434443
to the atomic output of the model.
@@ -449,9 +458,9 @@ def apply_out_stat(
449458

450459
def change_out_bias(
451460
self,
452-
sample_merged,
461+
sample_merged: Union[Callable[[], list[dict]], list[dict]],
453462
stat_file_path: Optional[DPPath] = None,
454-
bias_adjust_mode="change-by-statistic",
463+
bias_adjust_mode: str = "change-by-statistic",
455464
) -> None:
456465
"""Change the output bias according to the input data and the pretrained model.
457466
@@ -501,7 +510,13 @@ def change_out_bias(
501510
def _get_forward_wrapper_func(self) -> Callable[..., paddle.Tensor]:
502511
"""Get a forward wrapper of the atomic model for output bias calculation."""
503512

504-
def model_forward(coord, atype, box, fparam=None, aparam=None):
513+
def model_forward(
514+
coord: paddle.Tensor,
515+
atype: paddle.Tensor,
516+
box: Optional[paddle.Tensor],
517+
fparam: Optional[paddle.Tensor] = None,
518+
aparam: Optional[paddle.Tensor] = None,
519+
) -> dict[str, paddle.Tensor]:
505520
with (
506521
paddle.no_grad()
507522
): # it's essential for pure paddle forward function to use auto_batchsize
@@ -530,7 +545,7 @@ def model_forward(coord, atype, box, fparam=None, aparam=None):
530545

531546
return model_forward
532547

533-
def _default_bias(self):
548+
def _default_bias(self) -> paddle.Tensor:
534549
ntypes = self.get_ntypes()
535550
return paddle.zeros([self.n_out, ntypes, self.max_out_size], dtype=dtype).to(
536551
device=device

deepmd/pd/model/atomic_model/dp_atomic_model.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import functools
33
import logging
44
from typing import (
5+
Any,
6+
Callable,
57
Optional,
68
Union,
79
)
@@ -48,10 +50,10 @@ class DPAtomicModel(BaseAtomicModel):
4850

4951
def __init__(
5052
self,
51-
descriptor,
52-
fitting,
53+
descriptor: BaseDescriptor,
54+
fitting: BaseFitting,
5355
type_map: list[str],
54-
**kwargs,
56+
**kwargs: Any,
5557
) -> None:
5658
super().__init__(type_map, **kwargs)
5759
ntypes = len(type_map)
@@ -169,7 +171,7 @@ def get_buffer_sel(self) -> paddle.Tensor:
169171
"""Get the neighbor selection."""
170172
return self.descriptor.get_buffer_sel()
171173

172-
def set_case_embd(self, case_idx: int):
174+
def set_case_embd(self, case_idx: int) -> None:
173175
"""
174176
Set the case embedding of this atomic model by the given case_idx,
175177
typically concatenated with the output of the descriptor and fed into the fitting net.
@@ -189,7 +191,9 @@ def mixed_types(self) -> bool:
189191
return self.descriptor.mixed_types()
190192

191193
def change_type_map(
192-
self, type_map: list[str], model_with_new_type_stat=None
194+
self,
195+
type_map: list[str],
196+
model_with_new_type_stat: Optional["DPAtomicModel"] = None,
193197
) -> None:
194198
"""Change the type related params to new ones, according to `type_map` and the original one in the model.
195199
If there are new types in `type_map`, statistics will be updated accordingly to `model_with_new_type_stat` for these new types.
@@ -230,7 +234,7 @@ def serialize(self) -> dict:
230234
return dd
231235

232236
@classmethod
233-
def deserialize(cls, data) -> "DPAtomicModel":
237+
def deserialize(cls, data: dict) -> "DPAtomicModel":
234238
data = data.copy()
235239
check_version_compatibility(data.pop("@version", 1), 2, 1)
236240
data.pop("@class", None)
@@ -275,13 +279,13 @@ def enable_compression(
275279

276280
def forward_atomic(
277281
self,
278-
extended_coord,
279-
extended_atype,
280-
nlist,
282+
extended_coord: paddle.Tensor,
283+
extended_atype: paddle.Tensor,
284+
nlist: paddle.Tensor,
281285
mapping: Optional[paddle.Tensor] = None,
282286
fparam: Optional[paddle.Tensor] = None,
283287
aparam: Optional[paddle.Tensor] = None,
284-
comm_dict: Optional[list[paddle.Tensor]] = None,
288+
comm_dict: Optional[dict[str, paddle.Tensor]] = None,
285289
) -> dict[str, paddle.Tensor]:
286290
"""Return atomic prediction.
287291
@@ -344,8 +348,9 @@ def get_out_bias(self) -> paddle.Tensor:
344348

345349
def compute_or_load_stat(
346350
self,
347-
sampled_func,
351+
sampled_func: Callable[[], list[dict]],
348352
stat_file_path: Optional[DPPath] = None,
353+
compute_or_load_out_stat: bool = True,
349354
) -> None:
350355
"""
351356
Compute or load the statistics parameters of the model,
@@ -361,6 +366,9 @@ def compute_or_load_stat(
361366
The lazy sampled function to get data frames from different data systems.
362367
stat_file_path
363368
The dictionary of paths to the statistics files.
369+
compute_or_load_out_stat : bool
370+
Whether to compute the output statistics.
371+
If False, it will only compute the input statistics (e.g. mean and standard deviation of descriptors).
364372
"""
365373
if stat_file_path is not None and self.type_map is not None:
366374
# descriptors and fitting net with different type_map
@@ -384,16 +392,29 @@ def wrapped_sampler():
384392
self.fitting_net.compute_input_stats(
385393
wrapped_sampler, protection=self.data_stat_protect
386394
)
387-
self.compute_or_load_out_stat(wrapped_sampler, stat_file_path)
395+
if compute_or_load_out_stat:
396+
self.compute_or_load_out_stat(wrapped_sampler, stat_file_path)
388397

389398
def get_dim_fparam(self) -> int:
390399
"""Get the number (dimension) of frame parameters of this atomic model."""
391400
return self.fitting_net.get_dim_fparam()
392401

402+
def get_buffer_dim_fparam(self) -> paddle.Tensor:
403+
"""Get the number (dimension) of frame parameters of this atomic model."""
404+
return self.fitting_net.get_buffer_dim_fparam()
405+
406+
def has_default_fparam(self) -> bool:
407+
"""Check if the model has default frame parameters."""
408+
return self.fitting_net.has_default_fparam()
409+
393410
def get_dim_aparam(self) -> int:
394411
"""Get the number (dimension) of atomic parameters of this atomic model."""
395412
return self.fitting_net.get_dim_aparam()
396413

414+
def get_buffer_dim_aparam(self) -> paddle.Tensor:
415+
"""Get the number (dimension) of atomic parameters of this atomic model."""
416+
return self.fitting_net.get_buffer_dim_aparam()
417+
397418
def get_sel_type(self) -> list[int]:
398419
"""Get the selected atom types of this model.
399420

deepmd/pd/model/descriptor/se_a.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ def get_rcut_smth(self) -> float:
127127
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
128128
return self.sea.get_rcut_smth()
129129

130+
def get_buffer_rcut(self) -> paddle.Tensor:
131+
"""Returns the cut-off radius."""
132+
return self.sea.get_buffer_rcut()
133+
134+
def get_buffer_rcut_smth(self) -> paddle.Tensor:
135+
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
136+
return self.sea.get_buffer_rcut_smth()
137+
130138
def get_nsel(self) -> int:
131139
"""Returns the number of selected atoms in the cut-off radius."""
132140
return self.sea.get_nsel()

0 commit comments

Comments
 (0)