Skip to content

Conversation

@Chengqian-Zhang
Copy link
Collaborator

@Chengqian-Zhang Chengqian-Zhang commented Aug 27, 2025

In finetuing process, the computation of fitting stat is skipped in previous code. There are two situations:

  1. Finetuning from pretrained model's branch: it means pretrained model also has fparam or aparam which has the same meaning of finetuning task. The key fparam_avg/fparam_inv_std/ aparam_avg/aparam_inv_std load from the pretrained model. It is correct.
  2. Finetuning using RANDOM fitting. The fitting stat should be calculated in this situation. But the computation of fitting stat is skipped now. There is some error.

Summary by CodeRabbit

  • New Features

    • Automatic computation of input statistics during bias-adjustment when using "set-by-statistic" mode; added an explicit operation to compute fitting input statistics.
  • Tests

    • Broadened test coverage to include additional parameter categories in training comparisons.
    • Added a test-only configuration option to control batching used for data-statistics computation.

@Chengqian-Zhang Chengqian-Zhang marked this pull request as draft August 27, 2025 08:19
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 27, 2025

📝 Walkthrough

Walkthrough

Adds a new compute_fitting_stat API to atomic model base classes and DP implementations, invokes it from make_model when bias_adjust_mode == "set-by-statistic", and updates tests to set model.data_stat_nbatch and broaden state-dict comparison keys.

Changes

Cohort / File(s) Summary
Base atomic model interfaces
deepmd/pd/model/atomic_model/base_atomic_model.py, deepmd/pt/model/atomic_model/base_atomic_model.py
Add public placeholder method compute_fitting_stat(self, sample_merged: Union[Callable[[], list[dict]], list[dict]]) -> None with docstring; implementation is a no-op.
DP atomic model implementations
deepmd/pd/model/atomic_model/dp_atomic_model.py, deepmd/pt/model/atomic_model/dp_atomic_model.py
Add compute_fitting_stat delegating to self.fitting_net.compute_input_stats(sample_merged, protection=self.data_stat_protect); PT variant adds Union import and replaces direct calls with this helper.
Model creation hooks
deepmd/pd/model/model/make_model.py, deepmd/pt/model/model/make_model.py
In change_out_bias, when bias_adjust_mode == "set-by-statistic", call self.atomic_model.compute_fitting_stat(merged) after existing bias adjustment.
Test suite updates
source/tests/pd/test_training.py, source/tests/pt/test_training.py
Set model.data_stat_nbatch = 100 in test setups; broaden assertions to include keys containing fparam or aparam when comparing against random-finetuned state.

Sequence Diagram

sequenceDiagram
    participant MakeModel as make_model
    participant AtomicModel
    participant FittingNet

    MakeModel->>AtomicModel: change_out_bias(merged, bias_adjust_mode)
    alt bias_adjust_mode == "set-by-statistic"
        MakeModel->>AtomicModel: compute_fitting_stat(merged)
        AtomicModel->>FittingNet: compute_input_stats(sample_merged, protection)
        FittingNet-->>AtomicModel: stats computed
    else other modes
        Note over MakeModel: no compute_fitting_stat call
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review attention:
    • Correct use of self.data_stat_protect in delegation.
    • Proper handling of callable vs list inputs in compute_fitting_stat call sites.
    • Ensure change_out_bias calls are restricted to "set-by-statistic".
    • Test changes: verify new data_stat_nbatch integration and broadened key checks.

Possibly related PRs

Suggested reviewers

  • njzjz
  • wanghan-iapcm
  • iProzd

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 72.00% 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 PR title clearly and specifically describes the main change: calculating fitting statistics during random fitting in finetuning, which aligns with the changeset's core objective.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84.23%. Comparing base (25fa707) to head (5ebd7e3).

Files with missing lines Patch % Lines
deepmd/pd/model/atomic_model/base_atomic_model.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #4928   +/-   ##
=======================================
  Coverage   84.23%   84.23%           
=======================================
  Files         709      709           
  Lines       70078    70092   +14     
  Branches     3619     3619           
=======================================
+ Hits        59032    59044   +12     
- Misses       9880     9883    +3     
+ Partials     1166     1165    -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.

@Chengqian-Zhang Chengqian-Zhang marked this pull request as ready for review November 7, 2025 09:32
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

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7778e2e and 59c50c8.

📒 Files selected for processing (8)
  • deepmd/pd/model/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/pd/model/atomic_model/dp_atomic_model.py (1 hunks)
  • deepmd/pd/model/model/make_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/dp_atomic_model.py (2 hunks)
  • deepmd/pt/model/model/make_model.py (1 hunks)
  • source/tests/pd/test_training.py (2 hunks)
  • source/tests/pt/test_training.py (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Always run ruff check . and ruff format . before committing changes to Python code

Files:

  • deepmd/pt/model/atomic_model/dp_atomic_model.py
  • deepmd/pt/model/atomic_model/base_atomic_model.py
  • source/tests/pd/test_training.py
  • deepmd/pd/model/atomic_model/dp_atomic_model.py
  • deepmd/pd/model/atomic_model/base_atomic_model.py
  • deepmd/pd/model/model/make_model.py
  • deepmd/pt/model/model/make_model.py
  • source/tests/pt/test_training.py
🧠 Learnings (2)
📚 Learning: 2025-09-18T11:37:10.532Z
Learnt from: CR
Repo: deepmodeling/deepmd-kit PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-09-18T11:37:10.532Z
Learning: Applies to source/tests/tf/test_dp_test.py : Keep the core TensorFlow test `source/tests/tf/test_dp_test.py` passing; use it for quick validation

Applied to files:

  • source/tests/pd/test_training.py
  • source/tests/pt/test_training.py
📚 Learning: 2024-09-19T04:25:12.408Z
Learnt from: njzjz
Repo: deepmodeling/deepmd-kit PR: 4144
File: source/api_cc/tests/test_deeppot_dpa_pt.cc:166-246
Timestamp: 2024-09-19T04:25:12.408Z
Learning: Refactoring between test classes `TestInferDeepPotDpaPt` and `TestInferDeepPotDpaPtNopbc` is addressed in PR #3905.

Applied to files:

  • source/tests/pd/test_training.py
  • source/tests/pt/test_training.py
🧬 Code graph analysis (6)
deepmd/pt/model/atomic_model/dp_atomic_model.py (3)
deepmd/pd/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (406-424)
deepmd/pt/model/atomic_model/base_atomic_model.py (1)
  • compute_fitting_stat (496-512)
deepmd/pt/model/task/fitting.py (1)
  • compute_input_stats (78-157)
deepmd/pt/model/atomic_model/base_atomic_model.py (1)
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (336-354)
deepmd/pd/model/atomic_model/dp_atomic_model.py (3)
deepmd/pd/model/atomic_model/base_atomic_model.py (1)
  • compute_fitting_stat (518-534)
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (336-354)
deepmd/pd/model/task/fitting.py (1)
  • compute_input_stats (75-160)
deepmd/pd/model/atomic_model/base_atomic_model.py (1)
deepmd/pd/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (406-424)
deepmd/pd/model/model/make_model.py (3)
deepmd/pd/model/atomic_model/base_atomic_model.py (1)
  • compute_fitting_stat (518-534)
deepmd/pd/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (406-424)
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (336-354)
deepmd/pt/model/model/make_model.py (2)
deepmd/pt/model/atomic_model/base_atomic_model.py (1)
  • compute_fitting_stat (496-512)
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (336-354)
⏰ 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-win_amd64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test C++ (false)
  • 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: Analyze (c-cpp)
  • GitHub Check: Analyze (python)
🔇 Additional comments (10)
deepmd/pd/model/model/make_model.py (1)

231-232: LGTM! Correct invocation of compute_fitting_stat.

The conditional call to compute_fitting_stat after change_out_bias appropriately addresses the PR objective of computing fitting statistics when using random fitting in finetuning (set-by-statistic mode). The implementation correctly reuses the merged data.

source/tests/pd/test_training.py (2)

92-100: LGTM! Appropriate test assertion for fparam/aparam keys.

The broadened exclusion condition correctly validates that fparam and aparam statistics are preserved during random finetuning, aligning with the PR's objective to compute fitting statistics properly.


197-197: LGTM! Configuration for data statistics batching.

Adding data_stat_nbatch = 100 appropriately exercises the data statistics batching behavior that's central to this PR's fitting statistics computation.

deepmd/pt/model/atomic_model/base_atomic_model.py (1)

496-512: LGTM! Appropriate placeholder for PT base atomic model.

The no-op implementation is correct for the base class, allowing derived classes to provide concrete implementations. The documentation correctly references torch.Tensor for the PyTorch path.

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

235-236: LGTM! Correct invocation of compute_fitting_stat in PT path.

The implementation mirrors the PD path and correctly invokes compute_fitting_stat when bias_adjust_mode is "set-by-statistic", addressing the PR's objective for the PyTorch path.

source/tests/pt/test_training.py (2)

95-103: LGTM! Test assertions align with PD path.

The broadened exclusion condition for fparam/aparam keys correctly validates the new fitting statistics computation during random finetuning in the PyTorch path.


263-263: LGTM! Configuration mirrors PD test setup.

Setting data_stat_nbatch = 100 appropriately exercises data statistics batching in the PyTorch path, consistent with the PD tests.

deepmd/pt/model/atomic_model/dp_atomic_model.py (3)

8-8: LGTM! Union import for type hints.

The Union import is correctly added to support the type hints for the new compute_fitting_stat method signature.


332-332: LGTM! Refactored to use compute_fitting_stat.

Good refactoring that centralizes fitting statistics computation through the new compute_fitting_stat method, improving code organization and maintainability.


336-354: LGTM! Proper implementation of compute_fitting_stat.

The method correctly delegates to fitting_net.compute_input_stats with the data_stat_protect parameter, providing a clean interface for computing fitting statistics from packed data.

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

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59c50c8 and 646977c.

📒 Files selected for processing (2)
  • deepmd/pd/model/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/dp_atomic_model.py (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Always run ruff check . and ruff format . before committing changes to Python code

Files:

  • deepmd/pd/model/atomic_model/base_atomic_model.py
  • deepmd/pt/model/atomic_model/dp_atomic_model.py
🧬 Code graph analysis (2)
deepmd/pd/model/atomic_model/base_atomic_model.py (2)
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (336-354)
deepmd/pd/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (406-424)
deepmd/pt/model/atomic_model/dp_atomic_model.py (2)
deepmd/pt/model/atomic_model/base_atomic_model.py (1)
  • compute_fitting_stat (496-512)
deepmd/pt/model/task/fitting.py (1)
  • compute_input_stats (78-157)
⏰ 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). (28)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (3, 3.9)
  • GitHub Check: Test Python (1, 3.12)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (4, 3.9)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (2, 3.12)
  • GitHub Check: Test Python (1, 3.9)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Test C++ (true)
  • GitHub Check: Test C++ (false)
  • 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: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (cpu, cpu)
  • GitHub Check: Build C++ (rocm, rocm)
🔇 Additional comments (2)
deepmd/pt/model/atomic_model/dp_atomic_model.py (2)

8-8: LGTM: Import addition supports new method signature.

The Union import is necessary for the Union[Callable[[], list[dict]], list[dict]] type hint in the new compute_fitting_stat method.


332-332: Good refactoring to use the public API.

Calling self.compute_fitting_stat(wrapped_sampler) instead of directly accessing self.fitting_net.compute_input_stats establishes a consistent public interface and enables proper encapsulation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
deepmd/pt/model/atomic_model/base_atomic_model.py (1)

496-512: LGTM! Clean API extension for computing fitting statistics.

The method provides a well-documented hook for derived classes to compute fitting statistics during finetuning. The no-op default (pass) is appropriate since not all atomic model types require fitting statistics computation.

Optional: Consider adding a clarifying comment

To make the intent clearer, you could add a brief comment before the pass statement:

         the lazy function helps by only sampling once.
         """
+        # No-op in base class; derived classes override if fitting statistics are needed.
         pass

Reminder: Run code quality checks

As per coding guidelines, ensure you run the following before committing:

ruff check .
ruff format .
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 646977c and 52d29ef.

📒 Files selected for processing (4)
  • deepmd/pd/model/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/pd/model/atomic_model/dp_atomic_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/base_atomic_model.py (1 hunks)
  • deepmd/pt/model/atomic_model/dp_atomic_model.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • deepmd/pd/model/atomic_model/base_atomic_model.py
  • deepmd/pt/model/atomic_model/dp_atomic_model.py
  • deepmd/pd/model/atomic_model/dp_atomic_model.py
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Always run ruff check . and ruff format . before committing changes to Python code

Files:

  • deepmd/pt/model/atomic_model/base_atomic_model.py
🧬 Code graph analysis (1)
deepmd/pt/model/atomic_model/base_atomic_model.py (2)
deepmd/pd/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (406-424)
deepmd/pt/model/atomic_model/dp_atomic_model.py (1)
  • compute_fitting_stat (336-354)
⏰ 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: Test Python (4, 3.9)
  • GitHub Check: Test Python (5, 3.9)
  • GitHub Check: Test Python (3, 3.12)
  • GitHub Check: Test Python (6, 3.12)
  • GitHub Check: Test Python (6, 3.9)
  • GitHub Check: Test Python (5, 3.12)
  • GitHub Check: Test Python (4, 3.12)
  • GitHub Check: Test Python (2, 3.9)
  • GitHub Check: Test Python (3, 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: Test C++ (false)
  • GitHub Check: Analyze (c-cpp)
  • GitHub Check: Analyze (python)
  • GitHub Check: Build wheels for cp311-macosx_arm64
  • GitHub Check: Build wheels for cp310-manylinux_aarch64
  • GitHub Check: Build wheels for cp311-win_amd64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build wheels for cp311-macosx_x86_64
  • GitHub Check: Build wheels for cp311-manylinux_x86_64
  • GitHub Check: Build C++ (cuda120, cuda)
  • GitHub Check: Build C++ (clang, clang)
  • GitHub Check: Build C++ (rocm, rocm)
  • GitHub Check: Build C++ (cuda, cuda)
  • GitHub Check: Build C++ (cpu, cpu)
  • 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)

else:
raise RuntimeError("Unknown bias_adjust_mode mode: " + bias_adjust_mode)

def compute_fitting_stat(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would recommend changing the method name to compute_fitting_input_stat.

@wanghan-iapcm
Copy link
Collaborator

Limitation: the imput stat is not implemented in the python backend.

self,
sample_merged: Union[Callable[[], list[dict]], list[dict]],
) -> None:
"""Compute the input statistics (e.g. mean and stddev) for the fittings from packed data.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""Compute the input statistics (e.g. mean and stddev) for the fittings from packed data.
"""Compute the input statistics (e.g. mean and stddev) for the atomic model from packed data.

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