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(jax/array-api): dpa1 #4160

Merged
merged 17 commits into from
Oct 9, 2024
Merged

feat(jax/array-api): dpa1 #4160

merged 17 commits into from
Oct 9, 2024

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Sep 24, 2024

Summary by CodeRabbit

  • New Features

    • Updated method for converting input to NumPy arrays, enhancing performance and compatibility with array-like structures.
    • Simplified handling of weight, bias, and identity variables for improved compatibility with array backends.
    • Introduced new network classes and enhanced network management functionalities.
    • Added support for the new array_api_strict backend in testing.
  • Bug Fixes

    • Fixed serialization process to ensure accurate conversion of weights and biases.
  • Tests

    • Added tests to validate the new functionalities and ensure compatibility across various backends, including JAX and Array API Strict.
  • Chores

    • Continued improvements to project structure and dependencies for better maintainability.

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

coderabbitai bot commented Sep 24, 2024

📝 Walkthrough
<details>
<summary>📝 Walkthrough</summary>

## Walkthrough
The changes involve updates to several files in the DeepMD project, focusing on improving array handling and network management. Key modifications include the replacement of `np.asarray` with `np.from_dlpack` in the `to_numpy_array` function, enhancements to the `deserialize` method in the `NativeLayer` class, and the introduction of new classes and methods in the JAX network module. These updates aim to streamline operations with weights, biases, and identity variables, ensuring better compatibility across different array backends.

## Changes

| File Path                                      | Change Summary                                                                                                                                                                                                 |
|------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `deepmd/dpmodel/common.py`                     | Updated `to_numpy_array` function to use `np.from_dlpack` instead of `np.asarray`, retaining the `None` check.                                                                                             |
| `deepmd/dpmodel/descriptor/dpa1.py`           | Refactored `np_softmax` and `np_normalize` to use `array_api_compat`; modified `DescrptDPA1` class methods to utilize array API; updated `NeighborGatedAttention` and `NeighborGatedAttentionLayer` classes for array API compatibility. |
| `deepmd/dpmodel/utils/exclude_mask.py`        | Enhanced `AtomExcludeMask` and `PairExcludeMask` classes for array API compatibility; replaced NumPy functions with `xp` equivalents.                                                                         |
| `deepmd/dpmodel/utils/network.py`              | Modified `NativeLayer` and `LayerNorm` classes to enhance array API compatibility; updated serialization methods and normalization processes.                                                                  |
| `deepmd/dpmodel/utils/nlist.py`                | Updated `build_neighbor_list`, `nlist_distinguish_types`, and `extend_coord_with_ghosts` functions to use `array_api_compat` for array operations.                                                          |
| `deepmd/dpmodel/utils/type_embed.py`           | Renamed `concatenate` method to `concat` in `TypeEmbedNet` class.                                                                                                                                          |
| `deepmd/jax/utils/network.py`                  | Introduced new classes (`ArrayAPIParam`, `NetworkCollection`) and restructured existing network classes to enhance compatibility with array API.                                                             |
| `pyproject.toml`                               | Added dependency `flax>=0.8.0;python_version>="3.10"` to `jax` optional dependencies.                                                                                                                       |
| `source/tests/array_api_strict/utils/network.py` | Added new classes and functionality for neural network layers and collections with array API compliance.                                                                                                     |
| `source/tests/consistent/common.py`            | Enhanced `CommonTest` class to support `array_api_strict` backend; added methods and properties for evaluation and serialization.                                                                            |
| `source/tests/consistent/descriptor/test_dpa1.py` | Updated `TestDPA1` class to include support for JAX and Array API Strict backends; added properties and methods for conditional testing.                                                                       |
| `source/tests/consistent/test_type_embedding.py` | Added support for `array_api_strict` in `TestTypeEmbedding` class; introduced evaluation method and conditional imports.                                                                                     |

</details>

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.

variables = data.pop("@variables")
embeddings = data.pop("embeddings")
attention_layers = data.pop("attention_layers")
env_mat = data.pop("env_mat")

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable env_mat 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: 28

Outside diff range and nitpick comments (19)
deepmd/jax/utils/exclude_mask.py (1)

12-16: LGTM: Well-implemented class with custom attribute handling.

The PairExcludeMask class effectively extends PairExcludeMaskDP and provides custom handling for the type_mask attribute. The implementation ensures that type_mask is always stored as a JAX array, which is crucial for compatibility with JAX-based operations.

Consider using a set for faster lookup of attribute names:

 class PairExcludeMask(PairExcludeMaskDP):
+    _jax_attributes = {"type_mask"}
     def __setattr__(self, name: str, value: Any) -> None:
-        if name in {"type_mask"}:
+        if name in self._jax_attributes:
             value = to_jax_array(value)
         return super().__setattr__(name, value)

This change allows for easier extension if more attributes need similar handling in the future.

source/tests/array_api_strict/utils/exclude_mask.py (1)

14-17: LGTM with suggestions: __setattr__ implementation is correct but could be more explicit.

The __setattr__ method correctly overrides the parent class to provide custom behavior for the type_mask attribute. However, consider the following suggestions:

  1. Add a docstring to explain the purpose of this override and what to_array_api_strict_array does.
  2. Consider using a more explicit condition, such as if name == "type_mask" instead of if name in {"type_mask"}, unless you plan to add more attributes to this set in the future.

Here's a suggested improvement:

def __setattr__(self, name: str, value: Any) -> None:
    """
    Override __setattr__ to ensure 'type_mask' is converted to a strict array API compliant array.
    
    This method intercepts assignments to 'type_mask' and applies the to_array_api_strict_array
    conversion before setting the attribute. All other attributes are set normally.
    
    Args:
        name (str): The name of the attribute being set.
        value (Any): The value to assign to the attribute.
    """
    if name == "type_mask":
        value = to_array_api_strict_array(value)
    return super().__setattr__(name, value)
source/tests/array_api_strict/common.py (3)

10-10: Consider adding the return type to the function signature.

While the docstring specifies the return type, it would be beneficial to add it to the function signature as well for better type hinting.

Consider updating the function signature as follows:

def to_array_api_strict_array(array: Optional[np.ndarray]) -> Optional[array_api_strict.Array]:

This change will provide more explicit type information and improve code readability.


11-22: Improve docstring for consistency and accuracy.

The docstring is well-structured, but there are a few inconsistencies that should be addressed:

  1. The parameter type should be Optional[np.ndarray] to match the function signature.
  2. The return type should be Optional[array_api_strict.Array] to accurately reflect the function's behavior and the use of array_api_strict.
  3. The docstring should mention that the function returns None if the input is None.

Consider updating the docstring as follows:

"""
Convert a numpy array to a JAX array.

Parameters
----------
array : Optional[np.ndarray]
    The numpy array to convert, or None.

Returns
-------
Optional[array_api_strict.Array]
    The JAX array, or None if the input is None.
"""

These changes will improve the accuracy and consistency of the documentation.


23-25: LGTM: Implementation is correct and concise.

The function correctly handles the case where the input is None and uses the appropriate method to convert the array.

Consider adding explicit error handling for invalid input types. For example:

def to_array_api_strict_array(array: Optional[np.ndarray]) -> Optional[array_api_strict.Array]:
    if array is None:
        return None
    if not isinstance(array, np.ndarray):
        raise TypeError(f"Expected np.ndarray or None, got {type(array)}")
    return array_api_strict.asarray(array)

This addition would make the function more robust against potential misuse.

deepmd/jax/common.py (1)

Line range hint 23-33: Update the docstring to reflect optional input and output.

The function signature has been updated to handle optional input and output, but the docstring doesn't reflect this change. Please update the docstring to accurately describe the new behavior.

Here's a suggested update for the docstring:

def to_jax_array(array: Optional[np.ndarray]) -> Optional[jnp.ndarray]:
    """Convert a numpy array to a JAX array or handle None input.

    Parameters
    ----------
    array : Optional[np.ndarray]
        The numpy array to convert, or None.

    Returns
    -------
    Optional[jnp.ndarray]
        The JAX tensor, or None if the input is None.
    """
source/tests/array_api_strict/utils/type_embed.py (1)

17-22: LGTM with suggestions: Custom attribute setting looks good.

The __setattr__ method implementation effectively customizes attribute setting for "econf_tebd" and "embedding_net". This approach aligns well with the PR objective of implementing JAX or Array API compatibility.

Suggestions for improvement:

  1. Consider using a more specific type hint for the value parameter instead of Any to improve type safety.
  2. The serialization and deserialization of "embedding_net" might benefit from a comment explaining its purpose (e.g., creating a deep copy or ensuring a specific format).

Here's a suggested improvement for the method signature:

from typing import Union
from numpy import ndarray
from jax import Array

def __setattr__(self, name: str, value: Union[ndarray, Array, EmbeddingNet]) -> None:

This change would provide more specific type hinting for the value parameter, improving type safety and code readability.

deepmd/jax/utils/network.py (1)

44-45: LGTM: LayerNorm class is correctly implemented.

The LayerNorm class effectively combines functionalities from LayerNormDP and NativeLayer through multiple inheritance. The empty class body is appropriate as no additional methods or attributes are needed.

Consider adding a docstring to explain the purpose of this class and its inheritance structure. For example:

class LayerNorm(LayerNormDP, NativeLayer):
    """
    A layer normalization class that combines functionality from LayerNormDP and NativeLayer.
    
    This class inherits methods and properties from both parent classes without modification.
    """
    pass
source/tests/array_api_strict/utils/network.py (1)

29-29: Nitpick: Redundant return statement in __setattr__ method.

In the __setattr__ method, the return statement is unnecessary because super().__setattr__(name, value) does not return a meaningful value (it returns None). Omitting the return statement can improve readability.

Apply this diff to remove the redundant return statement:

-        return super().__setattr__(name, value)
+        super().__setattr__(name, value)
deepmd/jax/descriptor/dpa1.py (1)

65-67: Clarify the handling of the env_mat attribute.

The attribute env_mat has a comment indicating it doesn't store any value, followed by a pass statement. To enhance code clarity, consider explicitly setting value to None for env_mat.

Apply this diff for explicit assignment:

 elif name == "env_mat":
     # env_mat doesn't store any value
+    value = None
     pass
deepmd/dpmodel/utils/exclude_mask.py (2)

118-130: Ensure consistent behavior of complex array operations across backends

The sequence of array operations involving xp.concat, xp.reshape, xp.where, xp.take, and advanced indexing should be verified for consistency across all supported array backends. Differences in backend implementations could lead to subtle bugs or unexpected behavior.


126-126: Remove commented-out code to improve code cleanliness

The line # type_j = xp.take_along_axis(ae, index, axis=1).reshape(nf, nloc, nnei) appears to be obsolete. Removing commented-out code enhances readability and maintainability.

deepmd/dpmodel/utils/nlist.py (5)

101-103: Simplify the conditional assignment of xmax using a ternary operator

To make the code more concise, consider using a ternary operator for assigning xmax.

Apply this diff to simplify the code:

-if coord.size > 0:
-    xmax = xp.max(coord) + 2.0 * rcut
-else:
-    xmax = 2.0 * rcut
+xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut
Tools
Ruff

100-103: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)


276-276: Include cell in array namespace initialization

When initializing the array namespace with array_api_compat, include all arrays (coord, atype, cell) to ensure they are compatible within the function.

Apply this diff to include cell:

-xp = array_api_compat.array_namespace(coord, atype)
+xp = array_api_compat.array_namespace(coord, atype, cell)

