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

Add compression API to BaseModel and AtomicModel #4298

Merged
merged 14 commits into from
Nov 2, 2024

Conversation

cherryWangY
Copy link
Collaborator

@cherryWangY cherryWangY commented Nov 1, 2024

Summary by CodeRabbit

  • New Features

    • Introduced enable_compression method across multiple classes to allow configuration of compression settings for descriptors.
    • Enhanced robustness of output definitions and serialization processes in the DPAtomicModel class.
    • Added enable_compression method to LinearEnergyAtomicModel for improved model compression capabilities.
  • Bug Fixes

    • Improved error handling in the fitting_output_def method to ensure fallback functionality when the fitting network is unavailable.

These updates enhance the functionality and reliability of the model management features.

Copy link
Contributor

coderabbitai bot commented Nov 1, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces a new method called enable_compression across multiple classes related to atomic models and models in the DeepMD framework. This method allows for configuring compression settings by accepting various parameters. Additionally, some existing methods are updated to enhance functionality, including handling fallback logic and serialization for the fitting network. Overall, these changes expand the capabilities of the models without disrupting existing functionalities.

Changes

File Path Change Summary
deepmd/dpmodel/atomic_model/dp_atomic_model.py Added enable_compression method to DPAtomicModel class; updated fitting_output_def and serialize methods.
deepmd/dpmodel/model/make_model.py Added enable_compression method to CM class.
deepmd/dpmodel/atomic_model/make_base_atomic_model.py Added enable_compression method to BAM class; updated fwd method signature.
deepmd/dpmodel/model/base_model.py Added enable_compression method to BaseBaseModel class.
deepmd/pt/model/atomic_model/dp_atomic_model.py Added enable_compression method to DPAtomicModel class; updated fitting_output_def and serialize methods.
deepmd/pt/model/model/make_model.py Added enable_compression method to CM class.
deepmd/dpmodel/atomic_model/linear_atomic_model.py Added enable_compression method to LinearEnergyAtomicModel class.
deepmd/pt/model/atomic_model/linear_atomic_model.py Added enable_compression method to LinearEnergyAtomicModel class; updated deserialize method signature.

Possibly related PRs

  • Add 4 pt descriptor compression #4227: This PR adds a new enable_compression method to the DescrptDPA1 class, which is directly related to the compression functionality introduced in the main PR for the DPAtomicModel class.

Suggested labels

OP

Suggested reviewers

  • wanghan-iapcm
  • njzjz
  • iProzd

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

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
deepmd/dpmodel/atomic_model/dp_atomic_model.py (1)

89-119: LGTM! Implementation looks correct.

The method properly delegates compression configuration to the descriptor component, maintaining good separation of concerns.

Consider enhancing the docstring with:

  • A brief description of what compression does and its benefits
  • Parameter value ranges or constraints
  • Example usage
     def enable_compression(
         self,
         min_nbor_dist: float,
         table_extrapolate: float = 5,
         table_stride_1: float = 0.01,
         table_stride_2: float = 0.1,
         check_frequency: int = -1,
     ) -> None:
-        """Call descriptor enable_compression()
+        """Enable compression for the descriptor to optimize memory usage and performance.
+        
+        Compression reduces memory footprint by using lookup tables for atomic interactions.
+        The accuracy of the compression can be controlled via the stride parameters.
 
         Parameters
         ----------
         min_nbor_dist
-            The nearest distance between atoms
+            The nearest distance between atoms. Must be positive.
         table_extrapolate
-            The scale of model extrapolation
+            The scale for model extrapolation beyond training data range. Must be >= 1.
         table_stride_1
-            The uniform stride of the first table
+            The uniform stride of the first lookup table. Smaller values increase accuracy but memory usage.
         table_stride_2
-            The uniform stride of the second table
+            The uniform stride of the second lookup table. Smaller values increase accuracy but memory usage.
         check_frequency
