Skip to content

fix(dpmodel): handle zero-node DPA4C spin graphs - #6007

Merged
OutisLi merged 2 commits into
deepmodeling:masterfrom
LOGO127:fix/native-spin-zero-node-graph
Oct 15, 2026
Merged

OutisLi merged 2 commits into
deepmodeling:masterfrom
LOGO127:fix/native-spin-zero-node-graph

Conversation

@LOGO127

@LOGO127 LOGO127 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #5991.

The native-spin cross Gram has shape (N, quadrupole_width, C_2). Reshaping it to (N, -1) raises for N == 0, since the feature width cannot be inferred from an empty array. Use the existing quadrupole_width * degree_channels[2] instead. This preserves the feature layout for nonempty inputs and requires no empty-graph special case.

Add regression tests through the actual DescrptDPA4C.call_graph() API in both NumPy and PyTorch. They cover float32/float64, channels 8/32, zero nodes and two isolated nodes, with the spin gate open. Check output shape/dtype, and PyTorch device and spin gradients.

Validation

Built from upstream 58a12b1e0a72cfcc322c64d70c21469220bef74f on Linux x86-64 / Python 3.12.3, with PyTorch 2.11.0+cpu and TensorFlow CPU 2.21.0. Compiled the native C++ libraries and TensorFlow/PyTorch operators from this source.

Before the production change, the new tests produced 8 failed, 8 passed: all eight zero-node cases failed at the reported reshape, while the isolated-node controls passed. After the change, 16 passed.

python -m pytest -q \
  source/tests/common/dpmodel/test_descriptor_dpa4c.py \
  source/tests/pt_expt/descriptor/test_dpa4c.py \
  source/tests/pt_expt/descriptor/test_dpa4c_cpu.py \
  source/tests/pt_expt/model/test_dpa4c_graph_lower.py \
  source/tests/tf/test_dp_test.py::TestDPTestEner::test_1frame

Result: 173 passed, 17 skipped. This includes all 31 CPU-operator tests, existing nonempty-input parity/gradient contracts, graph lowering, and a TensorFlow inference smoke test. The skipped cases are one JAX test (JAX is not installed) and 16 CUDA-only tests; GPU execution is not claimed. The run emitted operator-registration and TorchScript deprecation warnings.

ruff check ., ruff format --check ., and git diff --check passed.

AI assistance

This implementation and its local verification were prepared with assistance from OpenAI Codex.

Summary by CodeRabbit

  • Bug Fixes

    • Made spin descriptor feature dimensions explicit and consistent across supported configurations.
    • Ensured edge-free and isolated-node graphs produce valid, finite outputs without requiring rotation matrices.
  • Tests

    • Added coverage for native spin graphs in float32 and float64, with multiple channel widths and zero or isolated nodes.
    • Verified output shape, dtype, device, finiteness, and finite spin gradients.

@github-actions github-actions Bot added the Python label Sep 5, 2026
@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fba17196-5b52-48ce-982f-6627766c204c
📥 Commits

Reviewing files that changed from the base of the PR and between 7959824 and b111fba.

📒 Files selected for processing: 1
  • source/tests/pt_expt/descriptor/test_dpa4c.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The native-spin DPA4C descriptor now uses an explicit cross-Gram output width. Tests cover edge-free graphs with zero or isolated nodes across precisions and channel widths.

Changes: Native-spin empty graph handling

Layer / File(s) Summary
Explicit cross-Gram shape and regression coverage
deepmd/dpmodel/descriptor/dpa4c_nn/spin.py, source/tests/common/dpmodel/test_descriptor_dpa4c.py, source/tests/pt_expt/descriptor/test_dpa4c.py
The cross-Gram reshape specifies quadrupole_width * degree_channels[2]. Native-spin tests validate output shape, dtype, device, finite values, absent rotation data, and finite spin gradients for edge-free graphs.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to b111f

The zero-node native-spin path and its regression coverage appear ready to merge after normal checks.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the main change: fixing zero-node native-spin DPA4C graphs in dpmodel.
Linked Issues check Passed Issue #5991 requires the native-spin call_graph() path to support zero-node graphs and return shape (0, descriptor.get_dim_out()). deepmd/dpmodel/descriptor/dpa4c_nn/spin.py now reshapes to `(sp…
Out of Scope Changes check Passed The diff contains one production reshape fix and regression tests for the zero-node native-spin graph path. The tests directly support issue #5991 and its requested behavior. No unrelated production o…
Docstring Coverage Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 3 files.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

@njzjz-bot njzjz-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fix replaces the ambiguous empty-tensor reshape with the already-known feature width, so it preserves the nonempty layout while making the zero-node case well-defined. The new NumPy/PyTorch tests cover zero-node and isolated-node graphs across precisions/channel widths and include the spin-gradient path. I did not find a blocking issue. Upstream currently shows only the successful triage check; the PR also reports focused source-build validation.

Agent: ChatGPT
Model: GPT-5.6 Sol

@OutisLi
OutisLi self-requested a review October 15, 2026 05:50
@OutisLi
OutisLi enabled auto-merge October 15, 2026 05:57

@OutisLi OutisLi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The explicit cross-Gram feature width matches the actual tensor layout and get_dim_out(), fixes zero-node graphs without a special-case branch, and preserves nonempty-input semantics and serialization. Verified the regression by restoring the original reshape in memory: all 8 zero-node cases failed while all 8 isolated-node controls passed; all 16 cases passed with this fix, including spin-gradient checks. Related dpmodel/pt_expt descriptor and graph-lower suites: 142 passed, 16 skipped. Ruff and diff checks passed. Local validation was CPU-only; GPU execution was not verified. No blocking issues found.

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.60%. Comparing base (025f4f6) to head (b111fba).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6007      +/-   ##
==========================================
- Coverage   77.85%   77.60%   -0.25%     
==========================================
  Files        1170     1170              
  Lines      140914   140914              
  Branches     5056     5063       +7     
==========================================
- Hits       109705   109354     -351     
- Misses      29325    29678     +353     
+ Partials     1884     1882       -2     

☔ 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.

@OutisLi
OutisLi added this pull request to the merge queue Oct 15, 2026
Merged via the queue into deepmodeling:master with commit 4274e3a Oct 15, 2026
58 checks passed
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.

[BUG] Native-spin DPA4C graph path fails on zero-node graphs

3 participants