Skip to content

Conversation

@njzjz
Copy link
Member

@njzjz njzjz commented May 26, 2025

  1. dpmodel has different model output keys;
  2. in the current code, energy, force, virial, etc are necessary keys.
  3. update more_loss

Summary by CodeRabbit

  • Bug Fixes
    • Adjusted internal handling and reporting of energy, force, virial, and atomic energy loss metrics for improved clarity and consistency.
  • Tests
    • Updated test cases to align with the new prediction key structure, ensuring continued accuracy and reliability of energy loss evaluations.

1. dpmodel has different model output keys;
2. in the current code, energy, force, virial, etc are necessary keys.

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn>
@njzjz njzjz requested review from Copilot and wanghan-iapcm May 26, 2025 13:39
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 updates the dpmodel loss components to use new output key names and ensures that energy, force, virial, and atom energies are correctly mapped.

  • Updated the test suite to use a new prediction mapping dictionary (predict_dpmodel_style)
  • Re-mapped keys in loss computation in ener.py to match the new dpmodel output
  • Removed conditional checks in label_requirement to enforce mandatory keys

Reviewed Changes

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

File Description
source/tests/consistent/loss/test_ener.py Introduces a new prediction mapping (predict_dpmodel_style) to test dpmodel outputs using new key names
deepmd/dpmodel/loss/ener.py Adjusts the key mappings in the loss function and unconditionally requires related keys in label_requirement
Comments suppressed due to low confidence (1)

deepmd/dpmodel/loss/ener.py:268

  • Since the conditional check has been removed, update the function documentation in label_requirement to reflect that energy, force, virial, and atom energy keys are now mandatory.
-        if self.has_e:

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 26, 2025

📝 Walkthrough
## Walkthrough

The changes update the keys used to access model outputs and label requirements in the `EnergyLoss` class and its corresponding tests. The new keys reflect a different expected output structure from the model, and the test code is updated to use these keys when preparing prediction dictionaries for evaluation.

## Changes

| File(s)                                   | Change Summary                                                                                                     |
|-------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| deepmd/dpmodel/loss/ener.py                | Updated `EnergyLoss` to use new model output keys (`energy_redu`, `energy_derv_r`, `energy_derv_c_redu`, `energy`) in `call` and `label_requirement`. Renamed loss reporting keys and added total RMSE key. |
| source/tests/consistent/loss/test_ener.py | Updated test class to use a new prediction dictionary (`predict_dpmodel_style`) with the new key scheme in relevant evaluation methods.                |

## Sequence Diagram(s)

```mermaid
sequenceDiagram
    participant Test as TestEner
    participant Model as EnergyLoss
    participant ModelOutput as Model Output Dict

    Test->>Model: call(model_dict with new keys)
    Model->>ModelOutput: Access energy_redu, energy_derv_r, energy_derv_c_redu, energy
    ModelOutput-->>Model: Return values
    Model-->>Test: Return loss calculation

Possibly related PRs

Suggested reviewers

  • njzjz
  • wanghan-iapcm
  • iProzd

</details>

<!-- walkthrough_end -->


---

<details>
<summary>📜 Recent review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**


<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 2f5a91e1a9e66d7cc2fbc9f0af9eecc59ec69460 and 841fc342f430b1303ac89267bf8ba44a969b8dbe.

</details>

<details>
<summary>📒 Files selected for processing (1)</summary>

* `deepmd/dpmodel/loss/ener.py` (7 hunks)

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧬 Code Graph Analysis (1)</summary>

<details>
<summary>deepmd/dpmodel/loss/ener.py (4)</summary><blockquote>

<details>
<summary>deepmd/dpmodel/loss/loss.py (2)</summary>

* `display_if_exist` (38-54)
* `label_requirement` (34-35)

</details>
<details>
<summary>deepmd/pt/loss/loss.py (2)</summary>

* `display_if_exist` (36-46)
* `label_requirement` (31-33)

</details>
<details>
<summary>deepmd/pd/loss/ener.py (2)</summary>

* `label_requirement` (410-485)
* `label_requirement` (594-607)

</details>
<details>
<summary>deepmd/utils/data.py (1)</summary>

* `DataRequirementItem` (742-822)

</details>

</blockquote></details>

</details>

</details>

<details>
<summary>🔇 Additional comments (3)</summary><blockquote>

<details>
<summary>deepmd/dpmodel/loss/ener.py (3)</summary>

`180-180`: **Approve the loss key renaming for better consistency.**

The renaming of loss keys from `"l2_*_loss"` to `"rmse_*"` format provides better semantic clarity and consistency. The changes correctly maintain the same underlying calculations while improving the naming convention.




Also applies to: 192-192, 208-208, 224-226, 233-235, 255-255

---

`258-258`: <details>
<summary>❓ Verification inconclusive</summary>

**Verify the mathematical correctness of the new "rmse" calculation.**

The addition of `more_loss["rmse"] = xp.sqrt(loss)` calculates the root mean square error of the total loss. 


Please verify this is mathematically correct for representing RMSE, as `loss` already contains weighted sums of different loss components:

---

🏁 Script executed:

```shell
#!/bin/bash
# Description: Check how the total loss is used elsewhere in the codebase

