-
Notifications
You must be signed in to change notification settings - Fork 22.8k
ggml : add ggml_conv_1d_grouped #22833
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa8e71b
ops: add Conv1dGrouped operation
Juste-Leo2 27bc1fc
ggml : fix merge conflict artifacts in ggml_col2im_1d and ggml_conv_1…
Juste-Leo2 00f513b
Move the operation to the correct location
Juste-Leo2 1c4f530
ggml : add BF16 kernel support in conv_1d, add BF16 tests
Juste-Leo2 dbfc2cb
ggml : add BF16 kernel support in conv_1d, register test in cmake
Juste-Leo2 4f9d82f
test: adjust floating-point tolerance for ggml_conv_1d_grouped
Juste-Leo2 c0a44ac
fix: increase the tolerance slightly again
Juste-Leo2 bcb1d6b
Fix: Added the missing case where groups == IC == OC
Juste-Leo2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Test for ggml_conv_1d_grouped | ||
| // | ||
| // Verifies grouped 1D convolution by comparing against manual per-group computation. | ||
|
|
||
| #include "ggml.h" | ||
| #include "ggml-backend.h" | ||
| #include "ggml-cpu.h" | ||
|
|
||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <cmath> | ||
| #include <vector> | ||
|
|
||
| static void fill_random_f32(float * data, int n) { | ||
| for (int i = 0; i < n; i++) { | ||
| data[i] = ((float)rand() / RAND_MAX) * 2.0f - 1.0f; | ||
| } | ||
| } | ||
|
|
||
| static bool all_close(const float * a, const float * b, int n, float eps = 2e-2f) { | ||
| for (int i = 0; i < n; i++) { | ||
| if (fabsf(a[i] - b[i]) > eps) { | ||
| fprintf(stderr, " mismatch at [%d]: %.6f vs %.6f (diff=%.6f)\n", | ||
| i, a[i], b[i], fabsf(a[i] - b[i])); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // Compute grouped conv1d on CPU naively for reference | ||
| // kernel (F16): [K, IC_G, OC], input (F32): [L, IC, N], output: [OL, OC, N] | ||
| static void conv1d_grouped_ref( | ||
| const ggml_fp16_t * kernel, const float * input, float * output, | ||
| int K, int IC, int OC, int L, int N, int groups, int stride, int padding) { | ||
| int IC_G = IC / groups; | ||
| int OC_G = OC / groups; | ||
| int OL = (L + 2 * padding - K) / stride + 1; | ||
|
|
||
| memset(output, 0, (size_t)OL * OC * N * sizeof(float)); | ||
|
|
||
| for (int n = 0; n < N; n++) { | ||
| for (int g = 0; g < groups; g++) { | ||
| for (int oc = 0; oc < OC_G; oc++) { | ||
| int oc_global = g * OC_G + oc; | ||
| for (int ol = 0; ol < OL; ol++) { | ||
| float sum = 0.0f; | ||
| for (int ic = 0; ic < IC_G; ic++) { | ||
| for (int k = 0; k < K; k++) { | ||
| int il = ol * stride + k - padding; | ||
| if (il >= 0 && il < L) { | ||
| int ic_global = g * IC_G + ic; | ||
| // kernel: [K, IC_G, OC] -> k + ic * K + oc_global * (IC_G * K) | ||
| float w = ggml_fp16_to_fp32(kernel[k + ic * K + oc_global * (IC_G * K)]); | ||
| // input: [L, IC, N] -> il + ic_global * L + n * (IC * L) | ||
| float x = input[il + ic_global * L + n * (IC * L)]; | ||
| sum += w * x; | ||
| } | ||
| } | ||
| } | ||
| // output: [OL, OC, N] -> ol + oc_global * OL + n * (OC * OL) | ||
| output[ol + oc_global * OL + n * (OC * OL)] = sum; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static bool run_test(const char * label, int IC, int OC, int K, int L, int groups, int stride, int padding, | ||
| enum ggml_type kernel_type = GGML_TYPE_F16, int N = 1) { | ||
| printf(" TEST: %s (IC=%d OC=%d K=%d L=%d N=%d G=%d s=%d p=%d) kernel=%s\n", | ||
| label, IC, OC, K, L, N, groups, stride, padding, | ||
| ggml_type_name(kernel_type)); | ||
|
|
||
| int IC_G = IC / groups; | ||
| int OL = (L + 2 * padding - K) / stride + 1; | ||
|
|
||
| size_t ctx_size = 256 * 1024 * 1024; | ||
| struct ggml_init_params params = { | ||
| /*.mem_size =*/ ctx_size, | ||
| /*.mem_buffer =*/ NULL, | ||
| /*.no_alloc =*/ false, | ||
| }; | ||
| struct ggml_context * ctx = ggml_init(params); | ||
|
|
||
| // generate kernel data | ||
| std::vector<float> kernel_f32(K * IC_G * OC); | ||
| fill_random_f32(kernel_f32.data(), K * IC_G * OC); | ||
|
|
||
| // kernel for op: [K, IC_G, OC] and reference (F16) | ||
| struct ggml_tensor * a = ggml_new_tensor_3d(ctx, kernel_type, K, IC_G, OC); | ||
| std::vector<ggml_fp16_t> kernel_f16(K * IC_G * OC); | ||
| for (int i = 0; i < K * IC_G * OC; i++) { | ||
| if (kernel_type == GGML_TYPE_BF16) { | ||
| ggml_bf16_t b = ggml_fp32_to_bf16(kernel_f32[i]); | ||
| ((ggml_bf16_t *)a->data)[i] = b; | ||
| kernel_f16[i] = ggml_fp32_to_fp16(ggml_bf16_to_fp32(b)); | ||
| } else { | ||
| kernel_f16[i] = ggml_fp32_to_fp16(kernel_f32[i]); | ||
| } | ||
| } | ||
| if (kernel_type != GGML_TYPE_BF16) { | ||
| memcpy(a->data, kernel_f16.data(), K * IC_G * OC * sizeof(ggml_fp16_t)); | ||
| } | ||
|
|
||
| // generate reference input (F32) | ||
| std::vector<float> input_f32(L * IC * N); | ||
| fill_random_f32(input_f32.data(), L * IC * N); | ||
|
|
||
| // input for op: [L, IC, N] | ||
| struct ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, L, IC, N); | ||
| memcpy(b->data, input_f32.data(), L * IC * N * sizeof(float)); | ||
|
|
||
| // reference | ||
| std::vector<float> ref(OL * OC * N); | ||
| conv1d_grouped_ref(kernel_f16.data(), input_f32.data(), ref.data(), | ||
| K, IC, OC, L, N, groups, stride, padding); | ||
|
|
||
| // ggml | ||
| struct ggml_tensor * result = ggml_conv_1d_grouped(ctx, a, b, stride, padding, 1, groups); | ||
|
|
||
| struct ggml_cgraph * gf = ggml_new_graph(ctx); | ||
| ggml_build_forward_expand(gf, result); | ||
|
|
||
| ggml_backend_t backend = ggml_backend_cpu_init(); | ||
| ggml_backend_graph_compute(backend, gf); | ||
|
|
||
| bool ok = true; | ||
|
|
||
| if (result->ne[0] != OL || result->ne[1] != OC || result->ne[2] != N) { | ||
| fprintf(stderr, " FAIL: shape [%lld, %lld, %lld], expected [%d, %d, %d]\n", | ||
| (long long)result->ne[0], (long long)result->ne[1], (long long)result->ne[2], OL, OC, N); | ||
| ok = false; | ||
| } | ||
|
|
||
| if (ok) { | ||
| ok = all_close((float *)result->data, ref.data(), OL * OC * N); | ||
| } | ||
|
|
||
| printf(" %s\n", ok ? "PASS" : "FAIL"); | ||
|
|
||
| ggml_backend_free(backend); | ||
| ggml_free(ctx); | ||
| return ok; | ||
| } | ||
|
|
||
| int main(void) { | ||
| srand(42); | ||
|
|
||
| printf("Testing ggml_conv_1d_grouped\n\n"); | ||
|
|
||
| int n_pass = 0, n_fail = 0; | ||
|
|
||
| struct { const char * label; int IC, OC, K, L, G, s, p; int N; } scenarios[] = { | ||
| { "groups=1 (standard conv1d)", 128, 256, 3, 32, 1, 1, 0, 1 }, | ||
| { "ZAYA1-8B exact params", 1280, 1280, 2, 16, 10, 1, 0, 1 }, | ||
| { "small 2 groups", 4, 4, 2, 8, 2, 1, 0, 1 }, | ||
| { "with padding", 8, 8, 2, 16, 4, 1, 1, 1 }, | ||
| { "IC != OC", 12, 6, 3, 10, 3, 1, 0, 1 }, | ||
| { "stride=2", 8, 8, 2, 16, 4, 2, 0, 1 }, | ||
| { "longer sequence", 1280, 1280, 2, 128, 10, 1, 0, 1 }, | ||
| { "depthwise (groups==IC==OC)", 8, 8, 3, 16, 8, 1, 0, 1 }, | ||
| }; | ||
|
|
||
| enum ggml_type kernel_types[] = { GGML_TYPE_F16, GGML_TYPE_BF16 }; | ||
| for (auto kt : kernel_types) { | ||
| if (kt != GGML_TYPE_F16) { | ||
| printf("\n--- %s ---\n\n", ggml_type_name(kt)); | ||
| } | ||
| for (auto &s : scenarios) { | ||
| if (run_test(s.label, s.IC, s.OC, s.K, s.L, s.G, s.s, s.p, kt, s.N)) { n_pass++; } else { n_fail++; } | ||
| } | ||
| } | ||
|
|
||
| printf("\nResult: %d passed, %d failed\n", n_pass, n_fail); | ||
| return n_fail > 0 ? 1 : 0; | ||
| } |
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.
Note : PR #23660 changed the im2col casting logic. To maintain mul_mat compatibility and prevent crashes with BF16 weights, the kernel must be explicitly cast to F32 here. Other convolution operations might require a similar follow-up fix.