308-309: Use xp.transpose instead of xp.permute_dims for better compatibility

The function xp.transpose is commonly used across different array libraries and enhances readability.

Apply this diff to use xp.transpose:

-shift_vec = xp.permute_dims(shift_vec, (1, 0, 2))
+shift_vec = xp.transpose(shift_vec, (1, 0, 2))

92-93: Correct the typo in the comment: 'implemantation' to 'implementation'

There's a typo in the comment; 'implemantation' should be 'implementation'.

Apply this diff to correct the typo:

-## translated from torch implemantation by chatgpt
+## Translated from Torch implementation by ChatGPT

97-98: Fix spelling errors in docstrings

There are several typos in the docstrings, such as 'neightbor' instead of 'neighbor' and 'exptended' instead of 'extended'.

Apply this diff to correct the typos:

-"""Build neightbor list for a single frame. keeps nsel neighbors.

Parameters
----------
coord : np.ndarray
    exptended coordinates of shape [batch_size, nall x 3]
    ...
+"""Build neighbor list for a single frame. Keeps nsel neighbors.

Parameters
----------
coord : np.ndarray
    extended coordinates of shape [batch_size, nall x 3]
    ...

This improves the readability and professionalism of the documentation.

source/tests/consistent/common.py (2)

83-83: Add docstring for array_api_strict_class

To maintain consistency with other class variables, please add a docstring for array_api_strict_class.

Apply this diff to add the docstring:

 array_api_strict_class: ClassVar[Optional[type]]
+"""Array API Strict model class."""

Line range hint 267-273: Update docstring to reflect the new order of reference backends

The docstring for get_reference_backend lists the order of checking as "Order of checking for ref: DP, TF, PT." Since you've added JAX and ARRAY_API_STRICT, please update the docstring to reflect the current order.

Apply this diff to update the docstring:

     def get_reference_backend(self):
         """Get the reference backend.

-        Order of checking for ref: DP, TF, PT.
+        Order of checking for ref: DP, TF, PT, JAX, ARRAY_API_STRICT.
         """
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0b72dae and cad9034.

Files selected for processing (25)
  • deepmd/dpmodel/descriptor/dpa1.py (13 hunks)
  • deepmd/dpmodel/utils/env_mat.py (2 hunks)
  • deepmd/dpmodel/utils/exclude_mask.py (5 hunks)
  • deepmd/dpmodel/utils/network.py (6 hunks)
  • deepmd/dpmodel/utils/nlist.py (4 hunks)
  • deepmd/dpmodel/utils/region.py (5 hunks)
  • deepmd/dpmodel/utils/type_embed.py (1 hunks)
  • deepmd/jax/common.py (2 hunks)
  • deepmd/jax/descriptor/init.py (1 hunks)
  • deepmd/jax/descriptor/dpa1.py (1 hunks)
  • deepmd/jax/utils/exclude_mask.py (1 hunks)
  • deepmd/jax/utils/network.py (2 hunks)
  • source/tests/array_api_strict/init.py (1 hunks)
  • source/tests/array_api_strict/common.py (1 hunks)
  • source/tests/array_api_strict/descriptor/init.py (1 hunks)
  • source/tests/array_api_strict/descriptor/dpa1.py (1 hunks)
  • source/tests/array_api_strict/utils/init.py (1 hunks)
  • source/tests/array_api_strict/utils/exclude_mask.py (1 hunks)
  • source/tests/array_api_strict/utils/network.py (1 hunks)
  • source/tests/array_api_strict/utils/type_embed.py (1 hunks)
  • source/tests/common/dpmodel/test_descriptor_dpa1.py (1 hunks)
  • source/tests/consistent/common.py (10 hunks)
  • source/tests/consistent/descriptor/common.py (4 hunks)
  • source/tests/consistent/descriptor/test_dpa1.py (5 hunks)
  • source/tests/consistent/test_type_embedding.py (4 hunks)
Files skipped from review due to trivial changes (4)
  • deepmd/jax/descriptor/init.py
  • source/tests/array_api_strict/init.py
  • source/tests/array_api_strict/descriptor/init.py
  • source/tests/array_api_strict/utils/init.py
Additional context used
Ruff
deepmd/dpmodel/descriptor/dpa1.py

1046-1046: Local variable env_mat is assigned to but never used

Remove assignment to unused variable env_mat

(F841)

deepmd/dpmodel/utils/env_mat.py

79-82: Use ternary operator env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight instead of if-else-block

Replace if-else-block with env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight

(SIM108)

deepmd/dpmodel/utils/nlist.py

100-103: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)

GitHub Check: CodeQL
deepmd/dpmodel/descriptor/dpa1.py

[notice] 1046-1046: Unused local variable
Variable env_mat is not used.

source/tests/consistent/descriptor/test_dpa1.py

[notice] 42-42: Unused global variable
The global variable 'DescrptDPA1JAX' is not used.

Additional comments not posted (49)
deepmd/jax/utils/exclude_mask.py (2)

1-1: LGTM: Appropriate license header.

The file includes the correct LGPL-3.0-or-later license header, which is good practice for open-source projects.


2-9: LGTM: Appropriate imports.

The imports are well-organized and include all necessary components for the class implementation. The use of type hinting (Any from typing) is a good practice for improving code readability and maintainability.

source/tests/array_api_strict/utils/exclude_mask.py (2)

1-11: LGTM: File structure and imports are well-organized.

The file structure follows good practices:

  • SPDX license identifier is correctly placed at the top.
  • Imports are properly organized, separating standard library imports from local imports.

13-13: LGTM: Class definition and inheritance are appropriate.

The PairExcludeMask class is well-defined:

  • It clearly extends the functionality of PairExcludeMaskDP.
  • The class name follows the convention of the parent class.
source/tests/array_api_strict/common.py (2)

1-8: LGTM: Imports are correct and necessary.

The imports are appropriate for the function implementation. The order follows the convention of importing from the standard library first, followed by third-party libraries.


1-25: Overall assessment: Good implementation with minor improvements suggested.

The to_array_api_strict_array function is well-implemented and serves its purpose effectively. The suggestions made in the review will further enhance its robustness and documentation:

  1. Add return type to the function signature.
  2. Update the docstring for consistency and accuracy.
  3. Consider adding explicit error handling for invalid input types.

These changes will improve type hinting, documentation clarity, and error handling, making the function more maintainable and user-friendly.

deepmd/jax/common.py (4)

3-3: LGTM: Import statement updated correctly.

The addition of Optional to the import statement is consistent with the changes made to the function signature and is necessary for proper type hinting.


22-22: LGTM: Function signature updated correctly.

The change to use Optional[np.ndarray] for both input and output types allows the function to handle None input, which is a valid use case. The overloaded signatures provide clear type hints for different input types.


Line range hint 34-36: LGTM: Function implementation handles optional input correctly.

The function implementation correctly handles the new optional input case by checking for None and returning None in that case. For non-None input, it properly converts the numpy array to a JAX array. The behavior is consistent with the updated signature and overloaded definitions.


Line range hint 1-36: Summary: Improved flexibility of to_jax_array function

The changes in this PR successfully implement the feat(jax/array-api): dpa1 objective by modifying the to_jax_array function to handle optional input. This improvement allows the function to work with None values, increasing its flexibility and usability in various scenarios.

Key points:

  1. The function signature and implementation have been updated correctly.
  2. Proper type hinting has been added, including overloaded function signatures.
  3. The function behavior is consistent with the new type hints.

The only suggestion for improvement is to update the function's docstring to reflect the new optional nature of the input and output.

Overall, this is a well-implemented feature that enhances the functionality of the deepmd/jax/common.py module.

source/tests/array_api_strict/utils/type_embed.py (2)

1-13: LGTM: File structure and imports are well-organized.

The file structure follows good practices with a license identifier at the top. The imports are appropriate for the implemented functionality, and the use of relative imports suggests a well-structured project.


16-16: LGTM: Class definition aligns with PR objective.

The TypeEmbedNet class, inheriting from TypeEmbedNetDP, appears to be a wrapper or extension designed to provide custom attribute setting behavior. This aligns well with the PR objective of implementing JAX or Array API compatibility.

deepmd/jax/utils/network.py (3)

4-5: LGTM: Import statements are correctly updated.

The new imports (ClassVar and Dict) are necessary for type hinting in the NetworkCollection class. The imported classes (LayerNormDP and NetworkCollectionDP) are used as base classes for the new classes defined in this file. The imports are well-organized and follow Python's import style guidelines.

Also applies to: 11-11, 13-13


36-41: LGTM: NetworkCollection class is well-implemented.

The NetworkCollection class is correctly defined, inheriting from NetworkCollectionDP. The NETWORK_TYPE_MAP class variable is appropriately type-hinted using ClassVar and provides a clear mapping between string identifiers and network types. This implementation follows good practices and can be useful for dynamic network creation or configuration.


Line range hint 1-45: Summary: JAX-specific network implementations added successfully.

The changes in this file introduce JAX-specific implementations of NetworkCollection and LayerNorm classes, extending the existing DeepMD functionality. These additions are consistent with the PR objective (feat(jax/array-api): dpa1) and follow good coding practices. The new classes leverage multiple inheritance and type hinting to create a clear and maintainable structure.

Key points:

  1. NetworkCollection provides a mapping between string identifiers and network types, which can facilitate dynamic network creation or configuration.
  2. LayerNorm combines functionality from LayerNormDP and NativeLayer, potentially allowing for JAX-specific optimizations.

These changes appear to be a solid foundation for integrating JAX capabilities into the DeepMD framework. As the feature develops, ensure that any JAX-specific optimizations or behaviors are well-documented for users transitioning from the standard DeepMD implementation.

source/tests/common/dpmodel/test_descriptor_dpa1.py (1)

40-57: LGTM! Well-structured test for multi-frame consistency.

The new test method test_multiple_frames is well-structured and effectively tests the consistency of DescrptDPA1 across multiple frames. It properly initializes the descriptor, prepares multi-frame input data, and asserts the consistency of the output.

Enhance error messages for easier debugging.

Consider improving the error messages in the assertions to provide more context about the failing comparisons.

You could modify the error messages as follows:

-            np.testing.assert_allclose(mm0[ii][0], mm0[ii][2], err_msg=f"{ii} 0~2")
-            np.testing.assert_allclose(mm0[ii][1], mm0[ii][3], err_msg=f"{ii} 1~3")
+            np.testing.assert_allclose(mm0[ii][0], mm0[ii][2], err_msg=f"Mismatch in output {ii} between frame 0 and 2")
+            np.testing.assert_allclose(mm0[ii][1], mm0[ii][3], err_msg=f"Mismatch in output {ii} between frame 1 and 3")

Add a comment explaining the test's purpose.

It would be helpful to add a brief comment explaining the purpose of this test method at the beginning.

Consider adding a docstring like this:

def test_multiple_frames(self):
    """
    Test that DescrptDPA1 produces consistent output across multiple frames.
    This test duplicates the input data to create a two-frame scenario and
    verifies that the output for both frames is identical.
    """

Verify the correctness of frame duplication.

The test assumes that duplicating the input data (coord_ext, atype_ext, nlist) correctly represents multiple frames. It's important to ensure this approach accurately simulates multi-frame behavior in the actual use case.

To verify this, we can check if there are any frame-specific operations in the DescrptDPA1 class:

If this search returns results, it might indicate that the current test method doesn't fully capture the multi-frame behavior, and we may need to adjust the test accordingly.

