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

fix(jax): handle DPA-2 pbc/nopbc without mapping #4363

Merged
merged 4 commits into from
Nov 19, 2024

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Nov 15, 2024

In the C++ API, generate the mapping for the no PBC and throw the error for PBC. Considering I forgot setting atom_modify map yes when testing it, others may also forget.

Summary by CodeRabbit

  • New Features

    • Introduced a function to determine if the model supports message passing, enhancing the model's interface.
    • Added a private member variable to facilitate message passing functionality.
    • Implemented unit tests for the DeepPot class, validating its functionality under various conditions.
  • Bug Fixes

    • Improved error handling for TensorFlow function retrieval, ensuring more specific exceptions are thrown.
    • Enhanced compatibility with earlier model versions by managing exceptions related to the new message passing variable.

In the C++ API, generate the mapping for the no PBC and throw the error for PBC. Considering I forgot setting `atom_modify map yes` when testing it, others may also forget.

Signed-off-by: Jinzhe Zeng <[email protected]>
Copy link
Contributor

coderabbitai bot commented Nov 15, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces a new function has_message_passing to the tf_model object in the deserialize_to_file function, which indicates message passing capabilities. Additionally, a private member variable do_message_passing is added to the DeepPotJAX class. Modifications in the DeepPotJAX.cc file enhance error handling and control flow, ensuring backward compatibility and providing specific error reporting related to TensorFlow function retrieval.

Changes

File Change Summary
deepmd/jax/jax2tf/serialization.py Added method has_message_passing() to tf_model in deserialize_to_file, returning a TensorFlow constant.
source/api_cc/include/DeepPotJAX.h Added private member variable bool do_message_passing to DeepPotJAX class.
source/api_cc/src/DeepPotJAX.cc Modified error handling in get_func_op, added try-catch in init, and conditional check in compute for do_message_passing. Updated signatures of init and compute methods.

Possibly related PRs

Suggested labels

LAMMPS, OP

Suggested reviewers

  • wanghan-iapcm

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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: 3

🧹 Outside diff range and nitpick comments (3)
source/api_cc/include/DeepPotJAX.h (1)

192-193: Consider documenting the new member variable.

The addition of do_message_passing is well-placed and follows the class's conventions. Consider adding a brief comment explaining its purpose for better maintainability.

+  // whether to perform message passing for periodic boundary conditions
   bool do_message_passing;
deepmd/jax/jax2tf/serialization.py (1)

297-302: Add docstring to the new function.

The implementation looks good and follows the established patterns in the file. However, consider adding a docstring to maintain consistency with other functions and improve maintainability.

 @tf.function
 def has_message_passing() -> tf.Tensor:
+    """Return whether the model has message passing capabilities.
+
+    Returns
+    -------
+    tf.Tensor
+        Boolean tensor indicating if the model supports message passing.
+    """
     return tf.constant(model.has_message_passing(), dtype=tf.bool)
source/api_cc/src/DeepPotJAX.cc (1)

599-608: Provide clear user guidance when mapping is required

The code correctly throws an exception when do_message_passing is true but mapping is not provided. The exception message advises the user to set atom_modify map yes in LAMMPS, which is helpful. Ensure that this requirement is also documented in the user guides or documentation to prevent confusion.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0ad4289 and 23f720b.

📒 Files selected for processing (3)
  • deepmd/jax/jax2tf/serialization.py (1 hunks)
  • source/api_cc/include/DeepPotJAX.h (1 hunks)
  • source/api_cc/src/DeepPotJAX.cc (3 hunks)

source/api_cc/include/DeepPotJAX.h Show resolved Hide resolved
source/api_cc/src/DeepPotJAX.cc Outdated Show resolved Hide resolved
source/api_cc/src/DeepPotJAX.cc Outdated Show resolved Hide resolved
Signed-off-by: Jinzhe Zeng <[email protected]>
@njzjz njzjz requested a review from Copilot November 15, 2024 19:37

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 3 changed files in this pull request and generated no suggestions.

Files not reviewed (2)
  • source/api_cc/include/DeepPotJAX.h: Language not supported
  • source/api_cc/src/DeepPotJAX.cc: Language not supported
Comments skipped due to low confidence (1)

deepmd/jax/jax2tf/serialization.py:298

  • The new function has_message_passing does not have explicit test coverage. Consider adding tests to ensure its correct behavior.
