Skip to content

feat(c++,pt-expt): add DeepPotModelDevi C++ tests for .pt2 backend#5342

Open
wanghan-iapcm wants to merge 2 commits intodeepmodeling:masterfrom
wanghan-iapcm:feat-pt-expt-c-modeldevi
Open

feat(c++,pt-expt): add DeepPotModelDevi C++ tests for .pt2 backend#5342
wanghan-iapcm wants to merge 2 commits intodeepmodeling:masterfrom
wanghan-iapcm:feat-pt-expt-c-modeldevi

Conversation

@wanghan-iapcm
Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm commented Mar 26, 2026

Summary

  • Add C++ DeepPotModelDevi unit tests for the pt_expt (.pt2) backend
  • Generate two SE(A)+fparam/aparam .pt2 models with different seeds (gen_model_devi.py) for meaningful deviations
  • Both models include default_fparam=[0.25852028] for attribute detection tests

Tests added (14 total: 2 test classes × {double, float})

TestInferDeepPotModeDeviPtExpt (6 tests per type):

  • attrs — cutoff, numb_types, dim_fparam==1, dim_aparam==1, has_default_fparam
  • cpu_build_nlist / cpu_build_nlist_atomic — individual dp0/dp1 vs model_devi consistency
  • cpu_lmp_list / cpu_lmp_list_atomic — same with LAMMPS nlist + fold_back
  • cpu_lmp_list_std — manual vs API compute_avg, compute_std_e, compute_std_f, compute_relative_std_f

TestInferDeepPotModeDeviPtExptPrecomputed (1 test per type):

  • cpu_lmp_list_std — max/min/avg std_f and max/min/mystd std_v against hardcoded reference values

Known limitations

  • default_fparam compute path is not tested: DeepPotPTExpt::compute creates a zero-size tensor when fparam is empty, which crashes the .pt2 runner. The C++ API supports has_default_fparam() detection but not automatic value substitution.

Test plan

  • gen_model_devi.py generates two .pt2 models without errors
  • All 14 TYPED_TEST cases pass for both double and float
  • Precomputed deviation values match
  • No regressions in existing 94 PtExpt tests (108 total pass)
  • No compiler warnings
  • Pre-commit hooks pass

Summary by CodeRabbit

  • Tests
    • Added a comprehensive test suite validating multi-model deviation inference and statistical outputs for the PyTorch-enabled backend.
    • Added an automated artifact-generation step to produce paired model artifacts used for deviation comparisons.
    • Updated test setup to run the new generation step and conditionally enable these tests only when PyTorch support is available.

Add model deviation tests using two SE(A)+fparam/aparam .pt2 models with
different seeds. Tests cover attrs, build_nlist, lmp_list, atomic variants,
and std/avg/relative_std deviation statistics with precomputed references.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 26, 2026

📝 Walkthrough

Walkthrough

Adds end-to-end test support for DeepPotModelDevi with the PyTorch .pt2 backend: a new C++ gtest validating inference and statistics, a Python generator creating paired .pt2 models and reference vectors, and a test runner hook to generate artifacts when PyTorch is enabled.

Changes

Cohort / File(s) Summary
C++ Test
source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc
New GoogleTest translation unit validating DeepPotModelDevi metadata, neighbor-list inference (standard and atomic), and statistical APIs; conditionally skipped when PyTorch is disabled.
Python generator
source/tests/infer/gen_model_devi.py
New executable script that builds two SE(A)-based models with different seeds, serializes them to .pt2, runs inference on fixed inputs, and prints C++-style reference vectors and deviation statistics.
Test invocation
source/install/test_cc_local.sh
Added conditional invocation of the Python generator when ENABLE_PYTORCH is TRUE to produce .pt2 artifacts prior to running C++ tests.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • iProzd
  • njzjz
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely summarizes the main change: adding DeepPotModelDevi C++ tests for the .pt2 backend, which matches the primary objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc`:
- Around line 56-72: The test asserts dp_md.has_default_fparam() but never
exercises the default-parameter path; add a short subcase inside the
TestInferDeepPotModeDeviPtExpt(attrs) test that calls the
DeepPotModelDevi::compute(...) overload that omits fparam (i.e., invoke
dp_md.compute(...) with the same inputs but without passing fparam) and assert
the expected behavior (e.g., no throw and outputs match the explicit-fparam call
or whatever the regression expects). Locate dp_md and the existing compute calls
in this test and add the empty-fparam invocation and corresponding EXPECT_*
checks to lock down the real backend behavior.
- Around line 6-17: The file defines EPSILON using std::is_same (and VALUETYPE)
but does not include <type_traits>, relying on transitive includes; add a direct
`#include` <type_traits> near the other includes so std::is_same resolves reliably
for the EPSILON macro definition (refer to the EPSILON macro and the use of
std::is_same/VALUETYPE).