Verification successful

Frame duplication approach is valid and effective for multi-frame testing.

The extensive code search results provide strong evidence that the DescrptDPA1 class and related components are designed to handle multiple frames. The test_multiple_frames method in test_descriptor_dpa1.py appropriately leverages this capability by duplicating input data to create a two-frame scenario. This approach is consistent with how multi-frame data is processed throughout the codebase, as evidenced by numerous nframes-related operations and reshaping. The test method effectively verifies the descriptor's behavior across multiple frames, which is crucial for ensuring the model's performance in real-world scenarios with varying numbers of atoms and frames.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for frame-specific operations in DescrptDPA1 class

# Test: Search for frame-related operations in DescrptDPA1
ast-grep --lang python --pattern $'class DescrptDPA1($_):
  $$$
  def $_($$$):
    $$$
    for $_ in range($_):  # Possible frame iteration
      $$$
'

Length of output: 152


Script:

#!/bin/bash
# Description: Search for methods and conditionals related to frame handling in DescrptDPA1 class

# Search for methods that might handle multiple frames
ast-grep --lang python --pattern $'class DescrptDPA1($_):
  $$$
  def $_($$$):
    $$$
    if "frame" in $_:
      $$$
'

# Additionally, search for any attributes that might relate to frames
rg --ignore-case "frame" $(fd --type f --extension py)

Length of output: 306399

deepmd/dpmodel/utils/type_embed.py (1)

110-110: Verify API compatibility and update other occurrences

The change from xp.concatenate to xp.concat aligns with newer array API standards. While this maintains the core functionality, consider the following:

  1. Ensure this change is consistent across the entire codebase.
  2. Verify that this doesn't break compatibility with older versions of the array library being used.
  3. Update the documentation if necessary to reflect this API change.

To ensure consistency and identify potential issues, run the following script:

This script will help identify if there are other occurrences of concatenate that might need updating, verify the correct import of array_api_compat, and check for any explicit imports of concatenate that might conflict with this change.

source/tests/array_api_strict/utils/network.py (4)

25-30: LGTM!

The NativeLayer class correctly overrides __setattr__ to ensure that attributes w, b, and idt are converted to strict array API arrays using to_array_api_strict_array. This maintains compatibility with the strict array API.


32-34: LGTM!

The network classes NativeNet, EmbeddingNet, and FittingNet are properly constructed using the provided factory functions and correctly utilize NativeLayer.


37-42: LGTM!

The NetworkCollection class defines NETWORK_TYPE_MAP appropriately, mapping network type strings to their corresponding classes.


45-46: Verify the method resolution order (MRO) in LayerNorm class.

The LayerNorm class inherits from both LayerNormDP and NativeLayer. Multiple inheritance can introduce complexity due to the method resolution order. Please verify that the MRO aligns with your expectations and that there are no conflicts between methods or attributes inherited from LayerNormDP and NativeLayer.

deepmd/dpmodel/utils/region.py (1)

72-72: Ensure phys2inter handles edge cases before usage

In normalize_coord, the function phys2inter is called, which relies on inverting the cell matrix. Ensure that cell is always invertible in this context or add appropriate error handling in phys2inter to prevent potential exceptions.

Also applies to: 74-74

deepmd/jax/descriptor/dpa1.py (4)

58-63: Consistent handling of None values for embeddings.

In DescrptBlockSeAtten, when name is "embeddings" or "embeddings_strip", you check if value is not None before deserializing. This is good practice. Ensure that all assignments consider the possibility of None values to prevent unexpected errors.


76-80: Consistency in deserialization methods.

In DescrptDPA1, when setting se_atten and type_embedding, you deserialize value using their respective classes. This maintains consistency across the deserialization process and ensures that attributes are properly initialized.


31-35: Verify the compatibility of NativeLayer deserialization.

In the GatedAttentionLayer class, when setting the attributes in_proj and out_proj, the code deserializes value using NativeLayer.deserialize(value.serialize()). Ensure that value indeed has serialize and deserialize methods and that this operation is appropriate for all expected types of value.


69-69: Ensure value has required attributes for emask.

When assigning to emask, the code assumes that value has ntypes and exclude_types attributes. If value may not always have these attributes, add error handling to prevent attribute access errors.

Verification successful

value consistently has the ntypes and exclude_types attributes when assigning to emask.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `value` always has `ntypes` and `exclude_types` when setting `emask`.

# Test: Search for all instances where `emask` is set and check the type of `value`.
# Expect: `value` should be an instance with `ntypes` and `exclude_types`.

rg --type python 'emask\s*='
# Further inspection may be needed based on the search results.

Length of output: 61


Script:

#!/bin/bash
# Description: Verify that `value` always has `ntypes` and `exclude_types` when setting `emask`.

# Search for all instances where `emask` is set in Python files
rg 'emask\s*=' -- '*.py'

Length of output: 71


Script:

#!/bin/bash
# Description: Verify that `value` always has `ntypes` and `exclude_types` when setting `emask`.

# Search for all instances where `emask` is set in Python files using the correct glob pattern
rg 'emask\s*=' -g '*.py'

Length of output: 1622

deepmd/dpmodel/utils/exclude_mask.py (4)

7-7: Appropriate import of array_api_compat for backend compatibility

Importing array_api_compat ensures that the code can operate with different array backends, enhancing portability and flexibility.


53-55: Utilization of array_namespace enhances array backend support

Initializing xp using array_api_compat.array_namespace(atype) and replacing NumPy functions with their xp counterparts ensures compatibility across various array backends, which is beneficial for broader support.


Line range hint 73-84: Local definition and reshaping of type_mask improves encapsulation

Defining type_mask locally and then reshaping it before assigning to self.type_mask enhances code clarity and reduces the potential for unintended side effects from external modifications.


111-114: Efficient handling of empty exclude_types with early return

By returning xp.ones_like(nlist, dtype=xp.int32) when exclude_types is empty, the function avoids unnecessary computations, thus optimizing performance.

source/tests/consistent/test_type_embedding.py (3)

16-16: LGTM

The addition of INSTALLED_ARRAY_API_STRICT to the imports ensures that the availability of array_api_strict is correctly checked.


132-138: Implementation of eval_array_api_strict is consistent

The eval_array_api_strict method is correctly implemented and follows the pattern established by other eval_* methods.


42-47: Verify the correctness of the relative import path

Please ensure that the relative import on line 45 is correct:

from ..array_api_strict.utils.type_embed import TypeEmbedNet as TypeEmbedNetStrict

Incorrect import paths can lead to ImportError at runtime. Verify that the TypeEmbedNet class exists in the specified module.

Run the following script to confirm the presence of the TypeEmbedNet class in the codebase:

Verification successful

Import path is correct.

The TypeEmbedNet class is properly defined in source/tests/array_api_strict/utils/type_embed.py, ensuring that the relative import path is valid and should not result in an ImportError at runtime.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that TypeEmbedNet is defined in array_api_strict/utils/type_embed.py

# Test: Search for the definition of TypeEmbedNet in the codebase.
rg --type python --files-with-matches "class TypeEmbedNet" | grep "array_api_strict/utils/type_embed.py"

Length of output: 135


Script:

#!/bin/bash
# Description: Verify that TypeEmbedNet is defined in array_api_strict/utils/type_embed.py

# Test: Search for the definition of TypeEmbedNet in the specified file.
rg "class TypeEmbedNet" array_api_strict/utils/type_embed.py

Length of output: 139


Script:

#!/bin/bash
# Description: Search for the TypeEmbedNet class definition in the entire codebase.

# Test: Use rg to find the definition of TypeEmbedNet in all Python files.
rg "class TypeEmbedNet" --glob "*.py"

Length of output: 449

source/tests/consistent/descriptor/common.py (3)

6-6: Import statement for numpy added correctly.

The import of numpy as np is appropriate and used correctly in the code.


17-18: Conditional installation flags updated.

The addition of INSTALLED_ARRAY_API_STRICT and INSTALLED_JAX ensures that the imports and method definitions for these libraries are conditionally included based on their availability.


36-41: Conditional imports for JAX and Array API Strict are properly implemented.

The imports under if INSTALLED_JAX and if INSTALLED_ARRAY_API_STRICT correctly handle the inclusion of jnp from JAX and array_api_strict when these libraries are installed.

deepmd/dpmodel/utils/nlist.py (4)

9-9: Import array_api_compat seems appropriate

The addition of array_api_compat ensures compatibility with different array-like structures, which is beneficial for extending support across various backends.


94-96: Initialize array namespace xp for array compatibility

The introduction of xp using array_api_compat.array_namespace and replacing np functions with xp functions enhances compatibility with multiple array libraries.


161-161: Initialize array namespace xp in nlist_distinguish_types function

Ensure that the array namespace xp is correctly initialized in the nlist_distinguish_types function for consistent array operations.


168-168: Confirm compatibility of xp.take_along_axis with Array API

Ensure that xp.take_along_axis is available and behaves as expected in the Array API compatibility layer, as not all array libraries may support it fully.

Run the following script to check the availability and usage of take_along_axis:

deepmd/dpmodel/utils/network.py (4)

151-162: Refactored deserialization improves clarity

The changes in the deserialize method simplify the unpacking and assignment of variables, enhancing code readability and maintainability.


371-375: Correct initialization of weights and biases in LayerNorm

Initializing weights to ones and biases to zeros is appropriate for LayerNorm. The use of xp.ones_like and xp.zeros_like ensures compatibility with different array backends.


483-488: Validation of axis calculations in normalization

The calculation of axes using tuple(range(-len(shape), 0)) correctly targets the last len(shape) dimensions, ensuring proper computation of mean and variance for layer normalization.


260-260: Ensure compatibility of xp.concat across backends

While replacing xp.concatenate with xp.concat aligns with the array API standards, please verify that all target backends support xp.concat, as some may only implement xp.concatenate.

Run the following script to check for the availability of concat in the array backends:

deepmd/dpmodel/descriptor/dpa1.py (5)

11-11: Added array_api_compat import for array backend compatibility

The import statement of array_api_compat is correct and necessary for ensuring compatibility with various array backends.


62-66: Handling NaN values without nan_to_num

The original line using xp.nan_to_num(x) is commented out, and replaced with x = xp.where(xp.isnan(x), xp.zeros_like(x), x). Since xp.nan_to_num may not be available in some array_api_compat backends, this substitution effectively handles NaN values.


70-71: Normalized vectors using array API compatible functions

The use of xp.linalg.vector_norm ensures that vector normalization is compatible across different array backends.


480-480: Initializing array namespace in call method

The addition of xp = array_api_compat.array_namespace(coord_ext, atype_ext, nlist) ensures that all subsequent array operations within the call method use the appropriate namespace for compatibility.


484-487: Reshaping and indexing with array API functions

The use of xp.reshape and xp.take functions aligns with array API standards, ensuring compatibility when manipulating arrays.

deepmd/dpmodel/utils/region.py Show resolved Hide resolved
deepmd/dpmodel/utils/region.py Show resolved Hide resolved
deepmd/dpmodel/utils/region.py Show resolved Hide resolved
deepmd/dpmodel/utils/region.py Show resolved Hide resolved
deepmd/dpmodel/utils/region.py Show resolved Hide resolved
deepmd/dpmodel/utils/network.py Outdated Show resolved Hide resolved
deepmd/dpmodel/descriptor/dpa1.py Show resolved Hide resolved
deepmd/dpmodel/descriptor/dpa1.py Show resolved Hide resolved
deepmd/dpmodel/descriptor/dpa1.py Show resolved Hide resolved
deepmd/dpmodel/descriptor/dpa1.py Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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: 0

