Commit 8c447d583 for llama.cpp

commit 8c447d583f5705723106cd989ea4c2e1019214f4
Author: Aman Gupta <amangupta052@gmail.com>
Date:   Thu Dec 3 12:56:40 2026 +0800

    ggml : add internal ops (#33000)

    A custom op without a function (fun == NULL) is an internal op: userdata
    points to a null-terminated string that identifies an op implemented in
    the backends. Such an op has no ggml_ function and is not part of the API.

    - the CPU backend finds the op by its id, supports_op is false for an id
      that it does not know
    - the params of the op are in op_params, after the custom op params
      (GGML_INTERNAL_OP_PARAMS)
    - ggml_op_desc() returns the id
    - ggml_internal_saxpy is the first op, with a test-backend-ops case

diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h
index 5ddfac509..a223d69f5 100644
--- a/ggml/include/ggml.h
+++ b/ggml/include/ggml.h
@@ -2656,6 +2656,17 @@ extern "C" {

     typedef void (*ggml_custom_op_t)(struct ggml_tensor * dst, int ith, int nth, void * userdata);

+    // Internal ops
+    //
+    // With fun == NULL the node is an internal op: userdata must point to a null-terminated string that identifies
+    // an op implemented in the backends (e.g. "ggml_internal_saxpy"). The string must outlive the graph.
+    //
+    // Internal ops work like the regular ops: the CPU backend implements all of them, the other backends report
+    // the ones they implement in supports_op. They are not part of the API and can change or be removed at any time.
+
+#define GGML_INTERNAL_OP_PARAMS 8
+    // the params of an internal op are stored in the op_params of the node, starting at this index
+
     GGML_API struct ggml_tensor * ggml_custom_4d(
             struct ggml_context * ctx,
             enum ggml_type        type,
diff --git a/ggml/src/ggml-cpu/ggml-cpu-impl.h b/ggml/src/ggml-cpu/ggml-cpu-impl.h
index 590215b8c..35aa0c3cf 100644
--- a/ggml/src/ggml-cpu/ggml-cpu-impl.h
+++ b/ggml/src/ggml-cpu/ggml-cpu-impl.h
@@ -540,6 +540,9 @@ void ggml_barrier(struct ggml_threadpool * tp);
 void ggml_threadpool_chunk_set(struct ggml_threadpool * tp, int value);
 int  ggml_threadpool_chunk_add(struct ggml_threadpool * tp, int value);

+// the internal ops of the CPU backend (see ggml_custom_4d)
+bool ggml_cpu_internal_op_supported(const char * id);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/ggml/src/ggml-cpu/ggml-cpu.cpp b/ggml/src/ggml-cpu/ggml-cpu.cpp
index e40195b0e..c251c6b7e 100644
--- a/ggml/src/ggml-cpu/ggml-cpu.cpp
+++ b/ggml/src/ggml-cpu/ggml-cpu.cpp
@@ -444,6 +444,10 @@ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const st
         case GGML_OP_FLASH_ATTN_EXT:
             // Q is read as F32 (a BF16 Q is not supported yet)
             return op->src[0]->type == GGML_TYPE_F32 && (!ggml_flash_attn_ext_has_bias(op) || ggml_flash_attn_ext_bias_is_indexed(op));
+        case GGML_OP_CUSTOM: {
+            const char * id = ggml_internal_op_id(op);
+            return id == NULL || ggml_cpu_internal_op_supported(id);
+        }
         case GGML_OP_CPY:
         case GGML_OP_SET_ROWS:
             return
diff --git a/ggml/src/ggml-cpu/ops.cpp b/ggml/src/ggml-cpu/ops.cpp
index e8a2cfd86..d960e94c4 100644
--- a/ggml/src/ggml-cpu/ops.cpp
+++ b/ggml/src/ggml-cpu/ops.cpp
@@ -12214,6 +12214,56 @@ void ggml_compute_forward_map_custom3(
     p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
 }

+// internal ops (custom ops without a function, see ggml_custom_4d)
+
+// dst = a*x + y
+static void ggml_compute_forward_internal_saxpy(
+        const ggml_compute_params * params,
+        ggml_tensor * dst) {
+    const ggml_tensor * x = dst->src[0];
+    const ggml_tensor * y = dst->src[1];
+
+    GGML_ASSERT(x->type == GGML_TYPE_F32 && y->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
+    GGML_ASSERT(ggml_are_same_shape(x, dst) && ggml_are_same_shape(y, dst));
+    GGML_ASSERT(ggml_is_contiguous(x) && ggml_is_contiguous(y) && ggml_is_contiguous(dst));
+
+    const float a = ggml_get_op_params_f32(dst, GGML_INTERNAL_OP_PARAMS);
+
+    const float * x_d   = (const float *) x->data;
+    const float * y_d   = (const float *) y->data;
+    float       * dst_d = (float *) dst->data;
+
+    const int64_t n  = ggml_nelements(dst);
+    const int64_t i0 = n*params->ith/params->nth;
+    const int64_t i1 = n*(params->ith + 1)/params->nth;
+
+    for (int64_t i = i0; i < i1; i++) {
+        dst_d[i] = a*x_d[i] + y_d[i];
+    }
+}
+
+typedef void (*ggml_internal_op_fun)(const ggml_compute_params * params, ggml_tensor * dst);
+
+static const struct {
+    const char         * id;
+    ggml_internal_op_fun fun;
+} ggml_internal_ops[] = {
+    { "ggml_internal_saxpy", ggml_compute_forward_internal_saxpy },
+};
+
+static ggml_internal_op_fun ggml_internal_op_find(const char * id) {
+    for (const auto & op : ggml_internal_ops) {
+        if (strcmp(op.id, id) == 0) {
+            return op.fun;
+        }
+    }
+    return nullptr;
+}
+
+bool ggml_cpu_internal_op_supported(const char * id) {
+    return ggml_internal_op_find(id) != nullptr;
+}
+
 // ggml_compute_forward_custom

 void ggml_compute_forward_custom(
@@ -12223,6 +12273,16 @@ void ggml_compute_forward_custom(
     struct ggml_custom_op_params p;
     memcpy(&p, dst->op_params, sizeof(p));

+    if (p.fun == NULL) {
+        const char * id = (const char *) p.userdata;
+        const ggml_internal_op_fun fun = ggml_internal_op_find(id);
+        if (fun == nullptr) {
+            GGML_ABORT("%s: unknown internal op '%s'", __func__, id);
+        }
+        fun(params, dst);
+        return;
+    }
+
     p.fun(dst, params->ith, params->nth, p.userdata);
 }

diff --git a/ggml/src/ggml-impl.h b/ggml/src/ggml-impl.h
index d7e7ed807..eff2d5779 100644
--- a/ggml/src/ggml-impl.h
+++ b/ggml/src/ggml-impl.h
@@ -239,6 +239,16 @@ struct ggml_custom_op_params {
     void           * userdata;
 };

+// the id of an internal op (see ggml_custom_4d), NULL for any other node
+static inline const char * ggml_internal_op_id(const struct ggml_tensor * tensor) {
+    if (tensor->op != GGML_OP_CUSTOM) {
+        return NULL;
+    }
+    struct ggml_custom_op_params p;
+    memcpy(&p, tensor->op_params, sizeof(p));
+    return p.fun == NULL ? (const char *) p.userdata : NULL;
+}
+
 // bitset

 typedef uint32_t ggml_bitset_t;
diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index 01bb6d069..dff1f3197 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -1413,6 +1413,10 @@ const char * ggml_op_desc(const struct ggml_tensor * t) {
         enum ggml_glu_op gop = ggml_get_glu_op(t);
         return ggml_glu_op_name(gop);
     }
+    const char * id = ggml_internal_op_id(t);
+    if (id != NULL) {
+        return id;
+    }
     return ggml_op_name(t->op);
 }

@@ -6318,6 +6322,9 @@ struct ggml_tensor * ggml_map_custom3_inplace(
     return ggml_map_custom3_impl(ctx, a, b, c, fun, n_tasks, userdata, true);
 }

+// the params of an internal op follow the custom op params
+static_assert(sizeof(struct ggml_custom_op_params) <= GGML_INTERNAL_OP_PARAMS*sizeof(int32_t), "GGML_INTERNAL_OP_PARAMS is too small");
+
 struct ggml_tensor * ggml_custom_4d(
         struct ggml_context * ctx,
         enum ggml_type        type,
@@ -6332,6 +6339,7 @@ struct ggml_tensor * ggml_custom_4d(
         void                * userdata) {

     GGML_ASSERT(n_args < GGML_MAX_SRC);
+    GGML_ASSERT(fun != NULL || userdata != NULL); // an internal op needs its id

     struct ggml_tensor * result = ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);

@@ -6360,6 +6368,7 @@ struct ggml_tensor * ggml_custom_inplace(
         void                * userdata) {

     GGML_ASSERT(n_args < GGML_MAX_SRC - 1);
+    GGML_ASSERT(fun != NULL || userdata != NULL); // an internal op needs its id

     struct ggml_tensor * result = ggml_view_tensor(ctx, a);

diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 82baacb5b..247a0e9ad 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -11219,6 +11219,34 @@ struct test_sleep : public test_case {
     }
 };

+// GGML_OP_CUSTOM: an internal op (see ggml_custom_4d)
+struct test_internal_saxpy : public test_case {
+    const std::array<int64_t, 4> ne;
+    const float                  a;
+
+    std::string vars() override { return VARS_TO_STR2(ne, a); }
+
+    test_internal_saxpy(std::array<int64_t, 4> ne = { 10, 5, 4, 3 }, float a = 2.0f)
+        : ne(ne), a(a) {}
+
+    ggml_tensor * build_graph(ggml_context * ctx) override {
+        ggml_tensor * x = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne[0], ne[1], ne[2], ne[3]);
+        ggml_set_name(x, "x");
+
+        ggml_tensor * y = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne[0], ne[1], ne[2], ne[3]);
+        ggml_set_name(y, "y");
+
+        // no function: the backends find the op by its id, the params follow the custom op params
+        ggml_tensor * args[] = { x, y };
+        ggml_tensor * out = ggml_custom_4d(ctx, GGML_TYPE_F32, ne[0], ne[1], ne[2], ne[3], args, 2, nullptr, GGML_N_TASKS_MAX, const_cast<char *>("ggml_internal_saxpy"));
+        memcpy(&out->op_params[GGML_INTERNAL_OP_PARAMS], &a, sizeof(a));
+
+        ggml_set_name(out, "out");
+
+        return out;
+    }
+};
+
 // GGML_OP_SOLVE_TRI
 struct test_solve_tri : public test_case {
     const ggml_type              type;
@@ -15050,6 +15078,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
     test_cases.emplace_back(new test_fill(2.0f, GGML_TYPE_F32, { 96, 4, 1, 1 }, 37));
     test_cases.emplace_back(new test_fill(2.0f, GGML_TYPE_F16, { 303, 7, 1, 1 }, 3));

+    test_cases.emplace_back(new test_internal_saxpy());
+    test_cases.emplace_back(new test_internal_saxpy({ 1000, 3, 1, 1 }, -0.5f));
+
     test_cases.emplace_back(new test_sleep(0));
     test_cases.emplace_back(new test_sleep(10, GGML_TYPE_F16, { 128, 4, 2, 2 }));
     test_cases.emplace_back(new test_sleep(10, GGML_TYPE_F32, { 128, 4, 2, 2 }));