Skip to content

feat: support aparam derivative in ener loss#5285

Draft
anyangml wants to merge 12 commits intodeepmodeling:masterfrom
anyangml:feat/support-grad-aparam
Draft

feat: support aparam derivative in ener loss#5285
anyangml wants to merge 12 commits intodeepmodeling:masterfrom
anyangml:feat/support-grad-aparam

Conversation

@anyangml
Copy link
Collaborator

@anyangml anyangml commented Mar 4, 2026

No description provided.

Copilot AI review requested due to automatic review settings March 4, 2026 08:48
@anyangml anyangml marked this pull request as draft March 4, 2026 08:48
@github-actions github-actions bot added the Python label Mar 4, 2026
@dosubot dosubot bot added the new feature label Mar 4, 2026
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 support for aparam (atomic parameter) derivative loss training in the energy loss module of DeePMD-kit's PyTorch backend. It enables models to be trained against the derivative of the total energy with respect to atomic parameters (d(∑E)/d(aparam)), using labeled grad_aparam data.

Changes:

  • Adds start_pref_ap / limit_pref_ap prefactor arguments to the energy loss argcheck configuration
  • Implements the aparam gradient loss computation in EnergyStdLoss.__init__ and forward, including serialization of the new prefactors
  • Injects numb_aparam from the model into the loss parameters during training setup in get_loss()

Reviewed changes

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

File Description
deepmd/utils/argcheck.py Adds start_pref_ap and limit_pref_ap arguments to the loss_ener configuration schema
deepmd/pt/loss/ener.py Implements aparam gradient loss in EnergyStdLoss: new init parameters, forward computation via torch.autograd.grad, label requirement, and serialization
deepmd/pt/train/training.py Injects numb_aparam into loss parameters when aparam gradient loss is configured

💡 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.

Comment on lines +579 to +583
"enable_atom_ener_coeff": self.enable_atom_ener_coeff,
"start_pref_gf": self.start_pref_gf,
"limit_pref_gf": self.limit_pref_gf,
"start_pref_ap": self.start_pref_ap,
"limit_pref_ap": self.limit_pref_ap,
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The numb_aparam field is stored as an instance attribute (self.numb_aparam) and is required to reconstruct the loss object when has_ap is true, but it is not included in the serialize() return dict. When deserialize() calls cls(**data), the numb_aparam argument will be absent, causing the reconstructed object to default to numb_aparam=0. If start_pref_ap or limit_pref_ap is non-zero in the serialized data, the __init__ will then raise RuntimeError("numb_aparam must be > 0 when aparam gradient loss is enabled"), making deserialization impossible for models that use this feature.

Additionally, since a new field is added to the serialized representation, the @version should be bumped (e.g. to 3) and check_version_compatibility in deserialize() updated to check_version_compatibility(data.pop("@version"), 3, 1) to reflect the schema change, as is the established convention in the codebase.

Copilot uses AI. Check for mistakes.
Comment on lines 1688 to +1697
if whether_hessian(loss_params):
loss_params["starter_learning_rate"] = start_lr
return EnergyHessianStdLoss(**loss_params)
elif loss_type == "ener":
loss_params["starter_learning_rate"] = start_lr
if (
loss_params.get("start_pref_ap", 0.0) != 0.0
or loss_params.get("limit_pref_ap", 0.0) != 0.0
):
loss_params["numb_aparam"] = _model.get_dim_aparam()
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

When whether_hessian() returns True, the code falls into the first branch (line 1688-1690) and creates EnergyHessianStdLoss. The numb_aparam injection (lines 1693-1697) is in the elif loss_type == "ener" branch, which is skipped. As a result, if a user configures both start_pref_h > 0 and start_pref_ap != 0.0, the EnergyHessianStdLoss constructor will receive numb_aparam=0 (the default), causing the RuntimeError "numb_aparam must be > 0 when aparam gradient loss is enabled". The numb_aparam injection logic should be moved to cover both branches, or factored out into a shared helper.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.20%. Comparing base (3b24cf6) to head (efdf69e).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
deepmd/pt/infer/deep_eval.py 38.09% 13 Missing ⚠️
deepmd/infer/deep_pot.py 25.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5285      +/-   ##
==========================================
+ Coverage   82.16%   82.20%   +0.04%     
==========================================
  Files         753      755       +2     
  Lines       75865    76095     +230     
  Branches     3648     3660      +12     
==========================================
+ Hits        62335    62555     +220     
- Misses      12362    12368       +6     
- Partials     1168     1172       +4     

☔ 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.

anyangml and others added 2 commits March 4, 2026 17:40
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Anyang Peng <137014849+anyangml@users.noreply.github.com>
@anyangml anyangml requested a review from Copilot March 4, 2026 10:09
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

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


💡 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.

"limit_pref_gf": self.limit_pref_gf,
"start_pref_ap": self.start_pref_ap,
"limit_pref_ap": self.limit_pref_ap,
"numb_generalized_coord": self.numb_generalized_coord,
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

numb_aparam is not serialized in the serialize() method. Without it, deserialize() will pass numb_aparam=0 to __init__, causing a RuntimeError when has_ap is True. Add "numb_aparam": self.numb_aparam to the serialized dict.

Suggested change
"numb_generalized_coord": self.numb_generalized_coord,
"numb_generalized_coord": self.numb_generalized_coord,
"numb_aparam": self.numb_aparam,

Copilot uses AI. Check for mistakes.
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.

2 participants