Outside diff range and nitpick comments (5)
deepmd/dpmodel/utils/nlist.py (1)

101-109: LGTM: Improved virtual atom handling with a minor optimization suggestion

The changes effectively adapt the virtual atom handling to use the new array API. The use of xp.where for conditional value setting is appropriate and maintains the original logic.

Consider using a ternary operator for a more concise expression:

xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

This change would slightly improve readability without altering the functionality.

deepmd/dpmodel/descriptor/dpa1.py (4)

Line range hint 351-368: Note unimplemented method and approve implemented ones

The compute_input_stats method is not implemented and raises a NotImplementedError. This suggests that this functionality is not yet available or needs to be implemented in the future.

The set_stat_mean_and_stddev and get_stat_mean_and_stddev methods are correctly implemented, delegating to the se_atten attribute, which is consistent with the class design.

Would you like assistance in implementing the compute_input_stats method or creating a GitHub issue to track this task?


Line range hint 407-501: Replace xp.concat with xp.concatenate for array API compliance

The call method is well-implemented and correctly computes the descriptor based on the input data. The use of array_api_compat enhances compatibility across different array libraries, which is a good practice.

However, there are several instances where xp.concat is used. For better compliance with the array API standard, these should be replaced with xp.concatenate.

Please apply the following changes:

- grrg = xp.concat(
+ grrg = xp.concatenate(
- ss = xp.concat([ss, atype_embd_nlist, atype_embd_nnei], axis=-1)
+ ss = xp.concatenate([ss, atype_embd_nlist, atype_embd_nnei], axis=-1)
- ss = xp.concat([ss, atype_embd_nlist], axis=-1)
+ ss = xp.concatenate([ss, atype_embd_nlist], axis=-1)
- tt = xp.concat([atype_embd_nlist, atype_embd_nnei], axis=-1)
+ tt = xp.concatenate([atype_embd_nlist, atype_embd_nnei], axis=-1)

These changes will ensure consistency with the array API standard across different backend implementations.


Line range hint 563-605: Remove unused variable and approve deserialization implementation

The deserialize class method is well-implemented, correctly reconstructing a DescrptDPA1 object from a serialized dictionary. The version compatibility check is a good practice, and the reconstruction of nested objects is handled properly.

However, there's an unused variable that should be removed:

- env_mat = data.pop("env_mat")

This line assigns a value to env_mat, but the variable is never used afterwards. Removing it will clean up the code and eliminate unnecessary assignments.

Overall, the deserialization process is comprehensive and correct.


Line range hint 633-1445: Replace remaining xp.concat with xp.concatenate and approve class implementations

The implementations of DescrptBlockSeAtten, NeighborGatedAttention, NeighborGatedAttentionLayer, and GatedAttentionLayer classes are correct and consistent with the overall descriptor design. They properly implement the necessary methods for initialization, computation, serialization, and deserialization.

However, there are a few remaining instances of xp.concat that should be replaced with xp.concatenate for consistency with the array API standard:

In the DescrptBlockSeAtten class:

- ss = xp.concat([ss, atype_embd_nlist, atype_embd_nnei], axis=-1)
+ ss = xp.concatenate([ss, atype_embd_nlist, atype_embd_nnei], axis=-1)
- ss = xp.concat([ss, atype_embd_nlist], axis=-1)
+ ss = xp.concatenate([ss, atype_embd_nlist], axis=-1)
- tt = xp.concat([atype_embd_nlist, atype_embd_nnei], axis=-1)
+ tt = xp.concatenate([atype_embd_nlist, atype_embd_nnei], axis=-1)

These changes will ensure consistency across the entire implementation.

Overall, the classes are well-implemented, with proper use of array operations and attention mechanisms. The serialization and deserialization methods for each class ensure that the model state can be correctly saved and loaded.

Tools
Ruff

1047-1047: Local variable env_mat is assigned to but never used

Remove assignment to unused variable env_mat

(F841)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b3f0dd7 and 7de9ee3.

Files selected for processing (2)
  • deepmd/dpmodel/descriptor/dpa1.py (13 hunks)
  • deepmd/dpmodel/utils/nlist.py (4 hunks)
Additional context used
Ruff
deepmd/dpmodel/descriptor/dpa1.py

1047-1047: Local variable env_mat is assigned to but never used

Remove assignment to unused variable env_mat

(F841)

deepmd/dpmodel/utils/nlist.py

100-103: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)

Additional comments not posted (17)
deepmd/dpmodel/utils/nlist.py (11)

9-10: LGTM: Enhanced array compatibility

The addition of array_api_compat import is a good step towards improving compatibility with different array libraries, which aligns well with the overall changes in this file.


94-96: LGTM: Flexible array operations

The introduction of xp using array_api_compat.array_namespace enhances flexibility in array operations. The reshaping of coord maintains consistency with the original implementation while leveraging the new array API.


115-116: Optimize distance calculation using broadcasting

The current calculation of diff is correct but may consume significant memory for large arrays.

As suggested in a previous review, consider optimizing the operation to improve performance:

-diff = (
-    xp.reshape(coord1, [batch_size, -1, 3])[:, None, :, :]
-    - xp.reshape(coord0, [batch_size, -1, 3])[:, :, None, :]
-)
+coord1_reshaped = xp.reshape(coord1, (batch_size, -1, 3))
+coord0_reshaped = xp.reshape(coord0, (batch_size, -1, 3))
+diff = coord0_reshaped[:, :, xp.newaxis, :] - coord1_reshaped[:, xp.newaxis, :, :]

This optimization reduces the need for large intermediate arrays and takes advantage of broadcasting for better efficiency.


131-144: LGTM: Consistent use of array API for padding and masking

The changes in this segment effectively adapt the padding and masking operations to use the new array API. The logic remains consistent with the original implementation, while leveraging xp methods like xp.logical_or and xp.where. This ensures compatibility and maintains the intended functionality.


161-178: LGTM: Effective adaptation of type distinction logic to array API

The changes in the nlist_distinguish_types function successfully adapt the atom type distinction logic to use the new array API. The use of xp methods like xp.tile, xp.take_along_axis, and xp.argsort is appropriate and maintains the original functionality while ensuring compatibility with different array libraries.


276-283: LGTM: Consistent adaptation to array API in ghost coordinate extension

The changes in this segment of extend_coord_with_ghosts function effectively adapt the initial setup and the handling of the cell is None case to use the new array API. The use of xp methods maintains the original logic while ensuring compatibility with different array libraries.


285-292: LGTM: Effective adaptation of array operations in coordinate extension

The changes in this segment successfully adapt the array reshaping and nbuff calculations to use the new array API. The use of xp methods for operations like reshape, astype, and arange is appropriate and maintains the original functionality while ensuring compatibility with different array libraries.


302-317: LGTM: Effective adaptation of complex array operations

The changes in this final segment successfully adapt complex array operations to use the new array API. Notable improvements include:

  1. Appropriate use of xp methods for array manipulations.
  2. Replacement of einsum with tensordot, which maintains the original functionality while being compatible with the array API.

These changes ensure compatibility with different array libraries while preserving the intended behavior of the extend_coord_with_ghosts function.


Line range hint 1-317: Summary: Successful adaptation to array API with minor improvements needed

The changes in this file effectively adapt the neighbor list calculations to use the array_api_compat library, improving compatibility with different array libraries. The majority of the changes are well-implemented and maintain the original functionality.

Key points:

  1. Consistent use of the xp namespace for array operations.
  2. Appropriate adaptation of complex calculations to the new API.
  3. Maintained logic and functionality throughout the changes.

However, a few areas require attention:

  1. Potential optimization of the diff calculation for memory efficiency.
  2. Addressing the use of xp.eye for non-square matrices.
  3. Correcting the use of xp.linalg.outer to xp.outer.

Addressing these minor issues will further improve the code's robustness and efficiency. Overall, the transition to the array API is a positive change that enhances the flexibility and compatibility of the codebase.

Tools
Ruff

100-103: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)


119-123: ⚠️ Potential issue

Potential issue with xp.eye for non-square matrices

The changes to use xp.linalg.vector_norm and xp sorting methods are appropriate adaptations to the new array API. However, there's a potential issue with the use of xp.eye.

As noted in a previous review, the use of xp.eye(nloc, nall, dtype=diff.dtype) may raise an error if nloc and nall are not equal, as the eye function in the Array API standard typically supports only square matrices.

Consider replacing this line with an alternative approach:

-rr -= xp.eye(nloc, nall, dtype=diff.dtype)[xp.newaxis, :, :]
+# Create a mask to set self-distances to a large value
+self_mask = xp.arange(nloc)[:, xp.newaxis] == xp.arange(nall)
+rr += xp.where(self_mask, xp.inf, 0)[xp.newaxis, :, :]

This ensures that self-distances are effectively ignored during sorting without relying on xp.eye for non-square matrices.


293-301: ⚠️ Potential issue

Incorrect use of xp.linalg.outer instead of xp.outer

As noted in a previous review, the outer function should be called from the top-level namespace xp, not from xp.linalg. Using xp.linalg.outer may result in an AttributeError.

Please apply the following changes to fix the function calls:

-xyz = xp.linalg.outer(xi, xp.asarray([1, 0, 0]))[:, xp.newaxis, xp.newaxis, :]
+xyz = xp.outer(xi, xp.asarray([1, 0, 0]))[:, xp.newaxis, xp.newaxis, :]

...

-xyz = xyz + xp.linalg.outer(yi, xp.asarray([0, 1, 0]))[xp.newaxis, :, xp.newaxis, :]
+xyz = xyz + xp.outer(yi, xp.asarray([0, 1, 0]))[xp.newaxis, :, xp.newaxis, :]

...

-xyz = xyz + xp.linalg.outer(zi, xp.asarray([0, 0, 1]))[xp.newaxis, xp.newaxis, :, :]
+xyz = xyz + xp.outer(zi, xp.asarray([0, 0, 1]))[xp.newaxis, xp.newaxis, :, :]

This ensures compatibility with the Array API, as outer is typically in the main namespace rather than the linalg submodule.

deepmd/dpmodel/descriptor/dpa1.py (6)

Line range hint 1-161: LGTM: Well-structured imports and class definition

The import statements are organized correctly, and the DescrptDPA1 class is well-defined with appropriate inheritance. The detailed docstring provides comprehensive information about the descriptor, its parameters, and mathematical formulation, which is excellent for code maintainability and understanding.


Line range hint 162-292: LGTM: Comprehensive initialization with proper error handling

The init method is well-structured and initializes all necessary attributes. It handles backward compatibility with the deprecated 'stripped_type_embedding' parameter and properly raises NotImplementedError for unsupported features. The initialization of se_atten and type_embedding objects is correct and consistent with the class design.


Line range hint 294-349: LGTM: Well-implemented getter methods

The getter methods are correctly implemented, providing access to various descriptor properties. The delegation to the se_atten attribute maintains consistency and reduces code duplication. The additional logic in get_dim_out for concatenating output_tebd is appropriate for the descriptor's functionality.


Line range hint 370-405: LGTM: Comprehensive type map update handling

The change_type_map method is well-implemented, handling type map changes comprehensively. It correctly updates various attributes, remaps indices, and handles new types appropriately. The use of extend_descrpt_stat for new types ensures that the descriptor statistics are properly updated.


Line range hint 503-561: LGTM: Comprehensive serialization implementation

The serialize method is well-implemented, providing a comprehensive serialization of the descriptor's state. It correctly includes all necessary attributes and handles nested objects (embeddings, attention_layers, etc.) appropriately. The special case for 'strip' tebd_input_mode is properly addressed, ensuring all relevant data is captured in the serialized output.


Line range hint 607-631: LGTM: Correct implementation of selection update

The update_sel class method is well-implemented. It correctly updates the selection parameters and performs neighbor statistics using the UpdateSel().update_one_sel method. The method returns the updated local data and the minimum neighbor distance, which provides necessary information for further processing in the descriptor setup.

Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 99.41349% with 2 lines in your changes missing coverage. Please review.

Project coverage is 83.51%. Comparing base (d667929) to head (75a0b05).
Report is 4 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/jax/utils/network.py 92.59% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4160      +/-   ##
==========================================
+ Coverage   83.45%   83.51%   +0.05%     
==========================================
  Files         537      539       +2     
  Lines       52148    52347     +199     
  Branches     3047     3047              
==========================================
+ Hits        43520    43715     +195     
- Misses       7683     7685       +2     
- Partials      945      947       +2     

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

Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

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

  • Do we change back to take_along_axis when it is supported?
  • for modules implemented by jax, you may want to take a look at flax

@njzjz
Copy link
Member Author

njzjz commented Sep 25, 2024

  • Do we change back to take_along_axis when it is supported?

take_along_axis has been added to the Array API in data-apis/array-api#816, but I don't expect it to be released quickly. Note that jax does support take_along_axis, but I hope it can be more general and driven by any Array API library.

However, in d65206f, I add a Python implementation of take_along_axis (per data-apis/array-api#177 (comment)). It also uses arange and take, which is the same as my previous commits but more general.

  • for modules implemented by jax, you may want to take a look at flax

Is there any advantage to using flax?

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

🧹 Outside diff range and nitpick comments (8)
deepmd/dpmodel/utils/env_mat.py (1)

70-74: Approve changes and suggest improvements

The use of xp.astype and xp.expand_dims for weight calculation is appropriate and consistent with the array API compatibility layer. However, there are two suggestions for improvement:

  1. The if-else block can be simplified using a ternary operator for better readability.
  2. The function xp.concat should be xp.concatenate to align with the array API standard.

Consider applying the following changes:

- if radial_only:
-     env_mat = t0 * weight
- else:
-     env_mat = xp.concat([t0, t1], axis=-1) * weight
+ env_mat = t0 * weight if radial_only else xp.concatenate([t0, t1], axis=-1) * weight

This change enhances code readability and ensures compatibility with the array API standard.

🧰 Tools
Ruff

71-74: Use ternary operator env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight instead of if-else-block

Replace if-else-block with env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight

(SIM108)

deepmd/dpmodel/utils/nlist.py (1)

Line range hint 1-321: Summary of changes and recommendations

The changes in this file successfully integrate the array_api_compat library, enhancing compatibility with different array-like structures. Most of the changes are well-implemented and maintain the original functionality. However, there are a few areas that require attention:

  1. The distance calculation in build_neighbor_list could be optimized for better memory efficiency.
  2. The use of xp.eye in build_neighbor_list may cause issues with non-square matrices.
  3. The xp.linalg.outer calls in extend_coord_with_ghosts should be changed to xp.outer.
  4. A minor optimization using a ternary operator can be applied in build_neighbor_list.

Addressing these points will further improve the code's efficiency and correctness while maintaining the enhanced array compatibility.

🧰 Tools
Ruff

104-107: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)

deepmd/dpmodel/descriptor/dpa1.py (6)

65-69: Improved np_softmax function with better compatibility and NaN handling

The changes to the np_softmax function enhance its compatibility across different array libraries and improve its robustness by explicitly handling NaN values. These are positive improvements.

However, there's a minor optimization opportunity:

Consider combining the NaN handling and the exponential calculation to reduce the number of operations:

- x = xp.where(xp.isnan(x), xp.zeros_like(x), x)
- e_x = xp.exp(x - xp.max(x, axis=axis, keepdims=True))
+ max_x = xp.max(xp.where(xp.isnan(x), -xp.inf, x), axis=axis, keepdims=True)
+ e_x = xp.exp(xp.where(xp.isnan(x), 0, x - max_x))

This change would handle NaN values and compute the exponential in a single pass, potentially improving performance.


986-1029: Comprehensive serialization method for DescrptDPA1

The new serialize method provides a comprehensive way to convert the DescrptDPA1 object into a dictionary format. This is crucial for saving and loading models, and the method covers all relevant attributes, including special handling for the "strip" mode.

To improve maintainability, consider using a constant for the version number:

+ VERSION = 1
...
- "@version": 1,
+ "@version": self.VERSION,

This would make it easier to update the version number in the future if needed.


1031-1055: Robust deserialization method for DescrptDPA1

The new deserialize class method provides a robust way to reconstruct a DescrptDPA1 object from a serialized dictionary. The method includes version compatibility checks and correctly handles special cases like the "strip" mode.

Consider adding error handling for missing keys in the input dictionary:

+ required_keys = ["embeddings", "attention_layers", "env_mat", "tebd_input_mode"]
+ for key in required_keys:
+     if key not in data:
+         raise ValueError(f"Missing required key '{key}' in serialized data")

This would make the deserialization process more robust against incomplete or corrupted input data.

🧰 Tools
Ruff

1041-1041: Local variable env_mat is assigned to but never used

Remove assignment to unused variable env_mat

(F841)


Line range hint 1161-1180: Comprehensive serialization method for NeighborGatedAttention

The new serialize method provides a thorough way to convert the NeighborGatedAttention object into a dictionary format. This is essential for saving and loading models, and the method covers all relevant attributes, including the serialized attention layers.

For consistency with the DescrptDPA1 class, consider adding a VERSION class attribute:

+ VERSION = 1
...
- "@version": 1,
+ "@version": self.VERSION,

This would maintain a consistent approach to versioning across the codebase.


Line range hint 1182-1197: Robust deserialization method for NeighborGatedAttention

The new deserialize class method provides a solid way to reconstruct a NeighborGatedAttention object from a serialized dictionary. The method includes version compatibility checks and correctly reconstructs the attention layers.

Consider adding error handling for missing or invalid data:

+ if "attention_layers" not in data:
+     raise ValueError("Missing required key 'attention_layers' in serialized data")
+ if not isinstance(data["attention_layers"], list):
+     raise TypeError("'attention_layers' must be a list")

This would make the deserialization process more robust against incomplete or incorrectly formatted input data.


Line range hint 1349-1405: Improved GatedAttentionLayer.call method with better compatibility and potential performance enhancements

The changes to the call method enhance its compatibility across different array libraries by using array_api_compat. The manual linear projection and explicit reshaping operations are likely to be more efficient. These changes improve the overall quality and potential performance of the function.

Consider using xp.einsum for the matrix multiplications, which might be more readable and potentially more efficient:

- attn_weights = q @ xp.permute_dims(k, (0, 1, 3, 2))
+ attn_weights = xp.einsum('bhid,bhjd->bhij', q, k)

- o = attn_weights @ v
+ o = xp.einsum('bhij,bhjd->bhid', attn_weights, v)

This change would make the operations more explicit and might allow for better optimizations by the underlying array library.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7de9ee3 and d65206f.

📒 Files selected for processing (5)
  • deepmd/dpmodel/array_api.py (2 hunks)
  • deepmd/dpmodel/descriptor/dpa1.py (13 hunks)
  • deepmd/dpmodel/utils/env_mat.py (3 hunks)
  • deepmd/dpmodel/utils/exclude_mask.py (5 hunks)
  • deepmd/dpmodel/utils/nlist.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • deepmd/dpmodel/utils/exclude_mask.py
🧰 Additional context used
Ruff
deepmd/dpmodel/descriptor/dpa1.py

1041-1041: Local variable env_mat is assigned to but never used

Remove assignment to unused variable env_mat

(F841)

deepmd/dpmodel/utils/env_mat.py

71-74: Use ternary operator env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight instead of if-else-block

Replace if-else-block with env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight

(SIM108)

deepmd/dpmodel/utils/nlist.py

104-107: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)

🔇 Additional comments not posted (22)
deepmd/dpmodel/utils/env_mat.py (11)

15-15: LGTM: New import for array API compatibility

The addition of xp_take_along_axis import is consistent with the integration of the array API compatibility layer. This custom function likely provides a unified interface for different array backends.


48-51: LGTM: Array API compatibility integration

The introduction of the xp variable and the use of xp.reshape instead of np.reshape are good changes that enhance the flexibility of the code. This allows the function to work with different array backends, improving its compatibility and reusability.


53-53: LGTM: Consistent type handling in masking operation

The use of xp.astype in the masking operation ensures type consistency across different array backends. This change aligns well with the array API compatibility integration.


55-56: LGTM: Improved indexing with array API compatibility

The use of xp.tile, xp.reshape, and xp_take_along_axis demonstrates a good adaptation to the array API compatibility layer. These changes allow for consistent indexing operations across different array backends, enhancing the code's flexibility.


58-60: LGTM: Consistent use of array API in reshaping operations

The changes to use xp.reshape instead of np.reshape are appropriate and maintain consistency with the array API compatibility layer. This ensures that reshaping operations work across different array backends.


64-64: LGTM: Updated norm calculation for array API compatibility

The change from np.linalg.norm to xp.linalg.vector_norm is appropriate for array API compatibility. Note that the function name is slightly different (vector_norm instead of norm), which aligns with the array API standard. This change ensures consistent norm calculations across different array backends.


66-66: LGTM: Consistent mask handling with array API

The use of xp.astype and xp.expand_dims for mask handling is appropriate and maintains consistency with the array API compatibility layer. This ensures that mask operations work correctly across different array backends.


75-75: LGTM: Consistent type handling in return statement

The use of xp.astype and xp.expand_dims in the return statement is appropriate and maintains consistency with the array API compatibility layer. This ensures that the returned values have the correct type and shape across different array backends.


127-127: LGTM: Array API compatibility in class method

The introduction of the xp variable using array_api_compat.array_namespace in the class method is consistent with the array API compatibility changes throughout the file. This allows the method to work seamlessly with different array backends, improving its flexibility and reusability.


132-134: LGTM: Consistent array operations in data normalization

The use of xp.reshape and xp.take in the data normalization operations is appropriate and maintains consistency with the array API compatibility layer. These changes ensure that the normalization process works correctly across different array backends, enhancing the method's flexibility.


Line range hint 1-164: Summary: Successful integration of array API compatibility

The changes in this file consistently implement array API compatibility, allowing the code to work with different array backends. This enhances the flexibility and reusability of the EnvMat class and its associated functions. The core logic of the code remains intact, with only the necessary changes to use the abstract xp namespace instead of direct NumPy operations.

One minor suggestion for improvement was made regarding the use of a ternary operator and the correct function name for concatenation. Overall, the changes are well-implemented and improve the code's compatibility with different array libraries.

🧰 Tools
Ruff

71-74: Use ternary operator env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight instead of if-else-block

Replace if-else-block with env_mat = t0 * weight if radial_only else xp.concat([t0, t1], axis=-1) * weight

(SIM108)

deepmd/dpmodel/utils/nlist.py (8)

9-15: LGTM: Enhanced array compatibility

The addition of array_api_compat and xp_take_along_axis imports improves the flexibility of array operations in the code, allowing for better compatibility with different array-like structures.