echo "=== Checking usage of total loss/rmse in other components ==="
rg -A 5 -B 5 'more_loss\["rmse"\]' --type py
rg -A 5 -B 5 'l2_more\["rmse"\]' --type py
rg -A 5 -B 5 'rmse.*loss' --type py

Length of output: 41408


Verify weighted RMSE vs. true RMSE
The current line

more_loss["rmse"] = xp.sqrt(loss)

computes √(∑ᵢ wᵢ·MSEᵢ) rather than a conventional RMSE (√(∑ errors² / N)). This matches the Paddle/PyTorch backends but only yields a “weighted RMSE” when your component weights (pref_e, pref_f, etc.) don’t sum to 1.

Please confirm whether you intend to:

  • Display a weighted RMSE (i.e., √(∑wᵢ·MSEᵢ)), or
  • Compute a true overall RMSE by normalizing (e.g., xp.sqrt(loss / total_weight) or dividing by the total sample count).

If a pure RMSE is desired, adjust the formula accordingly.

Locations to review:

  • deepmd/dpmodel/loss/ener.py: more_loss["rmse"] = xp.sqrt(loss)
  • deepmd/pt/loss/ener.py: more_loss["rmse"] = torch.sqrt(loss.detach())
  • deepmd/pd/loss/ener.py: more_loss["rmse"] = paddle.sqrt(loss.detach())

266-311:

Details

❓ Verification inconclusive

Review the impact of removing conditional checks in label_requirement.

The label requirements are now added unconditionally, removing the previous conditional logic based on has_e, has_f, etc. This differs significantly from the pattern shown in the relevant code snippet from deepmd/pd/loss/ener.py.

This change may impact performance and data loading behavior. Please verify:

  1. Whether this aligns with the intended dpmodel behavior
  2. The impact on systems that don't have all these properties

🏁 Script executed:

#!/bin/bash
# Description: Compare label_requirement implementations across different loss modules

echo "=== Checking other loss implementations for conditional patterns ==="
rg -A 20 -B 5 'def label_requirement' --type py
rg -A 10 'has_e.*has_f.*has_v' --type py

echo "=== Checking if dpmodel requires different handling ==="
rg -A 10 -B 5 'dpmodel.*label.*requirement' --type py

Length of output: 25223


Review deepmd/dpmodel/loss/ener.py – Unconditional label_requirement additions

The label_requirement() method now unconditionally appends entries for energy, force, virial, atom_ener, and atom_pref, whereas other loss implementations (e.g. deepmd/pd/loss/ener.py) only include each DataRequirementItem when the corresponding has_* flag is True.

Please verify:

  • That DPModel is intended to always request these five labels (even if the dataset or model isn’t configured for virial/atom_ener/atom_pref).
  • The impact on data loading, performance, and downstream consumers when some of these properties are missing or unused.

• File: deepmd/dpmodel/loss/ener.py – label_requirement() (lines 266–311)

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link

codecov bot commented May 26, 2025

Codecov Report

❌ Patch coverage is 62.50000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.80%. Comparing base (75b175b) to head (841fc34).
⚠️ Report is 91 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/dpmodel/loss/ener.py 62.50% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4765      +/-   ##
==========================================
+ Coverage   84.79%   84.80%   +0.01%     
==========================================
  Files         698      698              
  Lines       67734    67742       +8     
  Branches     3540     3540              
==========================================
+ Hits        57432    57446      +14     
+ Misses       9171     9166       -5     
+ Partials     1131     1130       -1     

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

@njzjz njzjz marked this pull request as draft May 27, 2025 15:22
njzjz and others added 2 commits May 29, 2025 02:09
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
@njzjz njzjz marked this pull request as ready for review May 29, 2025 05:52
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue May 30, 2025
Merged via the queue into deepmodeling:devel with commit 51f61cd May 30, 2025
61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants