-
Notifications
You must be signed in to change notification settings - Fork 607
test(lmp): avoid duplicate pb conversions in tests #5400
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
Open
njzjz-bot
wants to merge
8
commits into
deepmodeling:master
Choose a base branch
from
njzjz-bot:fix/lmp-tests-avoid-duplicate-pb-convert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5d591e3
test(lmp): avoid duplicate pb conversions in tests
njzjz-bot 76fb1b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5756071
test(lmp): fix pre-commit fallout in conversion cache changes
njzjz-bot 549eda6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 06769a1
test(lmp): defer conversions until runtime guards pass
njzjz-bot 58d2142
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9826b61
test(lmp): address remaining review nits on conversion guards
njzjz-bot c416849
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Helpers for preparing converted TensorFlow graph files in LAMMPS tests.""" | ||
|
|
||
| from __future__ import ( | ||
| annotations, | ||
| ) | ||
|
|
||
| import errno | ||
| import os | ||
| import subprocess as sp | ||
| import sys | ||
| import tempfile | ||
| import time | ||
| from pathlib import ( | ||
| Path, | ||
| ) | ||
|
|
||
| _LOCK_TIMEOUT_SECONDS = 60.0 | ||
| _LOCK_POLL_SECONDS = 0.1 | ||
|
|
||
|
|
||
| def _is_up_to_date(source: Path, output: Path) -> bool: | ||
| return output.exists() and output.stat().st_mtime_ns >= source.stat().st_mtime_ns | ||
|
|
||
|
|
||
| def _read_lock_pid(lock_file: Path) -> int | None: | ||
| try: | ||
| for line in lock_file.read_text(encoding="utf-8").splitlines(): | ||
| if line.startswith("pid="): | ||
| return int(line.split("=", maxsplit=1)[1]) | ||
| except (FileNotFoundError, ValueError): | ||
| return None | ||
| return None | ||
|
|
||
|
|
||
| def _pid_is_running(pid: int) -> bool: | ||
| try: | ||
| os.kill(pid, 0) | ||
| except ProcessLookupError: | ||
| return False | ||
| except PermissionError: | ||
| return True | ||
| except OSError as err: | ||
| if err.errno == errno.ESRCH: | ||
| return False | ||
| raise | ||
| return True | ||
|
|
||
|
|
||
| def _should_break_stale_lock(lock_file: Path) -> bool: | ||
| try: | ||
| lock_stat = lock_file.stat() | ||
| except FileNotFoundError: | ||
| return False | ||
|
|
||
| lock_pid = _read_lock_pid(lock_file) | ||
| if lock_pid is not None: | ||
| return not _pid_is_running(lock_pid) | ||
|
|
||
| lock_age = time.time() - lock_stat.st_mtime | ||
| return lock_age > _LOCK_TIMEOUT_SECONDS | ||
|
|
||
|
|
||
| def ensure_converted_pb(source: Path, output: Path) -> Path: | ||
| """Convert ``source`` into ``output`` only when the target is missing or stale. | ||
|
|
||
| The conversion is protected by a simple lock file and uses atomic replacement so | ||
| repeated imports across multiple test modules do not regenerate the same model | ||
| more than once. | ||
| """ | ||
| source = source.resolve() | ||
| output = output.resolve() | ||
| output.parent.mkdir(parents=True, exist_ok=True) | ||
| lock_file = output.with_name(f".{output.name}.lock") | ||
| started = time.monotonic() | ||
|
|
||
| while True: | ||
| if _is_up_to_date(source, output): | ||
| return output | ||
| try: | ||
| fd = os.open(str(lock_file), os.O_CREAT | os.O_EXCL | os.O_WRONLY) | ||
| except FileExistsError as err: | ||
| if _should_break_stale_lock(lock_file): | ||
| lock_file.unlink(missing_ok=True) | ||
| continue | ||
| if time.monotonic() - started >= _LOCK_TIMEOUT_SECONDS: | ||
| raise TimeoutError(f"Timed out waiting for {lock_file}") from err | ||
| time.sleep(_LOCK_POLL_SECONDS) | ||
|
njzjz-bot marked this conversation as resolved.
|
||
| continue | ||
| break | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| tmp_path: Path | None = None | ||
| try: | ||
| with os.fdopen(fd, "w", encoding="utf-8") as handle: | ||
| handle.write(f"pid={os.getpid()}\n") | ||
|
|
||
| if _is_up_to_date(source, output): | ||
| return output | ||
|
|
||
| tmp_fd, tmp_name = tempfile.mkstemp( | ||
| dir=output.parent, | ||
| prefix=f".{output.name}.", | ||
| ) | ||
| os.close(tmp_fd) | ||
| tmp_path = Path(tmp_name) | ||
| sp.run( | ||
| [ | ||
| sys.executable, | ||
| "-m", | ||
| "deepmd", | ||
| "convert-from", | ||
| "pbtxt", | ||
| "-i", | ||
| str(source), | ||
| "-o", | ||
| str(tmp_path), | ||
| ], | ||
| check=True, | ||
| ) | ||
| tmp_path.replace(output) | ||
| tmp_path = None | ||
| return output | ||
| finally: | ||
| if tmp_path is not None: | ||
| tmp_path.unlink(missing_ok=True) | ||
| lock_file.unlink(missing_ok=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.