-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathmake_model.py
More file actions
714 lines (636 loc) · 26.4 KB
/
make_model.py
File metadata and controls
714 lines (636 loc) · 26.4 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
# SPDX-License-Identifier: LGPL-3.0-or-later
from collections.abc import (
Callable,
)
from typing import (
Any,
)
import torch
from deepmd.dpmodel import (
ModelOutputDef,
)
from deepmd.dpmodel.output_def import (
FittingOutputDef,
OutputVariableCategory,
OutputVariableOperation,
check_operation_applied,
)
from deepmd.pt.model.atomic_model.base_atomic_model import (
BaseAtomicModel,
)
from deepmd.pt.model.model.model import (
BaseModel,
)
from deepmd.pt.model.model.transform_output import (
communicate_extended_output,
fit_output_to_model_output,
)
from deepmd.pt.utils.env import (
GLOBAL_PT_ENER_FLOAT_PRECISION,
GLOBAL_PT_FLOAT_PRECISION,
PRECISION_DICT,
RESERVED_PRECISION_DICT,
)
from deepmd.pt.utils.nlist import (
extend_input_and_build_neighbor_list,
nlist_distinguish_types,
)
from deepmd.utils.path import (
DPPath,
)
def make_model(T_AtomicModel: type[BaseAtomicModel]) -> type:
"""Make a model as a derived class of an atomic model.
The model provide two interfaces.
1. the `forward_common_lower`, that takes extended coordinates, atyps and neighbor list,
and outputs the atomic and property and derivatives (if required) on the extended region.
2. the `forward_common`, that takes coordinates, atypes and cell and predicts
the atomic and reduced property, and derivatives (if required) on the local region.
Parameters
----------
T_AtomicModel
The atomic model.
Returns
-------
CM
The model.
"""
class CM(BaseModel):
def __init__(
self,
*args: Any,
# underscore to prevent conflict with normal inputs
atomic_model_: T_AtomicModel | None = None, # type: ignore
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
if atomic_model_ is not None:
self.atomic_model: T_AtomicModel = atomic_model_ # type: ignore
else:
self.atomic_model: T_AtomicModel = T_AtomicModel(*args, **kwargs) # type: ignore
self.precision_dict = PRECISION_DICT
self.reverse_precision_dict = RESERVED_PRECISION_DICT
self.global_pt_float_precision = GLOBAL_PT_FLOAT_PRECISION
self.global_pt_ener_float_precision = GLOBAL_PT_ENER_FLOAT_PRECISION
def model_output_def(self) -> ModelOutputDef:
"""Get the output def for the model."""
return ModelOutputDef(self.atomic_output_def())
@torch.jit.export
def model_output_type(self) -> list[str]:
"""Get the output type for the model."""
output_def = self.model_output_def()
var_defs = output_def.var_defs
# jit: Comprehension ifs are not supported yet
# type hint is critical for JIT
vars: list[str] = []
for kk, vv in var_defs.items():
# .value is critical for JIT
if vv.category == OutputVariableCategory.OUT.value:
vars.append(kk)
return vars
def enable_compression(
self,
table_extrapolate: float = 5,
table_stride_1: float = 0.01,
table_stride_2: float = 0.1,
check_frequency: int = -1,
) -> None:
"""Call atomic_model enable_compression().
Parameters
----------
table_extrapolate
The scale of model extrapolation
table_stride_1
The uniform stride of the first table
table_stride_2
The uniform stride of the second table
check_frequency
The overflow check frequency
"""
self.atomic_model.enable_compression(
self.get_min_nbor_dist(),
table_extrapolate,
table_stride_1,
table_stride_2,
check_frequency,
)
# cannot use the name forward. torch script does not work
def forward_common(
self,
coord: torch.Tensor,
atype: torch.Tensor,
box: torch.Tensor | None = None,
fparam: torch.Tensor | None = None,
aparam: torch.Tensor | None = None,
do_atomic_virial: bool = False,
coord_corr_for_virial: torch.Tensor | None = None,
charge_spin: torch.Tensor | None = None,
) -> dict[str, torch.Tensor]:
"""Return model prediction.
Parameters
----------
coord
The coordinates of the atoms.
shape: nf x (nloc x 3)
atype
The type of atoms. shape: nf x nloc
box
The simulation box. shape: nf x 9
fparam
frame parameter. nf x ndf
aparam
atomic parameter. nf x nloc x nda
do_atomic_virial
If calculate the atomic virial.
coord_corr_for_virial
The coordinates correction of the atoms for virial.
shape: nf x (nloc x 3)
Returns
-------
ret_dict
The result dict of type dict[str,torch.Tensor].
The keys are defined by the `ModelOutputDef`.
"""
cc, bb, fp, ap, input_prec = self._input_type_cast(
coord, box=box, fparam=fparam, aparam=aparam
)
del coord, box, fparam, aparam
(
extended_coord,
extended_atype,
mapping,
nlist,
) = extend_input_and_build_neighbor_list(
cc,
atype,
self.get_rcut(),
self.get_sel(),
# types will be distinguished in the lower interface,
# so it doesn't need to be distinguished here
mixed_types=True,
box=bb,
)
if coord_corr_for_virial is not None:
coord_corr_for_virial = coord_corr_for_virial.to(cc.dtype)
extended_coord_corr = torch.gather(
coord_corr_for_virial, 1, mapping.unsqueeze(-1).expand(-1, -1, 3)
)
else:
extended_coord_corr = None
model_predict_lower = self.forward_common_lower(
extended_coord,
extended_atype,
nlist,
mapping,
do_atomic_virial=do_atomic_virial,
fparam=fp,
aparam=ap,
extended_coord_corr=extended_coord_corr,
charge_spin=charge_spin,
)
model_predict = communicate_extended_output(
model_predict_lower,
self.model_output_def(),
mapping,
do_atomic_virial=do_atomic_virial,
)
model_predict = self._output_type_cast(model_predict, input_prec)
return model_predict
def get_out_bias(self) -> torch.Tensor:
return self.atomic_model.get_out_bias()
def set_out_bias(self, out_bias: torch.Tensor) -> None:
self.atomic_model.set_out_bias(out_bias)
def change_out_bias(
self,
merged: Any,
bias_adjust_mode: str = "change-by-statistic",
) -> None:
"""Change the output bias of atomic model according to the input data and the pretrained model.
Parameters
----------
merged : Union[Callable[[], list[dict]], list[dict]]
- list[dict]: A list of data samples from various data systems.
Each element, `merged[i]`, is a data dictionary containing `keys`: `torch.Tensor`
originating from the `i`-th data system.
- Callable[[], list[dict]]: A lazy function that returns data samples in the above format
only when needed. Since the sampling process can be slow and memory-intensive,
the lazy function helps by only sampling once.
bias_adjust_mode : str
The mode for changing output bias : ['change-by-statistic', 'set-by-statistic']
'change-by-statistic' : perform predictions on labels of target dataset,
and do least square on the errors to obtain the target shift as bias.
'set-by-statistic' : directly use the statistic output bias in the target dataset.
"""
self.atomic_model.change_out_bias(
merged,
bias_adjust_mode=bias_adjust_mode,
)
def forward_common_lower(
self,
extended_coord: torch.Tensor,
extended_atype: torch.Tensor,
nlist: torch.Tensor,
mapping: torch.Tensor | None = None,
fparam: torch.Tensor | None = None,
aparam: torch.Tensor | None = None,
do_atomic_virial: bool = False,
comm_dict: dict[str, torch.Tensor] | None = None,
extra_nlist_sort: bool = False,
extended_coord_corr: torch.Tensor | None = None,
charge_spin: torch.Tensor | None = None,
) -> dict[str, torch.Tensor]:
"""Return model prediction. Lower interface that takes
extended atomic coordinates and types, nlist, and mapping
as input, and returns the predictions on the extended region.
The predictions are not reduced.
Parameters
----------
extended_coord
coordinates in extended region. nf x (nall x 3)
extended_atype
atomic type in extended region. nf x nall
nlist
neighbor list. nf x nloc x nsel.
mapping
mapps the extended indices to local indices. nf x nall.
fparam
frame parameter. nf x ndf
aparam
atomic parameter. nf x nloc x nda
do_atomic_virial
whether calculate atomic virial.
comm_dict
The data needed for communication for parallel inference.
extra_nlist_sort
whether to forcibly sort the nlist.
extended_coord_corr
coordinates correction for virial in extended region. nf x (nall x 3)
Returns
-------
result_dict
the result dict, defined by the `FittingOutputDef`.
"""
nframes, nall = extended_atype.shape[:2]
extended_coord = extended_coord.view(nframes, -1, 3)
nlist = self.format_nlist(
extended_coord, extended_atype, nlist, extra_nlist_sort=extra_nlist_sort
)
cc_ext, _, fp, ap, input_prec = self._input_type_cast(
extended_coord, fparam=fparam, aparam=aparam
)
del extended_coord, fparam, aparam
force_coord = cc_ext
if self.atomic_model.do_grad_r() or self.atomic_model.do_grad_c():
if not force_coord.requires_grad:
force_coord = force_coord.clone().requires_grad_(True)
atomic_ret = self.atomic_model.forward_common_atomic(
force_coord,
extended_atype,
nlist,
mapping=mapping,
fparam=fp,
aparam=ap,
comm_dict=comm_dict,
charge_spin=charge_spin,
)
model_predict = fit_output_to_model_output(
atomic_ret,
self.atomic_output_def(),
force_coord,
do_atomic_virial=do_atomic_virial,
create_graph=self.training,
mask=atomic_ret["mask"] if "mask" in atomic_ret else None,
extended_coord_corr=extended_coord_corr,
)
model_predict = self._output_type_cast(model_predict, input_prec)
return model_predict
def _input_type_cast(
self,
coord: torch.Tensor,
box: torch.Tensor | None = None,
fparam: torch.Tensor | None = None,
aparam: torch.Tensor | None = None,
) -> tuple[
torch.Tensor,
torch.Tensor | None,
torch.Tensor | None,
torch.Tensor | None,
str,
]:
"""Cast the input data to global float type."""
input_prec = self.reverse_precision_dict[coord.dtype]
###
### type checking would not pass jit, convert to coord prec anyway
###
# for vv, kk in zip([fparam, aparam], ["frame", "atomic"]):
# if vv is not None and self.reverse_precision_dict[vv.dtype] != input_prec:
# log.warning(
# f"type of {kk} parameter {self.reverse_precision_dict[vv.dtype]}"
# " does not match"
# f" that of the coordinate {input_prec}"
# )
_lst: list[torch.Tensor | None] = [
vv.to(coord.dtype) if vv is not None else None
for vv in [box, fparam, aparam]
]
box, fparam, aparam = _lst
if (
input_prec
== self.reverse_precision_dict[self.global_pt_float_precision]
):
return coord, box, fparam, aparam, input_prec
else:
pp = self.global_pt_float_precision
return (
coord.to(pp),
box.to(pp) if box is not None else None,
fparam.to(pp) if fparam is not None else None,
aparam.to(pp) if aparam is not None else None,
input_prec,
)
def _output_type_cast(
self,
model_ret: dict[str, torch.Tensor],
input_prec: str,
) -> dict[str, torch.Tensor]:
"""Convert the model output to the input prec."""
do_cast = (
input_prec
!= self.reverse_precision_dict[self.global_pt_float_precision]
)
pp = self.precision_dict[input_prec]
odef = self.model_output_def()
for kk in odef.keys():
if kk not in model_ret.keys():
# do not return energy_derv_c if not do_atomic_virial
continue
if check_operation_applied(odef[kk], OutputVariableOperation.REDU):
model_ret[kk] = (
model_ret[kk].to(self.global_pt_ener_float_precision)
if model_ret[kk] is not None
else None
)
elif do_cast:
model_ret[kk] = (
model_ret[kk].to(pp) if model_ret[kk] is not None else None
)
return model_ret
def format_nlist(
self,
extended_coord: torch.Tensor,
extended_atype: torch.Tensor,
nlist: torch.Tensor,
extra_nlist_sort: bool = False,
) -> torch.Tensor:
"""Format the neighbor list.
1. If the number of neighbors in the `nlist` is equal to sum(self.sel),
it does nothong
2. If the number of neighbors in the `nlist` is smaller than sum(self.sel),
the `nlist` is pad with -1.
3. If the number of neighbors in the `nlist` is larger than sum(self.sel),
the nearest sum(sel) neighbors will be preserved.
Known limitations:
In the case of not self.mixed_types, the nlist is always formatted.
May have side effact on the efficiency.
Parameters
----------
extended_coord
coordinates in extended region. nf x nall x 3
extended_atype
atomic type in extended region. nf x nall
nlist
neighbor list. nf x nloc x nsel
extra_nlist_sort
whether to forcibly sort the nlist.
Returns
-------
formated_nlist
the formatted nlist.
"""
mixed_types = self.mixed_types()
nlist = self._format_nlist(
extended_coord,
nlist,
sum(self.get_sel()),
extra_nlist_sort=extra_nlist_sort,
)
if not mixed_types:
nlist = nlist_distinguish_types(nlist, extended_atype, self.get_sel())
return nlist
def _format_nlist(
self,
extended_coord: torch.Tensor,
nlist: torch.Tensor,
nnei: int,
extra_nlist_sort: bool = False,
) -> torch.Tensor:
n_nf, n_nloc, n_nnei = nlist.shape
# nf x nall x 3
extended_coord = extended_coord.view([n_nf, -1, 3])
rcut = self.get_rcut()
if n_nnei < nnei:
nlist = torch.cat(
[
nlist,
-1
* torch.ones(
[n_nf, n_nloc, nnei - n_nnei],
dtype=nlist.dtype,
device=nlist.device,
),
],
dim=-1,
)
if n_nnei > nnei or extra_nlist_sort:
n_nf, n_nloc, n_nnei = nlist.shape
m_real_nei = nlist >= 0
nlist = torch.where(m_real_nei, nlist, 0)
# nf x nloc x 3
coord0 = extended_coord[:, :n_nloc, :]
# nf x (nloc x nnei) x 3
index = nlist.view(n_nf, n_nloc * n_nnei, 1).expand(-1, -1, 3)
coord1 = torch.gather(extended_coord, 1, index)
# nf x nloc x nnei x 3
coord1 = coord1.view(n_nf, n_nloc, n_nnei, 3)
# nf x nloc x nnei
rr = torch.linalg.norm(coord0[:, :, None, :] - coord1, dim=-1)
rr = torch.where(m_real_nei, rr, float("inf"))
rr, nlist_mapping = torch.sort(rr, dim=-1)
nlist = torch.gather(nlist, 2, nlist_mapping)
nlist = torch.where(rr > rcut, -1, nlist)
nlist = nlist[..., :nnei]
else: # not extra_nlist_sort and n_nnei <= nnei:
pass # great!
assert nlist.shape[-1] == nnei
return nlist
def do_grad_r(
self,
var_name: str | None = None,
) -> bool:
"""Tell if the output variable `var_name` is r_differentiable.
if var_name is None, returns if any of the variable is r_differentiable.
"""
return self.atomic_model.do_grad_r(var_name)
def do_grad_c(
self,
var_name: str | None = None,
) -> bool:
"""Tell if the output variable `var_name` is c_differentiable.
if var_name is None, returns if any of the variable is c_differentiable.
"""
return self.atomic_model.do_grad_c(var_name)
def change_type_map(
self, type_map: list[str], model_with_new_type_stat: Any | None = 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.
"""
self.atomic_model.change_type_map(
type_map=type_map,
model_with_new_type_stat=model_with_new_type_stat.atomic_model
if model_with_new_type_stat is not None
else None,
)
def serialize(self) -> dict:
return self.atomic_model.serialize()
@classmethod
def deserialize(cls, data: Any) -> "CM":
return cls(atomic_model_=T_AtomicModel.deserialize(data))
def set_case_embd(self, case_idx: int) -> None:
self.atomic_model.set_case_embd(case_idx)
@torch.jit.export
def get_dim_fparam(self) -> int:
"""Get the number (dimension) of frame parameters of this atomic model."""
return self.atomic_model.get_dim_fparam()
@torch.jit.export
def has_default_fparam(self) -> bool:
"""Check if the model has default frame parameters."""
return self.atomic_model.has_default_fparam()
def get_default_fparam(self) -> torch.Tensor | None:
return self.atomic_model.get_default_fparam()
@torch.jit.export
def has_chg_spin_ebd(self) -> bool:
"""Check if the model has charge spin embedding."""
return self.atomic_model.has_chg_spin_ebd()
@torch.jit.export
def get_dim_chg_spin(self) -> int:
"""Get the dimension of charge_spin input."""
return self.atomic_model.get_dim_chg_spin()
@torch.jit.export
def has_default_chg_spin(self) -> bool:
"""Check if the model has default charge_spin values."""
return self.atomic_model.has_default_chg_spin()
@torch.jit.export
def get_default_chg_spin(self) -> torch.Tensor | None:
"""Get the default charge_spin values."""
return self.atomic_model.get_default_chg_spin()
@torch.jit.export
def get_dim_aparam(self) -> int:
"""Get the number (dimension) of atomic parameters of this atomic model."""
return self.atomic_model.get_dim_aparam()
@torch.jit.export
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.atomic_model.get_sel_type()
@torch.jit.export
def is_aparam_nall(self) -> bool:
"""Check whether the shape of atomic parameters is (nframes, nall, ndim).
If False, the shape is (nframes, nloc, ndim).
"""
return self.atomic_model.is_aparam_nall()
@torch.jit.export
def get_rcut(self) -> float:
"""Get the cut-off radius."""
return self.atomic_model.get_rcut()
@torch.jit.export
def get_type_map(self) -> list[str]:
"""Get the type map."""
return self.atomic_model.get_type_map()
@torch.jit.export
def get_nsel(self) -> int:
"""Returns the total number of selected neighboring atoms in the cut-off radius."""
return self.atomic_model.get_nsel()
@torch.jit.export
def get_nnei(self) -> int:
"""Returns the total number of selected neighboring atoms in the cut-off radius."""
return self.atomic_model.get_nnei()
def atomic_output_def(self) -> FittingOutputDef:
"""Get the output def of the atomic model."""
return self.atomic_model.atomic_output_def()
def compute_or_load_stat(
self,
sampled_func: Callable[[], Any],
stat_file_path: DPPath | None = None,
preset_observed_type: list[str] | None = None,
) -> None:
"""Compute or load the statistics."""
return self.atomic_model.compute_or_load_stat(
sampled_func,
stat_file_path,
preset_observed_type=preset_observed_type,
)
@torch.jit.export
def get_observed_type_list(self) -> list[str]:
"""Get observed types (elements) of the model during data statistics.
Returns
-------
observed_type_list: a list of the observed types in this model.
"""
type_map = self.get_type_map()
out_bias = self.atomic_model.get_out_bias()[0]
assert out_bias is not None, "No out_bias found in the model."
assert out_bias.dim() == 2, "The supported out_bias should be a 2D tensor."
assert out_bias.size(0) == len(type_map), (
"The out_bias shape does not match the type_map length."
)
bias_mask = (
torch.gt(torch.abs(out_bias), 1e-6).any(dim=-1).detach().cpu()
) # 1e-6 for stability
observed_type_list: list[str] = []
for i in range(len(type_map)):
if bias_mask[i]:
observed_type_list.append(type_map[i])
return observed_type_list
def get_sel(self) -> list[int]:
"""Returns the number of selected atoms for each type."""
return self.atomic_model.get_sel()
def mixed_types(self) -> bool:
"""If true, the model
1. assumes total number of atoms aligned across frames;
2. uses a neighbor list that does not distinguish different atomic types.
If false, the model
1. assumes total number of atoms of each atom type aligned across frames;
2. uses a neighbor list that distinguishes different atomic types.
"""
return self.atomic_model.mixed_types()
@torch.jit.export
def has_message_passing(self) -> bool:
"""Returns whether the model has message passing."""
return self.atomic_model.has_message_passing()
def need_sorted_nlist_for_lower(self) -> bool:
"""Returns whether the model needs sorted nlist when using `forward_lower`."""
return self.atomic_model.need_sorted_nlist_for_lower()
def forward(
self,
coord: torch.Tensor,
atype: torch.Tensor,
box: torch.Tensor | None = None,
fparam: torch.Tensor | None = None,
aparam: torch.Tensor | None = None,
do_atomic_virial: bool = False,
charge_spin: torch.Tensor | None = None,
) -> dict[str, torch.Tensor]:
# directly call the forward_common method when no specific transform rule
return self.forward_common(
coord,
atype,
box,
fparam=fparam,
aparam=aparam,
do_atomic_virial=do_atomic_virial,
charge_spin=charge_spin,
)
return CM