Skip to content

Conversation

@AsymmetryChou
Copy link
Collaborator

@AsymmetryChou AsymmetryChou commented Nov 28, 2025

This pull request updates the forward method in dptb/nn/energy.py to improve compatibility and correctness when using the numpy eigenvalue solver, especially when working with tensors on different devices and data types. The main changes ensure that all tensors are moved to the CPU before being converted to numpy arrays and that the output eigenvalues preserve the original data type. For #275.

Key changes:

Device and dtype handling improvements:

  • Ensured that tensors are moved to the CPU before conversion to numpy when using the numpy eigenvalue solver, preventing device mismatch errors.
  • When converting eigenvalues computed via numpy back to torch tensors, the dtype is now explicitly matched to the original Hamiltonian's dtype, preserving numerical consistency.

Code clarity and correctness:

  • Added logic to handle the case when the numpy solver is used without overlap, ensuring proper conversion to numpy arrays.…class

Summary by CodeRabbit

  • Bug Fixes
    • Fixed eigenvalue computation with the NumPy solver when overlap is present, preventing unintended in-place modifications that could corrupt results.
    • Ensured NumPy-based calculations run on the CPU and return results without losing numerical precision.
    • Preserved original data types and device alignment so NumPy and Torch code paths produce consistent, stable eigenvalues.

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

@coderabbitai
Copy link

coderabbitai bot commented Nov 28, 2025

📝 Walkthrough

Walkthrough

The NumPy branch of the energy calculation now computes a CPU NumPy transformed Hamiltonian using the lower-triangular factor of the overlap matrix, obtains eigenvalues from that transformed Hamiltonian in NumPy, and returns eigenvalues as a PyTorch tensor with the Hamiltonian's original dtype and device.

Changes

Cohort / File(s) Summary
NumPy solver tensor conversion handling
dptb/nn/energy.py
In the NumPy branch: convert overlap and Hamiltonian fields to CPU NumPy (.detach().cpu().numpy()), build Cholesky-related transform from NumPy S, compute h_transformed_np = chklowtinv @ h_np @ chklowtinv.T, compute eigenvalues in NumPy (no in-place tensor transforms), and convert eigenvalues back to PyTorch preserving original dtype (self.h2k.dtype) and device.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Review focus:
    • Confirm .detach().cpu().numpy() usage and no accidental device/dtype loss.
    • Validate Cholesky/inverse and matrix multiplication shapes and ordering.
    • Verify final eigenvalue tensor conversion preserves dtype and device.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 accurately describes the main change: fixing tensor conversion for the numpy solver in eigenvalue calculations, which is the core focus of the changes to dptb/nn/energy.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 c6f87e6 and 52d5928.

📒 Files selected for processing (1)
  • dptb/nn/energy.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • dptb/nn/energy.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). (1)
  • GitHub Check: build

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


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

@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: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fd65c44 and 95bd47c.

📒 Files selected for processing (1)
  • dptb/nn/energy.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
dptb/nn/energy.py (1)
dptb/utils/torch_geometric/data.py (2)
  • cpu (326-330)
  • to (316-324)
⏰ 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). (2)
  • GitHub Check: CodeQL analysis (python)
  • GitHub Check: build

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@AsymmetryChou AsymmetryChou added the Bug Report Something isn't working label Nov 28, 2025
Copilot finished reviewing on behalf of AsymmetryChou November 28, 2025 06:35
@AsymmetryChou AsymmetryChou requested a review from Lonya0 November 28, 2025 06:35
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

fallback for h_transformed_np when overlap is False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Report Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants