Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: allow tf32 datatype for matmul #4499

Draft
wants to merge 1 commit into
base: devel
Choose a base branch
from
Draft

Conversation

caic99
Copy link
Member

@caic99 caic99 commented Dec 24, 2024

Pytorch by default disables using TF32 data type starting from A100 GPU. Enabling TF32 utilizes tensor core on A100 GPUs, expecting better performance.

I will later attach some test results on the speed-up and accuracy of this PR.

Ref:
https://pytorch.org/docs/stable/notes/cuda.html#tf32-on-ampere
https://pytorch.org/docs/stable/generated/torch.set_float32_matmul_precision.html

Summary by CodeRabbit

  • New Features
    • Introduced support for TensorFloat-32 (TF32) in PyTorch to enhance matrix multiplication performance on compatible hardware.

Copy link
Contributor

coderabbitai bot commented Dec 24, 2024

📝 Walkthrough

Walkthrough

The pull request introduces a configuration change in the deepmd/pt/entrypoints/main.py file to enable TensorFloat-32 (TF32) support in PyTorch. Specifically, a new line is added within the get_trainer function to set torch.backends.cuda.matmul.allow_tf32 = True. This modification allows for potentially improved matrix multiplication performance on compatible CUDA-enabled hardware without altering the existing code logic or functionality of the training process.

Changes

File Change Summary
deepmd/pt/entrypoints/main.py Added configuration to enable TF32 support by setting torch.backends.cuda.matmul.allow_tf32 = True in the get_trainer function

Possibly related PRs

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 47412da and 1594898.

📒 Files selected for processing (1)
  • deepmd/pt/entrypoints/main.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • deepmd/pt/entrypoints/main.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

Copy link

codecov bot commented Dec 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.42%. Comparing base (c0914e1) to head (1594898).
Report is 7 commits behind head on devel.

Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #4499   +/-   ##
=======================================
  Coverage   84.41%   84.42%           
=======================================
  Files         670      670           
  Lines       62147    62141    -6     
  Branches     3487     3488    +1     
=======================================
+ Hits        52464    52465    +1     
+ Misses       8556     8549    -7     
  Partials     1127     1127           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

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

It is better to provide an option in the input script to control the behavior of using tensorcore.

@caic99
Copy link
Member Author

caic99 commented Dec 25, 2024

It is better to provide an option in the input script to control the behavior of using tensorcore.

@njzjz Do you have any suggestions on adding this option? Maybe as an option under "training", or somehow integrating with precision controlling flags?

@wanghan-iapcm
Copy link
Collaborator

it seems that the precision during the training and inference time should be consistent.
have you tested training at tf32 and inference at fp32? does it work or any loss of precision?

@njzjz
Copy link
Member

njzjz commented Dec 27, 2024

It is better to provide an option in the input script to control the behavior of using tensorcore.

@njzjz Do you have any suggestions on adding this option? Maybe as an option under "training", or somehow integrating with precision controlling flags?

I think we can make the decision after we see the benchmark result.

@caic99
Copy link
Member Author

caic99 commented Jan 2, 2025

I tested training the DPA-3 alpha model from scratch on AlMgCu dataset with different matmul precision on A800 GPU, and here is the result:

metric highest (FP32) high (TF32) medium (BF16)
Energy MAE eV 5.20E-01 4.93E-01 5.00E-01
Energy RMSE eV 8.59E-01 9.14E-01 8.47E-01
Energy MAE/Natoms eV 2.61E-02 2.47E-02 2.49E-02
Energy RMSE/Natoms eV 4.45E-02 4.95E-02 4.64E-02
Force MAE eV/A 5.88E-02 6.18E-02 6.05E-02
Force RMSE eV/A 8.37E-02 8.91E-02 8.67E-02
Virial MAE eV 9.85E-01 1.12E+00 1.02E+00
Virial RMSE eV 2.03E+00 3.19E+00 1.99E+00
Virial MAE/Natoms eV 4.84E-02 5.50E-02 5.09E-02
Virial RMSE/Natoms eV 9.69E-02 1.40E-01 9.88E-02

Note: the definition of highest, high, and medium comes here.
Please note that this precision setting only applies for the internal instructions used for MatMul.


1 GPU training speed:

  • FP32 (Highest): batch 10000: total wall time = 1466.63 s
  • TF32 (High): batch 10000: total wall time = 1271.87 s
  • BF16 (Medium): batch 10000: total wall time = 1272.94 s

Using TF32 improves ~15% training speed.


have you tested training at tf32 and inference at fp32? does it work or any loss of precision?

I used FP32 and TF32 for dp test, and the results are identical (the first 5 significant digits for dp test metrics are the same), regardless of the model trained in FP32 or TF32.

@caic99
Copy link
Member Author

caic99 commented Jan 3, 2025

I'll further test the impact of TF32 using OMat dataset.

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