Skip to content

[ATen][CPU][Sparse] Use Third-Party Eigen for sparse add and addmm#155357

Closed
Aidyn-A wants to merge 9 commits intopytorch:mainfrom
Aidyn-A:eigen_for_sparse_addmm_v2
Closed

[ATen][CPU][Sparse] Use Third-Party Eigen for sparse add and addmm#155357
Aidyn-A wants to merge 9 commits intopytorch:mainfrom
Aidyn-A:eigen_for_sparse_addmm_v2

Conversation

@Aidyn-A
Copy link
Collaborator

@Aidyn-A Aidyn-A commented Jun 6, 2025

This pull request adds the following ops for sparse matrices using Eigen library:

    add(a_csr, b_csr)
    add(a_csc, b_csc)

    addmm(c_csr, a_csr, b_csr)
    addmm(c_csr, a_csr, b_csc)
    addmm(c_csr, a_csc, b_csc)
    addmm(c_csr, a_csc, b_csr)

    addmm(c_csc, a_csr, b_csr)
    addmm(c_csc, a_csr, b_csc)
    addmm(c_csc, a_csc, b_csc)
    addmm(c_csc, a_csc, b_csr)

Currently, the operations for sparse matrices on CPU are available through MKL only. The non-existence of MKL on aarch64 causes the unavailability of these ops on any machines with ARM based CPUs, including Apple Silicon, AWS Graviton and NVIDIA Grace. This PR addresses this issue by using Eigen as a backend for the above ops.

This is a re-factored version of my previous PR #101814. The main difference with the old one, this does not enable Eigen by default.

@pytorch-bot
Copy link

pytorch-bot bot commented Jun 6, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/155357

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit c77592f with merge base 01bcf9a (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: sparse release notes category label Jun 6, 2025
@Aidyn-A Aidyn-A changed the title sparse add and addmm [ATen][CPU][Sparse] Use Third-Party Eigen for sparse add and addmm Jun 6, 2025
@Aidyn-A Aidyn-A requested review from cpuhrsch and malfet June 6, 2025 18:55
@mikaylagawarecki mikaylagawarecki requested a review from pearu June 6, 2025 22:02
@mikaylagawarecki mikaylagawarecki added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 6, 2025
@Aidyn-A
Copy link
Collaborator Author

Aidyn-A commented Jun 7, 2025

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased eigen_for_sparse_addmm_v2 onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout eigen_for_sparse_addmm_v2 && git pull --rebase)

@pytorchmergebot pytorchmergebot force-pushed the eigen_for_sparse_addmm_v2 branch from 71d7617 to 8db7338 Compare June 7, 2025 06:20
@Aidyn-A Aidyn-A force-pushed the eigen_for_sparse_addmm_v2 branch from 8db7338 to df77628 Compare June 7, 2025 18:03
Copy link
Collaborator

@pearu pearu left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, it looks good to me!

I have a number of nits and one question, see #155357 (comment)

@Aidyn-A Aidyn-A force-pushed the eigen_for_sparse_addmm_v2 branch from df77628 to 36d5c43 Compare July 14, 2025 08:51
@Aidyn-A Aidyn-A requested a review from pearu July 14, 2025 16:53
@Aidyn-A
Copy link
Collaborator Author

Aidyn-A commented Jul 23, 2025

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased eigen_for_sparse_addmm_v2 onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout eigen_for_sparse_addmm_v2 && git pull --rebase)

@pytorchmergebot pytorchmergebot force-pushed the eigen_for_sparse_addmm_v2 branch from 1afd750 to 2ea6b20 Compare July 23, 2025 23:52
Copy link
Collaborator

@pearu pearu left a comment

Choose a reason for hiding this comment

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

Here is another review iteration that removes duplicated code.

Question: there exists multiple paths for addmm depending on the layout combinations of input and output tensors. Are all these paths covered by existing unittests?

@Aidyn-A Aidyn-A force-pushed the eigen_for_sparse_addmm_v2 branch from 2ea6b20 to 2c69b71 Compare August 7, 2025 14:12
Copy link
Collaborator

@pearu pearu left a comment

Choose a reason for hiding this comment

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

This looks good to me, however, see my comment below about the suspicion that there exists a bug in addmm_out_sparse_eigen logic that should be revealed in tests.

@seemethere
Copy link
Member

seemethere commented Aug 21, 2025

@pytorchbot revert -c ghfirst -m "This is causing buck builds to fail since we didn't add the definition of AT_USE_EIGEN_SPARSE in the buckbuild.bzl file, will follow-up and re-land this."

@pytorch-bot
Copy link

