-
Notifications
You must be signed in to change notification settings - Fork 12k
musa: extract ggml_cuda_mul_mat_batched_cublas_gemm_batched_ex #13887
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
Closed
+188
−53
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c665fa
musa: enable fp16 mma (all) and cublas on qy2
yeahdongcn 39ab3de
Update ggml/src/ggml-cuda/ggml-cuda.cu
yeahdongcn 3e5b500
Address review comments
yeahdongcn cd42f3e
Address review comments
yeahdongcn 5d9ae95
musa: extract ggml_cuda_mul_mat_batched_cublas_gemm_batched_ex
yeahdongcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "mublas.cuh" | ||
|
||
static __global__ void k_compute_batched_ptrs( | ||
const half * src0_as_f16, const half * src1_as_f16, char * dst, | ||
const void ** ptrs_src, void ** ptrs_dst, | ||
int64_t ne12, int64_t ne13, | ||
int64_t ne23, | ||
size_t nb02, size_t nb03, | ||
size_t nb12, size_t nb13, | ||
size_t nbd2, size_t nbd3, | ||
int64_t r2, int64_t r3) { | ||
const int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x; | ||
const int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y; | ||
|
||
if (i13 >= ne13 || i12 >= ne12) { | ||
return; | ||
} | ||
|
||
const int64_t i03 = i13 / r3; | ||
const int64_t i02 = i12 / r2; | ||
|
||
ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03; | ||
ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13; | ||
ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3; | ||
} | ||
|
||
void ggml_cuda_mul_mat_batched_cublas_gemm_batched_ex( | ||
ggml_backend_cuda_context & ctx, | ||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, | ||
const half * src0_f16, const half * src1_f16, char * dst_t, | ||
const size_t nbd2, const size_t nbd3, | ||
const int64_t r2, const int64_t r3, | ||
const int64_t s11, const int64_t s12, const int64_t s13, | ||
const void * alpha, const void * beta, | ||
const cudaDataType_t cu_data_type, | ||
const cublasComputeType_t cu_compute_type, | ||
cudaStream_t main_stream | ||
) { | ||
GGML_TENSOR_BINARY_OP_LOCALS | ||
|
||
// use cublasGemmBatchedEx | ||
const int64_t ne23 = ne12*ne13; | ||
|
||
// Allocate memory for pointer arrays using cudaMalloc to avoid segmentation faults in muBLAS. | ||
const void ** ptrs_src; | ||
void ** ptrs_dst; | ||
CUDA_CHECK(cudaMalloc((void **)&ptrs_src, sizeof(void *)*2*ne23)); | ||
CUDA_CHECK(cudaMalloc((void **)&ptrs_dst, sizeof(void *)*1*ne23)); | ||
|
||
dim3 block_dims(ne13, ne12); | ||
k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>( | ||
src0_f16, src1_f16, dst_t, | ||
ptrs_src, ptrs_dst, | ||
ne12, ne13, | ||
ne23, | ||
nb02, nb03, | ||
src1->type == GGML_TYPE_F16 ? nb12 : s12*sizeof(half), | ||
src1->type == GGML_TYPE_F16 ? nb13 : s13*sizeof(half), | ||
nbd2, nbd3, | ||
r2, r3); | ||
CUDA_CHECK(cudaGetLastError()); | ||
|
||
// This operation is essential for musa; without it, generated tokens will | ||
// be garbled and may eventually cause MUBLAS_STATUS_INTERNAL_ERROR. | ||
CUDA_CHECK(cudaDeviceSynchronize()); | ||
|
||
CUBLAS_CHECK( | ||
cublasGemmBatchedEx(ctx.cublas_handle(), CUBLAS_OP_T, CUBLAS_OP_N, | ||
ne01, ne11, ne10, | ||
alpha, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, nb01/nb00, | ||
(const void **) (ptrs_src + 1*ne23), CUDA_R_16F, s11, | ||
beta, ( void **) (ptrs_dst + 0*ne23), cu_data_type, ne0, | ||
ne23, | ||
cu_compute_type, | ||
CUBLAS_GEMM_DEFAULT_TENSOR_OP)); | ||
|
||
CUDA_CHECK(cudaFree(ptrs_src)); | ||
CUDA_CHECK(cudaFree(ptrs_dst)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "ggml-cuda/common.cuh" | ||
|
||
void ggml_cuda_mul_mat_batched_cublas_gemm_batched_ex( | ||
ggml_backend_cuda_context & ctx, | ||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, | ||
const half * src0_f16, const half * src1_f16, char * dst_t, | ||
const size_t nbd2, const size_t nbd3, | ||
const int64_t r2, const int64_t r3, | ||
const int64_t s11, const int64_t s12, const int64_t s13, | ||
const void * alpha, const void * beta, | ||
const cudaDataType_t cu_data_type, | ||
const cublasComputeType_t cu_compute_type, | ||
cudaStream_t main_stream | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wouldn't be ok in CUDA, since
cublasGemmBatchedEx
is normally asynchronous and freeing the memory immediately would likely lead to a use after free.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking a look at this!
Yes, that's due to the current incompatibility between
cublasGemmBatchedEx
andmublasGemmBatchedEx
. Also,mublas.cu
is only compiled when using the MUSA backend.