In `@source/tests/infer/gen_model_devi.py`:
- Around line 137-144: The test unpacks multiple values from dp.eval and later
in the generator many of those bindings are unused (v, e0, ae0, av0, e1, ae1,
av1) and the loop index is unused, causing Ruff errors; rename any unused
variables returned by dp.eval (e.g., v -> _v, e0 -> _e0, ae0 -> _ae0, av0 ->
_av0, e1 -> _e1, ae1 -> _ae1, av1 -> _av1) or remove/underscore the unpacked
names you don't use, and if an enumerate loop index is unused replace
enumerate(...) with iterating values only (or rename the index to _i) so the
generator in gen_model_devi.py (the dp.eval unpack and the loop around line
~200) passes ruff check.
🪄 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: 17471cf5-6361-47c2-8791-1ea8d8b66149

📥 Commits

Reviewing files that changed from the base of the PR and between e97967b and d9f317f.

📒 Files selected for processing (3)
  • source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc
  • source/install/test_cc_local.sh
  • source/tests/infer/gen_model_devi.py

@codecov
Copy link

codecov bot commented Mar 26, 2026

Codecov Report

❌ Patch coverage is 80.22923% with 69 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.27%. Comparing base (e97967b) to head (3cb2b4c).

Files with missing lines Patch % Lines
...rce/api_cc/tests/test_deeppot_model_devi_ptexpt.cc 80.22% 69 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5342      +/-   ##
==========================================
- Coverage   82.28%   82.27%   -0.02%     
==========================================
  Files         797      798       +1     
  Lines       82100    82450     +350     
  Branches     4003     4050      +47     
==========================================
+ Hits        67557    67835     +278     
- Misses      13336    13406      +70     
- Partials     1207     1209       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

All other gen scripts import from deepmd.pt.utils.serialization before
calling load_custom_ops(), which loads the installed libdeepmd_op_pt.so
first. The guard in load_custom_ops() then skips the build-dir copy.

gen_model_devi.py was missing this import, so load_custom_ops() loaded
the build-dir .so first, then the later deepmd.infer import loaded the
installed .so — triggering a duplicate op registration crash.
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
source/tests/infer/gen_model_devi.py (1)

140-140: ⚠️ Potential issue | 🟠 Major

Fix unresolved Ruff violations for unused unpacked values and loop index.

v, e0, ae0, av0, e1, ae1, av1, and kk are unused, which triggers RUF059/B007 and can fail CI.

♻️ Proposed fix
-        e, f, v, ae, av = dp.eval(
+        e, f, _v, ae, av = dp.eval(
@@
-    e0, f0, v0, ae0, av0 = dp0.eval(
+    _e0, f0, v0, _ae0, _av0 = dp0.eval(
         coord, box, atype, fparam=fparam_val, aparam=aparam_val, atomic=True
     )
-    e1, f1, v1, ae1, av1 = dp1.eval(
+    _e1, f1, v1, _ae1, _av1 = dp1.eval(
         coord, box, atype, fparam=fparam_val, aparam=aparam_val, atomic=True
     )
@@
-        for kk, fk in enumerate([f0_flat, f1_flat]):
+        for fk in (f0_flat, f1_flat):
             for dd in range(3):

As per coding guidelines, **/*.py: Always run ruff check . and ruff format . before committing changes or CI will fail.

Also applies to: 184-187, 203-203

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@source/tests/infer/gen_model_devi.py` at line 140, Unresolved Ruff violations
come from unused unpacked variables (v, e0, ae0, av0, e1, ae1, av1) and an
unused loop index kk; update the unpacking and loop to use underscore
placeholders or underscore-prefixed names to mark them as intentionally unused.
For example, change the dp.eval(...) unpack to use underscores for unused slots
(e.g., _, f, _, ae, _ = dp.eval(...)) and rename loop index kk to _ or _kk where
it isn't used; apply the same underscore pattern to the other unpackings
referenced (the blocks around the unpackings that create e0/ae0/av0 and
e1/ae1/av1) to satisfy RUF059/B007. Ensure you only change the variable names
where they are unused so real uses (like f, ae) remain intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@source/tests/infer/gen_model_devi.py`:
- Line 140: Unresolved Ruff violations come from unused unpacked variables (v,
e0, ae0, av0, e1, ae1, av1) and an unused loop index kk; update the unpacking
and loop to use underscore placeholders or underscore-prefixed names to mark
them as intentionally unused. For example, change the dp.eval(...) unpack to use
underscores for unused slots (e.g., _, f, _, ae, _ = dp.eval(...)) and rename
loop index kk to _ or _kk where it isn't used; apply the same underscore pattern
to the other unpackings referenced (the blocks around the unpackings that create
e0/ae0/av0 and e1/ae1/av1) to satisfy RUF059/B007. Ensure you only change the
variable names where they are unused so real uses (like f, ae) remain intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f10a1f7b-5d59-4bb2-91ab-8684d6404121

📥 Commits

Reviewing files that changed from the base of the PR and between d9f317f and 3cb2b4c.

📒 Files selected for processing (1)
  • source/tests/infer/gen_model_devi.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant