Skip to content

fix(lmp): default fparam support#5311

Merged
iProzd merged 5 commits intodeepmodeling:masterfrom
iProzd:0312_test_lmp_3.2
Mar 15, 2026
Merged

fix(lmp): default fparam support#5311
iProzd merged 5 commits intodeepmodeling:masterfrom
iProzd:0312_test_lmp_3.2

Conversation

@iProzd
Copy link
Collaborator

@iProzd iProzd commented Mar 12, 2026

Summary by CodeRabbit

  • New Features

    • Public C API bumped to v26 and adds queries to check whether models use default frame parameters across model types and backends.
    • Backends and model interfaces now expose a consistent "has default fparam" status and fall back safely when backend hooks are absent.
  • Tests

    • Added extensive unit, integration, and LAMMPS tests plus a model fixture to validate default-frame-parameter behavior.

Copilot AI review requested due to automatic review settings March 12, 2026 12:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds end-to-end support for default frame parameters (“default fparam”) across the C++/C APIs and backends (notably PyTorch/JAX), enabling callers (e.g., LAMMPS paths) to omit fparam and have the model’s default applied.

Changes:

  • Add has_default_fparam() plumbing to the C++ backend interface and expose it through the C and deepmd.hpp wrapper APIs.
  • Update deepmd.hpp validation to allow an empty fparam when the model reports a default is available.
  • Add new inference fixtures/tests (C and C++/PyTorch) to validate empty-fparam behavior and backward compatibility with explicit fparam.

Reviewed changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
source/tests/infer/fparam_aparam_default.yaml Adds a model serialization fixture containing default_fparam.
source/api_cc/tests/test_deeppot_default_fparam_pt.cc New gtest coverage for PyTorch backend using empty vs explicit fparam.
source/api_cc/src/DeepSpinPT.cc Reads has_default_fparam from TorchScript during init.
source/api_cc/src/DeepPotPT.cc Reads has_default_fparam from TorchScript during init.
source/api_cc/src/DeepPotJAX.cc Reads has_default_fparam from exported JAX/TF functions when present.
source/api_cc/src/DeepBaseModel.cc Exposes DeepBaseModel::has_default_fparam() passthrough.
source/api_cc/include/DeepSpinTF.h Defines TF backend behavior for has_default_fparam() (always false).
source/api_cc/include/DeepSpinPT.h Adds has_default_fparam() API + member storage for PT backend.
source/api_cc/include/DeepPotTF.h Defines TF backend behavior for has_default_fparam() (always false).
source/api_cc/include/DeepPotPT.h Adds has_default_fparam() API + member storage for PT backend.
source/api_cc/include/DeepPotPD.h Defines Paddle backend behavior for has_default_fparam() (always false).
source/api_cc/include/DeepPotJAX.h Adds has_default_fparam() API + member storage for JAX backend.
source/api_cc/include/DeepBaseModel.h Extends backend interface with new pure virtual has_default_fparam().
source/api_c/tests/test_deeppot_default_fparam.cc New C-API wrapper test validating empty fparam uses defaults.
source/api_c/src/c_api.cc C API struct caching now includes has_default_fparam and exposes query functions.
source/api_c/include/deepmd.hpp Adds has_default_fparam() to wrapper and allows empty fparam when supported.
source/api_c/include/c_api_internal.h Extends C-API internal structs with has_default_fparam.
source/api_c/include/c_api.h Adds new C API query functions for default-fparam support.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 12, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0a81601b-b6f5-4fc1-add8-b5aa81deb40c

📥 Commits

Reviewing files that changed from the base of the PR and between 2e2f564 and c5bfd54.

📒 Files selected for processing (3)
  • source/api_c/src/c_api.cc
  • source/api_cc/src/DeepPotPT.cc
  • source/api_cc/src/DeepSpinPT.cc

📝 Walkthrough

Walkthrough

Adds a has_default_fparam flag and matching accessors across C and C++ APIs and backend implementations, bumps C API version to 26, and exposes new C-accessor functions. Includes unit tests, LAMMPS Python tests, and a YAML model fixture to validate default vs explicit fparam behavior.

Changes

