-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Improve cuBLAS performance by using a memory pool #1094
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <cuda_fp16.h> | ||
#include <atomic> | ||
#include "ggml-cuda.h" | ||
|
||
typedef uint16_t ggml_fp16_t; | ||
|
@@ -35,8 +37,6 @@ typedef struct { | |
} block_q4_3; | ||
static_assert(sizeof(block_q4_3) == 2 * sizeof(ggml_fp16_t) + QK4_3 / 2, "wrong q4_3 block size/padding"); | ||
|
||
|
||
|
||
static __global__ void dequantize_block_q4_0(const void * vx, float * y) { | ||
const block_q4_0 * x = (const block_q4_0 *) vx; | ||
|
||
|
@@ -131,24 +131,83 @@ static __global__ void dequantize_block_q4_3(const void * vx, float * y) { | |
} | ||
} | ||
|
||
extern "C" { | ||
__host__ void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_0; | ||
dequantize_block_q4_0<<<nb, 1, 0, stream>>>(vx, y); | ||
} | ||
void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_0; | ||
dequantize_block_q4_0<<<nb, 1, 0, stream>>>(vx, y); | ||
} | ||
|
||
__host__ void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_1; | ||
dequantize_block_q4_1<<<nb, 1, 0, stream>>>(vx, y); | ||
void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_1; | ||
dequantize_block_q4_1<<<nb, 1, 0, stream>>>(vx, y); | ||
} | ||
|
||
void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_2; | ||
dequantize_block_q4_2<<<nb, 1, 0, stream>>>(vx, y); | ||
} | ||
|
||
void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_3; | ||
dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y); | ||
} | ||
|
||
// lock-free, thread safe buffer pool for cuda | ||
#define MAX_CUDA_BUFFERS 16 | ||
struct cuda_buffer { | ||
std::atomic_uintptr_t ptr { 0 }; | ||
size_t size { 0 }; | ||
}; | ||
|
||
static cuda_buffer cuda_buffer_pool[MAX_CUDA_BUFFERS]; | ||
|
||
void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) { | ||
for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) { | ||
struct cuda_buffer * b = &cuda_buffer_pool[i]; | ||
if (b->size >= size) { | ||
uintptr_t ptr = atomic_load(&b->ptr); | ||
if (ptr) { | ||
if (std::atomic_compare_exchange_strong(&b->ptr, &ptr, 0)) { | ||
*actual_size = b->size; | ||
return (void *) ptr; | ||
} | ||
} | ||
} | ||
} | ||
|
||
__host__ void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_2; | ||
dequantize_block_q4_2<<<nb, 1, 0, stream>>>(vx, y); | ||
void * ptr; | ||
CUDA_CHECK(cudaMalloc((void **) &ptr, size)); | ||
*actual_size = size; | ||
return ptr; | ||
} | ||
|
||
void ggml_cuda_pool_free(void * ptr, size_t size) { | ||
for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) { | ||
struct cuda_buffer * b = &cuda_buffer_pool[i]; | ||
uintptr_t p = std::atomic_load(&b->ptr); | ||
if (p == 0) { | ||
if (std::atomic_compare_exchange_strong(&b->ptr, &p, (uintptr_t) ptr)) { | ||
b->size = size; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this introduces a race condition: another thread can observe a non-nullptr pointer with a wrong size. E.g., consider this execution history of two threads
So now |
||
return; | ||
} | ||
} | ||
} | ||
fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n"); | ||
CUDA_CHECK(cudaFree(ptr)); | ||
} | ||
|
||
cublasHandle_t cublasH = NULL; | ||
cudaStream_t cudaStream = NULL; | ||
|
||
void ggml_init_cublas(void) { | ||
if (cublasH == NULL) { | ||
// create cublas handle, bind a stream | ||
CUBLAS_CHECK(cublasCreate(&cublasH)); | ||
|
||
CUDA_CHECK(cudaStreamCreateWithFlags(&cudaStream, cudaStreamNonBlocking)); | ||
|
||
CUBLAS_CHECK(cublasSetStream(cublasH, cudaStream)); | ||
|
||
__host__ void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream) { | ||
const int nb = k / QK4_3; | ||
dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y); | ||
// configure logging to stdout | ||
// CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, NULL)); | ||
} | ||
} |
This file contains 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 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
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.
Doesn't this has an ABA problem? The scenario I'm thinking of goes like this:
ptr
to another threadptr
withcudaFree()
cudaMalloc()
with a smaller size and get the same pointer asptr
(i.e., it is equal to it as an integer), then frees it into the same pool slot we are trying to useThis is highly unlikely to happen in practice, but I think is technically possible, unless CUDART never returns the same pointer twice from
cudaMalloc()
.