|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +"""Helpers for preparing converted TensorFlow graph files in LAMMPS tests.""" |
| 3 | + |
| 4 | +from __future__ import ( |
| 5 | + annotations, |
| 6 | +) |
| 7 | + |
| 8 | +import errno |
| 9 | +import os |
| 10 | +import subprocess as sp |
| 11 | +import sys |
| 12 | +import tempfile |
| 13 | +import time |
| 14 | +from pathlib import ( |
| 15 | + Path, |
| 16 | +) |
| 17 | + |
| 18 | +_LOCK_TIMEOUT_SECONDS = 60.0 |
| 19 | +_LOCK_POLL_SECONDS = 0.1 |
| 20 | + |
| 21 | + |
| 22 | +def _is_up_to_date(source: Path, output: Path) -> bool: |
| 23 | + return output.exists() and output.stat().st_mtime_ns >= source.stat().st_mtime_ns |
| 24 | + |
| 25 | + |
| 26 | +def _read_lock_pid(lock_file: Path) -> int | None: |
| 27 | + try: |
| 28 | + for line in lock_file.read_text(encoding="utf-8").splitlines(): |
| 29 | + if line.startswith("pid="): |
| 30 | + return int(line.split("=", maxsplit=1)[1]) |
| 31 | + except (FileNotFoundError, ValueError): |
| 32 | + return None |
| 33 | + return None |
| 34 | + |
| 35 | + |
| 36 | +def _pid_is_running(pid: int) -> bool: |
| 37 | + try: |
| 38 | + os.kill(pid, 0) |
| 39 | + except ProcessLookupError: |
| 40 | + return False |
| 41 | + except PermissionError: |
| 42 | + return True |
| 43 | + except OSError as err: |
| 44 | + if err.errno == errno.ESRCH: |
| 45 | + return False |
| 46 | + raise |
| 47 | + return True |
| 48 | + |
| 49 | + |
| 50 | +def _should_break_stale_lock(lock_file: Path) -> bool: |
| 51 | + try: |
| 52 | + lock_stat = lock_file.stat() |
| 53 | + except FileNotFoundError: |
| 54 | + return False |
| 55 | + |
| 56 | + lock_pid = _read_lock_pid(lock_file) |
| 57 | + if lock_pid is not None: |
| 58 | + return not _pid_is_running(lock_pid) |
| 59 | + |
| 60 | + lock_age = time.time() - lock_stat.st_mtime |
| 61 | + return lock_age > _LOCK_TIMEOUT_SECONDS |
| 62 | + |
| 63 | + |
| 64 | +def ensure_converted_pb(source: Path, output: Path) -> Path: |
| 65 | + """Convert ``source`` into ``output`` only when the target is missing or stale. |
| 66 | +
|
| 67 | + The conversion is protected by a simple lock file and uses atomic replacement so |
| 68 | + repeated imports across multiple test modules do not regenerate the same model |
| 69 | + more than once. |
| 70 | + """ |
| 71 | + source = source.resolve() |
| 72 | + output = output.resolve() |
| 73 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 74 | + lock_file = output.with_name(f".{output.name}.lock") |
| 75 | + started = time.monotonic() |
| 76 | + |
| 77 | + while True: |
| 78 | + if _is_up_to_date(source, output): |
| 79 | + return output |
| 80 | + try: |
| 81 | + fd = os.open(str(lock_file), os.O_CREAT | os.O_EXCL | os.O_WRONLY) |
| 82 | + except FileExistsError as err: |
| 83 | + if _should_break_stale_lock(lock_file): |
| 84 | + lock_file.unlink(missing_ok=True) |
| 85 | + continue |
| 86 | + if time.monotonic() - started >= _LOCK_TIMEOUT_SECONDS: |
| 87 | + raise TimeoutError(f"Timed out waiting for {lock_file}") from err |
| 88 | + time.sleep(_LOCK_POLL_SECONDS) |
| 89 | + continue |
| 90 | + break |
| 91 | + |
| 92 | + tmp_path: Path | None = None |
| 93 | + try: |
| 94 | + with os.fdopen(fd, "w", encoding="utf-8") as handle: |
| 95 | + handle.write(f"pid={os.getpid()}\n") |
| 96 | + |
| 97 | + if _is_up_to_date(source, output): |
| 98 | + return output |
| 99 | + |
| 100 | + tmp_fd, tmp_name = tempfile.mkstemp( |
| 101 | + dir=output.parent, |
| 102 | + prefix=f".{output.name}.", |
| 103 | + ) |
| 104 | + os.close(tmp_fd) |
| 105 | + tmp_path = Path(tmp_name) |
| 106 | + sp.run( |
| 107 | + [ |
| 108 | + sys.executable, |
| 109 | + "-m", |
| 110 | + "deepmd", |
| 111 | + "convert-from", |
| 112 | + "pbtxt", |
| 113 | + "-i", |
| 114 | + str(source), |
| 115 | + "-o", |
| 116 | + str(tmp_path), |
| 117 | + ], |
| 118 | + check=True, |
| 119 | + ) |
| 120 | + tmp_path.replace(output) |
| 121 | + tmp_path = None |
| 122 | + return output |
| 123 | + finally: |
| 124 | + if tmp_path is not None: |
| 125 | + tmp_path.unlink(missing_ok=True) |
| 126 | + lock_file.unlink(missing_ok=True) |
0 commit comments