|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +import json |
| 3 | +import tempfile |
| 4 | +from typing import ( |
| 5 | + Dict, |
| 6 | + List, |
| 7 | + Optional, |
| 8 | +) |
| 9 | + |
| 10 | +import torch |
| 11 | + |
| 12 | +from deepmd.dpmodel.output_def import ( |
| 13 | + FittingOutputDef, |
| 14 | +) |
| 15 | +from deepmd.entrypoints.convert_backend import ( |
| 16 | + convert_backend, |
| 17 | +) |
| 18 | +from deepmd.pt.model.model.model import ( |
| 19 | + BaseModel, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@BaseModel.register("frozen") |
| 24 | +class FrozenModel(BaseModel): |
| 25 | + """Load model from a frozen model, which cannot be trained. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + model_file : str |
| 30 | + The path to the frozen model |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, model_file: str, **kwargs): |
| 34 | + super().__init__(**kwargs) |
| 35 | + self.model_file = model_file |
| 36 | + if model_file.endswith(".pth"): |
| 37 | + self.model = torch.jit.load(model_file) |
| 38 | + else: |
| 39 | + # try to convert from other formats |
| 40 | + with tempfile.NamedTemporaryFile(suffix=".pth") as f: |
| 41 | + convert_backend(INPUT=model_file, OUTPUT=f.name) |
| 42 | + self.model = torch.jit.load(f.name) |
| 43 | + |
| 44 | + @torch.jit.export |
| 45 | + def fitting_output_def(self) -> FittingOutputDef: |
| 46 | + """Get the output def of developer implemented atomic models.""" |
| 47 | + return self.model.fitting_output_def() |
| 48 | + |
| 49 | + @torch.jit.export |
| 50 | + def get_rcut(self) -> float: |
| 51 | + """Get the cut-off radius.""" |
| 52 | + return self.model.get_rcut() |
| 53 | + |
| 54 | + @torch.jit.export |
| 55 | + def get_type_map(self) -> List[str]: |
| 56 | + """Get the type map.""" |
| 57 | + return self.model.get_type_map() |
| 58 | + |
| 59 | + @torch.jit.export |
| 60 | + def get_sel(self) -> List[int]: |
| 61 | + """Returns the number of selected atoms for each type.""" |
| 62 | + return self.model.get_sel() |
| 63 | + |
| 64 | + @torch.jit.export |
| 65 | + def get_dim_fparam(self) -> int: |
| 66 | + """Get the number (dimension) of frame parameters of this atomic model.""" |
| 67 | + return self.model.get_dim_fparam() |
| 68 | + |
| 69 | + @torch.jit.export |
| 70 | + def get_dim_aparam(self) -> int: |
| 71 | + """Get the number (dimension) of atomic parameters of this atomic model.""" |
| 72 | + return self.model.get_dim_aparam() |
| 73 | + |
| 74 | + @torch.jit.export |
| 75 | + def get_sel_type(self) -> List[int]: |
| 76 | + """Get the selected atom types of this model. |
| 77 | +
|
| 78 | + Only atoms with selected atom types have atomic contribution |
| 79 | + to the result of the model. |
| 80 | + If returning an empty list, all atom types are selected. |
| 81 | + """ |
| 82 | + return self.model.get_sel_type() |
| 83 | + |
| 84 | + @torch.jit.export |
| 85 | + def is_aparam_nall(self) -> bool: |
| 86 | + """Check whether the shape of atomic parameters is (nframes, nall, ndim). |
| 87 | +
|
| 88 | + If False, the shape is (nframes, nloc, ndim). |
| 89 | + """ |
| 90 | + return self.model.is_aparam_nall() |
| 91 | + |
| 92 | + @torch.jit.export |
| 93 | + def mixed_types(self) -> bool: |
| 94 | + """If true, the model |
| 95 | + 1. assumes total number of atoms aligned across frames; |
| 96 | + 2. uses a neighbor list that does not distinguish different atomic types. |
| 97 | +
|
| 98 | + If false, the model |
| 99 | + 1. assumes total number of atoms of each atom type aligned across frames; |
| 100 | + 2. uses a neighbor list that distinguishes different atomic types. |
| 101 | +
|
| 102 | + """ |
| 103 | + return self.model.mixed_types() |
| 104 | + |
| 105 | + @torch.jit.export |
| 106 | + def forward( |
| 107 | + self, |
| 108 | + coord, |
| 109 | + atype, |
| 110 | + box: Optional[torch.Tensor] = None, |
| 111 | + fparam: Optional[torch.Tensor] = None, |
| 112 | + aparam: Optional[torch.Tensor] = None, |
| 113 | + do_atomic_virial: bool = False, |
| 114 | + ) -> Dict[str, torch.Tensor]: |
| 115 | + return self.model.forward( |
| 116 | + coord, |
| 117 | + atype, |
| 118 | + box=box, |
| 119 | + fparam=fparam, |
| 120 | + aparam=aparam, |
| 121 | + do_atomic_virial=do_atomic_virial, |
| 122 | + ) |
| 123 | + |
| 124 | + @torch.jit.export |
| 125 | + def get_model_def_script(self) -> str: |
| 126 | + """Get the model definition script.""" |
| 127 | + # try to use the original script instead of "frozen model" |
| 128 | + # Note: this cannot change the script of the parent model |
| 129 | + # it may still try to load hard-coded filename, which might |
| 130 | + # be a problem |
| 131 | + return self.model.get_model_def_script() |
| 132 | + |
| 133 | + def serialize(self) -> dict: |
| 134 | + from deepmd.pt.model.model import ( |
| 135 | + get_model, |
| 136 | + ) |
| 137 | + |
| 138 | + # try to recover the original model |
| 139 | + model_def_script = json.loads(self.get_model_def_script()) |
| 140 | + model = get_model(model_def_script) |
| 141 | + model.load_state_dict(self.model.state_dict()) |
| 142 | + return model.serialize() |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def deserialize(cls, data: dict): |
| 146 | + raise RuntimeError("Should not touch here.") |
| 147 | + |
| 148 | + @torch.jit.export |
| 149 | + def get_nnei(self) -> int: |
| 150 | + """Returns the total number of selected neighboring atoms in the cut-off radius.""" |
| 151 | + return self.model.get_nnei() |
| 152 | + |
| 153 | + @torch.jit.export |
| 154 | + def get_nsel(self) -> int: |
| 155 | + """Returns the total number of selected neighboring atoms in the cut-off radius.""" |
| 156 | + return self.model.get_nsel() |
| 157 | + |
| 158 | + @classmethod |
| 159 | + def update_sel(cls, global_jdata: dict, local_jdata: dict): |
| 160 | + """Update the selection and perform neighbor statistics. |
| 161 | +
|
| 162 | + Parameters |
| 163 | + ---------- |
| 164 | + global_jdata : dict |
| 165 | + The global data, containing the training section |
| 166 | + local_jdata : dict |
| 167 | + The local data refer to the current class |
| 168 | + """ |
| 169 | + return local_jdata |
| 170 | + |
| 171 | + @torch.jit.export |
| 172 | + def model_output_type(self) -> str: |
| 173 | + """Get the output type for the model.""" |
| 174 | + return self.model.model_output_type() |
0 commit comments