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): pass mapping from LAMMPS to PT #4351

Merged
merged 3 commits into from
Nov 14, 2024

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Nov 13, 2024

As discussed, this PR passes mapping from LAMMPS to the PT C++ interface, which is helpful for the external GNN models.

The mapping interface is synced from #4307.

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new function DP_NlistSetMapping for setting mappings in neighbor lists.
    • Added set_mapping method to InputNlist for mapping atoms to real atoms.
    • Enhanced compute methods in DeepPotPT, PairDeepMD, and FixDPLR classes to support new mapping functionalities.
  • Bug Fixes

    • Improved error handling in various classes to ensure robustness during execution.
  • Documentation

    • Updated and added comments for clarity and consistency in new and existing functions.

This will be useful for the external MACE model.

Signed-off-by: Jinzhe Zeng <[email protected]>
@njzjz njzjz requested a review from wanghan-iapcm November 13, 2024 08:55
Copy link
Contributor

coderabbitai bot commented Nov 13, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces several changes across multiple files to enhance the functionality and documentation of the API related to neighbor list management. The API version is incremented from 24 to 25, and a new function, DP_NlistSetMapping, is added to facilitate setting mappings for neighbor lists. Additionally, the InputNlist structure is updated with a new member and method for mapping. The compute methods in various classes are modified to incorporate this mapping functionality, while existing functionalities and signatures remain intact.

Changes

File Change Summary
source/api_c/include/c_api.h - Added method: extern void DP_NlistSetMapping(DP_Nlist* nl, int* mapping);
- Incremented API version from 24 to 25.
- Enhanced documentation for existing functions.
source/api_c/include/deepmd.hpp - Added method: void set_mapping(int *mapping) in InputNlist.
- Minor formatting adjustments.
source/api_c/src/c_api.cc - Added method: void DP_NlistSetMapping(DP_Nlist* nl, int* mapping) in DP_Nlist class.
source/lib/include/neighbor_list.h - Added member variable: int* mapping = nullptr; in InputNlist.
- Added method: void set_mapping(int* mapping_);
source/lmp/fix_dplr.cpp - Updated methods: pre_force, post_force, compute_scalar, and compute_vector to include mapping logic and MPI communication.
source/lmp/pair_deepmd.cpp - Updated methods: compute and settings to incorporate mapping logic and improve error handling.
source/lmp/pair_deepmd.cpp - Updated methods: compute and settings to incorporate mapping logic and improve error handling.

Possibly related PRs

Suggested labels

C++, LAMMPS, Core, Docs

Suggested reviewers

  • wanghan-iapcm
  • iProzd

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

🧹 Outside diff range and nitpick comments (5)
source/lib/include/neighbor_list.h (1)

Line range hint 47-107: Consider architectural improvements for the mapping feature.

While the implementation adds the required functionality, consider these architectural improvements:

  1. Memory Safety:

    • Consider using std::shared_ptr or std::unique_ptr for better memory management
    • Add RAII principles to prevent memory leaks
  2. API Design:

    • Consider making this a breaking change with a new version number
    • Add validation methods to ensure mapping integrity
  3. Thread Safety:

    • Document thread safety guarantees
    • Consider adding synchronization if concurrent access is expected

Example smart pointer implementation:

private:
  std::shared_ptr<int[]> mapping;
public:
  void set_mapping(std::shared_ptr<int[]> mapping_) {
    if (!mapping_) {
      throw std::invalid_argument("mapping cannot be null");
    }
    mapping = std::move(mapping_);
  }
source/api_cc/src/DeepPotPT.cc (1)

211-213: Consider adding error handling for tensor creation

The tensor creation could potentially fail if memory allocation fails. Consider wrapping it in a try-catch block.

-      mapping_tensor =
-          torch::from_blob(mapping.data(), {1, nall_real}, int_option)
-              .to(device);
+      try {
+          mapping_tensor =
+              torch::from_blob(mapping.data(), {1, nall_real}, int_option)
+                  .to(device);
+      } catch (const c10::Error& e) {
+          throw deepmd::deepmd_exception("Failed to create mapping tensor: " +
+                                       std::string(e.what()));
+      }
source/lmp/pair_deepmd.cpp (1)

209-211: LGTM! Consider adding documentation.

The mapping is correctly set in the InputNlist object with consistent conditions. Consider adding a comment explaining the purpose of this mapping for DPA-2 JAX and why it's only needed for single process.

