-
Notifications
You must be signed in to change notification settings - Fork 20.1k
Fuse rms_norm, mul, quantize_q8_1 #22710
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
Open
lnigam
wants to merge
3
commits into
ggml-org:master
Choose a base branch
from
lnigam:fuse_rms_norm_mul_qunatize_q8_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+252
−22
Open
Changes from all commits
Commits
Show all changes
3 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 |
|---|---|---|
|
|
@@ -2344,6 +2344,15 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_f(const ggml_tensor * tensor) { | |
| return use_mul_mat_vec_f; | ||
| } | ||
|
|
||
| static inline bool ggml_cuda_mmvq_eligible(const ggml_tensor * src0, | ||
| const ggml_tensor * src1, | ||
| const ggml_tensor * dst, | ||
| const bool bad_padding_clear) { | ||
| return ggml_is_quantized(src0->type) && !bad_padding_clear && | ||
| (src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_Q8_1) && dst->type == GGML_TYPE_F32 && | ||
| src1->ne[1] <= MMVQ_MAX_BATCH_SIZE; | ||
| } | ||
|
|
||
| static bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) { | ||
| ggml_tensor * src0 = tensor->src[0]; | ||
| ggml_tensor * src1 = tensor->src[1]; | ||
|
|
@@ -2353,8 +2362,7 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) { | |
| ggml_nbytes(src0) != ggml_backend_buffer_get_alloc_size(src0->buffer, src0) && | ||
| src0->view_src; | ||
|
|
||
| bool use_mul_mat_vec_q = ggml_is_quantized(src0->type) && !bad_padding_clear && src1->type == GGML_TYPE_F32 && | ||
| dst->type == GGML_TYPE_F32 && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE; | ||
| bool use_mul_mat_vec_q = ggml_cuda_mmvq_eligible(src0, src1, dst, bad_padding_clear); | ||
|
|
||
| // fusion is not universally faster on Pascal | ||
| const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc; | ||
|
|
@@ -2395,9 +2403,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor | |
| && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32; | ||
| bool use_mul_mat_f = !ggml_is_quantized(src0->type) | ||
| && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32; | ||
| bool use_mul_mat_vec_q = ggml_is_quantized(src0->type) && !bad_padding_clear | ||
| && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 | ||
| && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE; | ||
| bool use_mul_mat_vec_q = ggml_cuda_mmvq_eligible(src0, src1, dst, bad_padding_clear); | ||
| bool use_mul_mat_q = ggml_is_quantized(src0->type) && !bad_padding_clear | ||
| && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32; | ||
|
|
||
|
|
@@ -2472,7 +2478,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor * | |
| const ggml_tensor * src1 = dst->src[1]; | ||
| const ggml_tensor * ids = dst->src[2]; | ||
|
|
||
| GGML_ASSERT(src1->type == GGML_TYPE_F32); | ||
| GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_Q8_1); | ||
| GGML_ASSERT(dst->type == GGML_TYPE_F32); | ||
| GGML_ASSERT(!ggml_backend_buft_is_cuda_split(src0->buffer->buft) && "mul_mat_id does not support split buffers"); | ||
|
|
||
|
|
@@ -2481,7 +2487,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor * | |
| const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc; | ||
|
|
||
| // [TAG_MUL_MAT_ID_CUDA_GRAPHS] | ||
| if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { | ||
| if ((src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_Q8_1) && dst->type == GGML_TYPE_F32) { | ||
| static_assert(MMVQ_MAX_BATCH_SIZE == MMVF_MAX_BATCH_SIZE); | ||
| if (ne2 <= MMVQ_MAX_BATCH_SIZE) { | ||
| if (ggml_is_quantized(src0->type)) { | ||
|
|
@@ -2511,6 +2517,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor * | |
|
|
||
| // note: this path should not be reached when recording CUDA graphs, because it requires stream synchronization | ||
| // TODO: add asserts to verify this. should work with CUDA, HIP, etc. | ||
| GGML_ASSERT(src1->type == GGML_TYPE_F32 && "Q8_1 src1 must be handled by the MMVQ path above"); | ||
| cudaStream_t stream = ctx.stream(); | ||
|
|
||
| GGML_ASSERT(nb12 % nb11 == 0); | ||
|
|
@@ -3522,10 +3529,10 @@ static bool ggml_cuda_can_fuse(const struct ggml_cgraph * cgraph, | |
| GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32); | ||
| GGML_ASSERT(rms_norm->type == GGML_TYPE_F32); | ||
|
|
||
| //rms norm only supports F32 | ||
| if (mul->src[0]->type != GGML_TYPE_F32 || | ||
| mul->src[1]->type != GGML_TYPE_F32 || | ||
| mul->type != GGML_TYPE_F32) { | ||
| //rms norm only supports F32; Q8_1 mul output is only valid for the 2-op fusion | ||
| const bool mul_q8_1_ok = (ops.size() == 2) && (mul->type == GGML_TYPE_Q8_1); | ||
| if (mul->src[0]->type != GGML_TYPE_F32 || mul->src[1]->type != GGML_TYPE_F32 || | ||
| (mul->type != GGML_TYPE_F32 && !mul_q8_1_ok)) { | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -3990,7 +3997,12 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph | |
| } | ||
|
|
||
| if (ggml_cuda_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL }, {})) { | ||
| ggml_cuda_op_rms_norm_fused(*cuda_ctx, node, cgraph->nodes[i + 1]); | ||
| ggml_tensor * mul_node = cgraph->nodes[i + 1]; | ||
| if (mul_node->type == GGML_TYPE_Q8_1) { | ||
| ggml_cuda_op_rms_norm_mul_q8_1(*cuda_ctx, node, mul_node); | ||
| } else { | ||
| ggml_cuda_op_rms_norm_fused(*cuda_ctx, node, mul_node); | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
|
|
@@ -4342,13 +4354,70 @@ static void ggml_backend_cuda_event_wait(ggml_backend_t backend, ggml_backend_ev | |
| static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph * cgraph) { | ||
| ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *) backend->context; | ||
|
|
||
| // Change mul->type F32→Q8_1 when all downstream consumers are MMVQ-eligible. | ||
| // gallocr then allocates Q8_1-sized memory; try_fuse dispatches the Q8_1 kernel. | ||
| for (int i = 0; i + 1 < cgraph->n_nodes; i++) { | ||
| ggml_tensor * rms = cgraph->nodes[i]; | ||
| ggml_tensor * mul = cgraph->nodes[i + 1]; | ||
|
|
||
| if (rms->op != GGML_OP_RMS_NORM) { | ||
| continue; | ||
| } | ||
| if (mul->op != GGML_OP_MUL) { | ||
| continue; | ||
| } | ||
| if (mul->type != GGML_TYPE_F32) { | ||
| continue; | ||
| } | ||
| if (mul->src[0] != rms && mul->src[1] != rms) { | ||
| continue; | ||
| } | ||
|
|
||
| if (mul->ne[0] % MATRIX_ROW_PADDING != 0) { | ||
| continue; | ||
| } | ||
| if (mul->ne[0] % QK8_1 != 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const int32_t mul_use_count = ggml_node_get_use_count(cgraph, i + 1); | ||
| if (mul_use_count == 0) { | ||
| continue; | ||
| } | ||
| int found = 0; | ||
| bool all_mmvq = true; | ||
|
|
||
| for (int j = i + 2; j < cgraph->n_nodes && found < mul_use_count && all_mmvq; j++) { | ||
| ggml_tensor * cand = cgraph->nodes[j]; | ||
| if (cand->src[0] != mul && cand->src[1] != mul) { | ||
| continue; | ||
| } | ||
| found++; | ||
| const bool is_mmvq_op = cand->op == GGML_OP_MUL_MAT || cand->op == GGML_OP_MUL_MAT_ID; | ||
| const bool src0_quantized = cand->src[0] && ggml_is_quantized(cand->src[0]->type); | ||
| const int64_t batch = (cand->op == GGML_OP_MUL_MAT_ID) ? cand->ne[2] : cand->ne[1]; | ||
| if (!is_mmvq_op || !src0_quantized || batch > MMVQ_MAX_BATCH_SIZE) { | ||
| all_mmvq = false; | ||
| } | ||
|
Comment on lines
+4396
to
+4401
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. This is the wrong logic, it has to exactly mirror the kernel selection logic in |
||
| } | ||
|
|
||
| if (!all_mmvq || found != mul_use_count) { | ||
| continue; | ||
| } | ||
|
|
||
| mul->type = GGML_TYPE_Q8_1; | ||
| mul->nb[0] = ggml_type_size(GGML_TYPE_Q8_1); | ||
| mul->nb[1] = ggml_row_size(GGML_TYPE_Q8_1, mul->ne[0]); | ||
| mul->nb[2] = mul->nb[1] * mul->ne[1]; | ||
| mul->nb[3] = mul->nb[2] * mul->ne[2]; | ||
| } | ||
|
|
||
| #ifdef USE_CUDA_GRAPH | ||
| const void * graph_key = ggml_cuda_graph_get_key(cgraph); | ||
| const bool use_cuda_graph = ggml_cuda_graph_set_enabled(cuda_ctx, graph_key); | ||
| #else | ||
| const bool use_cuda_graph = false; | ||
| GGML_UNUSED(cuda_ctx); | ||
| GGML_UNUSED(cgraph); | ||
| #endif | ||
|
|
||
| static bool enable_graph_optimization = [] { | ||
|
|
||
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
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.
Just iterate over
GGML_MAX_SRC, I don't think it makes sense to risk a potential but in the future here.