-            The overflow check frequency
+            How often to check for numerical overflow. Set to -1 to disable checks.
+        
+        Example
+        -------
+        >>> model.enable_compression(
+        ...     min_nbor_dist=0.5,
+        ...     table_extrapolate=5,
+        ...     table_stride_1=0.01,
+        ...     table_stride_2=0.1,
+        ... )
         """
deepmd/dpmodel/atomic_model/base_atomic_model.py (2)

Line range hint 52-64: Fix mask shape and add documentation.

The shape of the "mask" output variable doesn't match its usage in forward_common_atomic. The mask is used to indicate real vs. virtual atoms and should have shape [nf, nloc] to match the batch size and number of local atoms.

Update the shape and add documentation:

     return FittingOutputDef(
         old_list  # noqa:RUF005
         + [
             OutputVariableDef(
                 name="mask",
-                shape=[1],
+                shape=["nf", "nloc"],
                 reducible=False,
                 r_differentiable=False,
                 c_differentiable=False,
+                description="Binary mask indicating real (1) vs virtual (0) atoms",
             )
         ]
     )

144-167: Consider adding base parameter validation.

To ensure consistent validation across all implementations, consider:

  1. Adding parameter validation in the base class before raising NotImplementedError
  2. Creating a utility method for compression parameter validation that can be reused by all implementations

Example implementation:

def _validate_compression_params(
    min_nbor_dist: float,
    table_extrapolate: float,
    table_stride_1: float,
    table_stride_2: float,
    check_frequency: int,
) -> None:
    """Validate compression parameters."""
    if min_nbor_dist <= 0:
        raise ValueError("min_nbor_dist must be positive")
    if table_extrapolate <= 0:
        raise ValueError("table_extrapolate must be positive")
    if table_stride_1 <= 0:
        raise ValueError("table_stride_1 must be positive")
    if table_stride_2 <= 0:
        raise ValueError("table_stride_2 must be positive")
    if check_frequency < -1:
        raise ValueError("check_frequency must be >= -1")

This utility method can be placed in a common module and imported by all model implementations.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8355947 and d1d49c3.

📒 Files selected for processing (3)
  • deepmd/dpmodel/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/dpmodel/atomic_model/dp_atomic_model.py (1 hunks)
  • deepmd/dpmodel/model/make_model.py (1 hunks)
🔇 Additional comments (1)
deepmd/dpmodel/model/make_model.py (1)

189-216: 🛠️ Refactor suggestion

Verify missing method and add parameter validation

A few concerns about the implementation:

  1. The method calls get_min_nbor_dist() but this method is not visible in the provided code.
  2. The implementation assumes the atomic model supports compression without checking.
  3. The parameters could benefit from validation.

Let's verify the existence and implementation of get_min_nbor_dist():

Consider adding parameter validation and interface checking:

 def enable_compression(
     self,
     table_extrapolate: float = 5,
     table_stride_1: float = 0.01,
     table_stride_2: float = 0.1,
     check_frequency: int = -1,
 ) -> None:
     """Call atomic_model enable_compression()

     Parameters
     ----------
     table_extrapolate
         The scale of model extrapolation
     table_stride_1
         The uniform stride of the first table
     table_stride_2
         The uniform stride of the second table
     check_frequency
         The overflow check frequency
     """
+    if not hasattr(self.atomic_model, 'enable_compression'):
+        raise NotImplementedError("Compression not supported by the atomic model")
+    
+    if table_extrapolate <= 0:
+        raise ValueError("table_extrapolate must be positive")
+    if table_stride_1 <= 0:
+        raise ValueError("table_stride_1 must be positive")
+    if table_stride_2 <= 0:
+        raise ValueError("table_stride_2 must be positive")
+    if check_frequency < -1:
+        raise ValueError("check_frequency must be >= -1")
+
     self.atomic_model.enable_compression(
         self.get_min_nbor_dist(),
         table_extrapolate,
         table_stride_1,
         table_stride_2,
         check_frequency,
     )

Consider enhancing the docstring to better explain:

  1. The purpose of compression
  2. The relationship between the parameters
  3. The meaning of the default values

deepmd/dpmodel/atomic_model/base_atomic_model.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the Python label Nov 1, 2024
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 (1)
deepmd/dpmodel/atomic_model/dp_atomic_model.py (1)

89-118: LGTM! Consider enhancing the documentation.

The implementation looks good with proper type hints and parameter forwarding to the descriptor.

Consider enhancing the docstring with:

  • Return value documentation (even if None)
  • Example usage
  • A note about the NotImplementedError in the base class
  • Explanation of what compression actually does and its impact on performance/accuracy

Example enhancement:

 def enable_compression(
     self,
     min_nbor_dist: float,
     table_extrapolate: float = 5,
     table_stride_1: float = 0.01,
     table_stride_2: float = 0.1,
     check_frequency: int = -1,
 ) -> None:
-    """Call descriptor enable_compression().
+    """Enable compression in the descriptor to optimize memory usage and performance.
+
+    This method configures compression parameters for atomic interactions. Unlike the
+    base class which raises NotImplementedError, this implementation delegates to
+    the descriptor's compression mechanism.

     Parameters
     ----------
     min_nbor_dist
         The nearest distance between atoms
     table_extrapolate
         The scale of model extrapolation
     table_stride_1
         The uniform stride of the first table
     table_stride_2
         The uniform stride of the second table
     check_frequency
         The overflow check frequency
+
+    Returns
+    -------
+    None
+
+    Examples
+    --------
+    >>> model = DPAtomicModel(descriptor, fitting, type_map)
+    >>> model.enable_compression(
+    ...     min_nbor_dist=0.8,
+    ...     table_extrapolate=6,
+    ...     check_frequency=10
+    ... )
     """
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d1d49c3 and 8699c66.

📒 Files selected for processing (3)
  • deepmd/dpmodel/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/dpmodel/atomic_model/dp_atomic_model.py (1 hunks)
  • deepmd/dpmodel/model/make_model.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • deepmd/dpmodel/model/make_model.py
🔇 Additional comments (2)
deepmd/dpmodel/atomic_model/dp_atomic_model.py (1)

89-118: Verify descriptor compression implementation.

Let's verify that the descriptor class properly implements the compression functionality that this method depends on.

✅ Verification successful

Compression functionality is properly implemented across descriptor classes

The verification shows that enable_compression is consistently implemented across the codebase:

  • The method is present in all descriptor implementations (se_a, se_r, se_t, se_atten, dpa1)
  • The method signature matches the expected parameters (min_nbor_dist, table_extrapolate, table_stride_1, table_stride_2)
  • Compression functionality is well-tested as evidenced by numerous test files (test_compressed_descriptor_*.py)
  • The implementation follows through from the high-level model down to the specific descriptor classes

The descriptor class properly implements the compression functionality that the dp_atomic_model.py's enable_compression method depends on.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for enable_compression implementation in descriptor classes
echo "Searching for enable_compression implementation in descriptor classes..."
ast-grep --pattern 'class $_ implements BaseDescriptor {
  $$$
  enable_compression($$$) {
    $$$
  }
  $$$
}'

# Search for compression-related tests
echo "Searching for compression-related tests..."
rg -l "enable_compression.*test" 

Length of output: 398


Script:

#!/bin/bash
# Let's try a different approach to find enable_compression implementations
echo "Searching for enable_compression method implementations..."
ast-grep --pattern 'def enable_compression($$$) {
  $$$
}'

# Search for descriptor base class and interface
echo "Searching for BaseDescriptor definition..."
rg -A 10 "class BaseDescriptor"

# Search for compression-related code
echo "Searching for compression-related code..."
rg -l "compression" --type python

# Search for descriptor implementations
echo "Searching for descriptor implementations..."
rg -l "class.*Descriptor" --type python

Length of output: 644


Script:

#!/bin/bash
# Let's try without file type filtering since python type is not recognized
echo "Searching for enable_compression method implementations..."
rg -B 2 -A 5 "def enable_compression"

# Search for descriptor base class and interface
echo "Searching for BaseDescriptor definition..."
rg -B 2 -A 10 "class BaseDescriptor"

# Search for compression-related code
echo "Searching for compression-related code..."
rg -l "compression"

# Search for descriptor implementations
echo "Searching for descriptor implementations..."
fd . -e py -x rg -l "class.*Descriptor" {}

Length of output: 16347

deepmd/dpmodel/atomic_model/base_atomic_model.py (1)

144-168: Previous review comment is still applicable.

The issues identified in the previous review regarding parameter validation, documentation, and typo still need to be addressed.

The base class implementation follows good OOP practices.

The abstract implementation in the base class is architecturally sound, forcing derived classes to implement their own compression logic.

The NotImplementedError pattern is correctly used here to establish a contract that derived classes must fulfill, promoting modularity and extensibility.

Copy link

codecov bot commented Nov 1, 2024

Codecov Report

Attention: Patch coverage is 44.44444% with 10 lines in your changes missing coverage. Please review.

Project coverage is 84.35%. Comparing base (8355947) to head (999400a).
Report is 7 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/dpmodel/atomic_model/linear_atomic_model.py 33.33% 2 Missing ⚠️
...eepmd/pt/model/atomic_model/linear_atomic_model.py 33.33% 2 Missing ⚠️
deepmd/dpmodel/atomic_model/dp_atomic_model.py 50.00% 1 Missing ⚠️
...pmd/dpmodel/atomic_model/make_base_atomic_model.py 50.00% 1 Missing ⚠️
deepmd/dpmodel/model/base_model.py 50.00% 1 Missing ⚠️
deepmd/dpmodel/model/make_model.py 50.00% 1 Missing ⚠️
deepmd/pt/model/atomic_model/dp_atomic_model.py 50.00% 1 Missing ⚠️
deepmd/pt/model/model/make_model.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4298      +/-   ##
==========================================
- Coverage   84.35%   84.35%   -0.01%     
==========================================
  Files         559      561       +2     
  Lines       52509    52622     +113     
  Branches     3054     3054              
==========================================
+ Hits        44292    44387      +95     
- Misses       7260     7275      +15     
- Partials      957      960       +3     

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

deepmd/dpmodel/atomic_model/base_atomic_model.py Outdated Show resolved Hide resolved
deepmd/dpmodel/model/make_model.py Show resolved Hide resolved
deepmd/dpmodel/model/make_model.py Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (5)
deepmd/dpmodel/atomic_model/make_base_atomic_model.py (2)

174-174: Fix typo in error message.

There's a typo in the error message: "atomi" should be "atomic".

-            raise NotImplementedError("This atomi model doesn't support compression!")
+            raise NotImplementedError("This atomic model doesn't support compression!")

161-173: Enhance parameter documentation.

The parameter documentation would benefit from additional details about:

  • Units for distance-based parameters (min_nbor_dist, table_stride_1, table_stride_2)
  • Valid ranges for each parameter
  • Explanation of what -1 means for check_frequency

Consider updating the documentation to include these details. For example:

"""
Parameters
----------
min_nbor_dist : float
    The nearest distance between atoms (in Angstroms). Must be positive.
table_extrapolate : float, optional
    The scale of model extrapolation. Must be positive. Default is 5.
table_stride_1 : float, optional
    The uniform stride of the first table (in Angstroms). Must be positive. Default is 0.01.
table_stride_2 : float, optional
    The uniform stride of the second table (in Angstroms). Must be positive. Default is 0.1.
check_frequency : int, optional
    The overflow check frequency. Use -1 to disable checks. Default is -1.
"""
deepmd/dpmodel/model/base_model.py (1)

194-214: Consider architectural improvements for the compression feature.

A few architectural suggestions to enhance the compression feature:

  1. Consider marking this method as @abstractmethod to enforce implementation in concrete classes, as it's a core feature that derived classes should implement.

  2. Consider adding a corresponding disable_compression() method to allow toggling the feature.

  3. Consider storing compression settings as instance variables to:

    • Allow querying current compression state
    • Support serialization/deserialization of compression settings
    • Enable runtime modifications of compression parameters

Example implementation pattern:

@abstractmethod
def enable_compression(
    self,
    table_extrapolate: float = 5,
    table_stride_1: float = 0.01,
    table_stride_2: float = 0.1,
    check_frequency: int = -1,
) -> None:
    """Enable model compression."""
    pass

@abstractmethod
def disable_compression(self) -> None:
    """Disable model compression."""
    pass

@property
def compression_settings(self) -> Optional[dict]:
    """Get current compression settings."""
    pass
deepmd/pt/model/atomic_model/dp_atomic_model.py (2)

Line range hint 58-64: Update docstring to reflect fallback behavior.

The method now includes a fallback to coord_denoise_net.output_def(), but this behavior isn't documented.

Consider updating the docstring:

    @torch.jit.export
    def fitting_output_def(self) -> FittingOutputDef:
-        """Get the output def of the fitting net."""
+        """Get the output definition.
+
+        Returns
+        -------
+        FittingOutputDef
+            The output definition from either the fitting net if available,
+            or the coordinate denoising net as a fallback.
+        """

Line range hint 102-115: Add error handling and consider version bump.

The serialization now includes the fitting network state, but consider these improvements:

  1. Add error handling for fitting_net serialization
  2. Consider bumping the version number since a new field was added

Here's a suggested implementation:

    def serialize(self) -> dict:
+        """Serialize the model to a dictionary.
+
+        Returns
+        -------
+        dict
+            The serialized model state.
+
+        Raises
+        ------
+        RuntimeError
+            If serialization of any component fails.
+        """
         dd = BaseAtomicModel.serialize(self)
+        try:
+            descriptor_state = self.descriptor.serialize()
+            fitting_state = self.fitting_net.serialize()
+        except Exception as e:
+            raise RuntimeError("Failed to serialize model components") from e
         dd.update(
             {
                 "@class": "Model",
-                "@version": 2,
+                "@version": 3,  # Bump version for new 'fitting' field
                 "type": "standard",
                 "type_map": self.type_map,
-                "descriptor": self.descriptor.serialize(),
-                "fitting": self.fitting_net.serialize(),
+                "descriptor": descriptor_state,
+                "fitting": fitting_state,
             }
         )
         return dd
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8699c66 and 6e93586.

📒 Files selected for processing (4)
  • deepmd/dpmodel/atomic_model/make_base_atomic_model.py (1 hunks)
  • deepmd/dpmodel/model/base_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/dp_atomic_model.py (1 hunks)
  • deepmd/pt/model/model/make_model.py (1 hunks)
🔇 Additional comments (3)
deepmd/dpmodel/atomic_model/make_base_atomic_model.py (1)

151-175: LGTM! The compression API implementation looks good.

The new enable_compression method:

  • Is well-integrated into the existing abstract base class
  • Follows the established pattern for abstract methods
  • Has comprehensive parameter documentation
  • Properly raises NotImplementedError as a placeholder for derived classes
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)

163-193: Verify compression feature usage across codebase.

Let's analyze how this compression feature is used across the codebase to ensure consistent implementation.

✅ Verification successful

Based on the comprehensive search results, I can now provide a final response regarding the compression feature usage across the codebase:

Compression implementation is consistent across frameworks

The compression feature is consistently implemented across TensorFlow, PyTorch and JAX frameworks with matching parameters and behavior:

  • All descriptors (se_a, se_t, se_r, se_atten) support compression with identical parameters
  • Common parameters across implementations:
    • min_nbor_dist: Minimum neighbor distance
    • table_extrapolate: Model extrapolation scale (default: 5)
    • table_stride_1: First table stride (default: 0.01)
    • table_stride_2: Second table stride (default: 0.1)
    • check_frequency: Overflow check frequency (optional)

The implementation in dp_atomic_model.py correctly follows this pattern by delegating to the descriptor's compression functionality with all required parameters.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage patterns of compression-related functionality

# Test 1: Find all calls to enable_compression
echo "=== Searching for enable_compression calls ==="
rg "enable_compression" -A 5

# Test 2: Find all compression-related configuration settings
echo "=== Searching for compression configuration ==="
rg -g "*.py" -g "*.json" "table_stride|table_extrapolate|min_nbor_dist"

# Test 3: Find related test files
echo "=== Searching for related test files ==="
fd -g "*test*.py" | xargs rg "compression|enable_compression"

Length of output: 62514

deepmd/pt/model/model/make_model.py (1)

101-128: Improve method documentation and add input validation

The implementation looks good, but there are a few improvements needed:

  1. The docstring should be enhanced to:

    • Explain the method's purpose and return value
    • Document the internally used get_min_nbor_dist() parameter
  2. Consider adding input parameter validation:

    • Ensure table_extrapolate is positive
    • Verify table_stride_1 and table_stride_2 are positive
    • Validate check_frequency is either -1 or positive

Here's a suggested improvement:

     def enable_compression(
         self,
         table_extrapolate: float = 5,
         table_stride_1: float = 0.01,
         table_stride_2: float = 0.1,
         check_frequency: int = -1,
     ) -> None:
