Commit c1d374661 for llama.cpp

commit c1d374661d60cfe7c5902daa29324c1c70aa4b1a
Author: Aman Gupta <amangupta052@gmail.com>
Date:   Thu Dec 3 18:17:44 2026 +0800

    CUDA: add internal ops (#33600)

diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index 21a3b8964..fbac41fdb 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -32,6 +32,7 @@
 #include "ggml-cuda/fwht.cuh"
 #include "ggml-cuda/getrows.cuh"
 #include "ggml-cuda/im2col.cuh"
+#include "ggml-cuda/internal-ops.cuh"
 #include "ggml-cuda/mmf.cuh"
 #include "ggml-cuda/mmq.cuh"
 #include "ggml-cuda/mmq-few.cuh"
@@ -2939,6 +2940,11 @@ static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct gg
         case GGML_OP_TOP_K_MASK:
             ggml_cuda_op_top_k_mask(ctx, dst);
             break;
+        case GGML_OP_CUSTOM:
+            if (!ggml_cuda_op_internal(ctx, dst)) {
+                return false;
+            }
+            break;
         default:
             return false;
     }
@@ -4670,6 +4676,8 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
             return ggml_cuda_lightning_indexer_supported(dev_ctx->device, op);
         case GGML_OP_TOP_K_MASK:
             return op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32;
+        case GGML_OP_CUSTOM:
+            return ggml_cuda_internal_op_supported(op);
         case GGML_OP_SLEEP:
 #if defined(GGML_USE_HIP) || defined(GGML_USE_MUSA)
             return false;
diff --git a/ggml/src/ggml-cuda/internal-ops.cu b/ggml/src/ggml-cuda/internal-ops.cu
new file mode 100644
index 000000000..428a4a821
--- /dev/null
+++ b/ggml/src/ggml-cuda/internal-ops.cu
@@ -0,0 +1,73 @@
+#include "internal-ops.cuh"
+
+#define CUDA_SAXPY_BLOCK_SIZE 256
+
+// dst = a*x + y
+static __global__ void saxpy_f32(const float * x, const float * y, float * dst, const float a, const int64_t n) {
+    const int64_t i = (int64_t) blockDim.x*blockIdx.x + threadIdx.x;
+    if (i >= n) {
+        return;
+    }
+    dst[i] = a*x[i] + y[i];
+}
+
+static bool ggml_cuda_internal_saxpy_supported(const ggml_tensor * op) {
+    const ggml_tensor * x = op->src[0];
+    const ggml_tensor * y = op->src[1];
+
+    return x != nullptr && y != nullptr &&
+        x->type == GGML_TYPE_F32 && y->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32 &&
+        ggml_are_same_shape(x, op) && ggml_are_same_shape(y, op) &&
+        ggml_is_contiguous(x) && ggml_is_contiguous(y) && ggml_is_contiguous(op);
+}
+
+static void ggml_cuda_internal_saxpy(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+    const ggml_tensor * x = dst->src[0];
+    const ggml_tensor * y = dst->src[1];
+
+    float a;
+    memcpy(&a, &dst->op_params[GGML_INTERNAL_OP_PARAMS], sizeof(a));
+
+    const int64_t n          = ggml_nelements(dst);
+    const int64_t num_blocks = (n + CUDA_SAXPY_BLOCK_SIZE - 1) / CUDA_SAXPY_BLOCK_SIZE;
+
+    saxpy_f32<<<num_blocks, CUDA_SAXPY_BLOCK_SIZE, 0, ctx.stream()>>>(
+        (const float *) x->data, (const float *) y->data, (float *) dst->data, a, n);
+}
+
+struct ggml_cuda_internal_op {
+    const char * id;
+    bool (*supported)(const ggml_tensor * op);
+    void (*forward)(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
+};
+
+static const ggml_cuda_internal_op ggml_cuda_internal_ops[] = {
+    { "ggml_internal_saxpy", ggml_cuda_internal_saxpy_supported, ggml_cuda_internal_saxpy },
+};
+
+static const ggml_cuda_internal_op * ggml_cuda_internal_op_find(const ggml_tensor * op) {
+    const char * id = ggml_internal_op_id(op);
+    if (id == nullptr) {
+        return nullptr;
+    }
+    for (const ggml_cuda_internal_op & iop : ggml_cuda_internal_ops) {
+        if (strcmp(iop.id, id) == 0) {
+            return &iop;
+        }
+    }
+    return nullptr;
+}
+
+bool ggml_cuda_internal_op_supported(const ggml_tensor * op) {
+    const ggml_cuda_internal_op * iop = ggml_cuda_internal_op_find(op);
+    return iop != nullptr && iop->supported(op);
+}
+
+bool ggml_cuda_op_internal(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+    const ggml_cuda_internal_op * iop = ggml_cuda_internal_op_find(dst);
+    if (iop == nullptr) {
+        return false;
+    }
+    iop->forward(ctx, dst);
+    return true;
+}
diff --git a/ggml/src/ggml-cuda/internal-ops.cuh b/ggml/src/ggml-cuda/internal-ops.cuh
new file mode 100644
index 000000000..f260b7cfd
--- /dev/null
+++ b/ggml/src/ggml-cuda/internal-ops.cuh
@@ -0,0 +1,8 @@
+#include "common.cuh"
+
+// internal ops (custom ops without a function, see ggml_custom_4d)
+
+bool ggml_cuda_internal_op_supported(const ggml_tensor * op);
+
+// false if the node is not an internal op of this backend
+bool ggml_cuda_op_internal(ggml_backend_cuda_context & ctx, ggml_tensor * dst);