Add documentation above the condition:

+    // Set mapping for DPA-2 JAX interface
+    // Only needed for single process to avoid complexity of distributed memory
     if (comm->nprocs == 1 && atom->map_style != Atom::MAP_NONE) {
       lmp_list.set_mapping(mapping_vec.data());
     }
source/api_c/include/c_api.h (1)

81-89: Documentation could be more descriptive.

While the documentation follows the established style, consider enhancing the description of the mapping parameter to:

  1. Explain the purpose of mapping from all atoms to real atoms
  2. Clarify the expected values/format of the mapping array
  3. Add an @return tag to document the behavior on success/failure

Apply this diff to improve the documentation:

 /**
  * @brief Set mapping for a neighbor list.
  *
  * @param nl Neighbor list.
- * @param mapping mapping from all atoms to real atoms, in size nall.
+ * @param mapping Array that maps indices from all atoms to real atoms. The array 
+ *               should be of size nall, where nall is the total number of atoms 
+ *               including ghost atoms. Each element maps an atom index to its 
+ *               corresponding real atom index.
  * @since API version 25
+ * @return void
  *
  **/
source/api_c/include/deepmd.hpp (1)

868-868: Consider enhancing the documentation with validation details.

While the parameter documentation is clear, it would be helpful to add:

  • Expected validation behavior for invalid mappings
  • Whether null mappings are allowed
  • Any assumptions about the mapping array's lifetime
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85e5e20 and 76cd66a.

📒 Files selected for processing (7)
  • source/api_c/include/c_api.h (4 hunks)
  • source/api_c/include/deepmd.hpp (1 hunks)
  • source/api_c/src/c_api.cc (1 hunks)
  • source/api_cc/src/DeepPotPT.cc (1 hunks)
  • source/lib/include/neighbor_list.h (2 hunks)
  • source/lmp/fix_dplr.cpp (2 hunks)
  • source/lmp/pair_deepmd.cpp (2 hunks)
🔇 Additional comments (9)
source/lib/include/neighbor_list.h (1)

47-48: 🛠️ Refactor suggestion

Add size validation and improve documentation for the mapping member.

The comment indicates the mapping size should be nall, but there's no member variable storing nall to validate this at runtime. Consider:

  1. Adding nall as a member variable
  2. Adding size validation in set_mapping
  3. Documenting the ownership semantics of the pointer

Let's verify if nall is available elsewhere in the codebase:

 /// mapping from all atoms to real atoms, in the size of nall
-int* mapping = nullptr;
+/// @note Caller retains ownership of the mapping pointer
+/// @param nall Total number of atoms (including ghost atoms)
+int nall = 0;
+int* mapping = nullptr;  // Size must match nall
source/api_cc/src/DeepPotPT.cc (2)

206-214: LGTM: Mapping implementation looks correct

The implementation properly handles the mapping from LAMMPS to PT by:

  1. Creating a mapping vector with the correct size
  2. Applying the forward mapping indices
  3. Converting to a tensor and moving it to the appropriate device

206-214: Verify error handling for null mapping

Consider adding a null check for lmp_list.mapping to prevent potential segmentation faults.

Consider adding a null check:

