Skip to content

Conversation

@OutisLi
Copy link
Collaborator

@OutisLi OutisLi commented Nov 27, 2025

Summary by CodeRabbit

  • New Features

    • Split compression into independent type-embedding (TEBD) and geometric modes, enabling partial compression when attention layer ≠ 0.
  • Documentation

    • Expanded backend-specific guidance describing full vs partial compression rules and prerequisites (e.g., TEBD input mode).
  • Tests

    • Added tests covering non-zero attention-layer scenarios to validate partial compression behavior.
  • Bug Fixes

    • Improved eligibility checks and clearer runtime warnings when geometric compression is skipped.

✏️ Tip: You can customize this high-level summary in your review settings.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request extends model compression support for the se_atten_v2 descriptor to work with any attn_layer value, not just when attn_layer == 0. When attn_layer != 0, only type embedding compression is applied, while the geometric part remains uncompressed. This enables partial compression for models using attention layers.

Key Changes:

  • Refactored compression flags in se_atten.py and dpa1.py to use separate tebd_compress and geo_compress flags instead of a single compress flag
  • Added conditional geometric compression based on attn_layer value in DPA1 and DPA2 descriptors
  • Updated documentation and argument descriptions to reflect the new partial compression capability
  • Added comprehensive test coverage for models with non-zero attn_layer values

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
source/tests/pt/test_model_compression_se_atten.py Added new test class TestDeepPotATNonZeroAttnLayer with comprehensive tests for partial compression, including model initialization, evaluation, and ASE integration tests
doc/model/train-se-atten.md Updated documentation to clarify that compression works for any attn_layer value, with partial compression (type embedding only) when attn_layer != 0
deepmd/utils/argcheck.py Updated attn_layer parameter documentation to explain compression behavior for different attn_layer values
deepmd/pt/model/descriptor/se_atten.py Refactored compression flags from single compress to separate tebd_compress and geo_compress flags for clearer distinction between type embedding and geometric compression
deepmd/pt/model/descriptor/dpa1.py Implemented conditional geometric compression based on attn_layer value, added warning for partial compression, and adopted separate compression flags
deepmd/pt/model/descriptor/dpa2.py Added conditional geometric compression logic and warning, but inconsistently still uses single compress flag instead of separate flags like DPA1

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 27, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Introduces separate compression flags for type-embedding (tebd_compress) and geometry (geo_compress), gates geometric compression on attention-layer and input-mode, updates enable_compression flows across DPA1/DPA2 and se_atten, adds tests for non-zero attn_layer behavior, and updates docs/argstrings accordingly.

Changes

Cohort / File(s) Summary
Descriptor: DPA1 & DPA2
deepmd/pt/model/descriptor/dpa1.py, deepmd/pt/model/descriptor/dpa2.py
Added warnings use, split single compress into tebd_compress and geo_compress; updated enable_compression to require tebd_input_mode=="strip", always compress type-embedding first, and only perform geometric compression when attn_layer == 0 (otherwise emit UserWarning). Adjusted guards and state transitions accordingly.
SE attention block
deepmd/pt/model/descriptor/se_atten.py
Replaced compress with tebd_compress and geo_compress attributes; updated forward paths and compression toggles to use the two flags so type-embedding and geometric compression are independently controlled.
Tests
source/tests/pt/test_model_compression_se_atten.py
Added non-zero attn_layer test initialization _init_models_nonzero_attn_layer() and new test class TestDeepPotATNonZeroAttnLayer with parity, frame, atomic, and ASE tests exercising partial-compression behavior. Integrated new fixtures/artifacts into setup/teardown.
Documentation
doc/model/train-se-atten.md, doc/model/dpa2.md
Updated Model compression text to describe backend- and attn_layer-dependent rules: TF requires attn_layer=0 for geometric compression; PyTorch allows any attn_layer but geometric compression only when attn_layer=0, otherwise only type-embedding compressed. Generalized previous blanket constraint.
Argstrings / docstrings
deepmd/utils/argcheck.py
Updated attn_layer argument docstrings to reflect backend-dependent compression behavior; no signature changes.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay attention to:
    • enable_compression branching in dpa1.py and dpa2.py (ensure state flags set consistently).
    • Forward logic in se_atten.py where tebd_compress and geo_compress replace compress (verify all code paths covered).
    • Tests in source/tests/pt/test_model_compression_se_atten.py for new non-zero attn-layer fixtures and assertions.
    • Documentation changes for accuracy and parity with runtime behavior.