Cohort / File(s) Summary
C API declarations
source/api_c/include/c_api.h, source/api_c/include/c_api_internal.h
Bump DP_C_API_VERSION 25→26; add has_default_fparam fields to DP base structs and declare six new DP_*HasDefaultFParam public accessors.
C API implementation
source/api_c/src/c_api.cc
Initialize has_default_fparam in DP wrapper constructors and implement new C-accessor functions returning the flag.
C++ base & interface
source/api_cc/include/DeepBaseModel.h, source/api_cc/src/DeepBaseModel.cc
Add DeepBaseModelBackend::has_default_fparam() pure virtual and DeepBaseModel::has_default_fparam() public delegate; DeepBaseModelDevi forwards to first dpbase.
C++ backend headers
source/api_cc/include/DeepPotPT.h, source/api_cc/include/DeepPotJAX.h, source/api_cc/include/DeepSpinPT.h
Add private has_default_fparam_ members and public has_default_fparam() accessors (with inited assertions) to track/expose flag.
C++ backend stubs
source/api_cc/include/DeepPotTF.h, source/api_cc/include/DeepPotPD.h, source/api_cc/include/DeepSpinTF.h
Add has_default_fparam() accessors that return false (backend-specific stubs).
C++ backend inits
source/api_cc/src/DeepPotJAX.cc, source/api_cc/src/DeepPotPT.cc, source/api_cc/src/DeepSpinPT.cc
During init, attempt to read has_default_fparam from model/module; fall back to false if missing to preserve compatibility.
API C++ wrappers & structs
source/api_c/include/deepmd.hpp, source/api_c/include/c_api_internal.h
Add/from-propagate has_default_fparam state and public accessor in C++ wrapper types; internal structs gain has_default_fparam field.
Tests: C/C++ / LAMMPS / Python
source/api_c/tests/test_deeppot_default_fparam.cc, source/api_cc/tests/test_deeppot_default_fparam_pt.cc, src/lmp/tests/test_lammps_faparam_pt.py
Add unit tests validating has_default_fparam and numeric behavior for empty vs explicit fparam; PyTorch/LAMMPS tests with backend guards.
Test model fixture
source/tests/infer/fparam_aparam_default.yaml
Add large YAML model fixture including fparam/aparam defaults and weights used by new tests.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Client
participant C_API
participant Cpp_Model
participant Backend
Client->>C_API: DP_DeepPotHasDefaultFParam(dp)
C_API->>Cpp_Model: dp->has_default_fparam()
Cpp_Model->>Backend: backend->has_default_fparam()
Backend-->>Cpp_Model: bool (rgba(0,128,0,0.5))
Cpp_Model-->>C_API: bool (rgba(0,128,0,0.5))
C_API-->>Client: bool (rgba(0,128,0,0.5))

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • wanghan-iapcm
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% 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 title accurately describes the main change: adding support for default fparam (frame parameters) in the LAMMPS integration, which is the central focus of this PR across multiple files.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

CodeRabbit can use OpenGrep to find security vulnerabilities and bugs across 17+ programming languages.

OpenGrep is compatible with Semgrep configurations. Add an opengrep.yml or semgrep.yml configuration file to your project to enable OpenGrep analysis.

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: 6

