Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Incorrect results from DPLR model in python inference interface #4625

Open
ChiahsinChu opened this issue Mar 1, 2025 · 7 comments
Open
Labels
bug critical Critical bugs that may break the results without messages

Comments

@ChiahsinChu
Copy link
Contributor

ChiahsinChu commented Mar 1, 2025

Bug summary

In deepmd v3, the inference results from the DPLR model are incorrect when using the Python interface. In deepmd-v3, the long-range correction (i.e., the data modifier) is not calculated and added to the final results. It should be relevant to the change in architecture in the inference module.

DeePMD-kit Version

DeePMD-kit v3.0.1

Backend and its version

TensorFlow v2.18.0-rc2-4-g6550e4bd802

How did you download the software?

pip

Input Files, Running Commands, Error Log, etc.

Test python script for v2:

from ase import build
import numpy as np

from deepmd.infer import DeepPot


dp = DeepPot("graph.pb")
dm = dp.dm

type_map = ["O", "H"]

atoms = build.molecule("H2O")
atoms.set_cell([10, 10, 10])
atoms.center()

atype = np.array([type_map.index(a) for a in atoms.symbols])


out = dp.eval(
    atoms.get_positions().reshape(1, -1),
    atoms.get_cell().reshape(1, -1),
    atype,
)
print(out[0])

out_dm = dm.eval(
    atoms.get_positions().reshape(1, -1),
    atoms.get_cell().reshape(1, -1),
    atype,
)
print(out_dm[0])

Output:

[[-494.9365821]]
[46.573121]

Test python script for v3:

from ase import build
import numpy as np

from deepmd.tf.infer.data_modifier import DipoleChargeModifier
from deepmd.infer import DeepPot


dp = DeepPot("graph.pb")
dm = DipoleChargeModifier(
    "dw_model.pb",
    model_charge_map=[-8],
    sys_charge_map=[6, 1],
    ewald_h=0.5,
    ewald_beta=0.4,
)

type_map = ["O", "H"]

atoms = build.molecule("H2O")
atoms.set_cell([10, 10, 10])
atoms.center()

atype = np.array([type_map.index(a) for a in atoms.symbols])


out = dp.eval(
    atoms.get_positions().reshape(1, -1),
    atoms.get_cell().reshape(1, -1),
    atype,
)
print(out[0])

out_dm = dm.eval(
    atoms.get_positions().reshape(1, -1),
    atoms.get_cell().reshape(1, -1),
    atype,
)
print(out_dm[0])

Output:

[[-541.5097031]]
[46.573121]

In both cases, the energies from data modifier are identical. However, the total energy in v3 = the total energy in v2 + the energy of data modifier.

Steps to Reproduce

Please download the attached models and run the scripts above with deepmd v2/v3.

Further Information, Files, and Links

H2O_dplr_model.zip

@ChiahsinChu ChiahsinChu added the bug label Mar 1, 2025
@njzjz
Copy link
Member

njzjz commented Mar 6, 2025

if self.modifier_type is not None and isinstance(self.model_type, DeepPot):

This line seems wrong; it should be issubclass(self.model_type, DeepPot).

@njzjz
Copy link
Member

njzjz commented Mar 6, 2025

@ChiahsinChu
Copy link
Contributor Author

ChiahsinChu commented Mar 6, 2025

This line seems wrong; it should be issubclass(self.model_type, DeepPot).

Yes, but there should be more than this. I tried temporality to remove this condition but it still did not work. The problem seems to come from the impropriate initialisation of the DeepEval class, as the result of which _init_tensors and _init_attr methods are not called.

@ChiahsinChu
Copy link
Contributor Author

However, I don't understand why this test can pass:

Where did the reference data come from? When I tried to run this test in v2.2.11, I got the error about the missing dipole model, which makes sense to me:

Traceback (most recent call last):
  File "/home/jxzhu/workspace/test/test.py", line 7, in <module>
    dp = DeepPot("lrmodel.pb")
  File "/home/jxzhu/apps/deepmd/2.2.11/deepmd/infer/deep_pot.py", line 205, in __init__
    self.dm = DipoleChargeModifier(
  File "/home/jxzhu/apps/deepmd/2.2.11/deepmd/infer/data_modifier.py", line 61, in __init__
    DeepDipole.__init__(
  File "/home/jxzhu/apps/deepmd/2.2.11/deepmd/infer/deep_dipole.py", line 58, in __init__
    DeepTensor.__init__(
  File "/home/jxzhu/apps/deepmd/2.2.11/deepmd/infer/deep_tensor.py", line 71, in __init__
    DeepEval.__init__(
  File "/home/jxzhu/apps/deepmd/2.2.11/deepmd/infer/deep_eval.py", line 64, in __init__
    self.graph = self._load_graph(
  File "/home/jxzhu/apps/deepmd/2.2.11/deepmd/infer/deep_eval.py", line 189, in _load_graph
    graph_def.ParseFromString(f.read())
  File "/home/jxzhu/apps/miniconda3/envs/deepmd-v2.2.11/lib/python3.10/site-packages/tensorflow/python/lib/io/file_io.py", line 114, in read
    self._preread_check()
  File "/home/jxzhu/apps/miniconda3/envs/deepmd-v2.2.11/lib/python3.10/site-packages/tensorflow/python/lib/io/file_io.py", line 76, in _preread_check
    self._read_buf = _pywrap_file_io.BufferedInputStream(
tensorflow.python.framework.errors_impl.NotFoundError: lrdipole.pb; No such file or directory

In other words, the v3 code does not try to load the dipole model...

@njzjz
Copy link
Member

njzjz commented Mar 7, 2025

Where did the reference data come from?

It comes from #2545.

@ChiahsinChu
Copy link
Contributor Author

Where did the reference data come from?

It comes from #2545.

I see. I checked the orginal test in v2, and found that the reference data used in the UT in v3 is actually the sr term. That explains it.

@njzjz
Copy link
Member

njzjz commented Mar 14, 2025

I see. Ideally it should reproduce the following result.

expected_e_lr_efield_constant = -40.57352302
expected_f_lr_efield_constant = np.array(
[
[0.47635071, 0.15088380, -1.20378471],
[-0.52759976, -0.01182856, -0.72773815],
[-0.70317794, 0.29171446, 1.22375302],
[0.20500683, -0.50210283, -0.04263579],
[0.82463041, 0.04231172, 0.47856560],
[-0.27521024, 0.02902140, 0.27184004],
]
)

@njzjz njzjz added the critical Critical bugs that may break the results without messages label Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug critical Critical bugs that may break the results without messages
Projects
None yet
Development

No branches or pull requests

3 participants