Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pt): Support atomic property fitting #4642

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from

Conversation

Chengqian-Zhang
Copy link
Collaborator

@Chengqian-Zhang Chengqian-Zhang commented Mar 7, 2025

See discussion #4603
One may want to use DeepMD-kit to fit per-atom properties, such as charge, magnetization for each atom.

Summary by CodeRabbit

  • New Features
    • Enhanced the loss computation process with additional configuration options, offering more granular control over scaling factors for property-related metrics.
    • Expanded metric tracking to include extra performance indicators for both general and atomic properties.
    • Improved consistency in applying learning rate parameters across training processes, contributing to a more stable training experience.

@Chengqian-Zhang Chengqian-Zhang marked this pull request as draft March 7, 2025 09:47
@github-actions github-actions bot added the Python label Mar 7, 2025
Copy link
Contributor

coderabbitai bot commented Mar 7, 2025

📝 Walkthrough

Walkthrough

This pull request updates the loss computation workflow for property predictions. The changes modify the PropertyLoss class to include new parameters for scaling property and atomic property losses, update assertions and metric handling, and adjust label requirements accordingly. In addition, the training module’s loss retrieval function now explicitly handles the learning rate for property loss, and the argument checking utility is extended with new parameters to support these modifications.

Changes

File Change Summary
deepmd/pt/loss/property.py Updated PropertyLoss: added new parameters (starter_learning_rate, start_pref_property, limit_pref_property, start_pref_aproperty, limit_pref_aproperty), introduced assertions, updated the forward logic for scaling loss, and expanded metrics.
deepmd/pt/train/training.py Modified get_loss to assign starter_learning_rate for the "property" loss type.
deepmd/utils/argcheck.py Extended the loss_property function signature by adding four new arguments for property and atomic property preferences with default values.

Sequence Diagram(s)

sequenceDiagram
    participant T as Training Module
    participant L as PropertyLoss
    participant A as Argcheck Utility

    T->>L: Instantiate PropertyLoss with new parameters
    L->>L: Validate parameters & initialize scaling factors
    T->>A: Retrieve loss configuration parameters
    L->>L: Compute loss in forward() using prefactors
    L->>T: Return loss value and updated metrics
Loading

Possibly related PRs

Suggested labels

LAMMPS

Suggested reviewers

  • njzjz
  • wanghan-iapcm
✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

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

🧹 Nitpick comments (3)
deepmd/utils/argcheck.py (1)

2745-2798: Enhance the clarity of usage and defaults for the new property parameters.

The documentation and defaults for start_pref_property, limit_pref_property, start_pref_aproperty, and limit_pref_aproperty look good. However, consider clarifying in the docstrings how the behavior changes if a user sets one of these parameters to zero (e.g., disabling partial training on that axis or phasing it out over time). Providing explicit examples can help users avoid unintentional zero weighting.

deepmd/pt/loss/property.py (2)

26-32: Guard against zero or negative starter_learning_rate.

By design, the code uses learning_rate / self.starter_learning_rate to compute a scaling coefficient. Consider adding an assertion that starter_learning_rate > 0.0 to prevent potential division by zero or negative weighting scenarios.


79-85: Fix typographical error and validate edge cases.

  1. There is a small spelling error in the assertion message: "Can not assian zero weight..." → "Can not assign zero weight...".
  2. Consider clarifying whether zero is allowed if a user deliberately wants to disable both property and aproperty after some schedule, or strictly forbid it under all circumstances.
-            "Can not assian zero weight both to `pref` and `pref_atomic`"
+            "Can not assign zero weight both to `pref` and `pref_atomic`"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5aa7a8f and 1086fdb.

📒 Files selected for processing (3)
  • deepmd/pt/loss/property.py (5 hunks)
  • deepmd/pt/train/training.py (1 hunks)
  • deepmd/utils/argcheck.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Test C++ (false)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Analyze (python)
🔇 Additional comments (8)
deepmd/pt/train/training.py (1)

1269-1269: Properly added learning rate for property loss

The change provides consistent handling of the learning rate parameter for the property loss type, aligning it with the implementation of other loss types in the function. This is an essential component for supporting atomic property fitting.

deepmd/pt/loss/property.py (7)

65-69: Looks correct.

Assigning the new instance variables is straightforward and consistent with the incoming constructor parameters.


86-91: Verify partial usage scenarios for property vs. aproperty.

The assertion ensures that at least one of property or atomic property is enabled. However, if users set start_pref_property != 0.0 but limit_pref_property = 0.0, the code will consider that property “disabled” for the entire training, because the boolean check requires both to be nonzero. Verify that this logic matches intended partial usage behavior and does not confuse users expecting a gradual fade to zero.


125-135: Check division by zero for starter_learning_rate.

When computing the coefficient (coef = learning_rate / self.starter_learning_rate), a zero or near-zero starter_learning_rate could lead to NaN or infinite values. Ensure that the caller or constructor either enforces a positive starter_learning_rate or handles this edge case gracefully.


160-220: Property loss logic looks good.

The scaling factor application, standardization, and metric handling appear correct. No immediate performance or correctness issues noted.


221-285: Atomic property loss logic looks good.

This block correctly mirrors the property case for atomic properties. The approach to standardization and separate metrics is cleanly implemented.


292-302: Appropriate label requirement for atomic property.

Adding a data requirement item for the atomic variant of the property ensures correct hooking of labels. The design is consistent with the new atomic property logic.


303-311: Label requirement for standard property fits well.

Retaining a separate requirement item for the non-atomic property preserves backward compatibility and clarity. Implementation looks good.

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.

1 participant