98-100: LGTM: Consistent use of array namespace

The introduction of xp as the array namespace and its use in reshaping coord is consistent with the new imports and enhances array compatibility while maintaining functional equivalence.


105-113: LGTM: Consistent array operations

The changes in this segment maintain the original logic for handling virtual atoms while consistently using the xp namespace for array operations. This enhances compatibility without altering the functionality.


135-148: LGTM: Consistent use of array operations

The changes in this segment correctly use xp.concatenate, xp.logical_or, and xp.where for array operations. This maintains the original functionality while enhancing compatibility with different array-like structures.


165-173: LGTM: Consistent array operations in nlist_distinguish_types

The changes in this segment correctly use the xp namespace for array operations and xp_take_along_axis for indexing. This enhances compatibility while maintaining the original functionality of the nlist_distinguish_types function.


176-182: LGTM: Consistent use of array operations

The changes in this segment correctly use the xp namespace for all array operations, including sorting, masking, and concatenation. This enhances compatibility with different array-like structures while maintaining the original functionality.


280-282: LGTM: Consistent array operations in extend_coord_with_ghosts

The introduction of xp as the array namespace and its use in xp.tile and xp.arange operations is consistent with the new array compatibility approach. This enhances flexibility while maintaining the original functionality.


285-290: LGTM: Consistent use of array operations

The changes in this segment correctly use the xp namespace for reshaping operations on coord and cell. This enhances compatibility with different array-like structures while maintaining the original functionality.

deepmd/dpmodel/descriptor/dpa1.py (3)

11-21: Improved array compatibility and custom array operations

The addition of array_api_compat and xp_take_along_axis enhances the code's compatibility with different array libraries and introduces custom array operations. This change is likely to improve the flexibility and portability of the code across different array backends.


73-74: Improved np_normalize function with better compatibility and efficiency

The changes to the np_normalize function enhance its compatibility across different array libraries by using array_api_compat. The use of xp.linalg.vector_norm for normalization is likely to be more efficient and numerically stable than a manual implementation. These changes are well-considered and improve the overall quality of the function.


483-483: Consistent improvements in array compatibility throughout the file

The numerous small changes throughout the file, particularly the use of array_api_compat and the xp namespace, significantly enhance the code's compatibility with different array libraries. These modifications make the code more flexible and portable, allowing it to work seamlessly with various array backends.

Key improvements include:

  1. Consistent use of xp namespace for array operations.
  2. Replacing NumPy-specific functions with more generic alternatives.
  3. Using xp_take_along_axis for indexed array operations.
  4. Adjusting reshaping and dimension permutation operations to use the xp namespace.

These changes collectively contribute to a more robust and adaptable implementation of the DPA-1 descriptor.

Also applies to: 485-485, 487-490, 502-503, 549-550, 696-696, 701-701, 709-709, 711-711, 716-716, 724-724, 852-852, 854-855, 866-866, 868-869, 882-882, 890-890, 892-893, 895-895, 897-897, 899-899, 901-901, 905-907, 909-911, 915-916, 922-922, 925-925, 935-935, 942-942, 948-953, 959-960, 964-965, 967-968, 971-975

deepmd/dpmodel/utils/nlist.py Show resolved Hide resolved
deepmd/dpmodel/utils/nlist.py Show resolved Hide resolved
deepmd/dpmodel/utils/nlist.py Show resolved Hide resolved
deepmd/dpmodel/utils/nlist.py Show resolved Hide resolved
deepmd/dpmodel/descriptor/dpa1.py Show resolved Hide resolved
deepmd/dpmodel/array_api.py Outdated Show resolved Hide resolved
deepmd/dpmodel/array_api.py Outdated Show resolved Hide resolved
@njzjz
Copy link
Member Author

njzjz commented Sep 25, 2024

Here, I make some additional notes for Array API:

  • arr.reshape(shape) and arr.astype(dtype) are not supported; should use xp.reshape(arr, shape) and xp.astype(arr, dtype);
  • concatenate -> concat;
  • transpose -> permute_dims;
  • arr[integer_idx] is not supported; use xp.take(arr, integer_idx);
  • einsum is not supported.

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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d65206f and aff0b42.

📒 Files selected for processing (1)
  • deepmd/dpmodel/array_api.py (2 hunks)
🧰 Additional context used
Ruff
deepmd/dpmodel/array_api.py

59-62: Use ternary operator indices = xp.reshape(indices, (-1, n)) if n != 0 else xp.reshape(indices, (0, 0)) instead of if-else-block

Replace if-else-block with indices = xp.reshape(indices, (-1, n)) if n != 0 else xp.reshape(indices, (0, 0))

(SIM108)

🔇 Additional comments not posted (1)
deepmd/dpmodel/array_api.py (1)

4-4: LGTM: Import and comments provide necessary context

The added import statement for array_api_compat is essential for the new functions. The comments explaining the implementation of take_along_axis provide valuable context about the origin and purpose of the code.

Also applies to: 34-37

deepmd/dpmodel/array_api.py Show resolved Hide resolved
deepmd/dpmodel/array_api.py Show resolved Hide resolved
Signed-off-by: Jinzhe Zeng <[email protected]>
@wanghan-iapcm
Copy link
Collaborator

Is there any advantage to using flax?

jax does not natively support any equivalent to torch.nn.Module. flax provides something similar, which may be used for better support on parameter initialization and backward propagation.

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

🧹 Outside diff range and nitpick comments (10)
deepmd/jax/env.py (1)

8-17: Consider documenting the rationale for introducing Flax's nnx.

The addition of nnx from Flax suggests a shift towards using Flax for JAX-based modules. To ensure clarity for all team members and future contributors, it would be beneficial to document:

  1. The rationale behind introducing Flax's nnx.
  2. The expected benefits and use cases within the project.
  3. Any architectural or development practice changes this introduction might entail.

This documentation could be added as a comment in this file or in a separate document (e.g., README or ARCHITECTURE.md).

deepmd/jax/utils/type_embed.py (2)

Line range hint 18-24: LGTM: __setattr__ implementation, with a suggestion for improvement.

The __setattr__ method is well-implemented, handling specific attributes appropriately:

  • econf_tebd is converted to a JAX array.
  • embedding_net is serialized and deserialized, likely to ensure compatibility with Flax.

The use of type annotations and super().__setattr__ is commendable.

Consider adding a brief comment explaining the rationale behind the special handling of econf_tebd and embedding_net. This would improve code maintainability. For example:

def __setattr__(self, name: str, value: Any) -> None:
    # Convert econf_tebd to JAX array for compatibility
    if name == "econf_tebd":
        value = to_jax_array(value)
    # Ensure embedding_net is properly serialized for Flax compatibility
    elif name == "embedding_net":
        value = EmbeddingNet.deserialize(value.serialize())
    return super().__setattr__(name, value)

Line range hint 1-24: Overall assessment: Changes align with PR objectives and improve JAX/Flax integration.

The modifications to TypeEmbedNet successfully integrate Flax functionality:

  1. The @flax_module decorator likely enhances support for parameter initialization and backward propagation.
  2. The __setattr__ method ensures proper handling of JAX arrays and Flax-compatible serialization.

These changes align well with the PR objectives of using Flax for JAX-implemented modules. The implementation is correct and consistent with the stated goals.

To further improve this implementation:

  1. Consider adding documentation explaining the benefits of using the @flax_module decorator for this specific class.
  2. Add comments in the __setattr__ method to clarify the rationale behind the special handling of econf_tebd and embedding_net.
  3. If not already present, consider adding unit tests to verify the correct behavior of the Flax integration, especially focusing on the serialization and deserialization of the embedding_net attribute.
deepmd/jax/utils/network.py (6)

25-31: LGTM: NativeLayer implementation looks good.

The NativeLayer class is correctly decorated with @flax_module and inherits from NativeLayerDP. The custom __setattr__ method appropriately converts specific attributes to JAX arrays using to_jax_array.

Consider using a set for faster lookup of attribute names:

CONVERT_TO_JAX = {"w", "b", "idt"}

def __setattr__(self, name: str, value: Any) -> None:
    if name in CONVERT_TO_JAX:
        value = to_jax_array(value)
    return super().__setattr__(name, value)

This change would slightly improve performance, especially if the method is called frequently.


33-35: LGTM: NativeNet class is correctly defined.

The NativeNet class is appropriately decorated with @flax_module and inherits from the result of make_multilayer_network(NativeLayer, NativeOP). This structure aligns with the PR objectives of implementing JAX-specific versions of existing classes.

Consider adding a docstring to explain the purpose of this class and its relationship to the parent class:

@flax_module
class NativeNet(make_multilayer_network(NativeLayer, NativeOP)):
    """
    A JAX-compatible implementation of a multi-layer network.
    
    This class inherits all functionality from the parent class created by
    make_multilayer_network, using NativeLayer and NativeOP as building blocks.
    """
    pass

38-40: LGTM: EmbeddingNet class is correctly defined.

The EmbeddingNet class is appropriately decorated with @flax_module and inherits from the result of make_embedding_network(NativeNet, NativeLayer). This structure is consistent with the implementation of JAX-specific versions of existing classes.

Consider adding a docstring to explain the purpose of this class and its relationship to the parent class:

@flax_module
class EmbeddingNet(make_embedding_network(NativeNet, NativeLayer)):
    """
    A JAX-compatible implementation of an embedding network.
    
    This class inherits all functionality from the parent class created by
    make_embedding_network, using NativeNet and NativeLayer as building blocks.
    """
    pass

43-45: LGTM: FittingNet class is correctly defined.

The FittingNet class is appropriately decorated with @flax_module and inherits from the result of make_fitting_network(EmbeddingNet, NativeNet, NativeLayer). This structure is consistent with the implementation of JAX-specific versions of existing classes.

Consider adding a docstring to explain the purpose of this class and its relationship to the parent class:

@flax_module
class FittingNet(make_fitting_network(EmbeddingNet, NativeNet, NativeLayer)):
    """
    A JAX-compatible implementation of a fitting network.
    
    This class inherits all functionality from the parent class created by
    make_fitting_network, using EmbeddingNet, NativeNet, and NativeLayer as building blocks.
    """
    pass

48-54: LGTM: NetworkCollection class is well-structured.

The NetworkCollection class is appropriately decorated with @flax_module and inherits from NetworkCollectionDP. The NETWORK_TYPE_MAP class variable provides a centralized way to map network types to their JAX-specific implementations, which is a good design choice.

Consider adding a docstring to explain the purpose of this class and the NETWORK_TYPE_MAP:

@flax_module
class NetworkCollection(NetworkCollectionDP):
    """
    A collection of JAX-compatible network implementations.
    
    This class provides a mapping between network type identifiers and their
    corresponding JAX-specific implementations.
    """
    NETWORK_TYPE_MAP: ClassVar[Dict[str, type]] = {
        "network": NativeNet,
        "embedding_network": EmbeddingNet,
        "fitting_network": FittingNet,
    }

Additionally, you might want to consider using an Enum for the network type keys to prevent typos and improve type checking:

from enum import Enum, auto

class NetworkType(Enum):
    NETWORK = auto()
    EMBEDDING_NETWORK = auto()
    FITTING_NETWORK = auto()

NETWORK_TYPE_MAP: ClassVar[Dict[NetworkType, type]] = {
    NetworkType.NETWORK: NativeNet,
    NetworkType.EMBEDDING_NETWORK: EmbeddingNet,
    NetworkType.FITTING_NETWORK: FittingNet,
}

57-59: LGTM: LayerNorm class is correctly defined.

The LayerNorm class is appropriately decorated with @flax_module and inherits from both LayerNormDP and NativeLayer. This multiple inheritance structure effectively combines the functionality of LayerNormDP with the JAX-specific NativeLayer.

Consider adding a docstring to explain the purpose of this class and its relationship to the parent classes:

@flax_module
class LayerNorm(LayerNormDP, NativeLayer):
    """
    A JAX-compatible implementation of Layer Normalization.
    
    This class combines the functionality of LayerNormDP with the JAX-specific
    features of NativeLayer to provide a layer normalization implementation
    compatible with the JAX ecosystem.
    """
    pass
deepmd/jax/common.py (1)

44-77: Good implementation, but consider improving class initialization.

The flax_module function is well-implemented and documented. The use of a dynamic metaclass to handle multiple inheritance is a good approach. However, there's a potential issue with the class creation.

Consider modifying the FlaxModule class to properly initialize both parent classes. Here's a suggested improvement:

class FlaxModule(module, nnx.Module, metaclass=MixedMetaClass):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        nnx.Module.__init__(self)

This ensures that both module and nnx.Module are properly initialized.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e7aeca0 and bac980e.

📒 Files selected for processing (7)
  • deepmd/jax/common.py (3 hunks)
  • deepmd/jax/descriptor/dpa1.py (1 hunks)
  • deepmd/jax/env.py (1 hunks)
  • deepmd/jax/utils/exclude_mask.py (1 hunks)
  • deepmd/jax/utils/network.py (1 hunks)
  • deepmd/jax/utils/type_embed.py (1 hunks)
  • pyproject.toml (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • deepmd/jax/descriptor/dpa1.py
  • deepmd/jax/utils/exclude_mask.py
🔇 Additional comments (8)
deepmd/jax/env.py (2)

17-17: LGTM! Consistent addition to __all__.

The addition of nnx to the __all__ list is consistent with the import change and follows the existing format. This change correctly exposes nnx when using wildcard imports from this module.


8-10: LGTM! Verify the usage of nnx in the codebase.

The import of nnx from flax is correctly added and aligns with the discussion about using Flax for modules implemented by JAX. This change looks good and follows the existing code style.

To ensure this addition is utilized effectively, let's verify its usage across the codebase:

✅ Verification successful

nnx Usage Verified

The usage of nnx in deepmd/jax/common.py has been confirmed, and no TODO comments related to nnx were found. This confirms that the addition aligns with the project objectives and is properly implemented.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of `nnx` in the codebase

# Search for `nnx` usage in Python files
echo "Searching for 'nnx' usage in Python files:"
rg --type python 'nnx\.' -C 2

# Check if there are any TODO comments related to 'nnx'
echo "Checking for TODO comments related to 'nnx':"
rg --type python 'TODO.*nnx'

Length of output: 330


Script:

#!/bin/bash
# Description: Check for usage of `nnx` in Python files with corrected file type

# Search for `nnx` usage in Python files
echo "Searching for 'nnx' usage in Python files:"
rg --type py 'nnx\.' -C 2

# Check if there are any TODO comments related to 'nnx'
echo "Checking for TODO comments related to 'nnx':"
rg --type py 'TODO.*nnx'

Length of output: 1255

deepmd/jax/utils/type_embed.py (1)

8-8: LGTM: Import of flax_module is correct.

The import of flax_module from deepmd.jax.common is correctly placed and necessary for the @flax_module decorator used in the file.

deepmd/jax/utils/network.py (1)

4-5: LGTM: New imports are appropriate for the changes.

The added imports are necessary for the new class definitions and type annotations. The imports from deepmd.jax.common indicate the introduction of JAX-specific functionality, which aligns with the PR objectives.

Also applies to: 11-11, 13-13, 20-21

deepmd/jax/common.py (3)

3-3: LGTM: Import changes are appropriate.

The added imports (Optional, NativeOP, and nnx) are necessary for the new function signatures and implementations in this file.

Also applies to: 9-11, 14-14


26-26: LGTM: Function signature update improves flexibility.

The change from Union[np.ndarray] to Optional[np.ndarray] in the to_jax_array function signature is appropriate. It accurately reflects the function's ability to handle None input and improves type hinting. This change is consistent with the overloaded function signatures defined earlier in the file.


Line range hint 1-77: LGTM: File structure and consistency maintained.

The overall file structure is clean and well-organized. The new additions are consistent with the existing code style and align well with the PR objectives, particularly the integration of Flax support. Good job maintaining code quality and consistency throughout the changes.

pyproject.toml (1)

137-137: LGTM! Consider verifying compatibility across Python versions.

The addition of Flax as an optional dependency for JAX is appropriate and aligns with the discussion mentioned in the PR objectives. This change enhances support for parameter initialization and backward propagation when using JAX.

To ensure this change doesn't introduce any unexpected issues, please run the following verification script:

This script will help verify that:

  1. Flax is properly integrated when using Python 3.10 or higher.
  2. Flax is not imported when using Python 3.9 or lower.
  3. There are no unexpected warnings or errors related to the Flax integration.

deepmd/jax/utils/type_embed.py Show resolved Hide resolved
Signed-off-by: Jinzhe Zeng <[email protected]>
@njzjz njzjz requested a review from wanghan-iapcm September 29, 2024 14:39
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: 6

🧹 Outside diff range and nitpick comments (7)
source/tests/array_api_strict/utils/network.py (1)

25-28: Consider generalizing attribute handling in __setattr__

Currently, the __setattr__ method checks for specific attribute names "w", "b", and "idt". To enhance maintainability and accommodate future extensions, consider applying to_array_api_strict_array based on attribute types or other criteria, especially if more attributes may require similar processing.

deepmd/dpmodel/utils/exclude_mask.py (2)

Line range hint 73-84: Consistency: Replace np.array with xp.array for array API compatibility

In the __init__ method of the PairExcludeMask class, type_mask is initialized using np.array. Since you're moving towards array API compatibility and using xp namespace elsewhere, consider replacing np.array with xp.array. However, xp is not available in the __init__ method. One approach is to set self.type_mask during the first call to build_type_exclude_mask when xp is available, or convert self.type_mask to an xp array within that method.

Consider modifying build_type_exclude_mask to convert self.type_mask to an xp array:

 def build_type_exclude_mask(
     self,
     nlist: np.ndarray,
     atype_ext: np.ndarray,
 ):
     """Compute type exclusion mask for atom pairs."""

     xp = array_api_compat.array_namespace(nlist, atype_ext)
+    type_mask_xp = xp.asarray(self.type_mask)
     if len(self.exclude_types) == 0:
         # safely return 1 if nothing is excluded.
         return xp.ones_like(nlist, dtype=xp.int32)
     nf, nloc, nnei = nlist.shape
     nall = atype_ext.shape[1]
     # add virtual atom of type ntypes. nf x nall+1
     ae = xp.concat(
         [atype_ext, self.ntypes * xp.ones([nf, 1], dtype=atype_ext.dtype)], axis=-1
     )
     type_i = xp.reshape(atype_ext[:, :nloc], (nf, nloc)) * (self.ntypes + 1)
     # nf x nloc x nnei
     index = xp.reshape(
         xp.where(nlist == -1, xp.full_like(nlist, nall), nlist), (nf, nloc * nnei)
     )
     type_j = xp_take_along_axis(ae, index, axis=1)
     type_j = xp.reshape(type_j, (nf, nloc, nnei))
     type_ij = type_i[:, :, None] + type_j
     # nf x (nloc x nnei)
     type_ij = xp.reshape(type_ij, (nf, nloc * nnei))
     mask = xp.reshape(
         xp.take(
-            self.type_mask,
+            type_mask_xp,
             xp.reshape(type_ij, (-1,))
         ),
         (nf, nloc, nnei)
     )
     return mask

111-114: Ensure Correct Data Types with xp.ones_like

In the build_type_exclude_mask method, when returning early because self.exclude_types is empty, you use xp.ones_like(nlist, dtype=xp.int32). Ensure that xp.int32 correctly represents the integer data type in the array API namespace. Depending on the backend, you might need to use a standard data type like "int32" or use nlist.dtype if appropriate.

Consider updating the dtype specification:

-    return xp.ones_like(nlist, dtype=xp.int32)
+    return xp.ones_like(nlist, dtype="int32")
source/tests/consistent/common.py (3)

80-80: Add missing docstring for array_api_strict_class

The class variable array_api_strict_class lacks a docstring. Adding one will enhance code documentation and maintain consistency.

Apply this diff to add the docstring:

     array_api_strict_class: ClassVar[Optional[type]]
+    """array_api_strict model class."""

174-183: Mark eval_array_api_strict as an abstract method and improve docstring

To maintain consistency with other eval_* methods like eval_dp and eval_pt, consider marking eval_array_api_strict with @abstractmethod. Also, adjust the docstring formatting for clarity.

Apply this diff:

+    @abstractmethod
     def eval_array_api_strict(self, array_api_strict_obj: Any) -> Any:
         """Evaluate the return value of array_api_strict.

-        Parameters
-
-        ----------
-        array_api_strict_obj : Any
-            The object of array_api_strict
-        """
+        Parameters
+        ----------
+        array_api_strict_obj : Any
+            The object of array_api_strict
+        """

         raise NotImplementedError("Not implemented")

275-276: Update docstring to reflect new backend order

The get_reference_backend method now includes ARRAY_API_STRICT in its checks. Update the docstring to match the new order of backends.

Apply this diff:

     """Get the reference backend.

-    Order of checking for ref: DP, TF, PT.
+    Order of checking for ref: DP, TF, PT, JAX, ARRAY_API_STRICT.
     """
deepmd/dpmodel/utils/network.py (1)

258-258: Clarify the logic behind concatenating input tensors

In the residual connection, when self.w.shape[1] is twice self.w.shape[0], the code concatenates x with itself along the last axis. To enhance readability, consider adding a comment explaining the rationale behind this operation.

Apply this diff to add a clarifying comment:

             elif self.resnet and self.w.shape[1] == 2 * self.w.shape[0]:
+                # When output dimension is double the input, concatenate x with itself
                 y += xp.concat([x, x], axis=-1)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 13e85f7 and 75a0b05.

📒 Files selected for processing (11)
  • deepmd/dpmodel/descriptor/dpa1.py (13 hunks)
  • deepmd/dpmodel/utils/exclude_mask.py (5 hunks)
  • deepmd/dpmodel/utils/network.py (6 hunks)
  • deepmd/dpmodel/utils/nlist.py (4 hunks)
  • deepmd/dpmodel/utils/type_embed.py (1 hunks)
  • deepmd/jax/utils/network.py (1 hunks)
  • pyproject.toml (1 hunks)
  • source/tests/array_api_strict/utils/network.py (1 hunks)
  • source/tests/consistent/common.py (10 hunks)
  • source/tests/consistent/descriptor/test_dpa1.py (5 hunks)
  • source/tests/consistent/test_type_embedding.py (4 hunks)
🧰 Additional context used
📓 Learnings (1)
deepmd/dpmodel/utils/nlist.py (1)
Learnt from: njzjz
PR: deepmodeling/deepmd-kit#4160
File: deepmd/dpmodel/utils/nlist.py:292-321
Timestamp: 2024-09-25T06:36:03.578Z
Learning: In the Array API, `outer` is only available in `xp.linalg`, not in the main namespace `xp`.
🪛 Ruff
deepmd/dpmodel/descriptor/dpa1.py

1039-1039: Local variable env_mat is assigned to but never used

Remove assignment to unused variable env_mat

(F841)

deepmd/dpmodel/utils/nlist.py

102-105: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)

🪛 GitHub Check: CodeQL
deepmd/dpmodel/descriptor/dpa1.py

[notice] 1039-1039: Unused local variable
Variable env_mat is not used.

🔇 Additional comments (32)
source/tests/consistent/test_type_embedding.py (4)

15-15: LGTM: Import of INSTALLED_ARRAY_API_STRICT

The addition of INSTALLED_ARRAY_API_STRICT to the imports is consistent with the existing pattern and necessary for the conditional import of array_api_strict related code.


41-44: LGTM: Conditional import of TypeEmbedNetStrict

The conditional import of TypeEmbedNetStrict is well-implemented, following the established pattern for other backends. Setting it to None when not installed is a good practice to prevent potential AttributeErrors.


79-79: LGTM: Addition of array_api_strict_class and skip_array_api_strict

The additions of array_api_strict_class and skip_array_api_strict are consistent with the existing pattern for other backends. The skip_array_api_strict flag is correctly implemented based on the installation status.

Thank you for implementing the suggestion from the previous review to add the skip_array_api_strict condition.

Also applies to: 82-82


Line range hint 1-174: Overall assessment: Well-implemented array_api_strict support

The changes to this file successfully integrate support for the array_api_strict backend. The implementation is consistent with existing patterns for other backends and includes:

  1. Proper imports and conditional logic
  2. Addition of necessary class attributes
  3. Implementation of the eval_array_api_strict method

These changes enhance the test suite's capability to handle the new array_api_strict backend while maintaining existing functionality. All previous review comments have been addressed, demonstrating attentiveness to feedback.

Great job on this implementation!

deepmd/dpmodel/utils/type_embed.py (1)

109-109: LGTM! Verify consistency across the codebase.

The change from concatenate to concat is correct and aligns with the Array API standard. This modification is consistent with the PR objectives.

To ensure consistency, let's check for any remaining uses of concatenate in the codebase:

✅ Verification successful

Consistency verified: 'concatenate' to 'concat' change applied throughout the codebase.

The change from 'concatenate' to 'concat' in the file deepmd/dpmodel/utils/type_embed.py is consistent with changes made throughout the codebase. No occurrences of 'concatenate' were found, while numerous instances of 'concat' are present across various files. This indicates that the change has been applied systematically, aligning with the PR objectives to replace 'concatenate' with 'concat'.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining uses of 'concatenate' in Python files

# Search for 'concatenate' in Python files
echo "Searching for 'concatenate' in Python files:"
rg --type python 'concatenate' -C 3

# Search for 'concat' in Python files to verify the changes
echo "Searching for 'concat' in Python files:"
rg --type python 'concat' -C 3

Length of output: 328


Script:

#!/bin/bash
# Description: Check for any remaining uses of 'concatenate' and verify 'concat' in Python files

# Search for 'concatenate' in Python files
echo "Searching for 'concatenate' in Python files:"
rg --type py 'concatenate' -C 3

# Search for 'concat' in Python files to verify the changes
echo "Searching for 'concat' in Python files:"
rg --type py 'concat' -C 3

Length of output: 369813

pyproject.toml (1)

140-140: LGTM: Addition of Flax dependency aligns with PR objectives

The addition of Flax (>=0.8.0) as a dependency for Python versions 3.10 and above is a good choice. This aligns with the discussion in the PR comments about using Flax for modules implemented by JAX. Flax provides a structure similar to torch.nn.Module, which can enhance support for parameter initialization and backward propagation in JAX-based implementations.

source/tests/array_api_strict/utils/network.py (3)

36-42: LGTM

The NetworkCollection class and its NETWORK_TYPE_MAP are well-defined, providing a clear mapping of network types to their corresponding classes.


44-45: Verify method resolution order (MRO) for LayerNorm

In the multiple inheritance of class LayerNorm(LayerNormDP, NativeLayer), the order of base classes affects the method resolution order. Verify that this order ensures the desired behavior, and that methods from both LayerNormDP and NativeLayer are correctly inherited and invoked.

Run the following script to inspect the inheritance hierarchy and confirm the MRO:

#!/bin/bash
# Description: Inspect the class definition and inheritance of LayerNorm.

# Search for the LayerNorm class definition.
rg --type python -A 5 $'class LayerNorm'

# Optionally, check other classes that inherit from LayerNormDP or NativeLayer.
rg --type python -A 5 $'(LayerNormDP|NativeLayer)'

31-33: Verify compatibility of network constructors with NativeLayer

Ensure that make_multilayer_network, make_embedding_network, and make_fitting_network are compatible with the updated NativeLayer class. Confirm that these functions accept the new parameters and integrate smoothly with the modified layer definitions.

Run the following script to check the definitions and usages of the network constructors:

deepmd/jax/utils/network.py (4)

27-39: Implementation of ArrayAPIParam enhances interoperability

The ArrayAPIParam class correctly extends nnx.Param and implements array interface methods such as __array__, __array_namespace__, __dlpack__, and __dlpack_device__. This ensures that parameters can seamlessly integrate with different array operations and backends, improving the flexibility and compatibility of the code.


41-49: Properly wrapping parameters in NativeLayer with ArrayAPIParam

In the __setattr__ method of NativeLayer, the attributes w, b, and idt are suitably converted to JAX arrays using to_jax_array(value). If the value is not None, it is wrapped with ArrayAPIParam. This approach ensures that these parameters support the necessary array interfaces for downstream computations.


64-71: NetworkCollection correctly defines NETWORK_TYPE_MAP for dynamic network selection

The NETWORK_TYPE_MAP in NetworkCollection appropriately maps string identifiers to their corresponding network classes. The use of ClassVar with explicit type hints ensures proper type checking. This setup facilitates dynamic selection and instantiation of different network types based on configuration.


73-74: Verify the method resolution order (MRO) in the LayerNorm class

The LayerNorm class inherits from both LayerNormDP and NativeLayer. Multiple inheritance can sometimes lead to unexpected behaviors if there are overlapping methods or attributes. Ensure that the MRO is as intended and that the correct methods from parent classes are being invoked.

To inspect the MRO and confirm the inheritance hierarchy, run the following script:

This will output the MRO, helping you verify that the inheritance structure behaves as expected.

deepmd/dpmodel/utils/nlist.py (4)

103-105: Consider simplifying the if-else block with a ternary operator

The current if-else statement can be condensed into a single line using a ternary operator for improved readability.

🧰 Tools
🪛 Ruff

102-105: Use ternary operator xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut instead of if-else-block

Replace if-else-block with xmax = xp.max(coord) + 2.0 * rcut if coord.size > 0 else 2.0 * rcut

(SIM108)


170-171: Ensure correct broadcasting in xp_take_along_axis and subsequent operations

In the nlist_distinguish_types function, verify that the use of xp_take_along_axis and the subsequent squeeze() operation correctly handle the array dimensions. Improper squeezing might lead to unexpected behavior if dimensions are collapsed incorrectly.

Please check the shapes of tnlist before and after squeezing to ensure that they align with the expected dimensions for further processing.


295-303: Confirmed correct usage of xp.linalg.outer

The use of xp.linalg.outer is appropriate here, as per the Array API standard, outer is available in the linalg namespace and not in the main namespace xp.


123-125: ⚠️ Potential issue

Potential compatibility issue with xp.eye for non-square matrices

The use of xp.eye(nloc, nall, dtype=diff.dtype) may not be supported across all array backends if nloc and nall are not equal. Some implementations of the Array API's eye function only support creating square matrices.

Please verify whether the array backends you intend to support allow non-square matrices with xp.eye. If not, consider an alternative approach to exclude self-distances, such as creating a mask with broadcasting.

source/tests/consistent/descriptor/test_dpa1.py (4)

196-224: Refactor duplicated parameter unpacking in skip_jax method

The unpacking of self.param is repeated from lines 199 to 218, similar to other methods. Consider refactoring this into a helper method to improve maintainability.


226-257: Refactor duplicated parameter unpacking in skip_array_api_strict method

Lines 229 to 248 repeat the unpacking of self.param. Refactoring this into a shared helper method can reduce code duplication and potential errors.


392-401: eval_jax method implementation is correct

The eval_jax method correctly invokes eval_jax_descriptor with the appropriate parameters. This ensures consistency with the evaluation methods of other backends.


402-411: eval_array_api_strict method implementation looks good

The eval_array_api_strict method appropriately calls eval_array_api_strict_descriptor with the necessary arguments, aligning with the structure of existing evaluation methods.

source/tests/consistent/common.py (6)

13-15: Import find_spec appropriately

The import of find_spec from importlib.util is correctly added to check for the presence of the array_api_strict module.


39-39: Detect array_api_strict installation using find_spec

Properly sets INSTALLED_ARRAY_API_STRICT by checking if array_api_strict is installed.


63-63: Include INSTALLED_ARRAY_API_STRICT in public API

Adds INSTALLED_ARRAY_API_STRICT to the __all__ list, exposing it as part of the module's public interface.


191-191: Add ARRAY_API_STRICT to RefBackend enum

The addition of ARRAY_API_STRICT to the RefBackend enum is appropriate and extends backend support.


257-261: Implement serialization method for array_api_strict

The get_array_api_strict_ret_serialization_from_cls method correctly retrieves return values and serialization data for array_api_strict.


290-295: Support ARRAY_API_STRICT in get_reference_ret_serialization

Adds handling for ARRAY_API_STRICT in get_reference_ret_serialization, ensuring it can retrieve return values and serialization data.

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

1351-1353: ⚠️ Potential issue

Assign variable v before usage

The variable v is used later but has not been assigned. Please ensure v is properly initialized to avoid NameError.

Apply this diff to assign v:

     _query = self.in_proj(query)
     q = _query[..., 0 : self.head_dim]
     k = _query[..., self.head_dim : self.head_dim * 2]
+    v = _query[..., self.head_dim * 2 : self.head_dim * 3]

Likely invalid or redundant comment.

deepmd/dpmodel/utils/network.py (4)

149-160: Approved

The deserialization logic correctly handles the assignment and reshaping of w, b, and idt. Using ravel() ensures that these variables are flattened appropriately before being set to the object's attributes. The code is clear and maintains consistency.


369-373: Approved

The initialization of self.w and self.b appropriately utilizes the array API namespace. The use of xp.squeeze ensures that the weight shape is maintained as [num_in]. Setting self.w and self.b with xp.ones_like and xp.zeros_like when uni_init is True is correct and ensures consistency across backends.


386-387: Approved

The serialize method now correctly uses to_numpy_array for converting self.w and self.b. This change ensures consistent serialization across different backends and maintains compatibility, addressing previous concerns.


481-486: Approved

The layer_norm_numpy method correctly employs the array API namespace for calculating the mean and variance. The use of tuple(range(-len(shape), 0)) for the axis parameter ensures that the normalization operates over the correct dimensions, accommodating inputs of varying shapes. The updated calculations enhance compatibility and maintain the functionality of layer normalization.

deepmd/jax/utils/network.py Show resolved Hide resolved
deepmd/dpmodel/utils/exclude_mask.py Show resolved Hide resolved
deepmd/dpmodel/descriptor/dpa1.py Show resolved Hide resolved
deepmd/dpmodel/utils/network.py Show resolved Hide resolved
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.

3 participants