-
Notifications
You must be signed in to change notification settings - Fork 611
Expand file tree
/
Copy pathdeep_eval.py
More file actions
877 lines (809 loc) · 30.1 KB
/
deep_eval.py
File metadata and controls
877 lines (809 loc) · 30.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# SPDX-License-Identifier: LGPL-3.0-or-later
import io
import json
import logging
from collections.abc import (
Callable,
)
from typing import (
TYPE_CHECKING,
Any,
Optional,
)
import numpy as np
import torch
from deepmd.dpmodel.common import PRECISION_DICT as NP_PRECISION_DICT
from deepmd.dpmodel.output_def import (
ModelOutputDef,
OutputVariableCategory,
OutputVariableDef,
)
from deepmd.infer.deep_dipole import (
DeepDipole,
)
from deepmd.infer.deep_dos import (
DeepDOS,
)
from deepmd.infer.deep_eval import DeepEval as DeepEvalWrapper
from deepmd.infer.deep_eval import (
DeepEvalBackend,
)
from deepmd.infer.deep_polar import (
DeepGlobalPolar,
DeepPolar,
)
from deepmd.infer.deep_pot import (
DeepPot,
)
from deepmd.infer.deep_property import (
DeepProperty,
)
from deepmd.infer.deep_wfc import (
DeepWFC,
)
from deepmd.pt.model.model import (
get_model,
)
from deepmd.pt.model.network.network import (
TypeEmbedNetConsistent,
)
from deepmd.pt.train.wrapper import (
ModelWrapper,
)
from deepmd.pt.utils import (
env,
)
from deepmd.pt.utils.auto_batch_size import (
AutoBatchSize,
)
from deepmd.pt.utils.env import (
DEVICE,
GLOBAL_PT_FLOAT_PRECISION,
RESERVED_PRECISION_DICT,
)
from deepmd.pt.utils.utils import (
to_numpy_array,
to_torch_tensor,
)
from deepmd.utils.econf_embd import (
sort_element_type,
)
from deepmd.utils.model_branch_dict import (
get_model_dict,
)
if TYPE_CHECKING:
import ase.neighborlist
from deepmd.pt.model.model.model import (
BaseModel,
)
log = logging.getLogger(__name__)
class DeepEval(DeepEvalBackend):
"""PyTorch backend implementation of DeepEval.
Parameters
----------
model_file : Path
The name of the frozen model file.
output_def : ModelOutputDef
The output definition of the model.
*args : list
Positional arguments.
auto_batch_size : bool or int or AutomaticBatchSize, default: True
If True, automatic batch size will be used. If int, it will be used
as the initial batch size.
neighbor_list : ase.neighborlist.NewPrimitiveNeighborList, optional
The ASE neighbor list class to produce the neighbor list. If None, the
neighbor list will be built natively in the model.
**kwargs : dict
Keyword arguments.
"""
def __init__(
self,
model_file: str,
output_def: ModelOutputDef,
*args: Any,
auto_batch_size: bool | int | AutoBatchSize = True,
neighbor_list: Optional["ase.neighborlist.NewPrimitiveNeighborList"] = None,
head: str | int | None = None,
no_jit: bool = False,
**kwargs: Any,
) -> None:
self.output_def = output_def
self.model_path = model_file
if str(self.model_path).endswith(".pt"):
state_dict = torch.load(
model_file, map_location=env.DEVICE, weights_only=True
)
if "model" in state_dict:
state_dict = state_dict["model"]
self.input_param = state_dict["_extra_state"]["model_params"]
self.model_def_script = self.input_param
self.multi_task = "model_dict" in self.input_param
if self.multi_task:
model_alias_dict, model_branch_dict = get_model_dict(
self.input_param["model_dict"]
)
model_keys = list(self.input_param["model_dict"].keys())
if head is None and "Default" in model_alias_dict:
head = "Default"
log.info(
f"Using default head {model_alias_dict[head]} for multitask model."
)
if isinstance(head, int):
head = model_keys[0]
assert head is not None, (
f"Head must be set for multitask model! Available heads are: {model_keys}, "
f"use `dp --pt show your_model.pt model-branch` to show detail information."
)
if head not in model_alias_dict:
# preprocess with potentially case-insensitive input
head_lower = head.lower()
for mk in model_alias_dict:
if mk.lower() == head_lower:
# mapped the first matched head
head = mk
break
# replace with alias
assert head in model_alias_dict, (
f"No head or alias named {head} in model! Available heads are: {model_keys},"
f"use `dp --pt show your_model.pt model-branch` to show detail information."
)
head = model_alias_dict[head]
self.input_param = self.input_param["model_dict"][head]
state_dict_head = {"_extra_state": state_dict["_extra_state"]}
for item in state_dict:
if f"model.{head}." in item:
state_dict_head[
item.replace(f"model.{head}.", "model.Default.")
] = state_dict[item].clone()
state_dict = state_dict_head
model = get_model(self.input_param).to(DEVICE)
if not self.input_param.get("hessian_mode") and not no_jit:
model = torch.jit.script(model)
self.dp = ModelWrapper(model)
# Filter out loss-related keys that may be present in old training checkpoints.
# This is for backward compatibility with checkpoints saved before the
# XASLoss refactor that removed persistent buffers from the loss module.
state_dict = {
k: v for k, v in state_dict.items() if not k.startswith("loss.")
}
self.dp.load_state_dict(state_dict)
elif str(self.model_path).endswith(".pth"):
extra_files = {"data_modifier.pth": ""}
model = torch.jit.load(
model_file, map_location=env.DEVICE, _extra_files=extra_files
)
modifier = None
# Load modifier if it exists in extra_files
if len(extra_files["data_modifier.pth"]) > 0:
# Create a file-like object from the in-memory data
modifier_data = extra_files["data_modifier.pth"]
if isinstance(modifier_data, bytes):
modifier_data = io.BytesIO(modifier_data)
# Load the modifier directly from the file-like object
modifier = torch.jit.load(modifier_data, map_location=env.DEVICE)
self.dp = ModelWrapper(model, modifier=modifier)
self.modifier = modifier
model_def_script = self.dp.model["Default"].get_model_def_script()
if model_def_script:
self.model_def_script = json.loads(model_def_script)
else:
self.model_def_script = {}
else:
raise ValueError("Unknown model file format!")
self.dp.eval()
self.rcut = self.dp.model["Default"].get_rcut()
self.type_map = self.dp.model["Default"].get_type_map()
if isinstance(auto_batch_size, bool):
if auto_batch_size:
self.auto_batch_size = AutoBatchSize()
else:
self.auto_batch_size = None
elif isinstance(auto_batch_size, int):
self.auto_batch_size = AutoBatchSize(auto_batch_size)
elif isinstance(auto_batch_size, AutoBatchSize):
self.auto_batch_size = auto_batch_size
else:
raise TypeError("auto_batch_size should be bool, int, or AutoBatchSize")
self._has_spin = getattr(self.dp.model["Default"], "has_spin", False)
if callable(self._has_spin):
self._has_spin = self._has_spin()
self._has_hessian = self.model_def_script.get("hessian_mode", False)
def get_rcut(self) -> float:
"""Get the cutoff radius of this model."""
return self.rcut
def get_ntypes(self) -> int:
"""Get the number of atom types of this model."""
return len(self.type_map)
def get_type_map(self) -> list[str]:
"""Get the type map (element name of the atom types) of this model."""
return self.type_map
def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this DP."""
return self.dp.model["Default"].get_dim_fparam()
def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this DP."""
return self.dp.model["Default"].get_dim_aparam()
def has_default_fparam(self) -> bool:
"""Check if the model has default frame parameters."""
try:
return self.dp.model["Default"].has_default_fparam()
except AttributeError:
# for compatibility with old models
return False
def get_intensive(self) -> bool:
return self.dp.model["Default"].get_intensive()
def get_var_name(self) -> str:
"""Get the name of the property."""
if hasattr(self.dp.model["Default"], "get_var_name") and callable(
getattr(self.dp.model["Default"], "get_var_name")
):
return self.dp.model["Default"].get_var_name()
else:
raise NotImplementedError
@property
def model_type(self) -> type["DeepEvalWrapper"]:
"""The the evaluator of the model type."""
model_output_type = self.dp.model["Default"].model_output_type()
if "energy" in model_output_type:
return DeepPot
elif "dos" in model_output_type:
return DeepDOS
elif "dipole" in model_output_type:
return DeepDipole
elif "polar" in model_output_type or "polarizability" in model_output_type:
return DeepPolar
elif "global_polar" in model_output_type:
return DeepGlobalPolar
elif "wfc" in model_output_type:
return DeepWFC
elif self.get_var_name() in model_output_type:
return DeepProperty
else:
raise RuntimeError("Unknown model type")
def get_sel_type(self) -> list[int]:
"""Get the selected atom types of this model.
Only atoms with selected atom types have atomic contribution
to the result of the model.
If returning an empty list, all atom types are selected.
"""
return self.dp.model["Default"].get_sel_type()
def get_numb_dos(self) -> int:
"""Get the number of DOS."""
return self.dp.model["Default"].get_numb_dos()
def get_task_dim(self) -> int:
"""Get the output dimension."""
return self.dp.model["Default"].get_task_dim()
def get_has_efield(self) -> bool:
"""Check if the model has efield."""
return False
def get_ntypes_spin(self) -> int:
"""Get the number of spin atom types of this model. Only used in old implement."""
return 0
def get_has_spin(self) -> bool:
"""Check if the model has spin atom types."""
return self._has_spin
def get_has_hessian(self) -> bool:
"""Check if the model has hessian."""
return self._has_hessian
def get_model_branch(self) -> tuple[dict[str, str], dict[str, dict[str, Any]]]:
"""Get the model branch information."""
if "model_dict" in self.model_def_script:
model_alias_dict, model_branch_dict = get_model_dict(
self.model_def_script["model_dict"]
)
return model_alias_dict, model_branch_dict
else:
# single-task model
return {"Default": "Default"}, {"Default": {"alias": [], "info": {}}}
def eval(
self,
coords: np.ndarray,
cells: np.ndarray | None,
atom_types: np.ndarray,
atomic: bool = False,
fparam: np.ndarray | None = None,
aparam: np.ndarray | None = None,
**kwargs: Any,
) -> dict[str, np.ndarray]:
"""Evaluate the energy, force and virial by using this DP.
Parameters
----------
coords
The coordinates of atoms.
The array should be of size nframes x natoms x 3
cells
The cell of the region.
If None then non-PBC is assumed, otherwise using PBC.
The array should be of size nframes x 9
atom_types
The atom types
The list should contain natoms ints
atomic
Calculate the atomic energy and virial
fparam
The frame parameter.
The array can be of size :
- nframes x dim_fparam.
- dim_fparam. Then all frames are assumed to be provided with the same fparam.
aparam
The atomic parameter
The array can be of size :
- nframes x natoms x dim_aparam.
- natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam.
- dim_aparam. Then all frames and atoms are provided with the same aparam.
**kwargs
Other parameters
Returns
-------
output_dict : dict
The output of the evaluation. The keys are the names of the output
variables, and the values are the corresponding output arrays.
"""
# convert all of the input to numpy array
atom_types = np.array(atom_types, dtype=np.int32)
coords = np.array(coords)
if cells is not None:
cells = np.array(cells)
natoms, numb_test = self._get_natoms_and_nframes(
coords, atom_types, len(atom_types.shape) > 1
)
request_defs = self._get_request_defs(atomic)
if "spin" not in kwargs or kwargs["spin"] is None:
out = self._eval_func(self._eval_model, numb_test, natoms)(
coords, cells, atom_types, fparam, aparam, request_defs
)
else:
out = self._eval_func(self._eval_model_spin, numb_test, natoms)(
coords,
cells,
atom_types,
np.array(kwargs["spin"]),
fparam,
aparam,
request_defs,
)
return dict(
zip(
[x.name for x in request_defs],
out,
)
)
def _get_request_defs(self, atomic: bool) -> list[OutputVariableDef]:
"""Get the requested output definitions.
When atomic is True, all output_def are requested.
When atomic is False, only energy (tensor), force, and virial
are requested.
Parameters
----------
atomic : bool
Whether to request the atomic output.
Returns
-------
list[OutputVariableDef]
The requested output definitions.
"""
if atomic:
output_defs = list(self.output_def.var_defs.values())
else:
output_defs = [
x
for x in self.output_def.var_defs.values()
if x.category
in (
OutputVariableCategory.OUT,
OutputVariableCategory.REDU,
OutputVariableCategory.DERV_R,
OutputVariableCategory.DERV_C_REDU,
OutputVariableCategory.DERV_R_DERV_R,
)
]
if not self.get_has_hessian():
output_defs = [
x
for x in output_defs
if x.category != OutputVariableCategory.DERV_R_DERV_R
]
return output_defs
def _eval_func(self, inner_func: Callable, numb_test: int, natoms: int) -> Callable:
"""Wrapper method with auto batch size.
Parameters
----------
inner_func : Callable
the method to be wrapped
numb_test : int
number of tests
natoms : int
number of atoms
Returns
-------
Callable
the wrapper
"""
if self.auto_batch_size is not None:
def eval_func(*args: Any, **kwargs: Any) -> Any:
return self.auto_batch_size.execute_all(
inner_func, numb_test, natoms, *args, **kwargs
)
else:
eval_func = inner_func
return eval_func
def _get_natoms_and_nframes(
self,
coords: np.ndarray,
atom_types: np.ndarray,
mixed_type: bool = False,
) -> tuple[int, int]:
if mixed_type:
natoms = len(atom_types[0])
else:
natoms = len(atom_types)
if natoms == 0:
assert coords.size == 0
else:
coords = np.reshape(np.array(coords), [-1, natoms * 3])
nframes = coords.shape[0]
return natoms, nframes
def _eval_model(
self,
coords: np.ndarray,
cells: np.ndarray | None,
atom_types: np.ndarray,
fparam: np.ndarray | None,
aparam: np.ndarray | None,
request_defs: list[OutputVariableDef],
) -> tuple[np.ndarray, ...]:
model = self.dp.to(DEVICE)
prec = NP_PRECISION_DICT[RESERVED_PRECISION_DICT[GLOBAL_PT_FLOAT_PRECISION]]
nframes = coords.shape[0]
if len(atom_types.shape) == 1:
natoms = len(atom_types)
atom_types = np.tile(atom_types, nframes).reshape(nframes, -1)
else:
natoms = len(atom_types[0])
coord_input = torch.tensor(
coords.reshape([nframes, natoms, 3]).astype(prec),
dtype=GLOBAL_PT_FLOAT_PRECISION,
device=DEVICE,
)
type_input = torch.tensor(
atom_types.astype(NP_PRECISION_DICT[RESERVED_PRECISION_DICT[torch.long]]),
dtype=torch.long,
device=DEVICE,
)
if cells is not None:
box_input = torch.tensor(
cells.reshape([nframes, 3, 3]).astype(prec),
dtype=GLOBAL_PT_FLOAT_PRECISION,
device=DEVICE,
)
else:
box_input = None
if fparam is not None:
fparam_input = to_torch_tensor(
fparam.reshape(nframes, self.get_dim_fparam())
)
else:
fparam_input = None
if aparam is not None:
aparam_input = to_torch_tensor(
aparam.reshape(nframes, natoms, self.get_dim_aparam())
)
else:
aparam_input = None
do_atomic_virial = any(
x.category == OutputVariableCategory.DERV_C for x in request_defs
)
batch_output = model(
coord_input,
type_input,
box=box_input,
do_atomic_virial=do_atomic_virial,
fparam=fparam_input,
aparam=aparam_input,
)
if isinstance(batch_output, tuple):
batch_output = batch_output[0]
results = []
for odef in request_defs:
pt_name = self._OUTDEF_DP2BACKEND[odef.name]
if pt_name in batch_output:
shape = self._get_output_shape(odef, nframes, natoms)
out = batch_output[pt_name].reshape(shape).detach().cpu().numpy()
results.append(out)
else:
shape = self._get_output_shape(odef, nframes, natoms)
results.append(
np.full(np.abs(shape), np.nan, dtype=prec)
) # this is kinda hacky
return tuple(results)
def _eval_model_spin(
self,
coords: np.ndarray,
cells: np.ndarray | None,
atom_types: np.ndarray,
spins: np.ndarray,
fparam: np.ndarray | None,
aparam: np.ndarray | None,
request_defs: list[OutputVariableDef],
) -> tuple[np.ndarray, ...]:
model = self.dp.to(DEVICE)
nframes = coords.shape[0]
if len(atom_types.shape) == 1:
natoms = len(atom_types)
atom_types = np.tile(atom_types, nframes).reshape(nframes, -1)
else:
natoms = len(atom_types[0])
coord_input = torch.tensor(
coords.reshape([nframes, natoms, 3]),
dtype=GLOBAL_PT_FLOAT_PRECISION,
device=DEVICE,
)
type_input = torch.tensor(atom_types, dtype=torch.long, device=DEVICE)
spin_input = torch.tensor(
spins.reshape([nframes, natoms, 3]),
dtype=GLOBAL_PT_FLOAT_PRECISION,
device=DEVICE,
)
if cells is not None:
box_input = torch.tensor(
cells.reshape([nframes, 3, 3]),
dtype=GLOBAL_PT_FLOAT_PRECISION,
device=DEVICE,
)
else:
box_input = None
if fparam is not None:
fparam_input = to_torch_tensor(
fparam.reshape(nframes, self.get_dim_fparam())
)
else:
fparam_input = None
if aparam is not None:
aparam_input = to_torch_tensor(
aparam.reshape(nframes, natoms, self.get_dim_aparam())
)
else:
aparam_input = None
do_atomic_virial = any(
x.category == OutputVariableCategory.DERV_C_REDU for x in request_defs
)
batch_output = model(
coord_input,
type_input,
spin=spin_input,
box=box_input,
do_atomic_virial=do_atomic_virial,
fparam=fparam_input,
aparam=aparam_input,
)
if isinstance(batch_output, tuple):
batch_output = batch_output[0]
results = []
for odef in request_defs:
pt_name = self._OUTDEF_DP2BACKEND[odef.name]
if pt_name in batch_output:
shape = self._get_output_shape(odef, nframes, natoms)
out = batch_output[pt_name].reshape(shape).detach().cpu().numpy()
results.append(out)
else:
shape = self._get_output_shape(odef, nframes, natoms)
results.append(
np.full(
np.abs(shape),
np.nan,
dtype=NP_PRECISION_DICT[
RESERVED_PRECISION_DICT[GLOBAL_PT_FLOAT_PRECISION]
],
)
) # this is kinda hacky
return tuple(results)
def _get_output_shape(
self, odef: OutputVariableDef, nframes: int, natoms: int
) -> list[int]:
if odef.category == OutputVariableCategory.DERV_C_REDU:
# virial
return [nframes, *odef.shape[:-1], 9]
elif odef.category == OutputVariableCategory.REDU:
# energy
return [nframes, *odef.shape, 1]
elif odef.category == OutputVariableCategory.DERV_C:
# atom_virial
return [nframes, *odef.shape[:-1], natoms, 9]
elif odef.category == OutputVariableCategory.DERV_R:
# force
return [nframes, *odef.shape[:-1], natoms, 3]
elif odef.category == OutputVariableCategory.OUT:
# atom_energy, atom_tensor
# Something wrong here?
# return [nframes, *shape, natoms, 1]
return [nframes, natoms, *odef.shape, 1]
elif odef.category == OutputVariableCategory.DERV_R_DERV_R:
return [nframes, 3 * natoms, 3 * natoms]
# return [nframes, *odef.shape, 3 * natoms, 3 * natoms]
else:
raise RuntimeError("unknown category")
def eval_typeebd(self) -> np.ndarray:
"""Evaluate output of type embedding network by using this model.
Returns
-------
np.ndarray
The output of type embedding network. The shape is [ntypes, o_size] or [ntypes + 1, o_size],
where ntypes is the number of types, and o_size is the number of nodes
in the output layer. If there are multiple type embedding networks,
these outputs will be concatenated along the second axis.
Raises
------
KeyError
If the model does not enable type embedding.
See Also
--------
deepmd.pt.model.network.network.TypeEmbedNetConsistent :
The type embedding network.
"""
out = []
for mm in self.dp.model["Default"].modules():
if mm.original_name == TypeEmbedNetConsistent.__name__:
out.append(mm(DEVICE))
if not out:
raise KeyError("The model has no type embedding networks.")
typeebd = torch.cat(out, dim=1)
return to_numpy_array(typeebd)
def get_model_def_script(self) -> dict:
"""Get model definition script."""
return self.model_def_script
def get_model_size(self) -> dict:
"""Get model parameter count.
Returns
-------
dict
A dictionary containing the number of parameters in the model.
The keys are 'descriptor', 'fitting_net', and 'total'.
"""
params_dict = dict(self.dp.named_parameters())
sum_param_des = sum(
params_dict[k].numel() for k in params_dict.keys() if "descriptor" in k
)
sum_param_fit = sum(
params_dict[k].numel()
for k in params_dict.keys()
if "fitting" in k and "_networks" not in k
)
return {
"descriptor": sum_param_des,
"fitting-net": sum_param_fit,
"total": sum_param_des + sum_param_fit,
}
def get_observed_types(self) -> dict:
"""Get observed types (elements) of the model during data statistics.
Returns
-------
dict
A dictionary containing the information of observed type in the model:
- 'type_num': the total number of observed types in this model.
- 'observed_type': a list of the observed types in this model.
"""
# Try metadata first (from model_def_script, already a dict)
observed_type_list = self.model_def_script.get("info", {}).get("observed_type")
if observed_type_list is not None:
return {
"type_num": len(observed_type_list),
"observed_type": observed_type_list,
}
# Fallback: bias-based approach for old models
observed_type_list = self.dp.model["Default"].get_observed_type_list()
return {
"type_num": len(observed_type_list),
"observed_type": sort_element_type(observed_type_list),
}
def get_model(self) -> "BaseModel":
"""Get the PyTorch model.
Returns
-------
BaseModel
The PyTorch model instance.
"""
return self.dp.model["Default"]
def eval_descriptor(
self,
coords: np.ndarray,
cells: np.ndarray | None,
atom_types: np.ndarray,
fparam: np.ndarray | None = None,
aparam: np.ndarray | None = None,
**kwargs: Any,
) -> np.ndarray:
"""Evaluate descriptors by using this DP.
Parameters
----------
coords
The coordinates of atoms.
The array should be of size nframes x natoms x 3
cells
The cell of the region.
If None then non-PBC is assumed, otherwise using PBC.
The array should be of size nframes x 9
atom_types
The atom types
The list should contain natoms ints
fparam
The frame parameter.
The array can be of size :
- nframes x dim_fparam.
- dim_fparam. Then all frames are assumed to be provided with the same fparam.
aparam
The atomic parameter
The array can be of size :
- nframes x natoms x dim_aparam.
- natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam.
- dim_aparam. Then all frames and atoms are provided with the same aparam.
Returns
-------
descriptor
Descriptors.
"""
model = self.dp.model["Default"]
model.set_eval_descriptor_hook(True)
self.eval(
coords,
cells,
atom_types,
atomic=False,
fparam=fparam,
aparam=aparam,
**kwargs,
)
descriptor = model.eval_descriptor()
model.set_eval_descriptor_hook(False)
return to_numpy_array(descriptor)
def eval_fitting_last_layer(
self,
coords: np.ndarray,
cells: np.ndarray | None,
atom_types: np.ndarray,
fparam: np.ndarray | None = None,
aparam: np.ndarray | None = None,
**kwargs: Any,
) -> np.ndarray:
"""Evaluate fitting before last layer by using this DP.
Parameters
----------
coords
The coordinates of atoms.
The array should be of size nframes x natoms x 3
cells
The cell of the region.
If None then non-PBC is assumed, otherwise using PBC.
The array should be of size nframes x 9
atom_types
The atom types
The list should contain natoms ints
fparam
The frame parameter.
The array can be of size :
- nframes x dim_fparam.
- dim_fparam. Then all frames are assumed to be provided with the same fparam.
aparam
The atomic parameter
The array can be of size :
- nframes x natoms x dim_aparam.
- natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam.
- dim_aparam. Then all frames and atoms are provided with the same aparam.
Returns
-------
fitting
Fitting output before last layer.
"""
model = self.dp.model["Default"]
model.set_eval_fitting_last_layer_hook(True)
self.eval(
coords,
cells,
atom_types,
atomic=False,
fparam=fparam,
aparam=aparam,
**kwargs,
)
fitting_net = model.eval_fitting_last_layer()
model.set_eval_fitting_last_layer_hook(False)
return to_numpy_array(fitting_net)