-
Couldn't load subscription status.
- Fork 13.4k
CUDA: fastdiv, launch bounds for mmvq + q8_1 quant #15802
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
Merged
JohannesGaessler
merged 2 commits into
ggml-org:master
from
JohannesGaessler:cuda-mmvq-fastdiv-3
Sep 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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 |
|---|---|---|
|
|
@@ -141,9 +141,10 @@ template <ggml_type type, int ncols_dst> | |
| __launch_bounds__(calc_nwarps(ncols_dst, get_device_table_id())*ggml_cuda_get_physical_warp_size(), 1) | ||
| static __global__ void mul_mat_vec_q( | ||
| const void * __restrict__ vx, const void * __restrict__ vy, const int32_t * __restrict__ ids, float * __restrict__ dst, | ||
| const int ncols_x, const int nchannels_y, const int stride_row_x, const int stride_col_y, const int stride_col_dst, | ||
| const int channel_ratio, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst, | ||
| const int sample_ratio, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst) { | ||
| const uint32_t ncols_x, const uint32_t nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y, | ||
| const uint32_t stride_col_dst, const uint3 channel_ratio, const uint32_t stride_channel_x, | ||
| const uint32_t stride_channel_y, const uint32_t stride_channel_dst, const uint3 sample_ratio, | ||
| const uint32_t stride_sample_x, const uint32_t stride_sample_y, const uint32_t stride_sample_dst) { | ||
|
|
||
| constexpr int qk = ggml_cuda_type_traits<type>::qk; | ||
| constexpr int qi = ggml_cuda_type_traits<type>::qi; | ||
|
|
@@ -161,12 +162,12 @@ static __global__ void mul_mat_vec_q( | |
| constexpr int blocks_per_iter = vdr * nwarps*warp_size / qi; | ||
|
|
||
| // The MUL_MAT_ID code path with ids != nullptr is only implemented for ncols_dst == 1. | ||
| const int channel_dst = blockIdx.y; | ||
| const int channel_x = ncols_dst == 1 && ids ? ids[channel_dst] : channel_dst / channel_ratio; | ||
| const int channel_y = ncols_dst == 1 && ids ? channel_dst % nchannels_y : channel_dst; | ||
| const int sample_dst = blockIdx.z; | ||
| const int sample_x = sample_dst / sample_ratio; | ||
| const int sample_y = sample_dst; | ||
| const uint32_t channel_dst = blockIdx.y; | ||
| const uint32_t channel_x = ncols_dst == 1 && ids ? ids[channel_dst] : fastdiv(channel_dst, channel_ratio); | ||
| const uint32_t channel_y = ncols_dst == 1 && ids ? channel_dst % nchannels_y : channel_dst; | ||
|
||
| const uint32_t sample_dst = blockIdx.z; | ||
| const uint32_t sample_x = fastdiv(sample_dst, sample_ratio); | ||
| const uint32_t sample_y = sample_dst; | ||
|
|
||
| // partial sum for each thread | ||
| float tmp[ncols_dst][rows_per_cuda_block] = {{0.0f}}; | ||
|
|
@@ -247,95 +248,79 @@ static void mul_mat_vec_q_switch_ncols_dst( | |
| GGML_ASSERT(ncols_x % ggml_blck_size(type) == 0); | ||
| GGML_ASSERT(ncols_dst <= MMVQ_MAX_BATCH_SIZE); | ||
|
|
||
| const int channel_ratio = nchannels_dst / nchannels_x; | ||
| const int sample_ratio = nsamples_dst / nsamples_x; | ||
| const uint3 channel_ratio = init_fastdiv_values(nchannels_dst / nchannels_x); | ||
| const uint3 sample_ratio = init_fastdiv_values(nsamples_dst / nsamples_x); | ||
|
|
||
| const int device = ggml_cuda_get_device(); | ||
| const int warp_size = ggml_cuda_info().devices[device].warp_size; | ||
| const mmvq_parameter_table_id table_id = get_device_table_id(ggml_cuda_info().devices[device].cc); | ||
|
|
||
| GGML_ASSERT(!ids || ncols_dst == 1); | ||
| switch (ncols_dst) { | ||
| case 1: | ||
| { | ||
| case 1: { | ||
| constexpr int c_ncols_dst = 1; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 2: | ||
| { | ||
| } break; | ||
| case 2: { | ||
| constexpr int c_ncols_dst = 2; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 3: | ||
| { | ||
| } break; | ||
| case 3: { | ||
| constexpr int c_ncols_dst = 3; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 4: | ||
| { | ||
| } break; | ||
| case 4: { | ||
| constexpr int c_ncols_dst = 4; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 5: | ||
| { | ||
| } break; | ||
| case 5: { | ||
| constexpr int c_ncols_dst = 5; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 6: | ||
| { | ||
| } break; | ||
| case 6: { | ||
| constexpr int c_ncols_dst = 6; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 7: | ||
| { | ||
| } break; | ||
| case 7: { | ||
| constexpr int c_ncols_dst = 7; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| case 8: | ||
| { | ||
| } break; | ||
| case 8: { | ||
| constexpr int c_ncols_dst = 8; | ||
| std::pair<dim3, dim3> dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); | ||
| mul_mat_vec_q<type, c_ncols_dst><<<dims.first, dims.second, 0, stream>>> | ||
| (vx, vy, ids, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, | ||
| channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, | ||
| sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst); | ||
| break; | ||
| } | ||
| } break; | ||
| default: | ||
| GGML_ABORT("fatal error"); | ||
| break; | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
qk,qi,vdrandQK8_1are all compile time constants. We should therefore be able to replace the divisons and modulo used to determinekbx,kbyandkqsin the first for loop withfastdiv/fastmoduloas well (lines 178-182)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.
Those are all powers of 2 though, shouldn't the compiler be able to replace the divisions/modulos with shifts/bitwise ands? Is the use of signed integers in this context problematic?
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.
If they are indeed powers of 2 the compiler will do the correct optimizations for us at compile-time (see godbolt). While it requires 2 more instructions to do signed integer division/modulo compared to unsigned (see this stack-overflow post if interested in details), that should be negligible for our use-case