Skip to content

Commit 5fcffe8

Browse files
committed
fix: select CALYPSO models by backend
Fixes #1906 Coding-Agent: Codex Codex-Version: codex-cli 0.149.0 Model: gpt-5.6-sol Reasoning-Effort: xhigh
1 parent d5ce577 commit 5fcffe8

4 files changed

Lines changed: 62 additions & 4 deletions

File tree

dpgen/generator/lib/calypso_check_outcar.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import glob
34
import os
45

56
import numpy as np
@@ -10,6 +11,14 @@
1011
"""
1112

1213

14+
def find_model_path():
15+
"""Find the first backend-specific model forwarded by DP-GEN."""
16+
models = sorted(glob.glob(os.path.join("..", "graph.*")))
17+
if not models:
18+
raise FileNotFoundError("No graph model was forwarded for CALYPSO recovery")
19+
return models[0]
20+
21+
1322
def Get_Element_Num(elements):
1423
"""Using the Atoms.symples to Know Element&Num."""
1524
element = []
@@ -86,7 +95,7 @@ def check():
8695
from ase.io import read
8796
from deepmd.calculator import DP
8897

89-
calc = DP(model="../graph.000.pb") # init the model before iteration
98+
calc = DP(model=find_model_path()) # initialize one model before iteration
9099

91100
to_be_opti = read("POSCAR")
92101
to_be_opti.calc = calc

dpgen/generator/lib/calypso_run_opt.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import glob
34
import os
45
import time
56

@@ -15,6 +16,14 @@
1516
"""
1617

1718

19+
def find_model_path():
20+
"""Find the first backend-specific model forwarded by DP-GEN."""
21+
models = sorted(glob.glob(os.path.join("..", "graph.*")))
22+
if not models:
23+
raise FileNotFoundError("No graph model was forwarded for CALYPSO optimization")
24+
return models[0]
25+
26+
1827
def Get_Element_Num(elements):
1928
"""Using the Atoms.symples to Know Element&Num."""
2029
element = []
@@ -112,7 +121,7 @@ def read_stress_fmax():
112121

113122
def run_opt(fmax, stress):
114123
"""Using the ASE&DP to Optimize Configures."""
115-
calc = DP(model="../graph.000.pb") # init the model before iteration
124+
calc = DP(model=find_model_path()) # initialize one model before iteration
116125
os.system("mv OUTCAR OUTCAR-last")
117126

118127
print("Start to Optimize Structures by DP----------")

dpgen/generator/lib/run_calypso.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929
calypso_model_devi_name = "model_devi_results"
3030

3131

32+
def _get_calypso_models(path, jdata):
33+
"""Return CALYPSO model files for the configured training backend."""
34+
backend = jdata.get("train_backend", "tensorflow")
35+
suffixes = {
36+
"tensorflow": ".pb",
37+
"pytorch": ".pth",
38+
"jax": ".savedmodel",
39+
}
40+
try:
41+
suffix = suffixes[backend]
42+
except KeyError as exc:
43+
supported = ", ".join(sorted(suffixes))
44+
raise ValueError(
45+
f"CALYPSO does not support training backend {backend!r}; "
46+
f"supported backends are: {supported}."
47+
) from exc
48+
return sorted(glob.glob(os.path.join(path, f"graph*{suffix}")))
49+
50+
3251
def gen_structures(
3352
iter_index, jdata, mdata, caly_run_path, current_idx, length_of_caly_runopt_list
3453
):
@@ -50,7 +69,7 @@ def gen_structures(
5069
calypso_path = mdata.get("model_devi_calypso_path")
5170
# calypso_input_path = jdata.get('calypso_input_path')
5271

53-
all_models = glob.glob(os.path.join(calypso_run_opt_path, "graph*pb"))
72+
all_models = _get_calypso_models(calypso_run_opt_path, jdata)
5473
model_names = [os.path.basename(ii) for ii in all_models]
5574

5675
deepmdkit_python = mdata.get("model_devi_deepmdkit_python")
@@ -492,7 +511,7 @@ def run_calypso_model_devi(iter_index, jdata, mdata):
492511
elif lines[-1].strip().strip("\n") == "3":
493512
# Model Devi
494513
_calypso_run_opt_path = os.path.abspath(caly_run_opt_list[0])
495-
all_models = glob.glob(os.path.join(_calypso_run_opt_path, "graph*pb"))
514+
all_models = _get_calypso_models(_calypso_run_opt_path, jdata)
496515
cwd = os.getcwd()
497516
os.chdir(calypso_model_devi_path)
498517
args = " ".join(

tests/generator/test_calypso.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import tempfile
34
import unittest
45
from pathlib import Path
56

@@ -8,6 +9,8 @@
89
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
910
__package__ = "generator"
1011

12+
from dpgen.generator.lib.run_calypso import _get_calypso_models
13+
1114
from .context import (
1215
_parse_calypso_dis_mtx,
1316
_parse_calypso_input,
@@ -174,6 +177,24 @@ def test_parse_calypso_input(self):
174177
)
175178
os.remove("input.dat")
176179

180+
def test_backend_specific_model_selection(self):
181+
with tempfile.TemporaryDirectory() as tmpdir:
182+
model_dir = Path(tmpdir)
183+
for suffix in ("pb", "pth", "savedmodel"):
184+
(model_dir / f"graph.000.{suffix}").touch()
185+
186+
cases = {
187+
"tensorflow": ".pb",
188+
"pytorch": ".pth",
189+
"jax": ".savedmodel",
190+
}
191+
for backend, suffix in cases.items():
192+
with self.subTest(backend=backend):
193+
models = _get_calypso_models(
194+
str(model_dir), {"train_backend": backend}
195+
)
196+
self.assertEqual(models, [str(model_dir / f"graph.000{suffix}")])
197+
177198

178199
if __name__ == "__main__":
179200
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)