🤖 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/include/DeepPotTF.h`:
- Around line 213-217: The method has_default_fparam() should not
unconditionally return false; instead add a private bool member (e.g.,
has_default_fparam_cached_) to class DeepPotTF, set that flag during init() when
parsing the SavedModel (use whatever logic already inspects get_default_fparam()
/ serialized metadata in init()), and make has_default_fparam() return the
cached value; ensure init() updates the cache even when called multiple times
and default to false until init() runs.

In `@source/api_cc/include/DeepSpinTF.h`:
- Around line 179-183: The has_default_fparam() method in class DeepSpinTF
should not unconditionally return false; instead return the parsed model state
indicating whether default frame parameters were serialized. Update
DeepSpinTF::has_default_fparam() to consult the TF backend's parsed
metadata/member (e.g., the parsed flag or stored default_fparam_ state populated
when loading the SavedModel) and return that boolean; ensure consistency with
DeepSpinTF::get_default_fparam() so that if the model contained default fparam
v3.1.2+ data the API reports true and get_default_fparam() will be valid.

In `@source/api_cc/src/DeepPotJAX.cc`:
- Around line 330-335: The catch that sets has_default_fparam_ = false when
get_scalar<bool>(ctx, "has_default_fparam", func_vector, device, status) throws
misreports older exports; instead, on catching tf_function_not_found, attempt to
recover capability from persisted SavedModel metadata: call
get_scalar<string>(ctx, "jax_exporter_version", func_vector, device, status) (or
the equivalent exporter/version key used in your exports), parse the semver and
if the exporter version is older than 3.1.2 then read the persisted default
fparam value via get_scalar<bool>(ctx, "get_default_fparam" or your stored
default fparam key, using the same ctx/func_vector/device/status) and set
has_default_fparam_ accordingly (if that key is absent, choose a conservative
default and/or surface a clear compatibility warning); only default to false if
metadata proves the model lacks default fparam or no recovery is possible and
document this compatibility hole.

In `@source/api_cc/src/DeepPotPT.cc`:
- Around line 149-152: The try/catch currently using catch(...) around
module.run_method("has_default_fparam") hides real runtime/type errors; instead
check for method existence with module.find_method("has_default_fparam") (or
equivalent API) and only call module.run_method("has_default_fparam") if it
exists, setting has_default_fparam_ accordingly; if you must call and catch,
catch the specific missing-method exception type (matching the translate_error
pattern used elsewhere) rather than using catch(...), so real errors from the
scripted method are not silently converted to false.

In `@source/api_cc/src/DeepSpinPT.cc`:
- Around line 121-125: The catch-all around
module.run_method("has_default_fparam") masks real errors; change it to handle
only the method-not-found case (as in DeepPotJAX.cc) or rethrow via
translate_error() so real exceptions propagate. Locate the block that sets
has_default_fparam_ (module.run_method("has_default_fparam") and
has_default_fparam_) and replace the bare catch(...) with either a specific
exception type check for a missing method or call translate_error() inside the
catch to surface real errors while still treating a true "method absent"
condition as false.

In `@source/api_cc/tests/test_deeppot_default_fparam_pt.cc`:
- Around line 23-26: The coord vector initializer contains a likely-typo numeric
literal "00.25" inside the std::vector<VALUETYPE> coord initializer; update that
element to "0.25" in the coord initialization (the std::vector<VALUETYPE> coord
initializer in test_deeppot_default_fparam_pt.cc) so the value is written in
conventional numeric form.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: db69dfcd-284a-49ee-b67e-8b1073a4cf76

📥 Commits

Reviewing files that changed from the base of the PR and between a8d2a8d and 2a82802.

📒 Files selected for processing (19)
  • source/api_c/include/c_api.h
  • source/api_c/include/c_api_internal.h
  • source/api_c/include/deepmd.hpp
  • source/api_c/src/c_api.cc
  • source/api_c/tests/test_deeppot_default_fparam.cc
  • source/api_cc/include/DeepBaseModel.h
  • source/api_cc/include/DeepPotJAX.h
  • source/api_cc/include/DeepPotPD.h
  • source/api_cc/include/DeepPotPT.h
  • source/api_cc/include/DeepPotTF.h
  • source/api_cc/include/DeepSpinPT.h
  • source/api_cc/include/DeepSpinTF.h
  • source/api_cc/src/DeepBaseModel.cc
  • source/api_cc/src/DeepPotJAX.cc
  • source/api_cc/src/DeepPotPT.cc
  • source/api_cc/src/DeepSpinPT.cc
  • source/api_cc/tests/test_deeppot_default_fparam_pt.cc
  • source/tests/infer/fparam_aparam_default.pth
  • source/tests/infer/fparam_aparam_default.yaml

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 80.09259% with 43 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.30%. Comparing base (c1a50ed) to head (c5bfd54).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...rce/api_cc/tests/test_deeppot_default_fparam_pt.cc 80.76% 20 Missing ⚠️
source/api_c/tests/test_deeppot_default_fparam.cc 77.96% 13 Missing ⚠️
source/api_c/include/deepmd.hpp 70.00% 3 Missing ⚠️
source/api_c/src/c_api.cc 90.00% 2 Missing ⚠️
source/api_cc/src/DeepPotJAX.cc 50.00% 2 Missing ⚠️
source/api_cc/src/DeepSpinPT.cc 33.33% 1 Missing and 1 partial ⚠️
source/api_cc/src/DeepPotPT.cc 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5311      +/-   ##
==========================================
- Coverage   82.30%   82.30%   -0.01%     
==========================================
  Files         773      775       +2     
  Lines       77414    77627     +213     
  Branches     3659     3677      +18     
==========================================
+ Hits        63715    63888     +173     
- Misses      12528    12566      +38     
- Partials     1171     1173       +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.

@iProzd iProzd requested review from njzjz and wanghan-iapcm March 13, 2026 15:02
Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

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

For me it looks ok, but please carefully resolve the comment from the AI reviewers. They look reasonable.

@iProzd iProzd enabled auto-merge March 15, 2026 02:46
@iProzd iProzd added this pull request to the merge queue Mar 15, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 15, 2026
@iProzd iProzd added this pull request to the merge queue Mar 15, 2026
Merged via the queue into deepmodeling:master with commit 09345bf Mar 15, 2026
70 checks passed
@iProzd iProzd deleted the 0312_test_lmp_3.2 branch March 15, 2026 07:06
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.

4 participants