Repository navigation
ggml : process data in smaller chunks in CUDA ggml_top_k() implementation to reduce temporary buffers memory usage #24776
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
Changes from 4 commits
c1a79a6
ca88122
4d0e838
8521ec4
807a9f7
f887bc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,20 @@ static __global__ void init_offsets(int * offsets, const int ncols, const int nr | |
| #endif // STRIDED_ITERATOR_AVAILABLE | ||
|
|
||
| #ifdef GGML_CUDA_USE_CUB | ||
|
|
||
| // returns the suggested maximum number of rows to process during one argsort_f32_i32_cuda_cub() call | ||
| int argsort_f32_i32_cuda_cub_chunk_nrows(const size_t nb01, const int64_t nrows) { | ||
| // perform argsort in chunks up to approximately this size (currently 64MB) | ||
| // to avoid excessive temporary buffers memory usage | ||
| const int chunk_bytes = 1 << 26; | ||
|
|
||
| // calculate how many rows will fit in one chunk (must be at least one) | ||
| const int chunk_nrows = chunk_bytes > nb01 ? chunk_bytes / nb01 : 1; | ||
|
|
||
| // limit the resulting amount to total nrows | ||
| return nrows < chunk_nrows ? nrows : chunk_nrows; | ||
|
Contributor
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. Preferably use
Contributor
Author
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.
@JohannesGaessler Done, there were two more ternary operators so I included them as well. |
||
| } | ||
|
|
||
| void argsort_f32_i32_cuda_cub(ggml_cuda_pool & pool, | ||
| const float * x, | ||
| int * dst, | ||
|
|
@@ -254,11 +268,22 @@ void ggml_cuda_op_argsort(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { | |
| const size_t shared_mem = ncols_pad * sizeof(int); | ||
| const size_t max_shared_mem = ggml_cuda_info().devices[ggml_cuda_get_device()].smpb; | ||
|
|
||
| if (shared_mem > max_shared_mem || ncols > 1024) { | ||
| ggml_cuda_pool & pool = ctx.pool(); | ||
| argsort_f32_i32_cuda_cub(pool, src0_d, (int *) dst_d, ncols, nrows, order, stream); | ||
| } else { | ||
| argsort_f32_i32_cuda_bitonic(src0_d, (int *) dst_d, ncols, nrows, order, stream); | ||
| // early return if we can use bitonic argsort | ||
| if (shared_mem <= max_shared_mem && ncols <= 1024) { | ||
| return argsort_f32_i32_cuda_bitonic(src0_d, (int *) dst_d, ncols, nrows, order, stream); | ||
|
fairydreaming marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| const int chunk_nrows = argsort_f32_i32_cuda_cub_chunk_nrows(src0->nb[1], nrows); | ||
|
|
||
| ggml_cuda_pool & pool = ctx.pool(); | ||
|
|
||
| for (int64_t i = 0; i < nrows; i += chunk_nrows) { | ||
| int iter_nrows = chunk_nrows < nrows - i ? chunk_nrows : nrows - i; | ||
|
|
||
| argsort_f32_i32_cuda_cub(pool, src0_d, (int *) dst_d, ncols, iter_nrows, order, stream); | ||
|
|
||
| src0_d += ncols * iter_nrows; | ||
| dst_d += ncols * iter_nrows; | ||
| } | ||
| #else | ||
| argsort_f32_i32_cuda_bitonic(src0_d, (int *) dst_d, ncols, nrows, order, stream); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.