deepmodeling/deepmd-kit

[Feature Request] support `eval_descriptor` usage in PT frozen model

Closed this issue · 3 comments

Summary

The DeepEval.eval_descriptor() function should support frozen model with extension name .pth

Detailed Description

Now the DeepEval.eval_descriptor() function can only support PT model with extension .pt, when use frozen model with extension pth, error orrurs:

In [22]: DPf = DeepPot('model-64.pth')
You can use the environment variable DP_INFER_BATCH_SIZE tocontrol the inference batch size (nframes * natoms). The default value is 1024.

In [23]: anodes2 = descriptor_from_model(onedata, DPf)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[23], line 1
----> 1 anodes2 = descriptor_from_model(onedata, DPf)

Cell In[7], line 7, in descriptor_from_model(sys, model)
      5 type_trans = np.array([model_type_map.index(i) for i in sys.data['atom_names']])
      6 atypes = list(type_trans[sys.data['atom_types']])
----> 7 predict = model.eval_descriptor(coords, cells, atypes)
      8 return predict

File /data/softwares/miniconda3/envs/deepmd-devel/lib/python3.12/site-packages/deepmd/infer/deep_eval.py:445, in DeepEval.eval_descriptor(self, coords, cells, atom_types, fparam, aparam, mixed_type, **kwargs)
    398 """Evaluate descriptors by using this DP.
    399 
    400 Parameters
   (...)
    434     Descriptors.
    435 """
    436 (
    437     coords,
    438     cells,
   (...)
    443     natoms,
    444 ) = self._standard_input(coords, cells, atom_types, fparam, aparam, mixed_type)
--> 445 descriptor = self.deep_eval.eval_descriptor(
    446     coords,
    447     cells,
    448     atom_types,
    449     fparam=fparam,
    450     aparam=aparam,
    451     **kwargs,
    452 )
    453 return descriptor

File /data/softwares/miniconda3/envs/deepmd-devel/lib/python3.12/site-packages/deepmd/pt/infer/deep_eval.py:648, in DeepEval.eval_descriptor(self, coords, cells, atom_types, fparam, aparam, **kwargs)
    616 """Evaluate descriptors by using this DP.
    617 
    618 Parameters
   (...)
    645     Descriptors.
    646 """
    647 model = self.dp.model["Default"]
--> 648 model.set_eval_descriptor_hook(True)
    649 self.eval(
    650     coords,
    651     cells,
   (...)
    656     **kwargs,
    657 )
    658 descriptor = model.eval_descriptor()

File /data/softwares/miniconda3/envs/deepmd-devel/lib/python3.12/site-packages/torch/jit/_script.py:826, in RecursiveScriptModule.__getattr__(self, attr)
    823     self.__dict__[attr] = script_method
    824     return script_method
--> 826 return super().__getattr__(attr)

File /data/softwares/miniconda3/envs/deepmd-devel/lib/python3.12/site-packages/torch/jit/_script.py:533, in ScriptModule.__getattr__(self, attr)
    531 def __getattr__(self, attr):
    532     if "_actual_script_module" not in self.__dict__:
--> 533         return super().__getattr__(attr)
    534     return getattr(self._actual_script_module, attr)

File /data/softwares/miniconda3/envs/deepmd-devel/lib/python3.12/site-packages/torch/nn/modules/module.py:1931, in Module.__getattr__(self, name)
   1929     if name in modules:
   1930         return modules[name]
-> 1931 raise AttributeError(
   1932     f"'{type(self).__name__}' object has no attribute '{name}'"
   1933 )

AttributeError: 'RecursiveScriptModule' object has no attribute 'set_eval_descriptor_hook'

Further Information, Files, and Links

No response

njzjz commented

It does support. In the unit tests, only .pth has been tested

descpt = self.dp.eval_descriptor(result.coord, result.box, result.atype)
expected_descpt = result.descriptor

(".pb", ".pth"), # model extension

The frozen process is done with 3.0.0b4 code, but the eval_descriptor usage is by using repo https://github.com/iProzd/deepmd-kit/tree/devel_compat_b4. The problem may be from there, but .pt model can be used normally

njzjz commented

#4214 is merged after 3.0.0b4.