fix(c-api): contain model deviation frame errors#5794
Conversation
Report unsupported model-deviation frame counts through the C API error state before accessing model, neighbor-list, or array inputs. Add coverage for every public multiframe variant and invalid representative counts. Coding-Agent: Codex Codex-Version: codex-cli 0.144.1 Model: gpt-5.6-sol Reasoning-Effort: xhigh
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughModel-deviation C API compute variants now reject frame counts other than one by recording an error and returning early. Tests verify error propagation across DeepPot and DeepSpin variants, including negative, zero, and multi-frame inputs. ChangesModel-deviation frame validation
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5794 +/- ##
==========================================
- Coverage 78.67% 78.44% -0.23%
==========================================
Files 1053 1053
Lines 120889 120983 +94
Branches 4383 4386 +3
==========================================
- Hits 95109 94907 -202
- Misses 24232 24520 +288
- Partials 1548 1556 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
wanghan-iapcm
left a comment
There was a problem hiding this comment.
Correct and thorough. The four model-devi helpers threw std::runtime_error from extern C entry points, so a C caller saw an exception unwind across the ABI instead of a retrievable error. Routing through validate_model_devi_nframes, which sets dp->exception and returns, matches the existing C-API error mechanism -- I confirmed DP_DeepPot/SpinModelDevi inherit DP_DeepBaseModelDevi and DP_DeepBaseModelDeviCheckOK returns string_to_char(dpbase->exception). Tightening nframes>1 to nframes!=1 is a real improvement: it rejects 0/negative before the std::vector(coord, coord+natoms*3) construction, which the -1/0 parametrized cases exercise against null pointers. The regression genuinely fails pre-fix (nframes=2 escapes the throw so ASSERT_NO_THROW fails; -1/0 would crash on null-pointer range construction) and covers all 12 nframes-accepting entry points across float/double, Pot/Spin, nlist/non-nlist, v2/v3. LGTM.
Dismissing to re-review through the /code-review skill per process.
wanghan-iapcm
left a comment
There was a problem hiding this comment.
Re-reviewed via the code-review skill. Correct and complete: the four model-devi helpers threw std::runtime_error from extern "C" entry points (UB across the C ABI, unretrievable); routing through validate_model_devi_nframes (sets dp->exception, returns) with nframes!=1 fixes it and also rejects 0/negative before any pointer-range construction. Verified DP_DeepPot/SpinModelDevi inherit DP_DeepBaseModelDevi and DP_DeepBaseModelDeviCheckOK returns string_to_char(exception). A reviewer built both versions and confirmed the tests fail pre-fix (12-variant ASSERT_NO_THROW failures; nframes=-1/0 SIGSEGV) and pass post-fix; all 12 nframes-accepting entry points are covered (exhaustive), and the accept path stays covered by the existing hpp tests. One non-blocking consistency note inline. LGTM.
Prefix direct model-deviation validation errors with the same DeePMD-kit Error marker used by exception-based C-API failures. Coding-Agent: Codex Codex-Version: codex-cli 0.144.4 Model: gpt-5.6-sol Reasoning-Effort: xhigh
|
Possible reviewers based on changed lines, exact file history, and exact-file review history:
No review request was made automatically. Coding agent: Codex |
Resolve the C API exception-test conflict by retaining both the model-deviation frame validation coverage and the newer InputNlist move-assignment regression. Coding-Agent: Codex Codex-Version: codex-cli 0.144.4 Model: gpt-5.6-sol Reasoning-Effort: xhigh
Summary
DP_*CheckOKinstead of throwing a C++ exception across the C ABInframes, including float/double, DeepPot/DeepSpin, neighbor-list/non-neighbor-list, and v2/v3 variantsRoot cause and fix
The four shared model-deviation helpers threw
std::runtime_errorfor multiple frames before reachingDP_REQUIRES_OK. Because that exception was outside the existing C API error boundary, a C caller could observe an unwinding C++ exception rather than a retrievable error string.This change adds one shared validation helper. Any
nframes != 1value now stores a precise error in the model-deviation object's shared exception state and returns immediately. Keeping the check at the top of each shared helper also makes zero and negative counts safe: no pointer range is constructed and no model or neighbor-list object is accessed.The four deprecated v1 APIs are not in the 12-entry regression matrix because they do not accept
nframesand always delegate with one frame.Regression coverage
The previous tests exercised C API exception formatting and model errors, but did not call the model-deviation compute entry points with an unsupported frame count. Consequently, they never crossed the exact boundary where
std::runtime_errorescaped.The new regression uses a fresh error carrier for every public entry point, asserts that no exception escapes, and verifies the exact
CheckOKmessage. On the previous implementation, thenframes = 2matrix fails immediately atASSERT_NO_THROW; the additional-1and0cases protect the early-validation invariant.Validation performed:
test_deepmd_exception.cc: 11 tests passedruff format --check .ruff check .git diff --checkCloses #5622.
Coding agent: Codex
Codex version: codex-cli 0.144.1
Model: gpt-5.6-sol
Reasoning effort: xhigh
Summary by CodeRabbit