Possibly related PRs

Suggested reviewers

  • iProzd
  • wanghan-iapcm
  • njzjz

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 54.55% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main feature: enabling type embedding compression when attn_layer != 0, which is the core change across all modified descriptor files (dpa1.py, dpa2.py, se_atten.py).
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4206f3c and 7026d9c.

📒 Files selected for processing (2)
  • deepmd/utils/argcheck.py (1 hunks)
  • doc/model/train-se-atten.md (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • deepmd/utils/argcheck.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (29)
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Test C++ (false)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Build C library (2.14, >=2.5.0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Analyze (python)
  • GitHub Check: Analyze (c-cpp)
🔇 Additional comments (2)
doc/model/train-se-atten.md (2)

137-139: Clear note to readers about backend-specific behavior.

This introductory note appropriately directs users to backend-specific documentation. Well-placed immediately after the se_atten_v2 section and before detailed compression guidance.


187-194: Well-structured backend-specific compression documentation.

The new sections clearly document the differences between TensorFlow and PyTorch:

  • TensorFlow limitation (static graph) is well-motivated
  • PyTorch behavior directly addresses the PR objective, allowing type-embedding compression even when attn_layer > 0
  • The distinction between attn_layer = 0 (compress both parts) and attn_layer > 0 (compress type embedding only) is explicit and helpful

Please verify that:

  1. The warning emitted during PyTorch compression with attn_layer > 0 is actually implemented in the code
  2. The documented behavior matches the updated enable_compression flows in the descriptor implementation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (5)
deepmd/utils/argcheck.py (1)

508-561: attn_layer doc now aligned with partial-compression behavior

The updated doc_attn_layer correctly documents that compression requires tebd_input_mode=='strip' and that only type embedding is compressed when attn_layer != 0, which matches the new DPA1/DPA2 logic. You might optionally mention se_atten_v2 explicitly here for clarity, since it always uses strip mode, but the current wording is already consistent with behavior.

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

2-2: Compression gating is correct, but attn_layer warning branch is currently unreachable

The updated enable_compression logic looks sound:

  • You still enforce tebd_input_mode == "strip" and basic sanity checks.
  • TEBD is always compressed via repinit.type_embedding_compression, and the geometric part is compressed only when repinit.attn_layer == 0, which matches the documented behavior.

However, in __init__ you instantiate repinit with attn_layer=0, so self.repinit.attn_layer is currently always zero and the new warning branch will never fire. If you intend to support nonzero attn_layer on repinit later, consider plumbing an attn_layer argument from RepinitArgs into DescrptBlockSeAtten; otherwise, the conditional can be simplified to avoid confusion.

Consider adding stacklevel to the warning for better tracebacks

To make it clearer to users where compression was invoked, it’s worth adding a stacklevel to the warnings.warn call:

-        warnings.warn(
-            "Attention layer is not 0, only type embedding is compressed. Geometric part is not compressed.",
-            UserWarning,
-        )
+        warnings.warn(
+            "Attention layer is not 0, only type embedding is compressed. Geometric part is not compressed.",
+            UserWarning,
+            stacklevel=2,
+        )

This also addresses the Ruff B028 hint.

Also applies to: 894-979

source/tests/pt/test_model_compression_se_atten.py (1)

42-57: Good coverage of nonzero-attn_layer partial-compression path

The new _init_models_nonzero_attn_layer helper and TestDeepPotATNonZeroAttnLayer test class nicely mirror the existing compression tests while targeting the attn_layer > 0 configuration:

  • The helper cleanly isolates artifacts (input-nonzero-attn.json, separate frozen/compressed model files) and uses se_atten_v2 with attn_layer=2, matching the new “TEBD-only compression” design.
  • The test class exercises attributes, 1‑frame and 2‑frame (atomic and non‑atomic) evals, plus ASE integration, and checks shapes and numerical agreement between original and compressed models.

There is some inevitable duplication with the other TestDeepPotAT* classes; if this pattern keeps growing, you might later factor out a small shared test mixin or parametrized helper to cut repetition, but it’s perfectly fine as-is for readability.

Also applies to: 126-172, 220-240, 256-258, 858-1016

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

308-323: Clear separation of TEBD vs geometric compression with correct gating

The new tebd_compress / geo_compress flags and updated enable_compression logic look good:

  • You now prevent double-compression explicitly by checking either flag.
  • Compression is restricted to tebd_input_mode == "strip", which aligns with the updated docs and argcheck text.
  • Type embedding is always compressed first; the geometric part is compressed only when attn_layer == 0, and for attn_layer != 0 you fall back to TEBD‑only compression with a warning. This matches the stated behavior for nonzero‑attn_layer models.

To improve debuggability of the warning, you can add a stacklevel so the user sees the call site:

-        else:
-            warnings.warn(
-                "Attention layer is not 0, only type embedding is compressed. Geometric part is not compressed.",
-                UserWarning,
-            )
+        else:
+            warnings.warn(
+                "Attention layer is not 0, only type embedding is compressed. Geometric part is not compressed.",
+                UserWarning,
+                stacklevel=2,
+            )

This also satisfies Ruff’s B028 recommendation.

Also applies to: 574-660

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

278-281: Consider adding documentation for type embedding compression flag.

While line 281 documents the geometric compression structures, the tebd_compress flag (line 278) lacks a parallel comment. For consistency and clarity, consider adding:

+        # For type embedding compression
         self.tebd_compress = False
         self.geo_compress = False
         self.is_sorted = False
         # For geometric compression
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ee33c8 and afe5dd4.

📒 Files selected for processing (6)
  • deepmd/pt/model/descriptor/dpa1.py (5 hunks)
  • deepmd/pt/model/descriptor/dpa2.py (2 hunks)
  • deepmd/pt/model/descriptor/se_atten.py (7 hunks)
  • deepmd/utils/argcheck.py (1 hunks)
  • doc/model/train-se-atten.md (2 hunks)
  • source/tests/pt/test_model_compression_se_atten.py (5 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-10-08T15:32:11.479Z
Learnt from: njzjz
Repo: deepmodeling/deepmd-kit PR: 4144
File: source/api_cc/tests/test_deeppot_dpa_pt.cc:166-246
Timestamp: 2024-10-08T15:32:11.479Z
Learning: Refactoring between test classes `TestInferDeepPotDpaPt` and `TestInferDeepPotDpaPtNopbc` is addressed in PR #3905.

Applied to files:

  • source/tests/pt/test_model_compression_se_atten.py
🧬 Code graph analysis (1)
source/tests/pt/test_model_compression_se_atten.py (2)
deepmd/pt/model/descriptor/se_atten.py (2)
  • get_ntypes (309-311)
  • get_rcut (293-295)
deepmd/calculator.py (1)
  • DP (36-159)
🪛 Ruff (0.14.6)
deepmd/pt/model/descriptor/dpa1.py

599-599: Avoid specifying long messages outside the exception class

(TRY003)


602-602: Avoid specifying long messages outside the exception class

(TRY003)


657-657: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)

deepmd/pt/model/descriptor/dpa2.py

971-971: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (31)
  • GitHub Check: Agent
  • GitHub Check: CodeQL analysis (python)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test C++ (false)
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Build C library (2.14, >=2.5.0,<2.15, libdeepmd_c_cu11.tar.gz)
  • GitHub Check: Build C library (2.18, libdeepmd_c.tar.gz)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Analyze (python)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
🔇 Additional comments (8)
doc/model/train-se-atten.md (1)

137-137: Docs accurately describe TEBD-only compression for nonzero attn_layer

Both the se_atten_v2 notice and the general “Model compression” section now correctly state that compression is allowed for any attn_layer in strip mode and becomes TEBD‑only when attn_layer != 0, which matches the updated PyTorch descriptor logic. The duplication between the two mentions is fine, since they target slightly different reader contexts.

Also applies to: 185-185

deepmd/pt/model/descriptor/se_atten.py (7)

470-471: LGTM: Proper validation for type embedding compression.

The method correctly validates that tebd_input_mode == "strip" before allowing compression and only sets the tebd_compress flag after successful precomputation.

Also applies to: 508-508


636-672: LGTM: Type embedding compression flag correctly applied.

The replacement of self.compress with self.tebd_compress at lines 636 and 650 correctly enables selective use of precomputed type embeddings in both one-side and two-side modes. The indexing logic for type pairs is consistent with the precomputation in type_embedding_compression.


677-698: LGTM: Geometric compression path properly isolated.

The use of self.geo_compress correctly controls the fusion computation path that bypasses attention layers. The empty gg tensor (lines 690-697) is appropriate since it will be returned as None when geo_compress is True (lines 725-726).


724-726: LGTM: Return value correctly reflects geometric compression state.

The conditional return of gg based on not self.geo_compress appropriately signals that the intermediate gg representation is not available when using the fusion computation path.


435-456: I'll help you verify this review comment. Let me investigate the geometric compression handling in the code.
<function_calls>

#!/bin/bash

First, search for enable_compression method and its calls

echo "=== Searching for enable_compression method and calls ==="
rg -n "enable_compression" deepmd/pt/model/descriptor/se_atten.py -A 5 -B 2


</function_calls>

<function_calls>

#!/bin/bash

Search for all calls to enable_compression in the codebase

echo "=== Finding all calls to enable_compression ==="
rg -rn ".enable_compression(" --type=py


</function_calls>

<function_calls>

#!/bin/bash

Look at the forward method and geo_compress usage around line 677

echo "=== Examining forward method and geo_compress usage ==="
sed -n '670,690p' deepmd/pt/model/descriptor/se_atten.py


</function_calls>

<function_calls>

#!/bin/bash

Search for tebd_input_mode usage and initialization

echo "=== Searching for tebd_input_mode ==="
rg -n "tebd_input_mode" deepmd/pt/model/descriptor/se_atten.py -B 2 -A 2


</function_calls>

<function_calls>

#!/bin/bash

Look at more context around the forward method to understand strip mode checking

echo "=== Examining broader context of strip mode handling ==="
rg -n "strip" deepmd/pt/model/descriptor/se_atten.py -B 2 -A 2


</function_calls>

<function_calls>

#!/bin/bash

Get the file size and look at the initialization/class structure

echo "=== File structure and init method ==="
wc -l deepmd/pt/model/descriptor/se_atten.py
echo ""
echo "=== Looking for init and compress_info initialization ==="
rg -n "init|compress_info|geo_compress" deepmd/pt/model/descriptor/se_atten.py | head -30


</function_calls>


278-279: Unable to verify review comment — repository access failed.

The verification scripts could not execute due to repository clone failures. Without access to the deepmodeling/deepmd-kit codebase, I cannot verify:

  1. Whether warnings are issued when geo_compress is enabled with attn_layer != 0
  2. How tebd_compress and geo_compress separation enables compression with any attn_layer value
  3. Whether validation logic prevents geo_compress when attn_layer > 0
  4. The context and usage of these flags at lines 278–279, 456, and 508

The review comment's concerns about the attn_layer relationship and compression behavior remain unverified pending repository access.


278-279: I'm encountering persistent technical issues accessing the repository to verify the removal of the old compress attribute. The repository cloning is failing repeatedly, which prevents me from searching the codebase for any remaining references to self.compress or validating that the replacement with tebd_compress and geo_compress is complete.

Unable to complete automated verification due to repository access limitations.

Since I cannot execute the necessary code searches to verify:

  • Whether self.compress references remain elsewhere in the codebase
  • If serialization/deserialization code still uses the old attribute name
  • The scope of changes across related descriptor classes

The review comment's recommendation to verify complete removal of the old compress attribute should be validated through manual code inspection or by running the verification scripts in a properly configured environment with direct repository access.

@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

❌ Patch coverage is 93.54839% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.33%. Comparing base (1ee33c8) to head (7026d9c).
⚠️ Report is 4 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/pt/model/descriptor/dpa1.py 93.33% 1 Missing ⚠️
deepmd/pt/model/descriptor/dpa2.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #5066   +/-   ##
=======================================
  Coverage   84.33%   84.33%           
=======================================
  Files         709      709           
  Lines       70435    70442    +7     
  Branches     3618     3618           
=======================================
+ Hits        59402    59408    +6     
  Misses       9867     9867           
- Partials     1166     1167    +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@njzjz njzjz left a comment

Choose a reason for hiding this comment

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

Also, I am wondering if the type embedding can be compressed at runtime, instead of executing dp compress.

@OutisLi
Copy link
Collaborator Author

OutisLi commented Nov 28, 2025

Also, I am wondering if the type embedding can be compressed at runtime, instead of executing dp compress.

maybe through another PR

@OutisLi OutisLi requested a review from njzjz November 28, 2025 02:12
@OutisLi OutisLi requested a review from njzjz November 30, 2025 04:46
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Dec 1, 2025
Merged via the queue into deepmodeling:devel with commit 0ad1ab6 Dec 1, 2025
60 checks passed
@OutisLi OutisLi deleted the pr/atten_comp branch December 2, 2025 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants