Skip to content

Conversation

@caic99
Copy link
Member

@caic99 caic99 commented May 28, 2025

node_ebd_ext contains embedding on expanded atoms, which might be large for a large cut-off. Current implementation do matmul first, then gather tensor by the neighbors. This introduces saving sub_node_ext_update of size nf * nall * ndim in each repflow layer, where nall might be multiple times larger than nloc.
This PR do gathering first, then compute matmul. After this PR, the peak memory size is of O(nlayer * nf * nloc), unrelated to nall.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced an optional setting to enable or disable local atom index mapping during descriptor computation, providing more flexibility in non-parallel inference scenarios.
  • Bug Fixes

    • Enhanced test coverage and consistency checks to ensure descriptor outputs remain equivalent whether local mapping is enabled or not.
  • Documentation

    • Improved parameter descriptions and comments to clarify the behavior and shape of key variables related to local mapping.
  • Tests

    • Added new tests to validate the functional equivalence of descriptor outputs with and without local mapping.
    • Updated existing tests to support and verify the new local mapping option.
  • Style

    • Improved assertion handling in tests by specifying absolute tolerance for numerical comparisons.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 28, 2025

📝 Walkthrough

Walkthrough

A new boolean parameter use_loc_mapping was introduced to the DPA3 descriptor and repflow components in both TensorFlow and PyTorch backends. This flag controls whether local atom index mapping is applied during non-parallel inference, affecting how neighbor lists and embeddings are constructed. Corresponding updates were made to utility functions, argument definitions, and tests to support and validate this new option.

Changes

File(s) Change Summary
deepmd/pt/model/descriptor/dpa3.py
deepmd/dpmodel/descriptor/dpa3.py
Added use_loc_mapping parameter to DescrptDPA3 class; conditional logic for embeddings based on this flag.
deepmd/pt/model/descriptor/repflows.py
deepmd/dpmodel/descriptor/repflows.py
Added use_loc_mapping to DescrptBlockRepflows; conditional remapping of neighbor lists and embeddings.
deepmd/pt/model/descriptor/repflow_layer.py Updated comment to clarify possible shapes of node_ebd_ext parameter.
deepmd/pt/model/network/utils.py
deepmd/dpmodel/utils/network.py
Added use_loc_mapping parameter to get_graph_index; changed frame shift logic based on its value.
deepmd/utils/argcheck.py Added use_loc_mapping argument to descrpt_dpa3_args() with documentation.
source/tests/pt/model/test_loc_mapping.py New test module validating output consistency of DPA3 descriptor with/without local mapping.
source/tests/universal/dpmodel/descriptor/test_descriptor.py Added use_loc_mapping parameter to descriptor test config and parameterization.
source/tests/pd/model/test_dpa3.py Adjusted test to use smaller, duplicated input slices for descriptor calls.
source/tests/universal/common/cases/model/utils.py Added explicit atol (absolute tolerance) to all np.testing.assert_allclose calls in test assertions.
deepmd/pd/model/descriptor/dpa3.py
deepmd/pd/model/descriptor/repflows.py
Added use_loc_mapping parameter to Paddle DPA3 and repflows classes; Paddle repflows asserts it is disabled.
source/tests/consistent/descriptor/test_dpa3.py Added use_loc_mapping parameter to test parameterization and integrated it into test setup and properties.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DescrptDPA3
    participant DescrptBlockRepflows
    participant get_graph_index

    User->>DescrptDPA3: call forward/call(coord_ext, atype_ext, nlist, mapping, ...)
    DescrptDPA3->>DescrptBlockRepflows: call forward/call(..., use_loc_mapping)
    DescrptBlockRepflows->>get_graph_index: get_graph_index(nlist, ..., use_loc_mapping)
    get_graph_index-->>DescrptBlockRepflows: returns graph indices (with or without local mapping)
    DescrptBlockRepflows-->>DescrptDPA3: returns descriptor output
    DescrptDPA3-->>User: returns descriptor output
Loading

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

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

🧹 Nitpick comments (2)
deepmd/pt/model/descriptor/dpa3.py (1)

478-478: Remove unused variable assignment.

The variable nall is assigned but never used in this method.