@tf.function
@njzjz njzjz changed the title fix(jax): handle DPA-2 pbc/nopbc no mapping fix(jax): handle DPA-2 pbc/nopbc without mapping Nov 15, 2024
Signed-off-by: Jinzhe Zeng <[email protected]>
@njzjz njzjz requested a review from wanghan-iapcm November 15, 2024 20:41
Copy link

codecov bot commented Nov 15, 2024

Codecov Report

Attention: Patch coverage is 97.95918% with 3 lines in your changes missing coverage. Please review.

Project coverage is 84.54%. Comparing base (0ad4289) to head (c374f39).
Report is 3 commits behind head on devel.

Files with missing lines Patch % Lines
source/api_cc/src/DeepPotJAX.cc 80.00% 1 Missing and 1 partial ⚠️
deepmd/jax/jax2tf/serialization.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4363      +/-   ##
==========================================
+ Coverage   84.50%   84.54%   +0.03%     
==========================================
  Files         596      597       +1     
  Lines       56665    56813     +148     
  Branches     3459     3487      +28     
==========================================
+ Hits        47884    48031     +147     
- Misses       7654     7655       +1     
  Partials     1127     1127              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Signed-off-by: Jinzhe Zeng <[email protected]>
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: 2

🧹 Outside diff range and nitpick comments (4)
source/api_cc/tests/test_deeppot_dpa_jax.cc (4)

16-18: Improve documentation for EPSILON definition.

The comment "1e-10 cannot pass; unclear bug or not" lacks sufficient context. Please document:

  1. The specific test cases that fail with 1e-10
  2. The observed behavior vs expected behavior
  3. Any investigation results or hypotheses about the root cause

28-43: Consider moving Python code to a separate file.

The Python code for generating test data could be moved to a separate script file and referenced in the comments. This would:

  1. Improve maintainability
  2. Make it easier to regenerate test data
  3. Allow version control of the data generation script

80-80: Consider making the model path configurable.

The hardcoded path "../../tests/infer/deeppot_dpa.savedmodel" could make the tests brittle. Consider:

  1. Using an environment variable
  2. Making it a configurable parameter
  3. Using a relative path from the test binary location

329-331: Consider generating neighbor list data programmatically.

The neighbor list data is currently hardcoded. Consider:

  1. Generating it programmatically based on distance criteria
  2. Making it more maintainable and less error-prone
  3. Adding comments explaining the neighbor selection criteria

Example:

std::vector<std::vector<int>> generate_nlist_data(
    const std::vector<VALUETYPE>& coord,
    int natoms,
    VALUETYPE cutoff) {
    std::vector<std::vector<int>> nlist_data(natoms);
    // Generate based on distance criteria
    return nlist_data;
}

Also applies to: 365-367

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d12344b and c374f39.

📒 Files selected for processing (1)
  • source/api_cc/tests/test_deeppot_dpa_jax.cc (1 hunks)
🧰 Additional context used
📓 Learnings (1)
source/api_cc/tests/test_deeppot_dpa_jax.cc (2)
Learnt from: njzjz
PR: deepmodeling/deepmd-kit#4144
File: source/api_cc/tests/test_deeppot_dpa_pt.cc:166-246
Timestamp: 2024-11-12T05:47:21.643Z
Learning: Refactoring between test classes `TestInferDeepPotDpaPt` and `TestInferDeepPotDpaPtNopbc` is addressed in PR #3905.
Learnt from: njzjz
PR: deepmodeling/deepmd-kit#4144
File: source/api_cc/tests/test_deeppot_dpa_pt.cc:166-246
Timestamp: 2024-11-12T05:47:21.643Z
Learning: Refactoring between test classes `TestInferDeepPotDpaPt` and `TestInferDeepPotDpaPtNopbc` is addressed in PR #3905.

source/api_cc/tests/test_deeppot_dpa_jax.cc Show resolved Hide resolved
source/api_cc/tests/test_deeppot_dpa_jax.cc Show resolved Hide resolved
@njzjz njzjz added this pull request to the merge queue Nov 19, 2024
Merged via the queue into deepmodeling:devel with commit f879b48 Nov 19, 2024
60 checks passed
@njzjz njzjz deleted the jax-throw-error-message-passing branch November 19, 2024 05:13
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