-    if (lmp_list.mapping) {
+    if (lmp_list.mapping != nullptr) {
source/lmp/pair_deepmd.cpp (1)

158-164: LGTM! Verify atom mapping configuration.

The mapping initialization is correctly implemented with proper boundary checks. The -1 initialization provides a safe default for invalid indices.

Let's verify the atom mapping configuration:

✅ Verification successful

The mapping implementation is correct and safe

The code's handling of atom mapping is properly implemented with appropriate checks:

  • The mapping is only used when map_style != Atom::MAP_NONE, preventing invalid access
  • The single process check (comm->nprocs == 1) is consistent with other similar checks in the codebase (fix_dplr.cpp)
  • The mapping is properly used in the InputNlist with the same safety checks
  • The example input scripts show proper atom mapping configuration with atom_modify map array
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check atom mapping style configuration in input scripts
# Expect: atom_style and atom_modify commands that set up mapping

# Search for atom_style commands
echo "Checking atom_style configuration:"
rg "atom_style"

# Search for atom_modify map commands
echo "Checking atom_modify map configuration:"
rg "atom_modify.*map"

Length of output: 1958


Script:

#!/bin/bash
# Let's check the LAMMPS documentation or implementation for default mapping behavior
# and verify how atom mapping is handled in pair_deepmd.cpp

# Check for any documentation about atom mapping in the codebase
echo "Checking documentation about atom mapping:"
rg -i "atom.*map" "doc/" -A 3

# Check how atom mapping is used in pair_deepmd.cpp
echo "Checking atom mapping usage in pair_deepmd.cpp:"
rg "map.*style|map\(" "source/lmp/pair_deepmd.cpp" -A 2

# Check for any other relevant atom map handling in the codebase
echo "Checking other atom map handling:"
rg "map.*style|MAP_NONE" "source/lmp/"

Length of output: 37849

source/api_c/include/c_api.h (2)

15-15: API version increment is appropriate.

The increment from 24 to 25 correctly reflects the addition of the new DP_NlistSetMapping function to the API.


Line range hint 34-70: Documentation improvements are comprehensive.

The enhanced documentation for DP_NewNlist_comm is well-structured and thorough:

  • Clear @details section explaining the function's purpose
  • Well-documented parameters with descriptive explanations
  • Proper formatting and consistent style
source/api_c/src/c_api.cc (1)

46-48: Verify the mapping implementation across the codebase.

The new mapping functionality needs to be consistently implemented across the codebase.

Run the following script to verify the mapping implementation:

✅ Verification successful

Based on the search results, I can now provide a final response:

The mapping implementation is consistent across the codebase

The mapping functionality is properly implemented throughout the codebase:

  • In C API (source/api_c/src/c_api.cc), DP_NlistSetMapping sets the mapping for neighbor lists
  • The mapping is consistently used in neighbor list handling across CPU/GPU implementations in source/lib/src/coord.cc and source/lib/src/gpu/coord.cu
  • The mapping is properly propagated through model implementations in PyTorch, JAX and TensorFlow
  • Test coverage exists for mapping functionality across different model types and architectures
  • The mapping is used consistently for:
    • Neighbor list construction
    • Force/energy/virial calculations
    • Handling periodic boundary conditions
    • Converting between extended and local indices
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the mapping implementation across the codebase.

# Test 1: Check for mapping-related functions and methods
echo "Checking for mapping-related functions and methods..."
rg -A 5 "set_mapping|mapping"

# Test 2: Check for InputNlist usage with mapping
echo "Checking for InputNlist usage with mapping..."
ast-grep --pattern $'InputNlist\s*\w+\s*\([^)]*\)'

Length of output: 463817

source/api_c/include/deepmd.hpp (2)

866-870: LGTM! The new mapping functionality is well integrated.

The new set_mapping method is correctly implemented and properly documented. The implementation appropriately delegates to the C API function DP_NlistSetMapping.


866-870: Verify the mapping functionality across the codebase.

Let's verify that this new mapping capability is properly handled throughout the codebase.

✅ Verification successful

Based on the search results, I can now provide a final response:

The mapping functionality is well-integrated and thoroughly tested across the codebase

The mapping capability is properly implemented and extensively tested:

  • Core implementation is in source/api_c/src/c_api.cc with DP_NlistSetMapping
  • Comprehensive test coverage exists across multiple test files:
    • Unit tests in source/api_c/tests/ covering various scenarios
    • Integration tests in source/tests/ validating mapping with different models
    • Cross-framework tests ensuring consistent behavior in TensorFlow, PyTorch and JAX

The mapping functionality is used consistently for:

  • Neighbor list management
  • Coordinate transformations
  • Force/energy calculations
  • Model evaluations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other uses of DP_NlistSetMapping to ensure consistent usage
rg "DP_NlistSetMapping"

# Search for any existing mapping-related functionality
ast-grep --pattern 'mapping'

# Look for tests covering this functionality
fd -e cc -e cpp -e py "test.*mapping"

Length of output: 85195

source/lib/include/neighbor_list.h Outdated Show resolved Hide resolved
source/api_c/src/c_api.cc Outdated Show resolved Hide resolved
source/lmp/fix_dplr.cpp Outdated Show resolved Hide resolved
source/lmp/fix_dplr.cpp Outdated Show resolved Hide resolved
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Nov 13, 2024
Merged via the queue into deepmodeling:devel with commit d5295d5 Nov 14, 2024
51 checks passed
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