Skip to content

Commit 0c3f8a4

Browse files
committed
switching to cublasltmatmul
1 parent 879e6b0 commit 0c3f8a4

1 file changed

Lines changed: 55 additions & 29 deletions

File tree

‎cpp/src/neighbors/detail/cagra/cagra_merge_scaffold.cuh‎

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <raft/core/device_mdarray.hpp>
1414
#include <raft/core/device_mdspan.hpp>
1515
#include <raft/core/error.hpp>
16-
#include <raft/core/resource/cublas_handle.hpp>
16+
#include <raft/core/resource/cublaslt_handle.hpp>
1717
#include <raft/core/resource/cuda_stream.hpp>
1818
#include <raft/core/resource/device_memory_resource.hpp>
1919
#include <raft/core/resources.hpp>
@@ -35,6 +35,7 @@
3535
#include <cmath>
3636
#include <cstdint>
3737
#include <limits>
38+
#include <memory>
3839
#include <type_traits>
3940
#include <utility>
4041
#include <vector>
@@ -558,7 +559,7 @@ inline constexpr cublasComputeType_t GEMM_COMPUTE_TYPE = CUBLAS_COMPUTE_32F_FAST
558559
* batch of row-major matrices with rows of length `row_width`:
559560
* out_i[b * a_rows + a] = A_i[a] . B_i[b].
560561
*
561-
* Pretty much a wrapper around `cublasGemmStridedBatchedEx`.
562+
* Pretty much a wrapper around strided-batched `cublasLtMatmul`.
562563
*
563564
* Every dataset scalar type is gathered to float before this call. Native INT8 cuBLAS paths are
564565
* not portable across architectures (e.g. Ada returns CUBLAS_STATUS_NOT_SUPPORTED for
@@ -578,33 +579,58 @@ inline void batched_row_dot_products(raft::resources const& res,
578579
int row_width,
579580
int batch_count)
580581
{
581-
float alpha = 1.0f;
582-
float beta = 0.0f;
583-
auto cublas_handle = raft::resource::get_cublas_handle(res);
584-
RAFT_CUBLAS_TRY(cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_HOST));
585-
RAFT_CUBLAS_TRY(cublasGemmStridedBatchedEx(cublas_handle,
586-
CUBLAS_OP_T,
587-
CUBLAS_OP_N,
588-
a_rows,
589-
b_rows,
590-
row_width,
591-
&alpha,
592-
a,
593-
CUDA_R_32F,
594-
row_width,
595-
a_stride,
596-
b,
597-
CUDA_R_32F,
598-
row_width,
599-
b_stride,
600-
&beta,
601-
out,
602-
CUDA_R_32F,
603-
a_rows,
604-
out_stride,
605-
batch_count,
606-
GEMM_COMPUTE_TYPE,
607-
CUBLAS_GEMM_DEFAULT));
582+
float alpha = 1.0f;
583+
float beta = 0.0f;
584+
585+
using matmul_descriptor = std::unique_ptr<std::remove_pointer_t<cublasLtMatmulDesc_t>,
586+
decltype(&cublasLtMatmulDescDestroy)>;
587+
using matrix_layout = std::unique_ptr<std::remove_pointer_t<cublasLtMatrixLayout_t>,
588+
decltype(&cublasLtMatrixLayoutDestroy)>;
589+
590+
cublasLtMatmulDesc_t operation_raw = nullptr;
591+
RAFT_CUBLAS_TRY(cublasLtMatmulDescCreate(&operation_raw, GEMM_COMPUTE_TYPE, CUDA_R_32F));
592+
matmul_descriptor operation{operation_raw, &cublasLtMatmulDescDestroy};
593+
cublasOperation_t transpose = CUBLAS_OP_T;
594+
RAFT_CUBLAS_TRY(cublasLtMatmulDescSetAttribute(
595+
operation.get(), CUBLASLT_MATMUL_DESC_TRANSA, &transpose, sizeof(transpose)));
596+
597+
auto make_layout = [batch_count](uint64_t rows,
598+
uint64_t columns,
599+
int64_t leading_dimension,
600+
int64_t batch_stride) {
601+
cublasLtMatrixLayout_t raw = nullptr;
602+
RAFT_CUBLAS_TRY(cublasLtMatrixLayoutCreate(&raw, CUDA_R_32F, rows, columns, leading_dimension));
603+
matrix_layout layout{raw, &cublasLtMatrixLayoutDestroy};
604+
auto count = static_cast<int32_t>(batch_count);
605+
RAFT_CUBLAS_TRY(cublasLtMatrixLayoutSetAttribute(
606+
raw, CUBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &count, sizeof(count)));
607+
RAFT_CUBLAS_TRY(cublasLtMatrixLayoutSetAttribute(
608+
raw, CUBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &batch_stride, sizeof(batch_stride)));
609+
return layout;
610+
};
611+
auto a_layout = make_layout(
612+
static_cast<uint64_t>(row_width), static_cast<uint64_t>(a_rows), row_width, a_stride);
613+
auto b_layout = make_layout(
614+
static_cast<uint64_t>(row_width), static_cast<uint64_t>(b_rows), row_width, b_stride);
615+
auto out_layout =
616+
make_layout(static_cast<uint64_t>(a_rows), static_cast<uint64_t>(b_rows), a_rows, out_stride);
617+
618+
RAFT_CUBLAS_TRY(cublasLtMatmul(raft::resource::get_cublaslt_handle(res),
619+
operation.get(),
620+
&alpha,
621+
a,
622+
a_layout.get(),
623+
b,
624+
b_layout.get(),
625+
&beta,
626+
out,
627+
out_layout.get(),
628+
out,
629+
out_layout.get(),
630+
nullptr,
631+
nullptr,
632+
0,
633+
raft::resource::get_cuda_stream(res)));
608634
}
609635

610636
/**

0 commit comments

Comments
 (0)