Skip to content

vulkan: Implement top-k - #17418

Merged
0cc4m merged 4 commits into
ggml-org:masterfrom
jeffbolznv:vk_topk
Nov 26, 2025
Merged

0cc4m merged 4 commits into
ggml-org:masterfrom
jeffbolznv:vk_topk

Conversation

@jeffbolznv

@jeffbolznv jeffbolznv commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

Based on #17365. Ready for review, but Draft until that lands.

@jeffbolznv
jeffbolznv marked this pull request as draft November 21, 2025 00:02
@jeffbolznv jeffbolznv mentioned this pull request Nov 21, 2025
3 of 5 tasks
@github-actions github-actions Bot added testing Everything test related Vulkan Issues specific to the Vulkan backend ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Nov 21, 2025
@ggerganov

Copy link
Copy Markdown
Member

I ran the exhaustive top-k tests on my Mac and it got stuck at some point:

diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 83ba67502..035656943 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -7654,9 +7654,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
     }
 
     // exhaustive top_k tests
-    //for (int i = 1; i < 9999; ++i) {
-    //    test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {i, 2, 1, 3}, rand() % i + 1));
-    //}
+    for (int i = 1; i < 9999; ++i) {
+        test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {i, 2, 1, 3}, rand() % i + 1));
+    }
 
     for (ggml_scale_mode mode : {GGML_SCALE_MODE_NEAREST, GGML_SCALE_MODE_BILINEAR, GGML_SCALE_MODE_BICUBIC}) {
         test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, {512, 512, 3, 2}, 2, mode));
make -j && ./bin/test-backend-ops -o TOP_K -b Vulkan0

...

  TOP_K(type=f32,ne=[2699,2,1,3],k=482): OK
  TOP_K(type=f32,ne=[2700,2,1,3],k=786): OK
^C

So there could be a bug somewhere.

@jeffbolznv

Copy link
Copy Markdown
Contributor Author

Yes, there was a bug in the pipeline selection. I'm also looking into a radix-ish implementation to see how the performance is.

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.
@jeffbolznv
jeffbolznv marked this pull request as ready for review November 25, 2025 14:18
@jeffbolznv

Copy link
Copy Markdown
Contributor Author

Rebased, this is ready for review.

@0cc4m
0cc4m merged commit 879d673 into ggml-org:master Nov 26, 2025
blime4 referenced this pull request in blime4/llama.cpp Feb 5, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
* vulkan: Implement top-k

Each pass launches workgroups that each sort 2^N elements (where N is usually 7-10)
and discards all but the top K. Repeat until only K are left. And there's a fast
path when K==1 to just find the max value rather than sorting.

* fix pipeline selection

* vulkan: Add N-ary search algorithm for topk

* microoptimizations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning testing Everything test related Vulkan Issues specific to the Vulkan backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants