Commit 3090e45a8 for llama.cpp

commit 3090e45a8675ebdefbc722f80609c75e5fa259ad
Author: Georgi Gerganov <ggerganov@gmail.com>
Date:   Tue Nov 24 16:34:08 2026 +0200

    metal : reserve the top-k scratch only for short rows (#32942)

    The bitonic top-k path is only faster for short rows. On M5 and M2 Ultra it
    wins up to 128 columns; above that the radix path is faster. Use the radix
    path for ncols > 128.

    The radix path selects in place and needs no scratch, but the buffer type
    always reserved 2*4 bytes per source element, which is 8 GiB for a
    524288 x 2048 score. Reserve the bitonic worst case at the 128-column
    threshold instead: 2*4*128*nrows, clamped to at least the output tensor size.
    This keeps the allocation bounded and consistent with the bitonic path.

    Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index e1f8610c5..c4c4f94dd 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -6265,19 +6265,39 @@ int ggml_metal_op_moe_reduce(ggml_metal_op_t ctx, int idx) {
     return n_fuse;
 }

-int ggml_metal_op_top_k(ggml_metal_op_t ctx, int idx) {
-    ggml_tensor * op = ctx->node(idx);
+static int ggml_metal_op_top_k_bitonic_ncols_thold(void) {
+    return 128;
+}
+
+bool ggml_metal_op_top_k_use_radix(const ggml_tensor * op) {
+    GGML_ASSERT(op->op == GGML_OP_TOP_K);

-    // radix-select has a fixed single-workgroup-per-row cost (~50-60us) that is only
-    // amortized for long rows, many rows, or a large k; otherwise the bitonic path wins
     const int ncols = op->src[0]->ne[0];
-    const int k     = op->ne[0];
-    const int nrows = ggml_nrows(op->src[0]);

-    const bool use_radix =
-        ncols > 2048 && (k > 64 || (nrows > 4 && ncols >= 8192));
+    return ncols > ggml_metal_op_top_k_bitonic_ncols_thold();
+}
+
+size_t ggml_metal_op_top_k_extra(const ggml_tensor * op) {
+    GGML_ASSERT(op->op == GGML_OP_TOP_K);
+
+    const int64_t nrows = ggml_nrows(op->src[0]);
+    const size_t tensor_bytes = ggml_nbytes(op);
+
+    size_t total;
+    if (ggml_metal_op_top_k_use_radix(op)) {
+        const size_t bitonic = 2*sizeof(int32_t)*ggml_metal_op_top_k_bitonic_ncols_thold()*nrows;
+        total = tensor_bytes > bitonic ? tensor_bytes : bitonic;
+    } else {
+        total = 2*sizeof(int32_t)*ggml_nelements(op->src[0]);
+    }
+
+    return total - tensor_bytes;
+}
+
+int ggml_metal_op_top_k(ggml_metal_op_t ctx, int idx) {
+    ggml_tensor * op = ctx->node(idx);

-    if (use_radix) {
+    if (ggml_metal_op_top_k_use_radix(op)) {
         ggml_metal_op_top_k_radix(ctx, idx);
     } else {
         ggml_metal_op_top_k_bitonic(ctx, idx);
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.h b/ggml/src/ggml-metal/ggml-metal-ops.h
index 7dae178d5..2330bff1e 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.h
+++ b/ggml/src/ggml-metal/ggml-metal-ops.h
@@ -49,6 +49,11 @@ size_t ggml_metal_op_flash_attn_ext_extra_rows(const struct ggml_tensor * op);

 size_t ggml_metal_op_argmax_extra(const struct ggml_tensor * op);

+// return true if the top-k op uses the radix path
+bool ggml_metal_op_top_k_use_radix(const struct ggml_tensor * op);
+
+size_t ggml_metal_op_top_k_extra(const struct ggml_tensor * op);
+
 size_t ggml_metal_op_conv_2d_extra_tmp(const struct ggml_tensor * op);

 int ggml_metal_op_concat            (ggml_metal_op_t ctx, int idx);
diff --git a/ggml/src/ggml-metal/ggml-metal.cpp b/ggml/src/ggml-metal/ggml-metal.cpp
index aca83e13d..22860653b 100644
--- a/ggml/src/ggml-metal/ggml-metal.cpp
+++ b/ggml/src/ggml-metal/ggml-metal.cpp
@@ -311,7 +311,7 @@ static size_t ggml_backend_metal_buffer_type_get_alloc_size(ggml_backend_buffer_
             } break;
         case GGML_OP_TOP_K:
             {
-                res = 2*sizeof(int32_t)*ggml_nelements(tensor->src[0]);
+                res += ggml_metal_op_top_k_extra(tensor);
             } break;
         case GGML_OP_CONV_2D:
             {