feat: dp test stress#5588
Conversation
📝 WalkthroughWalkthroughEnergy testing now computes stress from virial and cell volume for PBC cases, writes stress detail files, reports stress MAE/RMSE in per-test and aggregate summaries, and adds PT and TF regression tests for the new outputs. ChangesEnergy-test stress support
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@source/tests/pt/test_dp_test.py`:
- Around line 411-430: The temporary model file handle in the test setup is left
open after tempfile.NamedTemporaryFile(delete=False), which breaks Windows
compatibility for torch.jit.save and later os.unlink. In the test around
dp_test_ener, close the NamedTemporaryFile handle immediately after creating
tmp_model and before calling torch.jit.save, then continue using tmp_model.name
for DeepEval and cleanup so the file can be written and deleted reliably on all
platforms.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: e22fbaf7-cd5a-43b7-b046-8b88ff0292bf
📒 Files selected for processing (3)
deepmd/entrypoints/test.pysource/tests/pt/test_dp_test.pysource/tests/tf/test_dp_test.py
njzjz-bot
left a comment
There was a problem hiding this comment.
Thanks for adding stress reporting to dp test — this is a useful output to have. I found a couple of things that I think should be addressed before merging:
-
Stress sign convention looks inconsistent with the existing DeePMD API. The new code currently writes
prediction_stress = virial / volume reference_stress = test_data["virial"][:numb_test] / volume
but
deepmd/calculator.pyconverts virial to ASE stress asstress = -virial / volume(with the usual tensile-positive convention). The sign will cancel out in MAE/RMSE because both prediction and reference are transformed the same way, but the new.s.outdetail file is user-facing and would report the opposite sign from the existingstressAPI. If this file is meant to be mechanical stress, please use-virial / volumeand update the PT/TF tests accordingly; if it is intentionally virial density, I would avoid naming itstress. -
Please close the temporary model file before saving in the new PT test.
tempfile.NamedTemporaryFile(delete=False, suffix=".pth")leaves the file handle open, andtorch.jit.save(model, tmp_model.name)can fail on Windows or make later cleanup unreliable. Something liketmp_model.close()immediately after creation (or usingmkstemp/TemporaryDirectoryand closing the fd) should be enough.
CI note: at the time of review, the C++ test aggregate is failing because one C++ job exited with 143, and several Python jobs are still pending.
Authored by OpenClaw (model: custom-chat-jinzhezeng-group/gpt-5.5)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5588 +/- ##
==========================================
+ Coverage 82.27% 82.29% +0.01%
==========================================
Files 887 887
Lines 100331 100452 +121
Branches 4060 4059 -1
==========================================
+ Hits 82550 82667 +117
+ Misses 16320 16318 -2
- Partials 1461 1467 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
source/tests/pt/test_dp_test.py (2)
54-73: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick winMake temp-model cleanup exception-safe.
If
torch.jit.save,dp_test,DeepEval, ordp_test_enerraises,os.unlink(tmp_model_path)is skipped and the.pthfile leaks into the temp directory. This pattern is repeated at every new temp-model call site here, so it should be wrapped intry/finally(or a small helper/context manager) once and reused.Suggested pattern
tmp_fd, tmp_model_path = tempfile.mkstemp(suffix=".pth") os.close(tmp_fd) - torch.jit.save(model, tmp_model_path) - dp_test(...) - os.unlink(tmp_model_path) + try: + torch.jit.save(model, tmp_model_path) + dp_test(...) + finally: + os.unlink(tmp_model_path)Also applies to: 202-217, 258-273, 327-374, 415-435, 486-500
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@source/tests/pt/test_dp_test.py` around lines 54 - 73, The temporary model cleanup in the test helpers is not exception-safe, so a failed torch.jit.save, dp_test, DeepEval, or dp_test_ener call can leave .pth files behind. Update the repeated temp-model call sites in test_dp_test.py to use a try/finally block or a shared helper/context manager so os.unlink(tmp_model_path) always runs after torch.jit.save and the downstream test invocation. Apply the same pattern consistently across the dp_test, DeepEval, and dp_test_ener sections to remove the duplicated leak-prone cleanup logic.
382-382: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winUpdate the stress sign in the docstring.
The class docstring still documents
sigma = virial / volume, but the test now validates-virial / volume. Leaving the sign convention stale here will mislead the next person touching this regression.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@source/tests/pt/test_dp_test.py` at line 382, Update the docstring for the stress regression test to match the current sign convention: it should describe the ``dp test`` stress output as ``-virial / volume`` instead of ``virial / volume``. Locate the test by its stress-verification docstring in the ``dp test`` regression case and adjust the wording so it stays aligned with the assertion logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@source/tests/pt/test_dp_test.py`:
- Around line 54-73: The temporary model cleanup in the test helpers is not
exception-safe, so a failed torch.jit.save, dp_test, DeepEval, or dp_test_ener
call can leave .pth files behind. Update the repeated temp-model call sites in
test_dp_test.py to use a try/finally block or a shared helper/context manager so
os.unlink(tmp_model_path) always runs after torch.jit.save and the downstream
test invocation. Apply the same pattern consistently across the dp_test,
DeepEval, and dp_test_ener sections to remove the duplicated leak-prone cleanup
logic.
- Line 382: Update the docstring for the stress regression test to match the
current sign convention: it should describe the ``dp test`` stress output as
``-virial / volume`` instead of ``virial / volume``. Locate the test by its
stress-verification docstring in the ``dp test`` regression case and adjust the
wording so it stays aligned with the assertion logic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 82ab4add-838f-4120-81fa-49bbeac8a153
📒 Files selected for processing (3)
deepmd/entrypoints/test.pysource/tests/pt/test_dp_test.pysource/tests/tf/test_dp_test.py
🚧 Files skipped from review as they are similar to previous changes (2)
- source/tests/tf/test_dp_test.py
- deepmd/entrypoints/test.py
Summary by CodeRabbit
New Features
Bug Fixes
Tests