vulkan: make the radix top-k tie selection deterministic (#31931)

* vulkan: make the radix top-k tie selection deterministic

The radix select gathers the elements above the threshold and the ties
with an atomic counter, so with more ties than remaining slots the kept
columns depend on the thread scheduling.

Each subgroup now owns a contiguous chunk of the row with the same
column mapping in every pass, counts its kept elements, and writes them
at ranks from a scan of the subgroup totals; overflowing ties are ranked
in column order with a ballot, so the lowest tied columns win, as on
CUDA and Metal. The radix pipelines require subgroup arithmetic, ballot
and full subgroups. Enable the deterministic top-k check on Vulkan.

* optimize shader for AMD, Intel and Nvidia

---------

Co-authored-by: Ruben Ortlam <rortlam@redhat.com>
This commit is contained in:
PascalandRuben Ortlam authored and GitHub committed 2026-11-16 11:41:44 +01:00
1 parent 6b01a4767f
commit adf554d239
4 files changed
+165 -31

No files matched your search

+14 -8
View File
@@ -7496,10 +7496,10 @@ struct test_top_k : public test_case {
// the backends may break ties in different ways, but each backend must break them in the same way in each run
bool deterministic(ggml_backend_t backend) override {
// TODO: Vulkan is not deterministic yet, SYCL gathers the ties with an atomic counter as well
// TODO: SYCL is not deterministic yet, it gathers the ties with an atomic counter
const char * name = ggml_backend_reg_name(ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)));
// ROCm has no cub::DeviceTopK, every row goes through the radix or bitonic path
if (strcmp(name, "ROCm") == 0) {
// ROCm and Vulkan have no cub::DeviceTopK, every row goes through their own deterministic paths
if (strcmp(name, "ROCm") == 0 || strcmp(name, "Vulkan") == 0) {
return ties;
}
if (strcmp(name, "CUDA") != 0 && strcmp(name, "MTL") != 0) {
@@ -7673,11 +7673,12 @@ struct test_topk_qsa : public test_case {
}
std::string vars() override {
return VARS_TO_STR5(n_blocks, n_kv, n_tps, n_stream, width);
return VARS_TO_STR6(n_blocks, n_kv, n_tps, n_stream, width, tied);
}
test_topk_qsa(int64_t n_blocks = 512, int64_t n_kv = 2048, int64_t n_tps = 2, int64_t n_stream = 1, int width = 1500)
: n_blocks(n_blocks), n_kv(n_kv), n_tps(n_tps), n_stream(n_stream), width(width) {}
const bool tied;
test_topk_qsa(int64_t n_blocks = 512, int64_t n_kv = 2048, int64_t n_tps = 2, int64_t n_stream = 1, int width = 1500, bool tied = false)
: n_blocks(n_blocks), n_kv(n_kv), n_tps(n_tps), n_stream(n_stream), width(width), tied(tied) {}
double max_err() override { return 0.0; }
bool run_whole_graph() override { return true; }
@@ -7710,13 +7711,13 @@ struct test_topk_qsa : public test_case {
}
if (t->type == GGML_TYPE_I32) {
std::vector<int32_t> data(ggml_nelements(t));
for (auto & v : data) { v = rand() % n_blocks; }
for (size_t i = 0; i < data.size(); i++) { data[i] = tied ? (int32_t) ((i % n_kv) * n_blocks / n_kv) : rand() % n_blocks; }
ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(int32_t));
} else if (t->type == GGML_TYPE_F16) {
std::vector<ggml_fp16_t> data(ggml_nelements(t));
for (int64_t r = 0; r < ggml_nrows(t); r++) {
for (int64_t i = 0; i < n_kv; i++) {
data[r * n_kv + i] = ggml_fp32_to_fp16((float) i);
data[r * n_kv + i] = ggml_fp32_to_fp16(tied ? 0.0f : (float) i);
}
}
ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(ggml_fp16_t));
@@ -14032,8 +14033,13 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
for (auto cols : {8192, 32768, 131072}) {
for (auto nrows : {1, 2, 4, 8, 16, 32}) {
test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, 2048));
test_cases.emplace_back(new test_top_k(GGML_TYPE_F32, {cols, nrows, 1, 1}, 2048, true));
}
}
for (int n_tps : {1, 16, 64}) {
test_cases.emplace_back(new test_topk_qsa(516, 33024, n_tps, 1, 2051, true));
test_cases.emplace_back(new test_topk_qsa(2064, 132096, n_tps, 1, 2051, true));
}
// bitonic vs radix crossover
for (auto cols : {520, 1000, 2048, 3000, 4096}) {
for (auto nrows : {2, 16, 32, 48, 64, 128, 256, 512}) {