Skip to content

fix(dpmodel): materialize fitting buffers on active backend#5841

Open
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/dpmodel-general-fitting-buffers-5642
Open

fix(dpmodel): materialize fitting buffers on active backend#5841
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/dpmodel-general-fitting-buffers-5642

Conversation

@njzjz-bot

@njzjz-bot njzjz-bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #5642

Summary

  • retain fitting statistics and constants as portable serialized state
  • materialize default fparam, fparam/aparam normalization statistics, case embeddings, and atomic output biases in the runtime namespace, dtype, and device immediately before use
  • apply the same boundary to PolarFitting.scale and constant_matrix, which had the identical failure mode during polar postprocessing
  • avoid mutating persistent attributes during forward calls, preserving serialization and backend-wrapper behavior

The complete affected set is nine arrays: default_fparam_tensor, fparam_avg, fparam_inv_std, aparam_avg, aparam_inv_std, case_embd, bias_atom_e, polar scale, and polar constant_matrix.

Why existing tests missed this

Cross-backend fitting tests already exercise default fparam, parameter statistics, biases, and polar outputs, but they instantiate backend wrappers that eagerly convert every NumPy attribute. Those objects are homogeneous before forward execution and therefore never expose the generic dpmodel boundary.

Direct dpmodel fitting tests use NumPy descriptors, atom types, parameters, and buffers, so the selected namespace is also NumPy and all operations are valid. Dedicated PyTorch, Paddle, and TensorFlow implementations maintain their own backend-native state and do not exercise this shared dpmodel implementation. No previous test combined a raw dpmodel fitting object, backend-native runtime inputs, and still-NumPy portable buffers.

Validation

  • 21 common dpmodel fitting tests passed
  • direct invariant-fitting regression covers all seven GeneralFitting buffers, default fparam, explicit aparam normalization, case embedding, and nonzero atomic biases
  • direct polar regression covers nontrivial per-type scale and diagonal shift buffers
  • both regressions assert Torch output backend/dtype/device and numerical parity with NumPy
  • ruff format .
  • ruff check .

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

Summary by CodeRabbit

  • Bug Fixes

    • Improved compatibility when fitting calculations use different computational backends, devices, or data types.
    • Fixed handling of fitting parameters, case embeddings, atom biases, and polarizability scaling across supported backends.
    • Ensured runtime outputs preserve the active backend and expected precision.
  • Tests

    • Added regression coverage for backend-specific fitting and polarizability calculations.
    • Added validation for consistent results between NumPy and PyTorch execution.

Convert portable fitting constants locally into the runtime namespace, dtype, and device before default-fparam handling, parameter normalization, case embedding, atomic bias gathering, and polar scale/shift postprocessing.

Add direct dpmodel regressions with Torch runtime inputs while all nine persistent buffers remain NumPy, verifying backend identity and numerical parity without eager wrapper conversion.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@dosubot dosubot Bot added the bug label Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1f982a08-60f6-46e1-b48f-f8eef432949e

📥 Commits

Reviewing files that changed from the base of the PR and between 6c3b985 and 44b79c5.

📒 Files selected for processing (4)
  • deepmd/dpmodel/fitting/general_fitting.py
  • deepmd/dpmodel/fitting/polarizability_fitting.py
  • source/tests/common/dpmodel/test_fitting_invar_fitting.py
  • source/tests/common/dpmodel/test_fitting_polar_backend.py

📝 Walkthrough

Walkthrough

GeneralFitting and PolarFitting now materialize stored buffers on the active backend, dtype, and device before runtime operations. New Torch-conditional tests verify GeneralFitting and PolarFitting outputs against NumPy results.

Changes

Backend buffer materialization

Layer / File(s) Summary
GeneralFitting runtime buffers
deepmd/dpmodel/fitting/general_fitting.py, source/tests/common/dpmodel/test_fitting_invar_fitting.py
Default and runtime fparams, aparams, case embeddings, and atom biases are converted to the active backend before use; a Torch boundary test validates output type, dtype, device, and values.
PolarFitting scale and shift buffers
deepmd/dpmodel/fitting/polarizability_fitting.py, source/tests/common/dpmodel/test_fitting_polar_backend.py
Scale and constant-matrix buffers are materialized before atom-type gathering; a Torch regression test compares polarizability with the NumPy result.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: Python

Suggested reviewers: wanghan-iapcm

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PolarFitting backend-materialization changes and its new regression test are not part of the linked GeneralFitting issue. Remove or link the PolarFitting changes to a separate issue if they are intentional, otherwise keep the PR scoped to GeneralFitting.
Linked Issues check ❓ Inconclusive GeneralFitting buffer materialization matches the issue, but the summary doesn't confirm the requested backend test coverage for default fparams, case embeddings, and bias. Add or describe a regression test that uses backend-native descriptors with default fparams, nonzero case embeddings, and atomic bias values.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the main backend buffer materialization change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.32%. Comparing base (6c3b985) to head (44b79c5).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5841      +/-   ##
==========================================
- Coverage   78.58%   78.32%   -0.26%     
==========================================
  Files        1050     1050              
  Lines      120637   120648      +11     
  Branches     4356     4361       +5     
==========================================
- Hits        94801    94500     -301     
- Misses      24278    24587     +309     
- Partials     1558     1561       +3     

☔ View full report in Codecov by Harness.
📢 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.

@njzjz

njzjz commented Jul 18, 2026

Copy link
Copy Markdown
Member

Possible reviewers based on changed lines, exact file history, and exact-file review history:

  • @wanghan-iapcm — 20 commits on changed files; 22 reviews on exact changed files (deepmd/dpmodel/fitting/general_fitting.py, deepmd/dpmodel/fitting/polarizability_fitting.py, source/tests/common/dpmodel/test_fitting_invar_fitting.py).
  • @anyangml — 12 commits on changed files (deepmd/dpmodel/fitting/general_fitting.py, deepmd/dpmodel/fitting/polarizability_fitting.py).

No review request was made automatically.

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

@njzjz
njzjz requested review from anyangml and wanghan-iapcm July 18, 2026 07:18
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.

[Code scan] Materialize GeneralFitting buffers on the active backend

2 participants