Commit 3ab385146 for llama.cpp

commit 3ab385146cad1a374dadc40e042dcc50edceb046
Author: fox5611 <66219952+fox5611@users.noreply.github.com>
Date:   Fri Nov 27 06:32:42 2026 -0800

    CUDA: fix ADD_ID launch for more than 65535 tokens (#33162)

    The grid's y dimension came directly from the token count, so ADD_ID
    launches with more than 65535 tokens failed with "invalid configuration
    argument". NVIDIA and HIP are equally affected, since gridDim.y is
    capped at 65535 on both.

    Clamp the grid and stride over ne2 in the kernel, matching convert.cu
    and pad.cu. Add a test case at 70000 tokens; the existing cases top out
    at 129.

    Assisted-by: Claude Opus 5

diff --git a/ggml/src/ggml-cuda/add-id.cu b/ggml/src/ggml-cuda/add-id.cu
index 8d9cf692b..f9fa73dec 100644
--- a/ggml/src/ggml-cuda/add-id.cu
+++ b/ggml/src/ggml-cuda/add-id.cu
@@ -2,26 +2,28 @@

 static __global__ void add_id_kernel(
         const float * src0, const float * src1, const int32_t * src2, float * dst,
-        int64_t ne0, int64_t ne1,
+        int64_t ne0, int64_t ne1, int64_t ne2,
         size_t nb01, size_t nb02,
         size_t nb11,
         size_t nb21
     ) {

     const int64_t i1 = blockIdx.x;
-    const int64_t i2 = blockIdx.y;

-    const int i11 = *(const int32_t *) ((const char *) src2 + i1*sizeof(int32_t) + i2*nb21);
+    // gridDim.y is capped at 65535, blocks stride over larger ne2
+    for (int64_t i2 = blockIdx.y; i2 < ne2; i2 += gridDim.y) {
+        const int i11 = *(const int32_t *) ((const char *) src2 + i1*sizeof(int32_t) + i2*nb21);

-    const size_t nb1 = ne0 * sizeof(float);
-    const size_t nb2 = ne1 * nb1;
+        const size_t nb1 = ne0 * sizeof(float);
+        const size_t nb2 = ne1 * nb1;

-    float * dst_row = (float *)((char *)dst + i1*nb1 + i2*nb2);
-    const float * src0_row = (const float *)((const char *)src0 +  i1*nb01 + i2*nb02);
-    const float * src1_row = (const float *)((const char *)src1 + i11*nb11);
+        float * dst_row = (float *)((char *)dst + i1*nb1 + i2*nb2);
+        const float * src0_row = (const float *)((const char *)src0 +  i1*nb01 + i2*nb02);
+        const float * src1_row = (const float *)((const char *)src1 + i11*nb11);

-    for (int64_t i0 = threadIdx.x; i0 < ne0; i0 += blockDim.x) {
-        dst_row[i0] = src0_row[i0] + src1_row[i0];
+        for (int64_t i0 = threadIdx.x; i0 < ne0; i0 += blockDim.x) {
+            dst_row[i0] = src0_row[i0] + src1_row[i0];
+        }
     }
 }

@@ -47,10 +49,10 @@ void ggml_cuda_op_add_id(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
     float * dst_d = (float *)dst->data;

     int threads = std::min((int)ne00, 768); // cols
-    dim3 blocks(ne01, ne02); // n_experts_used, n_tokens
+    dim3 blocks(ne01, std::min(ne02, (int64_t)65535)); // n_experts_used, n_tokens
     add_id_kernel<<<blocks, threads, 0, ctx.stream()>>>(
         src0_d, src1_d, src2_d, dst_d,
-        ne0, ne1,
+        ne0, ne1, ne2,
         nb01, nb02,
         nb11,
         nb21
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 90f9976f4..383eca0cd 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -14167,6 +14167,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
         }
     }

+    // add_id with more than 65535 tokens, exercises the gridDim.y limit
+    test_cases.emplace_back(new test_add_id(GGML_TYPE_F32, GGML_TYPE_F32, 32, 4, 2, 70000));
+
     for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_F32}) {
         test_cases.emplace_back(new test_sqr       (type));
         test_cases.emplace_back(new test_sqrt      (type));