-
Notifications
You must be signed in to change notification settings - Fork 257
perf(cuda): add warp-cooperative TurboQuant FWHT #582
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
Amidwestnoob
wants to merge
2
commits into
Luce-Org:main
Choose a base branch
from
Amidwestnoob:exp/sm86-fwht-pr
base: main
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.
+261
−3
Open
Changes from all commits
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
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,187 @@ | ||
| #include "common.cuh" | ||
| #include "tq3-quant.cuh" | ||
|
|
||
| #include <cuda_runtime.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <vector> | ||
|
|
||
| #define TEST_CUDA_CHECK(expr) do { \ | ||
| const cudaError_t err = (expr); \ | ||
| if (err != cudaSuccess) { \ | ||
| std::fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, cudaGetErrorString(err)); \ | ||
| std::exit(1); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| static __global__ void scalar_reference( | ||
| const float * input, float * output, int64_t groups, int direction) { | ||
| const int64_t group = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; | ||
| if (group >= groups) { | ||
| return; | ||
| } | ||
| float values[128]; | ||
| for (int i = 0; i < 128; ++i) { | ||
| values[i] = input[group * 128 + i]; | ||
| } | ||
| if (direction == 0) { | ||
| tq3_rotate_forward(values); | ||
| } else { | ||
| tq3_rotate_inverse(values); | ||
| } | ||
| for (int i = 0; i < 128; ++i) { | ||
| output[group * 128 + i] = values[i]; | ||
| } | ||
| } | ||
|
|
||
| static __global__ void warp_candidate( | ||
| const float * input, float * output, int64_t groups, int direction) { | ||
| constexpr int warp_size = 32; | ||
| const int warp = threadIdx.x / warp_size; | ||
| const int lane = threadIdx.x & (warp_size - 1); | ||
| const int64_t group = (int64_t) blockIdx.x * (blockDim.x / warp_size) + warp; | ||
| if (group >= groups) { | ||
| return; | ||
| } | ||
| const int64_t base = group * 128 + lane * 4; | ||
| float v0 = input[base + 0]; | ||
| float v1 = input[base + 1]; | ||
| float v2 = input[base + 2]; | ||
| float v3 = input[base + 3]; | ||
| if (direction == 0) { | ||
| warp_tq3_rotate_forward(v0, v1, v2, v3); | ||
| } else { | ||
| warp_tq3_rotate_inverse(v0, v1, v2, v3); | ||
| } | ||
| output[base + 0] = v0; | ||
| output[base + 1] = v1; | ||
| output[base + 2] = v2; | ||
| output[base + 3] = v3; | ||
| } | ||
|
|
||
| static bool run_case(int64_t groups, int direction) { | ||
| const int64_t count = groups * 128; | ||
| std::vector<float> input((size_t) count); | ||
| for (int64_t i = 0; i < count; ++i) { | ||
| input[(size_t) i] = (float) ((i * 37 + 11) % 257 - 128) / 64.0f; | ||
| } | ||
|
|
||
| float * d_input = nullptr; | ||
| float * d_reference = nullptr; | ||
| float * d_candidate = nullptr; | ||
| TEST_CUDA_CHECK(cudaMalloc(&d_input, count * sizeof(float))); | ||
| TEST_CUDA_CHECK(cudaMalloc(&d_reference, count * sizeof(float))); | ||
| TEST_CUDA_CHECK(cudaMalloc(&d_candidate, count * sizeof(float))); | ||
| TEST_CUDA_CHECK(cudaMemcpy(d_input, input.data(), count * sizeof(float), cudaMemcpyHostToDevice)); | ||
|
|
||
| scalar_reference<<<(groups + 127) / 128, 128>>>(d_input, d_reference, groups, direction); | ||
| warp_candidate<<<(groups + 3) / 4, 128>>>(d_input, d_candidate, groups, direction); | ||
| TEST_CUDA_CHECK(cudaGetLastError()); | ||
| TEST_CUDA_CHECK(cudaDeviceSynchronize()); | ||
|
|
||
| std::vector<float> reference((size_t) count); | ||
| std::vector<float> candidate((size_t) count); | ||
| TEST_CUDA_CHECK(cudaMemcpy(reference.data(), d_reference, count * sizeof(float), cudaMemcpyDeviceToHost)); | ||
| TEST_CUDA_CHECK(cudaMemcpy(candidate.data(), d_candidate, count * sizeof(float), cudaMemcpyDeviceToHost)); | ||
|
|
||
| int64_t mismatches = 0; | ||
| for (int64_t i = 0; i < count; ++i) { | ||
| if (reference[(size_t) i] != candidate[(size_t) i]) { | ||
| ++mismatches; | ||
| if (mismatches <= 4) { | ||
| std::fprintf(stderr, "groups=%lld direction=%d i=%lld ref=%a got=%a\n", | ||
| (long long) groups, direction, (long long) i, | ||
| reference[(size_t) i], candidate[(size_t) i]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| cudaFree(d_candidate); | ||
| cudaFree(d_reference); | ||
| cudaFree(d_input); | ||
| std::printf("[%s] groups=%lld direction=%d mismatches=%lld\n", | ||
| mismatches == 0 ? "PASS" : "FAIL", (long long) groups, | ||
| direction, (long long) mismatches); | ||
| return mismatches == 0; | ||
| } | ||
|
|
||
| static double benchmark_case(int64_t groups, bool warp_kernel, int iterations) { | ||
| const int64_t count = groups * 128; | ||
| std::vector<float> input((size_t) count, 0.125f); | ||
| float * d_input = nullptr; | ||
| float * d_output = nullptr; | ||
| cudaEvent_t start = nullptr; | ||
| cudaEvent_t stop = nullptr; | ||
| TEST_CUDA_CHECK(cudaMalloc(&d_input, count * sizeof(float))); | ||
| TEST_CUDA_CHECK(cudaMalloc(&d_output, count * sizeof(float))); | ||
| TEST_CUDA_CHECK(cudaMemcpy(d_input, input.data(), count * sizeof(float), cudaMemcpyHostToDevice)); | ||
| TEST_CUDA_CHECK(cudaEventCreate(&start)); | ||
| TEST_CUDA_CHECK(cudaEventCreate(&stop)); | ||
|
|
||
| for (int i = 0; i < 100; ++i) { | ||
| if (warp_kernel) { | ||
| warp_candidate<<<(groups + 3) / 4, 128>>>(d_input, d_output, groups, 0); | ||
| } else { | ||
| scalar_reference<<<(groups + 127) / 128, 128>>>(d_input, d_output, groups, 0); | ||
| } | ||
| } | ||
| TEST_CUDA_CHECK(cudaDeviceSynchronize()); | ||
| TEST_CUDA_CHECK(cudaEventRecord(start)); | ||
| for (int i = 0; i < iterations; ++i) { | ||
| if (warp_kernel) { | ||
| warp_candidate<<<(groups + 3) / 4, 128>>>(d_input, d_output, groups, 0); | ||
| } else { | ||
| scalar_reference<<<(groups + 127) / 128, 128>>>(d_input, d_output, groups, 0); | ||
| } | ||
| } | ||
| TEST_CUDA_CHECK(cudaEventRecord(stop)); | ||
| TEST_CUDA_CHECK(cudaEventSynchronize(stop)); | ||
| float elapsed_ms = 0.0f; | ||
| TEST_CUDA_CHECK(cudaEventElapsedTime(&elapsed_ms, start, stop)); | ||
|
|
||
| cudaEventDestroy(stop); | ||
| cudaEventDestroy(start); | ||
| cudaFree(d_output); | ||
| cudaFree(d_input); | ||
| return (double) elapsed_ms * 1000.0 / iterations; | ||
| } | ||
|
|
||
| int main() { | ||
| int device_count = 0; | ||
| const cudaError_t device_status = cudaGetDeviceCount(&device_count); | ||
| if (device_status == cudaErrorNoDevice) { | ||
| std::puts("SKIP: no CUDA device"); | ||
| return 0; | ||
| } | ||
| TEST_CUDA_CHECK(device_status); | ||
| if (device_count == 0) { | ||
| std::puts("SKIP: no CUDA device"); | ||
| return 0; | ||
| } | ||
| TEST_CUDA_CHECK(cudaSetDevice(0)); | ||
|
|
||
| const int64_t group_counts[] = {1, 4, 32, 128, 384}; | ||
| int failures = 0; | ||
| for (const int direction : {0, 1}) { | ||
| for (const int64_t groups : group_counts) { | ||
| if (!run_case(groups, direction)) { | ||
| ++failures; | ||
| } | ||
| } | ||
| } | ||
| if (failures != 0) { | ||
| std::fprintf(stderr, "FAILED: %d cases\n", failures); | ||
| return 1; | ||
| } | ||
| std::puts("ALL PASS: scalar and warp FWHT outputs are bit-identical"); | ||
| for (const int64_t groups : {128LL, 384LL}) { | ||
| const double scalar_us = benchmark_case(groups, false, 10000); | ||
| const double warp_us = benchmark_case(groups, true, 10000); | ||
| std::printf("BENCH groups=%lld scalar=%.3f us warp=%.3f us speedup=%.2fx reduction=%.1f%%\n", | ||
| (long long) groups, scalar_us, warp_us, scalar_us / warp_us, | ||
| 100.0 * (1.0 - warp_us / scalar_us)); | ||
| } | ||
| return 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.
P3: The new
k_turbo_wht_warpduplicates the whole group-coordinate decode (gid -> g/rem -> i01/i02and thesrc_base + i01*src_nb1 + i02*src_nb2 + g*QK_TQ3_0_GROUProw-pointer math) plus the 12-argument signature and launch argument list that the scalar (k_turbo_wht_scalar) and fused-quantize (k_turbo_wht_quantize) kernels already own. Since the scalar and warp paths are chosen by#if defined(GGML_USE_HIP), only one compiles on a given backend, so today this is behavior-neutral. The risk is maintenance drift: a future change to the group indexing or stride handling would have to be applied identically in three places or the HIP and CUDA paths would diverge silently. Consider factoring the common decode (params + gid->(g,i01,i02) + row pointers) into a shared helper/inline used by all three kernels to keep them in sync.Prompt for AI agents