-        """Call atomic_model enable_compression().
+        """Enable model compression to reduce memory usage and potentially improve performance.
+
+        This method configures compression settings by calling the atomic_model's enable_compression
+        with the minimum neighbor distance and the provided parameters.

         Parameters
         ----------
         table_extrapolate
-            The scale of model extrapolation
+            The scale of model extrapolation (must be positive)
         table_stride_1
-            The uniform stride of the first table
+            The uniform stride of the first table (must be positive)
         table_stride_2
-            The uniform stride of the second table
+            The uniform stride of the second table (must be positive)
         check_frequency
-            The overflow check frequency
+            The overflow check frequency (-1 or positive integer)
+
+        Returns
+        -------
+        None
+
+        Raises
+        ------
+        ValueError
+            If any of the input parameters are invalid
         """
+        if table_extrapolate <= 0:
+            raise ValueError("table_extrapolate must be positive")
+        if table_stride_1 <= 0:
+            raise ValueError("table_stride_1 must be positive")
+        if table_stride_2 <= 0:
+            raise ValueError("table_stride_2 must be positive")
+        if check_frequency != -1 and check_frequency < 0:
+            raise ValueError("check_frequency must be -1 or positive")
+
         self.atomic_model.enable_compression(
             self.get_min_nbor_dist(),
             table_extrapolate,
             table_stride_1,
             table_stride_2,
             check_frequency,
         )

Let's verify the existence of the get_min_nbor_dist method:

deepmd/dpmodel/model/base_model.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
deepmd/dpmodel/atomic_model/linear_atomic_model.py (1)

160-174: Enhance parameter documentation.

The docstring should specify:

  1. Units for distance-based parameters
  2. Valid ranges for each parameter
  3. The meaning of special values (e.g., check_frequency=-1)

Consider updating the docstring:

 """Compress model.
 
 Parameters
 ----------
 min_nbor_dist
-    The nearest distance between atoms
+    The minimum distance between atoms (in Angstroms). Must be positive.
 table_extrapolate
-    The scale of model extrapolation
+    The scale of model extrapolation. Must be positive, typically 5-10.
 table_stride_1
-    The uniform stride of the first table
+    The uniform stride of the first table (in Angstroms). Must be positive, typically 0.01-0.1.
 table_stride_2
-    The uniform stride of the second table
+    The uniform stride of the second table (in Angstroms). Must be positive, typically 0.1-1.0.
 check_frequency
-    The overflow check frequency
+    The frequency to check for overflow. Use -1 to disable checking, or a positive integer for check interval.
 """
deepmd/pt/model/atomic_model/linear_atomic_model.py (1)

195-209: Enhance compression method documentation

The documentation should be expanded to include:

  • A description of what compression does and its impact on the model
  • The Returns section (even if it returns None)
  • Example usage
  • Any side effects or important notes about the compression process
     """Compress model.
+    This method enables compression for all sub-models in the linear combination.
+    Compression helps reduce model size and improve inference performance.
 
     Parameters
     ----------
     min_nbor_dist
         The nearest distance between atoms
     table_extrapolate
         The scale of model extrapolation
     table_stride_1
         The uniform stride of the first table
     table_stride_2
         The uniform stride of the second table
     check_frequency
         The overflow check frequency
+
+    Returns
+    -------
+    None
+
+    Examples
+    --------
+    >>> model = LinearEnergyAtomicModel(...)
+    >>> model.enable_compression(
+    ...     min_nbor_dist=0.5,
+    ...     table_extrapolate=5.0,
+    ...     table_stride_1=0.01,
+    ...     table_stride_2=0.1
+    ... )
+
+    Notes
+    -----
+    This operation modifies the internal state of all sub-models.
     """
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a3970d0 and 999400a.

📒 Files selected for processing (2)
  • deepmd/dpmodel/atomic_model/linear_atomic_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/linear_atomic_model.py (1 hunks)

@njzjz njzjz enabled auto-merge November 2, 2024 08:38
@njzjz njzjz added this pull request to the merge queue Nov 2, 2024
Merged via the queue into deepmodeling:devel with commit bfbe2ed Nov 2, 2024
60 checks passed
@cherryWangY cherryWangY deleted the addCompressionAPI branch December 5, 2024 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants