Skip to content

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

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ header_template_rule(
"@AT_BLAS_F2C@": "0",
"@AT_BLAS_USE_CBLAS_DOT@": "1",
"@AT_KLEIDIAI_ENABLED@": "0",
"@AT_USE_EIGEN_SPARSE@": "0",
},
)

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ option(USE_PRECOMPILED_HEADERS "Use pre-compiled headers to accelerate build."
option(USE_PROF "Use profiling" OFF)
option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" ON)
option(USE_SNPE "Use Qualcomm's SNPE library" OFF)
option(USE_EIGEN_SPARSE "Use Eigen Sparse Matrices" OFF)
option(USE_SYSTEM_EIGEN_INSTALL
"Use system Eigen instead of the one under third_party" OFF)
cmake_dependent_option(
Expand Down
5 changes: 5 additions & 0 deletions aten/src/ATen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ file(GLOB native_mkldnn_cpp "native/mkldnn/*.cpp")
file(GLOB vulkan_cpp "vulkan/*.cpp")
file(GLOB native_vulkan_cpp "native/vulkan/*.cpp" "native/vulkan/api/*.cpp" "native/vulkan/impl/*.cpp" "native/vulkan/ops/*.cpp")

file(GLOB native_eigen_cpp "native/sparse/eigen/*.cpp")

# Metal
file(GLOB metal_h "metal/*.h")
file(GLOB metal_cpp "metal/*.cpp")
Expand Down Expand Up @@ -338,6 +340,9 @@ if(USE_VULKAN)
else()
set(all_cpu_cpp ${all_cpu_cpp} ${vulkan_cpp})
endif()
if(USE_EIGEN_SPARSE)
set(all_cpu_cpp ${all_cpu_cpp} ${native_eigen_cpp})
endif()

if(USE_MTIA)
set(ATen_MTIA_SRCS ${ATen_MTIA_SRCS} ${mtia_cpp} ${mtia_h} ${native_mtia_cpp} ${native_mtia_h})
Expand Down
1 change: 1 addition & 0 deletions aten/src/ATen/Config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
#define AT_BLAS_F2C() @AT_BLAS_F2C@
#define AT_BLAS_USE_CBLAS_DOT() @AT_BLAS_USE_CBLAS_DOT@
#define AT_KLEIDIAI_ENABLED() @AT_KLEIDIAI_ENABLED@
#define AT_USE_EIGEN_SPARSE() @AT_USE_EIGEN_SPARSE@
8 changes: 8 additions & 0 deletions aten/src/ATen/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,14 @@ bool Context::hasLAPACK() {
#endif
}

bool Context::hasEigenSparse() {
#if AT_USE_EIGEN_SPARSE()
return true;
#else
return false;
#endif
}

at::QEngine Context::qEngine() const {
static auto _quantized_engine = []() {
at::QEngine qengine = at::kNoQEngine;
Expand Down
5 changes: 5 additions & 0 deletions aten/src/ATen/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class TORCH_API Context {
static bool hasKleidiAI();
static bool hasLAPACK();
static bool hasMKLDNN();
static bool hasEigenSparse();
static bool hasMAGMA() {
return detail::getCUDAHooks().hasMAGMA();
}
Expand Down Expand Up @@ -608,6 +609,10 @@ inline bool hasLAPACK() {
return globalContext().hasLAPACK();
}

inline bool hasEigenSparse() {
return globalContext().hasEigenSparse();
}

inline bool hasMAGMA() {
return globalContext().hasMAGMA();
}
Expand Down
19 changes: 12 additions & 7 deletions aten/src/ATen/native/sparse/SparseBlasImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <ATen/Parallel.h>
#endif

#if AT_USE_EIGEN_SPARSE()
#include <ATen/native/sparse/eigen/SparseBlasImpl.h>
#endif

namespace at::native::sparse::impl {

Expand Down Expand Up @@ -442,13 +445,15 @@ void add_out_sparse_csr(
const Tensor& mat2,
const Scalar& alpha,
const Tensor& result) {
#if !AT_MKL_ENABLED()
TORCH_CHECK(
false,
"Calling add on a sparse CPU tensor requires compiling PyTorch with MKL. ",
"Please use PyTorch built MKL support.");
#else
#if AT_USE_MKL_SPARSE()
sparse::impl::mkl::add_out_sparse_csr(mat1, mat2, alpha, result);
#elif AT_USE_EIGEN_SPARSE()
sparse::impl::eigen::add_out_sparse(mat1, mat2, alpha, result);
#else
TORCH_CHECK(
false,
"Calling add on a sparse CPU tensor requires compiling PyTorch with MKL. ",
"Please use PyTorch built MKL support.");
#endif
}

Expand All @@ -459,7 +464,7 @@ void triangular_solve_out_sparse_csr(
bool upper,
bool transpose,
bool unitriangular) {
#if !AT_MKL_ENABLED()
#if !AT_USE_MKL_SPARSE()
TORCH_CHECK(
false,
"Calling triangular_solve on a sparse CPU tensor requires compiling PyTorch with MKL. ",
Expand Down
20 changes: 19 additions & 1 deletion aten/src/ATen/native/sparse/SparseCsrTensorMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
#include <ATen/ops/zeros_like.h>
#endif

#if AT_USE_EIGEN_SPARSE()
#include <ATen/native/sparse/eigen/SparseBlasImpl.h>
#endif

#include <algorithm>

namespace at {
Expand Down Expand Up @@ -536,7 +540,12 @@ static void addmm_out_sparse_csr_native_cpu(
auto values = sparse.values();

scalar_t cast_alpha = alpha.to<scalar_t>();
r.mul_(beta);
// If beta is zero NaN and Inf should not be propagated to the result
if (beta.toComplexDouble() == 0.) {
r.zero_();
} else {
r.mul_(beta);
}
AT_DISPATCH_INDEX_TYPES(
col_indices.scalar_type(), "csr_mm_crow_indices", [&]() {
auto csr_accessor = csr.accessor<index_t, 1>();
Expand Down Expand Up @@ -648,6 +657,15 @@ Tensor& addmm_out_sparse_compressed_cpu(
return result;
}

#if AT_USE_EIGEN_SPARSE()
if ((result.layout() == kSparseCsr || result.layout() == kSparseCsc) &&
(mat1.layout() == kSparseCsr || mat1.layout() == kSparseCsc) &&
(mat2.layout() == kSparseCsr || mat2.layout() == kSparseCsc)) {
sparse::impl::eigen::addmm_out_sparse(mat1, mat2, result, alpha, beta);
return result;
}
#endif

#if !AT_USE_MKL_SPARSE()
// The custom impl addmm_out_sparse_csr_native_cpu only supports CSR @
// strided -> strided
Expand Down
Loading
Loading