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

Support mixed-precision for SpMV #907

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Changes from 1 commit
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
31 changes: 19 additions & 12 deletions include/matx/transforms/matmul/matvec_cusparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class MatVecCUSPARSEHandle_t {
using TB = typename TensorTypeB::value_type;
using TC = typename TensorTypeC::value_type;

// Mixed-precision compute type.
using TCOMP = std::conditional_t<
std::is_same_v<TC, matx::matxFp16> ||
std::is_same_v<TC, matx::matxBf16>, float, TC>;

/**
* Construct a SpMV handle
*/
Expand All @@ -87,12 +92,12 @@ class MatVecCUSPARSEHandle_t {
params_ = GetSpMVParams(c, a, b, stream, alpha, beta);

// Properly typed alpha, beta.
if constexpr (std::is_same_v<TC, cuda::std::complex<float>> ||
std::is_same_v<TC, cuda::std::complex<double>>) {
if constexpr (std::is_same_v<TCOMP, cuda::std::complex<float>> ||
std::is_same_v<TCOMP, cuda::std::complex<double>>) {
salpha_ = {alpha, 0};
sbeta_ = {beta, 0};
} else if constexpr (std::is_same_v<TC, float> ||
std::is_same_v<TC, double>) {
} else if constexpr (std::is_same_v<TCOMP, float> ||
std::is_same_v<TCOMP, double>) {
salpha_ = alpha;
sbeta_ = beta;
} else {
Expand Down Expand Up @@ -139,7 +144,7 @@ class MatVecCUSPARSEHandle_t {

// Allocate a workspace for SpMV.
const cusparseSpMVAlg_t algo = CUSPARSE_SPMV_ALG_DEFAULT;
const cudaDataType comptp = dtc; // TODO: support separate comp type?!
const cudaDataType comptp = MatXTypeToCudaType<TCOMP>();
ret =
cusparseSpMV_bufferSize(handle_, params_.opA, &salpha_, matA_, vecB_,
&sbeta_, vecC_, comptp, algo, &workspaceSize_);
Expand Down Expand Up @@ -188,7 +193,7 @@ class MatVecCUSPARSEHandle_t {
[[maybe_unused]] const TensorTypeB &b) {
MATX_NVTX_START("", matx::MATX_NVTX_LOG_INTERNAL);
const cusparseSpMVAlg_t algo = CUSPARSE_SPMV_ALG_DEFAULT;
const cudaDataType comptp = MatXTypeToCudaType<TC>(); // TODO: see above
const cudaDataType comptp = MatXTypeToCudaType<TCOMP>();
[[maybe_unused]] cusparseStatus_t ret =
cusparseSpMV(handle_, params_.opA, &salpha_, matA_, vecB_, &sbeta_,
vecC_, comptp, algo, workspace_);
Expand All @@ -203,8 +208,8 @@ class MatVecCUSPARSEHandle_t {
size_t workspaceSize_ = 0;
void *workspace_ = nullptr;
detail::MatVecCUSPARSEParams_t params_;
TC salpha_;
TC sbeta_;
TCOMP salpha_;
TCOMP sbeta_;
};

/**
Expand Down Expand Up @@ -287,10 +292,12 @@ void sparse_matvec_impl(TensorTypeC &C, const TensorTypeA &a,
"tensors must have SpMV rank");
static_assert(std::is_same_v<TC, TA> && std::is_same_v<TC, TB>,
"tensors must have the same data type");
// TODO: allow MIXED-PRECISION computation!
static_assert(std::is_same_v<TC, float> || std::is_same_v<TC, double> ||
std::is_same_v<TC, cuda::std::complex<float>> ||
std::is_same_v<TC, cuda::std::complex<double>>,
static_assert(std::is_same_v<TC, matx::matxFp16> ||
std::is_same_v<TC, matx::matxBf16> ||
std::is_same_v<TC, float> ||
std::is_same_v<TC, double> ||
std::is_same_v<TC, cuda::std::complex<float>> ||
std::is_same_v<TC, cuda::std::complex<double>>,
"unsupported data type");
MATX_ASSERT(a.Size(RANKA - 1) == b.Size(RANKB - 1) &&
a.Size(RANKA - 2) == c.Size(RANKC - 1),
Expand Down