From 77ab94c4412f229edf886fd027c93bdf173f6b72 Mon Sep 17 00:00:00 2001 From: Joel Farthing <262452229+joelfarthing@users.noreply.github.com> Date: Sat, 18 Jul 2026 14:10:53 -0500 Subject: [PATCH 1/4] indexer_topk: fix quantized q8_1 scratch sizing on CUDA The quantized-K path under-sized its q8_1 scratch buffer: it allocated q->ne[1]*max_rows blocks using the unpadded head dim, but quantize_mmq_q8_1_cuda writes q_padded/QK8_1 blocks per row and must process all q->ne[1]*nrows rows. Size the buffer by (q_padded/QK8_1) blocks x (q->ne[1]*max_rows) rows and pass the full q->ne[1]*nrows row count so the scratch cannot be overrun and every query row is quantized. CUDA graph identity: INDEXER_TOPK dispatches a source-type-specialized kernel (dense F16 vs quantized cache, F32 vs F16 mask). Source addresses alone do not identify the captured kernel, so snapshot each source type in ggml_graph_node_properties and force re-capture when an INDEXER_TOPK source type changes, preventing a reused graph from replaying the wrong kernel variant when sources are reallocated at the same address. CPU backend: report INDEXER_TOPK support via iqk_indexer_topk_supported (guarded by GGML_USE_IQK_MULMAT) so the scheduler places the node on a backend that can run it. Harden the scheduler's pass-5 node-assignment check from assert to GGML_ASSERT so a node no backend supports fails as a defined abort under NDEBUG instead of indexing sched->backends[-1]. --- ggml/src/ggml-backend.cpp | 23 +++- ggml/src/ggml-cuda.cu | 14 +++ ggml/src/ggml-cuda/graph.cuh | 1 + ggml/src/ggml-cuda/indexer_topk.cu | 4 +- ggml/src/iqk/iqk_mul_mat.cpp | 166 ++++++++++++++++++++++++++++- ggml/src/iqk/iqk_mul_mat.h | 1 + 6 files changed, 203 insertions(+), 6 deletions(-) diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp index 3d065d7e6e..1ebf9f5418 100644 --- a/ggml/src/ggml-backend.cpp +++ b/ggml/src/ggml-backend.cpp @@ -3,6 +3,9 @@ #include "ggml-impl.h" #include "ggml-rpc.h" #include "ggml-moe-prefetch.h" +#ifdef GGML_USE_IQK_MULMAT +#include "iqk/iqk_mul_mat.h" +#endif #include #include @@ -896,6 +899,11 @@ GGML_CALL static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t } GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) { +#ifdef GGML_USE_IQK_MULMAT + struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *) backend->context; +#else + GGML_UNUSED(backend); +#endif switch (op->op) { case GGML_OP_CPY: return @@ -906,11 +914,15 @@ GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const case GGML_OP_MUL_MAT: return true; //return op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == ggml_internal_get_type_traits(op->src[0]->type).vec_dot_type; + case GGML_OP_INDEXER_TOPK: +#ifdef GGML_USE_IQK_MULMAT + return iqk_indexer_topk_supported(op, cpu_ctx->n_threads); +#else + return false; +#endif default: return true; } - - GGML_UNUSED(backend); } GGML_CALL static bool ggml_backend_cpu_supports_buft(ggml_backend_t backend, ggml_backend_buffer_type_t buft) { @@ -1731,7 +1743,12 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg const int node_backend_id = tensor_backend_id(node); - assert(node_backend_id != -1); // all nodes should be assigned by now + // GGML_ASSERT (not assert) so an op that no backend supports fails as a defined + // abort even under NDEBUG, rather than falling through to sched->backends[-1] (UB). + // Reachable only in a build where the sole backend's supports_op rejects a node + // (e.g. INDEXER_TOPK on a CPU-only, non-IQK build); such configs already could not + // run the op, so this preserves base's diagnosable failure instead of a silent crash. + GGML_ASSERT(node_backend_id != -1 && "op has no supporting backend"); // check if we should start a new split based on the sources of the current node bool need_new_split = false; diff --git a/ggml/src/ggml-cuda.cu b/ggml/src/ggml-cuda.cu index 7e66572e3f..c0844a2c16 100644 --- a/ggml/src/ggml-cuda.cu +++ b/ggml/src/ggml-cuda.cu @@ -4435,6 +4435,7 @@ static void set_ggml_graph_node_properties(ggml_tensor * node, ggml_graph_node_p } for (int i = 0; i < GGML_MAX_SRC; i++) { graph_node_properties->src_address[i] = node->src[i] ? node->src[i]->data : nullptr; + graph_node_properties->src_type[i] = node->src[i] ? node->src[i]->type : GGML_TYPE_COUNT; } memcpy(graph_node_properties->op_params, node->op_params, GGML_MAX_OP_PARAMS); } @@ -4474,6 +4475,19 @@ static bool ggml_graph_node_has_matching_properties(ggml_tensor * node, ggml_gra return false; } + // INDEXER_TOPK dispatches a source-type-specialized kernel (dense F16 vs quantized cache, + // and an F32 vs F16 mask variant). Source addresses alone do not identify the captured + // kernel, so a reused graph whose sources were reallocated at the same addresses with a + // different type would replay the wrong kernel. Force re-capture when a source type changes. + if (node->op == GGML_OP_INDEXER_TOPK) { + for (int i = 0; i < GGML_MAX_SRC; i++) { + const ggml_type src_type = node->src[i] ? node->src[i]->type : GGML_TYPE_COUNT; + if (src_type != graph_node_properties->src_type[i]) { + return false; + } + } + } + return true; } diff --git a/ggml/src/ggml-cuda/graph.cuh b/ggml/src/ggml-cuda/graph.cuh index 1b3a30ff02..fd3c0fccd6 100644 --- a/ggml/src/ggml-cuda/graph.cuh +++ b/ggml/src/ggml-cuda/graph.cuh @@ -8,6 +8,7 @@ struct ggml_graph_node_properties { int64_t ne[GGML_MAX_DIMS]; size_t nb[GGML_MAX_DIMS]; void * src_address[GGML_MAX_SRC]; + ggml_type src_type[GGML_MAX_SRC]; int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)]; }; diff --git a/ggml/src/ggml-cuda/indexer_topk.cu b/ggml/src/ggml-cuda/indexer_topk.cu index a110a7aceb..a874f72454 100644 --- a/ggml/src/ggml-cuda/indexer_topk.cu +++ b/ggml/src/ggml-cuda/indexer_topk.cu @@ -160,7 +160,7 @@ void ggml_cuda_op_indexer_topk(ggml_backend_cuda_context & ctx, ggml_tensor * ds ggml_cuda_pool_alloc q_converted(ctx.pool()); auto q_padded = GGML_PAD(q->ne[0], MATRIX_ROW_PADDING); if (ggml_is_quantized(k->type)) { - auto nbytes_q = q->ne[1] * max_rows * sizeof(block_q8_1)/QK8_1; + auto nbytes_q = (size_t)(q_padded/QK8_1) * ((size_t) q->ne[1] * max_rows) * sizeof(block_q8_1); nbytes_q += get_mmq_x_max_host(ggml_cuda_info().devices[ctx.device].cc)*sizeof(block_q8_1_mmq); q_converted.alloc(nbytes_q); } else { @@ -177,7 +177,7 @@ void ggml_cuda_op_indexer_topk(ggml_backend_cuda_context & ctx, ggml_tensor * ds auto q_data = (const char *)q->data + istep*max_rows*q->nb[2]; auto m_data = (const char *)m->data + istep*max_rows*m->nb[1]; if (ggml_is_quantized(k->type)) { - quantize_mmq_q8_1_cuda((const float *)q_data, q_converted.get(), q->ne[0], nrows, 1, q_padded, k->type, ctx.stream()); + quantize_mmq_q8_1_cuda((const float *)q_data, q_converted.get(), q->ne[0], q->ne[1]*nrows, 1, q_padded, k->type, ctx.stream()); CUDA_CHECK(cudaGetLastError()); mmq_args args{(const char *)k->data, q_converted.get(), kq.get(), k->ne[0], k->ne[1], int64_t(k->nb[1]), diff --git a/ggml/src/iqk/iqk_mul_mat.cpp b/ggml/src/iqk/iqk_mul_mat.cpp index 5158c6c5a6..915ea1c7a5 100644 --- a/ggml/src/iqk/iqk_mul_mat.cpp +++ b/ggml/src/iqk/iqk_mul_mat.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include "ggml-impl.h" #include "ggml-quants.h" @@ -1737,6 +1739,157 @@ bool iqk_fused_delta_net(int head_dim, int n_heads, int gqa_ratio, int repeat_ty } namespace { +bool iqk_checked_mul_size(size_t a, size_t b, size_t & result) { + if (a != 0 && b > std::numeric_limits::max()/a) { + return false; + } + result = a*b; + return true; +} + +bool iqk_checked_add_size(size_t a, size_t b, size_t & result) { + if (b > std::numeric_limits::max() - a) { + return false; + } + result = a + b; + return true; +} + +bool iqk_plain_layout_supported(const ggml_tensor * tensor, ggml_type type, int used_dims) { + if (tensor == nullptr || tensor->type != type || used_dims < 1 || used_dims > GGML_MAX_DIMS) { + return false; + } + const size_t element_size = ggml_type_size(type); + if (tensor->nb[0] != element_size || tensor->view_offs % element_size != 0 || + (tensor->data != nullptr && (uintptr_t) tensor->data % element_size != 0)) { + return false; + } + size_t extent = element_size; + for (int i = 0; i < used_dims; ++i) { + if (tensor->ne[i] <= 0 || (i > 0 && tensor->nb[i] < extent)) { + return false; + } + size_t offset = 0; + if (!iqk_checked_mul_size((size_t) (tensor->ne[i] - 1), tensor->nb[i], offset) || + !iqk_checked_add_size(offset, extent, extent)) { + return false; + } + } + size_t end = 0; + return iqk_checked_add_size(tensor->view_offs, extent, end); +} + +bool iqk_k_layout_supported(const ggml_tensor * k) { + if (k == nullptr || k->type < 0 || k->type >= GGML_TYPE_COUNT) { + return false; + } + const int64_t block_size = ggml_blck_size(k->type); + if (block_size <= 0 || k->ne[0] <= 0 || k->ne[0] % block_size != 0) { + return false; + } + const size_t type_size = ggml_type_size(k->type); + size_t row_size = 0; + size_t row_offset = 0; + size_t span = 0; + size_t end = 0; + return type_size > 0 && k->nb[0] == type_size && + k->view_offs % type_size == 0 && + (k->data == nullptr || (uintptr_t) k->data % alignof(uint16_t) == 0) && + iqk_checked_mul_size((size_t) (k->ne[0]/block_size), type_size, row_size) && + k->nb[1] >= row_size && + iqk_checked_mul_size((size_t) (k->ne[1] - 1), k->nb[1], row_offset) && + iqk_checked_add_size(row_offset, row_size, span) && + iqk_checked_add_size(k->view_offs, span, end); +} + +bool iqk_indexer_topk_contract_supported(const ggml_tensor * dst, int nthread) { + if (dst == nullptr || dst->op != GGML_OP_INDEXER_TOPK || nthread <= 0 || + ggml_unary_op(dst->op_params[0]) != GGML_UNARY_OP_RELU) { + return false; + } + const ggml_tensor * k = dst->src[0]; + const ggml_tensor * q = dst->src[1]; + const ggml_tensor * w = dst->src[2]; + const ggml_tensor * m = dst->src[3]; + if (k == nullptr || q == nullptr || w == nullptr || m == nullptr) { + return false; + } + + const int64_t d = k->ne[0]; + const int64_t n_kv = k->ne[1]; + const int64_t n_head = q->ne[1]; + const int64_t n_tokens = q->ne[2]; + const int64_t n_top_k = dst->ne[0]; + if (d <= 0 || n_kv <= 0 || n_head <= 0 || n_tokens <= 0 || n_top_k <= 0 || + d > INT_MAX || n_kv > INT_MAX || n_head > INT_MAX || n_tokens > INT_MAX || + n_top_k > INT_MAX || n_top_k >= n_kv || + k->ne[2] != 1 || k->ne[3] != 1 || + q->ne[0] != d || q->ne[3] != 1 || + w->ne[0] != n_head || w->ne[1] != n_tokens || w->ne[2] != 1 || w->ne[3] != 1 || + m->ne[0] != n_kv || m->ne[1] != n_tokens || m->ne[2] != 1 || m->ne[3] != 1 || + dst->ne[1] != n_tokens || dst->ne[2] != 1 || dst->ne[3] != 1 || + !iqk_k_layout_supported(k) || + !iqk_plain_layout_supported(q, GGML_TYPE_F32, 3) || + !iqk_plain_layout_supported(w, GGML_TYPE_F32, 2) || + !iqk_plain_layout_supported(dst, GGML_TYPE_I32, 2) || + (m->type != GGML_TYPE_F32 && m->type != GGML_TYPE_F16) || + !iqk_plain_layout_supported(m, m->type, 2)) { + return false; + } + + const auto tt = ggml_internal_get_type_traits(k->type); + ggml_type q_type = q->type; + size_t quantize_size = 0; + if (tt.is_quantized && tt.vec_dot_type != q->type) { + const auto ttq = ggml_internal_get_type_traits(tt.vec_dot_type); + if (ttq.from_float == nullptr || q->nb[1] != (size_t) d*sizeof(float)) { + return false; + } + q_type = tt.vec_dot_type; + if (d % ggml_blck_size(q_type) != 0) { + return false; + } + const size_t row_size_q = ggml_row_size(q_type, d); + if (!iqk_checked_mul_size(row_size_q, (size_t) n_head, quantize_size)) { + return false; + } + } + + MulMat mm; + if (!MulMat::prepare(int(k->type), int(q_type), int(d), mm, int(n_head)) || + (n_tokens < nthread && n_kv % 32 != 0)) { + return false; + } + + size_t kq_size = 0; + size_t score_size = 0; + size_t sorted_size = 0; + size_t work_size = quantize_size; + if (!iqk_checked_mul_size((size_t) n_kv, (size_t) n_head, kq_size) || + !iqk_checked_mul_size(kq_size, sizeof(float), kq_size) || + !iqk_checked_mul_size((size_t) n_kv, sizeof(float), score_size) || + !iqk_checked_mul_size((size_t) n_kv, sizeof(int32_t), sorted_size)) { + return false; + } + if (n_tokens < nthread && quantize_size != 0 && + !iqk_checked_mul_size(quantize_size, (size_t) n_tokens, work_size)) { + return false; + } + if (!iqk_checked_add_size(work_size, kq_size, work_size) || + !iqk_checked_add_size(work_size, score_size, work_size) || + !iqk_checked_add_size(work_size, sorted_size, work_size)) { + return false; + } + if (n_tokens >= nthread) { + if (work_size > std::numeric_limits::max() - 127) { + return false; + } + work_size = GGML_PAD(work_size, 128); + return iqk_checked_mul_size(work_size, (size_t) nthread, work_size); + } + return true; +} + size_t iqk_idx_topk_work_wbs_per_thread(const struct ggml_tensor * dst, int nth) { auto k = dst->src[0]; auto q = dst->src[1]; @@ -1763,7 +1916,14 @@ inline void iqk_f16_to_f32(int n, const ggml_fp16_t * x, float * y) { } } +bool iqk_indexer_topk_supported(const struct ggml_tensor * dst, int nthread) { + return iqk_indexer_topk_contract_supported(dst, nthread); +} + size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor * dst, int nthread) { + if (!iqk_indexer_topk_supported(dst, nthread)) { + return 0; + } auto k = dst->src[0]; auto q = dst->src[1]; if (q->ne[2] >= nthread) { @@ -1782,7 +1942,7 @@ size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor * dst, int nthread } bool iqk_indexer_topk(struct ggml_tensor * dst, void * work_buffer, barrier_t barrier, void * barrier_data, int ith, int nth) { - if (dst->op != GGML_OP_INDEXER_TOPK) { + if (!iqk_indexer_topk_supported(dst, nth)) { return false; } auto op = ggml_unary_op(dst->op_params[0]); @@ -1975,6 +2135,10 @@ bool iqk_indexer_topk(struct ggml_tensor *, void *, barrier_t, void *, int, int) return false; } +bool iqk_indexer_topk_supported(const struct ggml_tensor *, int) { + return false; +} + size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor *, int) { return 0; } diff --git a/ggml/src/iqk/iqk_mul_mat.h b/ggml/src/iqk/iqk_mul_mat.h index aae2eb197c..4a9c9456a8 100644 --- a/ggml/src/iqk/iqk_mul_mat.h +++ b/ggml/src/iqk/iqk_mul_mat.h @@ -79,6 +79,7 @@ IQK_API bool iqk_fused_delta_net(int head_dim, int n_heads, int gqa_ratio, int r const float * state_in, float * out_data, float * state_out, float * saved_steps, int state_step_stride, int ith, int nth); IQK_API bool iqk_indexer_topk(struct ggml_tensor * dst, void * work_buffer, barrier_t barrier, void * barrier_data, int ith, int nth); +IQK_API bool iqk_indexer_topk_supported(const struct ggml_tensor * dst, int nthread); IQK_API size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor * dst, int nthread); #ifdef __cplusplus From 48e77a6e5105c8daf1400a2ad2f5a366de6dd635 Mon Sep 17 00:00:00 2001 From: Joel Farthing <262452229+joelfarthing@users.noreply.github.com> Date: Tue, 21 Jul 2026 09:00:42 -0500 Subject: [PATCH 2/4] indexer_topk: drop CPU capability predicate --- ggml/src/ggml-backend.cpp | 23 +---- ggml/src/iqk/iqk_mul_mat.cpp | 166 +---------------------------------- ggml/src/iqk/iqk_mul_mat.h | 1 - 3 files changed, 4 insertions(+), 186 deletions(-) diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp index 1ebf9f5418..3d065d7e6e 100644 --- a/ggml/src/ggml-backend.cpp +++ b/ggml/src/ggml-backend.cpp @@ -3,9 +3,6 @@ #include "ggml-impl.h" #include "ggml-rpc.h" #include "ggml-moe-prefetch.h" -#ifdef GGML_USE_IQK_MULMAT -#include "iqk/iqk_mul_mat.h" -#endif #include #include @@ -899,11 +896,6 @@ GGML_CALL static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t } GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) { -#ifdef GGML_USE_IQK_MULMAT - struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *) backend->context; -#else - GGML_UNUSED(backend); -#endif switch (op->op) { case GGML_OP_CPY: return @@ -914,15 +906,11 @@ GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const case GGML_OP_MUL_MAT: return true; //return op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == ggml_internal_get_type_traits(op->src[0]->type).vec_dot_type; - case GGML_OP_INDEXER_TOPK: -#ifdef GGML_USE_IQK_MULMAT - return iqk_indexer_topk_supported(op, cpu_ctx->n_threads); -#else - return false; -#endif default: return true; } + + GGML_UNUSED(backend); } GGML_CALL static bool ggml_backend_cpu_supports_buft(ggml_backend_t backend, ggml_backend_buffer_type_t buft) { @@ -1743,12 +1731,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg const int node_backend_id = tensor_backend_id(node); - // GGML_ASSERT (not assert) so an op that no backend supports fails as a defined - // abort even under NDEBUG, rather than falling through to sched->backends[-1] (UB). - // Reachable only in a build where the sole backend's supports_op rejects a node - // (e.g. INDEXER_TOPK on a CPU-only, non-IQK build); such configs already could not - // run the op, so this preserves base's diagnosable failure instead of a silent crash. - GGML_ASSERT(node_backend_id != -1 && "op has no supporting backend"); + assert(node_backend_id != -1); // all nodes should be assigned by now // check if we should start a new split based on the sources of the current node bool need_new_split = false; diff --git a/ggml/src/iqk/iqk_mul_mat.cpp b/ggml/src/iqk/iqk_mul_mat.cpp index 915ea1c7a5..5158c6c5a6 100644 --- a/ggml/src/iqk/iqk_mul_mat.cpp +++ b/ggml/src/iqk/iqk_mul_mat.cpp @@ -15,8 +15,6 @@ #include #include #include -#include -#include #include "ggml-impl.h" #include "ggml-quants.h" @@ -1739,157 +1737,6 @@ bool iqk_fused_delta_net(int head_dim, int n_heads, int gqa_ratio, int repeat_ty } namespace { -bool iqk_checked_mul_size(size_t a, size_t b, size_t & result) { - if (a != 0 && b > std::numeric_limits::max()/a) { - return false; - } - result = a*b; - return true; -} - -bool iqk_checked_add_size(size_t a, size_t b, size_t & result) { - if (b > std::numeric_limits::max() - a) { - return false; - } - result = a + b; - return true; -} - -bool iqk_plain_layout_supported(const ggml_tensor * tensor, ggml_type type, int used_dims) { - if (tensor == nullptr || tensor->type != type || used_dims < 1 || used_dims > GGML_MAX_DIMS) { - return false; - } - const size_t element_size = ggml_type_size(type); - if (tensor->nb[0] != element_size || tensor->view_offs % element_size != 0 || - (tensor->data != nullptr && (uintptr_t) tensor->data % element_size != 0)) { - return false; - } - size_t extent = element_size; - for (int i = 0; i < used_dims; ++i) { - if (tensor->ne[i] <= 0 || (i > 0 && tensor->nb[i] < extent)) { - return false; - } - size_t offset = 0; - if (!iqk_checked_mul_size((size_t) (tensor->ne[i] - 1), tensor->nb[i], offset) || - !iqk_checked_add_size(offset, extent, extent)) { - return false; - } - } - size_t end = 0; - return iqk_checked_add_size(tensor->view_offs, extent, end); -} - -bool iqk_k_layout_supported(const ggml_tensor * k) { - if (k == nullptr || k->type < 0 || k->type >= GGML_TYPE_COUNT) { - return false; - } - const int64_t block_size = ggml_blck_size(k->type); - if (block_size <= 0 || k->ne[0] <= 0 || k->ne[0] % block_size != 0) { - return false; - } - const size_t type_size = ggml_type_size(k->type); - size_t row_size = 0; - size_t row_offset = 0; - size_t span = 0; - size_t end = 0; - return type_size > 0 && k->nb[0] == type_size && - k->view_offs % type_size == 0 && - (k->data == nullptr || (uintptr_t) k->data % alignof(uint16_t) == 0) && - iqk_checked_mul_size((size_t) (k->ne[0]/block_size), type_size, row_size) && - k->nb[1] >= row_size && - iqk_checked_mul_size((size_t) (k->ne[1] - 1), k->nb[1], row_offset) && - iqk_checked_add_size(row_offset, row_size, span) && - iqk_checked_add_size(k->view_offs, span, end); -} - -bool iqk_indexer_topk_contract_supported(const ggml_tensor * dst, int nthread) { - if (dst == nullptr || dst->op != GGML_OP_INDEXER_TOPK || nthread <= 0 || - ggml_unary_op(dst->op_params[0]) != GGML_UNARY_OP_RELU) { - return false; - } - const ggml_tensor * k = dst->src[0]; - const ggml_tensor * q = dst->src[1]; - const ggml_tensor * w = dst->src[2]; - const ggml_tensor * m = dst->src[3]; - if (k == nullptr || q == nullptr || w == nullptr || m == nullptr) { - return false; - } - - const int64_t d = k->ne[0]; - const int64_t n_kv = k->ne[1]; - const int64_t n_head = q->ne[1]; - const int64_t n_tokens = q->ne[2]; - const int64_t n_top_k = dst->ne[0]; - if (d <= 0 || n_kv <= 0 || n_head <= 0 || n_tokens <= 0 || n_top_k <= 0 || - d > INT_MAX || n_kv > INT_MAX || n_head > INT_MAX || n_tokens > INT_MAX || - n_top_k > INT_MAX || n_top_k >= n_kv || - k->ne[2] != 1 || k->ne[3] != 1 || - q->ne[0] != d || q->ne[3] != 1 || - w->ne[0] != n_head || w->ne[1] != n_tokens || w->ne[2] != 1 || w->ne[3] != 1 || - m->ne[0] != n_kv || m->ne[1] != n_tokens || m->ne[2] != 1 || m->ne[3] != 1 || - dst->ne[1] != n_tokens || dst->ne[2] != 1 || dst->ne[3] != 1 || - !iqk_k_layout_supported(k) || - !iqk_plain_layout_supported(q, GGML_TYPE_F32, 3) || - !iqk_plain_layout_supported(w, GGML_TYPE_F32, 2) || - !iqk_plain_layout_supported(dst, GGML_TYPE_I32, 2) || - (m->type != GGML_TYPE_F32 && m->type != GGML_TYPE_F16) || - !iqk_plain_layout_supported(m, m->type, 2)) { - return false; - } - - const auto tt = ggml_internal_get_type_traits(k->type); - ggml_type q_type = q->type; - size_t quantize_size = 0; - if (tt.is_quantized && tt.vec_dot_type != q->type) { - const auto ttq = ggml_internal_get_type_traits(tt.vec_dot_type); - if (ttq.from_float == nullptr || q->nb[1] != (size_t) d*sizeof(float)) { - return false; - } - q_type = tt.vec_dot_type; - if (d % ggml_blck_size(q_type) != 0) { - return false; - } - const size_t row_size_q = ggml_row_size(q_type, d); - if (!iqk_checked_mul_size(row_size_q, (size_t) n_head, quantize_size)) { - return false; - } - } - - MulMat mm; - if (!MulMat::prepare(int(k->type), int(q_type), int(d), mm, int(n_head)) || - (n_tokens < nthread && n_kv % 32 != 0)) { - return false; - } - - size_t kq_size = 0; - size_t score_size = 0; - size_t sorted_size = 0; - size_t work_size = quantize_size; - if (!iqk_checked_mul_size((size_t) n_kv, (size_t) n_head, kq_size) || - !iqk_checked_mul_size(kq_size, sizeof(float), kq_size) || - !iqk_checked_mul_size((size_t) n_kv, sizeof(float), score_size) || - !iqk_checked_mul_size((size_t) n_kv, sizeof(int32_t), sorted_size)) { - return false; - } - if (n_tokens < nthread && quantize_size != 0 && - !iqk_checked_mul_size(quantize_size, (size_t) n_tokens, work_size)) { - return false; - } - if (!iqk_checked_add_size(work_size, kq_size, work_size) || - !iqk_checked_add_size(work_size, score_size, work_size) || - !iqk_checked_add_size(work_size, sorted_size, work_size)) { - return false; - } - if (n_tokens >= nthread) { - if (work_size > std::numeric_limits::max() - 127) { - return false; - } - work_size = GGML_PAD(work_size, 128); - return iqk_checked_mul_size(work_size, (size_t) nthread, work_size); - } - return true; -} - size_t iqk_idx_topk_work_wbs_per_thread(const struct ggml_tensor * dst, int nth) { auto k = dst->src[0]; auto q = dst->src[1]; @@ -1916,14 +1763,7 @@ inline void iqk_f16_to_f32(int n, const ggml_fp16_t * x, float * y) { } } -bool iqk_indexer_topk_supported(const struct ggml_tensor * dst, int nthread) { - return iqk_indexer_topk_contract_supported(dst, nthread); -} - size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor * dst, int nthread) { - if (!iqk_indexer_topk_supported(dst, nthread)) { - return 0; - } auto k = dst->src[0]; auto q = dst->src[1]; if (q->ne[2] >= nthread) { @@ -1942,7 +1782,7 @@ size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor * dst, int nthread } bool iqk_indexer_topk(struct ggml_tensor * dst, void * work_buffer, barrier_t barrier, void * barrier_data, int ith, int nth) { - if (!iqk_indexer_topk_supported(dst, nth)) { + if (dst->op != GGML_OP_INDEXER_TOPK) { return false; } auto op = ggml_unary_op(dst->op_params[0]); @@ -2135,10 +1975,6 @@ bool iqk_indexer_topk(struct ggml_tensor *, void *, barrier_t, void *, int, int) return false; } -bool iqk_indexer_topk_supported(const struct ggml_tensor *, int) { - return false; -} - size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor *, int) { return 0; } diff --git a/ggml/src/iqk/iqk_mul_mat.h b/ggml/src/iqk/iqk_mul_mat.h index 4a9c9456a8..aae2eb197c 100644 --- a/ggml/src/iqk/iqk_mul_mat.h +++ b/ggml/src/iqk/iqk_mul_mat.h @@ -79,7 +79,6 @@ IQK_API bool iqk_fused_delta_net(int head_dim, int n_heads, int gqa_ratio, int r const float * state_in, float * out_data, float * state_out, float * saved_steps, int state_step_stride, int ith, int nth); IQK_API bool iqk_indexer_topk(struct ggml_tensor * dst, void * work_buffer, barrier_t barrier, void * barrier_data, int ith, int nth); -IQK_API bool iqk_indexer_topk_supported(const struct ggml_tensor * dst, int nthread); IQK_API size_t iqk_idx_topk_work_buffer_size(const struct ggml_tensor * dst, int nthread); #ifdef __cplusplus From 02aec3be42264f6e1bfa29f726ceba668b87d250 Mon Sep 17 00:00:00 2001 From: Joel Farthing <262452229+joelfarthing@users.noreply.github.com> Date: Tue, 21 Jul 2026 10:11:24 -0500 Subject: [PATCH 3/4] indexer_topk: fall back when unsupported --- ggml/src/ggml-backend.cpp | 6 ++++++ src/graphs/build_deepseek2.cpp | 6 ++++-- src/graphs/build_openpangu.cpp | 14 ++++++++------ src/llama-build-context.cpp | 9 +++++++++ src/llama-build-context.h | 2 ++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp index 3d065d7e6e..315c7e310a 100644 --- a/ggml/src/ggml-backend.cpp +++ b/ggml/src/ggml-backend.cpp @@ -906,6 +906,12 @@ GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const case GGML_OP_MUL_MAT: return true; //return op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == ggml_internal_get_type_traits(op->src[0]->type).vec_dot_type; + case GGML_OP_INDEXER_TOPK: +#ifdef GGML_USE_IQK_MULMAT + return true; +#else + return false; +#endif default: return true; } diff --git a/src/graphs/build_deepseek2.cpp b/src/graphs/build_deepseek2.cpp index a8a300d227..5daf578099 100644 --- a/src/graphs/build_deepseek2.cpp +++ b/src/graphs/build_deepseek2.cpp @@ -497,8 +497,10 @@ ggml_tensor * llm_build_context::build_deepseek2_dsa_indexer( ggml_build_forward_expand(gf, indexer_score); } auto topk = ggml_indexer_topk(ctx0, indexer_k_b, indexer_q, indexer_weights, indexer_score, GGML_UNARY_OP_RELU, n_top_k); - ggml_build_forward_expand(gf, topk); - return topk; + if (supports_op(topk)) { + ggml_build_forward_expand(gf, topk); + return topk; + } } if (indexer_q->ne[2] <= 8) { diff --git a/src/graphs/build_openpangu.cpp b/src/graphs/build_openpangu.cpp index b0d9ca2008..c65a06ec7a 100644 --- a/src/graphs/build_openpangu.cpp +++ b/src/graphs/build_openpangu.cpp @@ -375,16 +375,18 @@ ggml_tensor * llm_build_context::build_openpangu_attention( !dsa_gather_allowed && openpangu_att_score_should_chunk(n_kv, hparams.param_sink_number, n_head, n_tokens, OPENPANGU_ATT_SCORE_CHUNK, OPENPANGU_ATT_FULL_KQ_MAX_MIB); + ggml_tensor * fused_sel_idx = nullptr; if (lctx.cparams.fused_idx_topk) { - // Fused indexer top-k (CPU-only op): one op computes sum_g w * relu(q.k) + causal - // mask -> top-k without materializing the [n_kv, n_ihead, T] score tensor, so no - // score chunking is needed. CUDA backends do not implement GGML_OP_INDEXER_TOPK; - // the scheduler runs it on CPU and copies the operands across the backend boundary. // The op reads the mask row-strided, so the raw view suffices. ggml_tensor * idx_mask = ggml_view_2d(ctx0, KQ_mask, n_kv, n_tokens, KQ_mask->nb[1], 0); - sel_idx = ggml_indexer_topk(ctx0, k_all_idx, q_idx, w_idx, idx_mask, - GGML_UNARY_OP_RELU, (int) topk); // [topk, T] i32 + fused_sel_idx = ggml_indexer_topk(ctx0, k_all_idx, q_idx, w_idx, idx_mask, + GGML_UNARY_OP_RELU, (int) topk); // [topk, T] i32 + } + if (fused_sel_idx && supports_op(fused_sel_idx)) { + // One op computes sum_g w * relu(q.k) + causal mask -> top-k without + // materializing the [n_kv, n_ihead, T] score tensor. + sel_idx = fused_sel_idx; if (il == 0) ggml_set_name(sel_idx, "opg0_idx_sel"); dsa_gather_engaged = dsa_gather_allowed; if (dsa_gather_engaged) { diff --git a/src/llama-build-context.cpp b/src/llama-build-context.cpp index 117ea2464e..e182b1ae7e 100644 --- a/src/llama-build-context.cpp +++ b/src/llama-build-context.cpp @@ -133,6 +133,15 @@ void llm_build_context::free() { } } +bool llm_build_context::supports_op(const ggml_tensor * op) const { + for (ggml_backend_t backend : lctx.backends) { + if (ggml_backend_supports_op(backend, op)) { + return true; + } + } + return false; +} + ggml_cgraph * llm_build_context::build_k_shift() { struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, model.max_nodes(n_tokens), false); diff --git a/src/llama-build-context.h b/src/llama-build-context.h index 854b7b5a85..450e80bf31 100644 --- a/src/llama-build-context.h +++ b/src/llama-build-context.h @@ -119,6 +119,8 @@ struct llm_build_context { void free(); + bool supports_op(const ggml_tensor * op) const; + ggml_cgraph * build_k_shift(); ggml_cgraph * build_s_copy(); From 57617ae0266de549866c014c6b7fcb4ab47c5fac Mon Sep 17 00:00:00 2001 From: Joel Farthing <262452229+joelfarthing@users.noreply.github.com> Date: Wed, 22 Jul 2026 06:57:06 -0500 Subject: [PATCH 4/4] cuda: drop speculative INDEXER_TOPK graph type matching --- ggml/src/ggml-cuda.cu | 14 -------------- ggml/src/ggml-cuda/graph.cuh | 1 - 2 files changed, 15 deletions(-) diff --git a/ggml/src/ggml-cuda.cu b/ggml/src/ggml-cuda.cu index c0844a2c16..7e66572e3f 100644 --- a/ggml/src/ggml-cuda.cu +++ b/ggml/src/ggml-cuda.cu @@ -4435,7 +4435,6 @@ static void set_ggml_graph_node_properties(ggml_tensor * node, ggml_graph_node_p } for (int i = 0; i < GGML_MAX_SRC; i++) { graph_node_properties->src_address[i] = node->src[i] ? node->src[i]->data : nullptr; - graph_node_properties->src_type[i] = node->src[i] ? node->src[i]->type : GGML_TYPE_COUNT; } memcpy(graph_node_properties->op_params, node->op_params, GGML_MAX_OP_PARAMS); } @@ -4475,19 +4474,6 @@ static bool ggml_graph_node_has_matching_properties(ggml_tensor * node, ggml_gra return false; } - // INDEXER_TOPK dispatches a source-type-specialized kernel (dense F16 vs quantized cache, - // and an F32 vs F16 mask variant). Source addresses alone do not identify the captured - // kernel, so a reused graph whose sources were reallocated at the same addresses with a - // different type would replay the wrong kernel. Force re-capture when a source type changes. - if (node->op == GGML_OP_INDEXER_TOPK) { - for (int i = 0; i < GGML_MAX_SRC; i++) { - const ggml_type src_type = node->src[i] ? node->src[i]->type : GGML_TYPE_COUNT; - if (src_type != graph_node_properties->src_type[i]) { - return false; - } - } - } - return true; } diff --git a/ggml/src/ggml-cuda/graph.cuh b/ggml/src/ggml-cuda/graph.cuh index fd3c0fccd6..1b3a30ff02 100644 --- a/ggml/src/ggml-cuda/graph.cuh +++ b/ggml/src/ggml-cuda/graph.cuh @@ -8,7 +8,6 @@ struct ggml_graph_node_properties { int64_t ne[GGML_MAX_DIMS]; size_t nb[GGML_MAX_DIMS]; void * src_address[GGML_MAX_SRC]; - ggml_type src_type[GGML_MAX_SRC]; int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)]; };