Skip to content

Conversation

@caic99
Copy link
Member

@caic99 caic99 commented May 19, 2025

By profiling results, using @ matmul creates 7 matmul to complete this operation, which are time-consuming. einsum only requires one matmul.
This optimization benefits smaller batch size and model.

Profiling results on PyTorch

image image


image

Summary by CodeRabbit

  • Refactor
    • Enhanced internal calculations for improved efficiency without changing any user-facing features.

Copilot AI review requested due to automatic review settings May 19, 2025 11:01
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 PR optimizes the virial computation by replacing multiple matmul operations with a single einsum call, reducing runtime overhead especially for smaller batches and models.

  • Switch PyTorch virial calc from @ matmul to torch.einsum
  • Switch Paddle virial calc from @ matmul to paddle.einsum

Reviewed Changes

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

File Description
deepmd/pt/model/model/transform_output.py Replace unsqueeze + @ matmul with torch.einsum for virial
deepmd/pd/model/model/transform_output.py Replace unsqueeze + @ matmul with paddle.einsum for virial
Comments suppressed due to low confidence (2)

deepmd/pt/model/model/transform_output.py:88

  • Add a unit test to verify that the new einsum-based virial matches the original unsqueeze + @ matmul output for various tensor shapes to ensure correctness.
extended_virial = torch.einsum("...ik,...ij->...ikj", extended_force, extended_coord)

deepmd/pd/model/model/transform_output.py:85

  • [nitpick] Add a gradient consistency test to ensure the Paddle einsum variant produces equivalent backward results compared to the original matmul approach.
extended_virial = paddle.einsum(

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 19, 2025

📝 Walkthrough

"""

Walkthrough

The changes update the computation of the extended_virial tensor in both Paddle and PyTorch model output transformation modules. The previous matrix multiplication approach is replaced with an equivalent Einstein summation (einsum) operation. No other logic, function signatures, or control flow is modified.

Changes

File(s) Change Summary
deepmd/pd/model/model/transform_output.py, deepmd/pt/model/model/transform_output.py Replaced matrix multiplication using unsqueeze and @ with einsum for calculating extended_virial. No other logic or signatures changed.

Suggested labels

Python

Suggested reviewers

  • njzjz
    """

📜 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 f5d6c30 and 74ba118.

📒 Files selected for processing (1)
  • deepmd/pt/model/model/transform_output.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • deepmd/pt/model/model/transform_output.py
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link

codecov bot commented May 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.69%. Comparing base (43e0288) to head (74ba118).
⚠️ Report is 88 commits behind head on devel.

Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #4746      +/-   ##
==========================================
- Coverage   84.69%   84.69%   -0.01%     
==========================================
  Files         697      697              
  Lines       67474    67473       -1     
  Branches     3540     3540              
==========================================
- Hits        57147    57145       -2     
+ Misses       9197     9196       -1     
- Partials     1130     1132       +2     

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

@caic99 caic99 requested a review from iProzd May 19, 2025 13:20
@iProzd iProzd added this pull request to the merge queue May 20, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 20, 2025
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue May 21, 2025
Merged via the queue into deepmodeling:devel with commit 4238481 May 21, 2025
60 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.

3 participants