-        nall = extended_coord.view(nframes, -1).shape[1] // 3
🧰 Tools
🪛 Ruff (0.11.9)

478-478: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

deepmd/pt/model/descriptor/repflows.py (1)

542-542: Rename unused loop variable.

The loop variable idx is not used within the loop body.

Apply this diff to follow Python conventions:

-        for idx, ll in enumerate(self.layers):
+        for _, ll in enumerate(self.layers):
🧰 Tools
🪛 Ruff (0.11.9)

542-542: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d74e6b5 and 61a29aa.

📒 Files selected for processing (7)
  • deepmd/pt/model/descriptor/dpa3.py (2 hunks)
  • deepmd/pt/model/descriptor/repflow_layer.py (1 hunks)
  • deepmd/pt/model/descriptor/repflows.py (5 hunks)
  • deepmd/pt/model/network/utils.py (2 hunks)
  • deepmd/utils/argcheck.py (1 hunks)
  • source/tests/pt/model/test_loc_mapping.py (1 hunks)
  • source/tests/universal/dpmodel/descriptor/test_descriptor.py (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
deepmd/pt/model/descriptor/repflows.py (1)
deepmd/pt/model/network/utils.py (1)
  • get_graph_index (49-138)
🪛 Ruff (0.11.9)
deepmd/pt/model/descriptor/repflows.py

542-542: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

deepmd/pt/model/descriptor/dpa3.py

156-156: Undefined name use_loc_mapping

(F821)


164-164: Undefined name use_loc_mapping

(F821)


478-478: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

🔇 Additional comments (15)
deepmd/pt/model/descriptor/repflow_layer.py (1)

687-687: LGTM! Documentation improvement aligns with the new feature.

The comment update accurately reflects that node_ebd_ext can have different shapes depending on whether local mapping is used in non-parallel mode, which aligns with the new use_loc_mapping functionality.

source/tests/universal/dpmodel/descriptor/test_descriptor.py (3)

489-489: LGTM! Proper addition of the new parameter.

The use_loc_mapping parameter is correctly added with a sensible default value of True.


535-535: LGTM! Parameter properly passed to configuration.

The parameter is correctly included in the input dictionary for descriptor configuration.


560-560: LGTM! Comprehensive test parameterization.

The parameterization with (True, False) ensures that both local mapping modes are tested, providing good test coverage for the new feature.

deepmd/pt/model/network/utils.py (2)

54-54: LGTM! Well-designed parameter addition.

The use_loc_mapping parameter is properly added with a sensible default value of True and correct type annotation.


104-106: LGTM! Correct conditional logic implementation.

The frame shift calculation properly switches between nall (global mapping) and nloc (local mapping) based on the use_loc_mapping flag. This enables the core functionality for local vs global atom indexing.

deepmd/pt/model/descriptor/dpa3.py (2)

474-474: LGTM! Clear parallel mode detection.

The parrallel_mode variable provides a clear way to detect parallel inference mode based on the presence of comm_dict.


480-483: LGTM! Correct conditional embedding logic.

The conditional logic properly switches between local and global atom type embeddings based on parallel mode and the use_loc_mapping flag. When not in parallel mode and local mapping is enabled, it uses only the local subset of atom types.

deepmd/pt/model/descriptor/repflows.py (5)

212-212: LGTM! Well-designed backward-compatible parameter.

The addition of use_loc_mapping with a default value of True maintains backward compatibility while providing control over local atom index mapping behavior.


420-422: Good defensive programming with parallel mode detection.

The addition of parallel_mode flag and the assertion for mapping in non-parallel mode ensures proper data flow and catches potential issues early.


474-476: Proper handling of extended atom type embeddings.

The unconditional assertion and extraction of local atom embeddings from the extended embeddings is correct and aligns with the local mapping design.


495-510: Well-implemented neighbor list remapping logic.

The conditional remapping of neighbor indices from global to local using torch.gather is correctly implemented. The logic properly:

  • Checks for non-parallel mode and use_loc_mapping flag
  • Reshapes for gathering and restores original shape
  • Passes the use_loc_mapping flag to get_graph_index for consistent behavior

537-553: Correct implementation of node embedding extension logic.

The conditional logic properly handles node embedding extension based on use_loc_mapping:

  • When use_loc_mapping is True in non-parallel mode, node_ebd_ext uses local embeddings directly
  • Otherwise, it gathers from global embeddings using the mapping tensor

This aligns perfectly with the neighbor list remapping logic above.

🧰 Tools
🪛 Ruff (0.11.9)

542-542: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

source/tests/pt/model/test_loc_mapping.py (2)

34-146: Excellent test coverage for the local mapping feature.

The test_consistency method thoroughly validates that the descriptor outputs remain consistent regardless of the use_loc_mapping setting. The test:

  • Covers multiple parameter combinations through itertools.product
  • Uses duplicated data to ensure identical inputs
  • Properly handles precision tolerances for GPU testing
  • Sets consistent random seeds for reproducibility

148-258: Well-designed test for dynamic selection mode.

The test_consistency_nosel method extends the testing to cover dynamic selection (use_dynamic_sel=True) with reduction factor, ensuring the local mapping feature works correctly in this mode as well.

caic99 and others added 3 commits May 28, 2025 06:39
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Chun Cai <amoycaic@gmail.com>
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/pt/model/descriptor/dpa3.py (3)

475-475: Fix the typo in variable name.

The variable name has a spelling error.

Apply this diff to fix the typo:

-        parrallel_mode = comm_dict is not None
+        parallel_mode = comm_dict is not None

Also update the usage in the conditional below:

-        if not parrallel_mode and self.use_loc_mapping:
+        if not parallel_mode and self.use_loc_mapping:

481-484: LGTM! Conditional embedding logic is correct.

The conditional logic properly implements the local mapping feature by computing embeddings only for local atoms when not in parallel mode and use_loc_mapping is enabled.

Consider adding the use_loc_mapping parameter to the class docstring for better documentation:

     use_tebd_bias : bool, Optional
         Whether to use bias in the type embedding layer.
+    use_loc_mapping : bool, Optional
+        Whether to use local atom index mapping in non-parallel inference.
     type_map : list[str], Optional
         A list of strings. Give the name to each type of atoms.

479-479: Remove unused variable.

The variable nall is computed but never used in the function.

Apply this diff to remove the unused variable:

         nframes, nloc, nnei = nlist.shape
-        nall = extended_coord.view(nframes, -1).shape[1] // 3
🧰 Tools
🪛 Ruff (0.11.9)

479-479: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 61a29aa and e4b3ae8.

📒 Files selected for processing (2)
  • deepmd/pt/model/descriptor/dpa3.py (3 hunks)
  • deepmd/utils/argcheck.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • deepmd/utils/argcheck.py
🧰 Additional context used
🪛 Ruff (0.11.9)
deepmd/pt/model/descriptor/dpa3.py

479-479: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

🔇 Additional comments (2)
deepmd/pt/model/descriptor/dpa3.py (2)

111-111: LGTM! Parameter addition is correct.

The use_loc_mapping parameter is properly added with appropriate type annotation and default value for backward compatibility.


157-157: LGTM! Parameter propagation is correct.

The use_loc_mapping parameter is properly passed to the DescrptBlockRepflows constructor and stored as an instance attribute for use in the forward method.

Also applies to: 165-165

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 (1)
deepmd/pt/model/descriptor/repflows.py (1)

538-552: Well-designed conditional node embedding logic.

The conditional construction of node_ebd_ext properly handles different execution modes:

  • Non-parallel with use_loc_mapping=False: Uses torch.gather for global mapping (preserves original behavior)
  • Non-parallel with use_loc_mapping=True: Uses node_ebd directly (new optimization)
  • Parallel mode: Uses existing communication logic

However, address the unused loop variable:

-        for idx, ll in enumerate(self.layers):
+        for _idx, ll in enumerate(self.layers):
🧰 Tools
🪛 Ruff (0.11.9)

543-543: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d595825 and 598c2da.

📒 Files selected for processing (3)
  • deepmd/pt/model/descriptor/dpa3.py (4 hunks)
  • deepmd/pt/model/descriptor/repflow_layer.py (1 hunks)
  • deepmd/pt/model/descriptor/repflows.py (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • deepmd/pt/model/descriptor/repflow_layer.py
🧰 Additional context used
🪛 Ruff (0.11.9)
deepmd/pt/model/descriptor/repflows.py

543-543: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

deepmd/pt/model/descriptor/dpa3.py

481-481: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

⏰ Context from checks skipped due to timeout of 90000ms (29)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test C++ (false)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
🔇 Additional comments (9)
deepmd/pt/model/descriptor/dpa3.py (4)

92-93: LGTM: Clear parameter documentation.

The documentation for use_loc_mapping is well-written and clearly explains its purpose for controlling local atom index mapping in non-parallel inference.


113-113: Good fix: Constructor parameter properly added.

This correctly addresses the previous review comment about the missing use_loc_mapping parameter in the constructor. The default value of True maintains backward compatibility.


159-159: LGTM: Proper parameter handling.

The use_loc_mapping parameter is correctly passed to the DescrptBlockRepflows constructor and stored as an instance attribute following standard patterns.

Also applies to: 167-167


477-477: Well-implemented conditional logic.

The parallel mode detection and conditional type embedding computation are well-designed:

  • Clean parallel mode detection using comm_dict is not None
  • Logical optimization: when not in parallel mode and use_loc_mapping=True, compute embeddings only on local atoms (extended_atype[:, :nloc]) rather than all extended atoms
  • Maintains backward compatibility when use_loc_mapping=False

Also applies to: 483-486

deepmd/pt/model/descriptor/repflows.py (5)

212-212: LGTM: Consistent parameter implementation.

The use_loc_mapping parameter is properly added to the constructor and stored as an instance variable, following the same pattern as in dpa3.py.

Also applies to: 243-243


421-422: Good refactoring: Improved readability.

The parallel mode detection is consistent with dpa3.py, and refactoring from comm_dict is None to not parallel_mode improves code readability and maintainability.


475-477: Good defensive programming.

The assertions ensure that extended_atype_embd is available and properly shaped, which helps catch issues early in the pipeline.


496-503: Core optimization implementation looks correct.

The neighbor list remapping logic properly converts global indices to local indices when use_loc_mapping=True in non-parallel mode. The use of torch.gather with proper reshaping is the correct approach for this index transformation.


507-511: Proper parameter propagation.

The use_loc_mapping parameter is correctly passed to get_graph_index, ensuring consistent behavior throughout the neighbor graph construction pipeline.

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 (1)
deepmd/dpmodel/descriptor/repflows.py (1)

541-541: Remove trailing whitespace.

-            )
-        
+            )
+
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 598c2da and 39880b9.

📒 Files selected for processing (3)
  • deepmd/dpmodel/descriptor/dpa3.py (5 hunks)
  • deepmd/dpmodel/descriptor/repflows.py (5 hunks)
  • deepmd/dpmodel/utils/network.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (29)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Analyze (python)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Test C++ (false)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Build C++ (cpu, cpu)
🔇 Additional comments (3)
deepmd/dpmodel/utils/network.py (1)

1004-1026: LGTM! The use_loc_mapping parameter is correctly implemented.

The changes properly implement the conditional frame shift calculation based on whether local atom index mapping is used. The default value of True maintains backward compatibility.

Also applies to: 1066-1066

deepmd/dpmodel/descriptor/dpa3.py (1)

274-275: LGTM! The use_loc_mapping parameter is properly integrated into the DPA3 descriptor.

The implementation correctly handles the conditional selection of type embeddings based on whether local atom index mapping is used. The parameter is properly documented and propagated to child components.

Also applies to: 295-295, 341-341, 350-350, 549-558

deepmd/dpmodel/descriptor/repflows.py (1)

148-149: The use_loc_mapping implementation in repflows is well-designed.

The neighbor list remapping and node embedding handling are correctly implemented. The logic properly differentiates between local and global atom indexing modes, which is essential for non-parallel inference scenarios.

Also applies to: 201-201, 235-235, 534-540, 545-545, 576-580

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 (1)
source/tests/pd/model/test_dpa3.py (1)

73-76: Test modification for use_loc_mapping feature looks correct.

The change to use duplicated input frames is consistent with testing the new use_loc_mapping feature. The implementation correctly duplicates the first frame across all input arrays (coord_ext, atype_ext, nlist, mapping) using proper array concatenation.

However, consider that this modification reduces test coverage by only testing with identical duplicated frames rather than varied input data.

Consider adding additional test cases that use varied frames to ensure the descriptor handles diverse input scenarios correctly:

+ # Test with varied frames as well
+ coord_ext_varied = np.concatenate([self.coord_ext[:1], self.coord_ext[1:2] if self.coord_ext.shape[0] > 1 else self.coord_ext[:1]], axis=0)
+ # Similar for other arrays...
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6f01d02 and 30b1225.

📒 Files selected for processing (1)
  • source/tests/pd/model/test_dpa3.py (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
source/tests/pd/model/test_dpa3.py (1)
deepmd/pd/model/descriptor/dpa3.py (3)
  • DescrptDPA3 (65-573)
  • deserialize (405-435)
  • serialize (368-402)
⏰ Context from checks skipped due to timeout of 90000ms (29)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Analyze (python)
  • GitHub Check: Test C++ (false)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Test Python (1, 3.9)
🔇 Additional comments (1)
source/tests/pd/model/test_dpa3.py (1)

114-117: Consistent usage of duplicated input arrays.

The test correctly uses the new duplicated input arrays throughout all descriptor calls (dd0, dd1 deserialized, and dd2 dp implementation). This maintains consistency and ensures all three implementations are tested with the same input data.

Also applies to: 122-125, 136-136

@caic99 caic99 changed the title feat(pt): add use_loc_mapping feat: add use_loc_mapping May 28, 2025
@codecov
Copy link

codecov bot commented May 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.78%. Comparing base (75b175b) to head (52f06f2).
⚠️ Report is 91 commits behind head on devel.

Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4772      +/-   ##
==========================================
- Coverage   84.79%   84.78%   -0.01%     
==========================================
  Files         698      698              
  Lines       67734    67775      +41     
  Branches     3540     3542       +2     
==========================================
+ Hits        57432    57465      +33     
- Misses       9171     9174       +3     
- Partials     1131     1136       +5     

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

@caic99 caic99 marked this pull request as ready for review May 28, 2025 12:44
@caic99 caic99 requested a review from iProzd May 28, 2025 12:44
Copy link
Collaborator

@iProzd iProzd left a comment

Choose a reason for hiding this comment

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

A consistent test also need to be added in source/tests/consistent/descriptor/test_dpa3.py (and skip pd), see https://github.com/deepmodeling/deepmd-kit/pull/4760/files#diff-2175c5f9ca14ee18e75a5fe46c7ecd66d02fae4612599a7763a2e29acdf12943 for example.

Signed-off-by: Duo <50307526+iProzd@users.noreply.github.com>
optim_update,
use_exp_switch,
use_dynamic_sel,
use_loc_mapping,

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable optim_update is not used.
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 (2)
deepmd/pt/model/descriptor/repflows.py (1)

546-546: Address static analysis hint about unused loop variable.

The loop variable idx is not used within the loop body. Consider renaming it to _idx to indicate it's intentionally unused, or remove the enumeration if the index isn't needed.

-        for idx, ll in enumerate(self.layers):
+        for _, ll in enumerate(self.layers):
🧰 Tools
🪛 Ruff (0.11.9)

546-546: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

deepmd/pt/model/descriptor/dpa3.py (1)

483-483: Remove unused variable assignment.

The nall variable is assigned but never used in the current implementation.

Apply this diff to remove the unused variable:

-        nall = extended_coord.view(nframes, -1).shape[1] // 3
🧰 Tools
🪛 Ruff (0.11.9)

483-483: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 42c9724 and 91aa1d6.

📒 Files selected for processing (8)
  • deepmd/dpmodel/descriptor/dpa3.py (8 hunks)
  • deepmd/dpmodel/descriptor/repflows.py (6 hunks)
  • deepmd/pd/model/descriptor/dpa3.py (7 hunks)
  • deepmd/pd/model/descriptor/repflows.py (3 hunks)
  • deepmd/pt/model/descriptor/dpa3.py (7 hunks)
  • deepmd/pt/model/descriptor/repflows.py (7 hunks)
  • deepmd/utils/argcheck.py (2 hunks)
  • source/tests/consistent/descriptor/test_dpa3.py (11 hunks)
✅ Files skipped from review due to trivial changes (1)
  • deepmd/utils/argcheck.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • deepmd/dpmodel/descriptor/dpa3.py
  • deepmd/dpmodel/descriptor/repflows.py
🧰 Additional context used
🧬 Code Graph Analysis (2)
deepmd/pd/model/descriptor/dpa3.py (1)
deepmd/utils/version.py (1)
  • check_version_compatibility (2-27)
deepmd/pt/model/descriptor/dpa3.py (1)
deepmd/utils/version.py (1)
  • check_version_compatibility (2-27)
🪛 Ruff (0.11.9)
deepmd/pt/model/descriptor/dpa3.py

483-483: Local variable nall is assigned to but never used

Remove assignment to unused variable nall

(F841)

deepmd/pt/model/descriptor/repflows.py

546-546: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

⏰ Context from checks skipped due to timeout of 90000ms (29)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (1, 3.9)
  • 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 wheels for cp311-macosx_arm64
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-win_amd64
  • 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: Test C++ (true)
  • GitHub Check: Test C++ (false)
🔇 Additional comments (20)
deepmd/pd/model/descriptor/repflows.py (2)

115-117: Good documentation of the new parameter.

The docstring clearly explains the purpose of use_loc_mapping and explicitly states it's not supported yet in Paddle, which sets proper user expectations.


167-167: Appropriate preparatory implementation for interface consistency.

The parameter is added with a safe default (False) and immediately asserted to prevent usage until the feature is fully implemented in Paddle. This maintains API consistency with the PyTorch implementation while preventing incorrect usage.

Also applies to: 203-204

source/tests/consistent/descriptor/test_dpa3.py (2)

70-70: Comprehensive test parameterization.

Good addition of use_loc_mapping to the parameterized test inputs to ensure both True and False values are tested across different configurations.


174-174: Correct skip logic for unsupported feature.

Properly skips Paddle tests when use_loc_mapping is enabled, which is consistent with the Paddle implementation that explicitly disables this feature.

deepmd/pd/model/descriptor/dpa3.py (3)

92-94: Clear documentation with implementation status.

The docstring appropriately documents the new parameter and clearly states it's not supported yet in Paddle, maintaining transparency about feature availability.


159-159: Proper parameter integration.

The parameter is correctly passed to DescrptBlockRepflows and stored as an instance attribute, maintaining consistency with the overall architecture.

Also applies to: 168-168


379-379: Excellent serialization version handling.

The version increment from 1 to 2, inclusion of use_loc_mapping in serialization, and updated compatibility check properly handle the new parameter while maintaining backward compatibility.

Also applies to: 390-390, 415-415

deepmd/pt/model/descriptor/repflows.py (5)

159-161: Well-documented new parameter.

The docstring clearly explains the purpose and behavior of use_loc_mapping, providing good context for when and how it should be used.


215-215: Verify default value choice.

The default value is True in PyTorch but False in Paddle. Since this is a memory optimization feature that's fully implemented in PyTorch, the True default makes sense to enable the optimization by default.

Please confirm that the different default values between PyTorch (True) and Paddle (False) are intentional, given that Paddle doesn't support the feature yet.


424-426: Clear parallel mode detection.

Good separation of logic between parallel and non-parallel modes, which helps maintain existing functionality while adding the new optimization.


499-506: Core memory optimization implementation.

This is the key change that enables the memory optimization by converting neighbor lists from global (nall) to local (nloc) indices when use_loc_mapping is enabled. This allows keeping embeddings at local size instead of expanding them.


541-555: Efficient embedding assignment strategy.

The conditional logic correctly chooses between the traditional approach (expanding embeddings to nall size) and the optimized approach (keeping embeddings at nloc size) based on the use_loc_mapping flag.

🧰 Tools
🪛 Ruff (0.11.9)

546-546: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

deepmd/pt/model/descriptor/dpa3.py (8)

92-94: LGTM! Clear documentation for the new parameter.

The documentation clearly explains the purpose and behavior of use_loc_mapping, addressing when it's applied and its effect on local indexing and mapping during descriptor computation.


114-114: LGTM! Constructor parameter properly added.

The use_loc_mapping parameter has been correctly added to the constructor with a sensible default value of True. This addresses the previous review comment about the missing constructor parameter.


160-160: LGTM! Parameter properly passed and stored.

The use_loc_mapping parameter is correctly passed to the DescrptBlockRepflows constructor and stored as an instance attribute for later use.

Also applies to: 168-168


374-374: LGTM! Version increment for serialization compatibility.

The version has been appropriately incremented from 1 to 2 to reflect the addition of the new use_loc_mapping parameter to the serialized data structure.


385-385: LGTM! Parameter included in serialization.

The use_loc_mapping parameter is correctly included in the serialized data to ensure the configuration is preserved when saving/loading models.


410-410: LGTM! Version compatibility properly updated.

The version compatibility check has been correctly updated to accept both version 1 and 2, ensuring backward compatibility while supporting the new serialization format.


479-479: LGTM! Clear parallel mode detection.

The parallel_mode variable provides clear detection of whether the system is running in parallel inference mode by checking if comm_dict is not None.


485-488: LGTM! Memory optimization logic correctly implemented.

The conditional logic properly implements the memory optimization strategy described in the PR objectives. When not in parallel mode and use_loc_mapping is enabled, only local atoms (extended_atype[:, :nloc]) are processed for type embedding, reducing memory usage from O(nall) to O(nloc).

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

♻️ Duplicate comments (1)
source/tests/consistent/descriptor/test_dpa3.py (1)

145-145: Consider removing unused variable unpacking.

The optim_update variable is unpacked but not used in this method. While this follows a consistent pattern across the class, consider whether all parameters need to be unpacked in every method.

This issue was previously flagged by static analysis tools and remains unresolved.

🧰 Tools
🪛 GitHub Check: CodeQL

[notice] 145-145: Unused local variable
Variable optim_update is not used.

🧹 Nitpick comments (2)
source/tests/consistent/descriptor/test_dpa3.py (2)

63-63: Parameter restriction reduces test coverage.

The update_angle parameter has been restricted from (True, False) to (True,), which reduces test coverage. Ensure this restriction is intentional and doesn't miss important test scenarios.


72-72: Parameter restriction reduces test coverage.

Similar to update_angle, the n_multi_edge_message parameter has been restricted from multiple values to (1,). Consider whether this reduction in test coverage is appropriate.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 91aa1d6 and 52f06f2.

📒 Files selected for processing (1)
  • source/tests/consistent/descriptor/test_dpa3.py (11 hunks)
🧰 Additional context used
🪛 GitHub Check: CodeQL
source/tests/consistent/descriptor/test_dpa3.py

[notice] 145-145: Unused local variable
Variable optim_update is not used.

⏰ Context from checks skipped due to timeout of 90000ms (26)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test C++ (false)
  • 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: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Build C library (2.14, >=2.5.0rc0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (rocm, rocm)
🔇 Additional comments (5)
source/tests/consistent/descriptor/test_dpa3.py (5)

70-70: New parameter integration looks good.

The use_loc_mapping parameter is properly added to the parameterized test configuration. Testing with both True and False values ensures comprehensive coverage of the new feature.


88-88: Parameter unpacking correctly updated.

The use_loc_mapping parameter is properly added to the parameter unpacking tuple, maintaining consistency with the parameterized decorator.


129-129: Parameter correctly added to data dictionary.

The use_loc_mapping parameter is properly included in the data dictionary that configures the descriptor, ensuring the test parameter is passed through to the implementation.


164-164: Proper handling of unsupported feature in PD backend.

The skip condition correctly excludes PD tests when use_loc_mapping is enabled, acknowledging that this feature is not yet supported in the PaddlePaddle backend.

Also applies to: 174-174


189-189: Consistent parameter handling across all methods.

The use_loc_mapping parameter is consistently unpacked in all relevant methods, maintaining the established pattern for parameter handling throughout the test class.

Also applies to: 208-208, 269-269, 351-351, 376-376

@iProzd iProzd added this pull request to the merge queue May 30, 2025
Merged via the queue into deepmodeling:devel with commit f460493 May 30, 2025
60 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