pytorch-bot bot commented Aug 21, 2025

❌ 🤖 pytorchbot command failed:

Got EOF while in a quoted string```
Try `@pytorchbot --help` for more info.

@seemethere
Copy link
Member

@pytorchbot revert -c ghfirst -m "This is causing buck builds to fail since we didn't add the definition of AT_USE_EIGEN_SPARSE in the buckbuild.bzl file, will follow-up and re-land this."

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

pytorchmergebot added a commit that referenced this pull request Aug 21, 2025
…addmm (#155357)"

This reverts commit ce048de.

Reverted #155357 on behalf of https://github.com/seemethere due to This is causing buck builds to fail since we didn't add the definition of AT_USE_EIGEN_SPARSE in the buckbuild.bzl file, will follow-up and re-land this. ([comment](#155357 (comment)))
@pytorchmergebot
Copy link
Collaborator

@Aidyn-A your PR has been successfully reverted.

@pytorchmergebot pytorchmergebot added Reverted ci-no-td Do not run TD on this PR labels Aug 21, 2025
Signed-off-by: Eli Uriegas <eliuriegas@meta.com>
@facebook-github-bot
Copy link
Contributor

@seemethere has imported this pull request. If you are a Meta employee, you can view this in D80750986.

wincent8 pushed a commit to wincent8/pytorch that referenced this pull request Aug 22, 2025
…addmm (pytorch#155357)"

This reverts commit ce048de.

Reverted pytorch#155357 on behalf of https://github.com/seemethere due to This is causing buck builds to fail since we didn't add the definition of AT_USE_EIGEN_SPARSE in the buckbuild.bzl file, will follow-up and re-land this. ([comment](pytorch#155357 (comment)))
@facebook-github-bot
Copy link
Contributor

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: Meta Internal-Only Changes Check

Details for Dev Infra team Raised by workflow job

@seemethere
Copy link
Member

@pytorchbot merge -f 'merged internally already'

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
…ytorch#155357)

This pull request adds the following ops for sparse matrices using Eigen library:
```python
    add(a_csr, b_csr)
    add(a_csc, b_csc)

    addmm(c_csr, a_csr, b_csr)
    addmm(c_csr, a_csr, b_csc)
    addmm(c_csr, a_csc, b_csc)
    addmm(c_csr, a_csc, b_csr)

    addmm(c_csc, a_csr, b_csr)
    addmm(c_csc, a_csr, b_csc)
    addmm(c_csc, a_csc, b_csc)
    addmm(c_csc, a_csc, b_csr)
```

Currently, the operations for sparse matrices on CPU are available through MKL only. The non-existence of MKL on `aarch64` causes the unavailability of these ops on any machines with ARM based CPUs, including Apple Silicon, AWS Graviton and NVIDIA Grace. This PR addresses this issue by using Eigen as a backend for the above ops.

This is a re-factored version of my previous PR pytorch#101814. The main difference with the old one, this does not enable Eigen by default.
Pull Request resolved: pytorch#155357
Approved by: https://github.com/pearu, https://github.com/eqy
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
…addmm (pytorch#155357)"

This reverts commit ce048de.

Reverted pytorch#155357 on behalf of https://github.com/seemethere due to This is causing buck builds to fail since we didn't add the definition of AT_USE_EIGEN_SPARSE in the buckbuild.bzl file, will follow-up and re-land this. ([comment](pytorch#155357 (comment)))
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
…ytorch#155357)

This pull request adds the following ops for sparse matrices using Eigen library:
```python
    add(a_csr, b_csr)
    add(a_csc, b_csc)

    addmm(c_csr, a_csr, b_csr)
    addmm(c_csr, a_csr, b_csc)
    addmm(c_csr, a_csc, b_csc)
    addmm(c_csr, a_csc, b_csr)

    addmm(c_csc, a_csr, b_csr)
    addmm(c_csc, a_csr, b_csc)
    addmm(c_csc, a_csc, b_csc)
    addmm(c_csc, a_csc, b_csr)
```

Currently, the operations for sparse matrices on CPU are available through MKL only. The non-existence of MKL on `aarch64` causes the unavailability of these ops on any machines with ARM based CPUs, including Apple Silicon, AWS Graviton and NVIDIA Grace. This PR addresses this issue by using Eigen as a backend for the above ops.

This is a re-factored version of my previous PR pytorch#101814. The main difference with the old one, this does not enable Eigen by default.
Pull Request resolved: pytorch#155357
Approved by: https://github.com/pearu, https://github.com/eqy

Co-authored-by: Eli Uriegas <eliuriegas@meta.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: sparse release notes category Reverted triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants