Commit 2477ee0b6 for llama.cpp
commit 2477ee0b614105f2a5198a703ce7469fed477a20
Author: gianni-cor <gianfranco.cordella@tether.io>
Date: Sat Nov 28 13:04:01 2026 +0100
ggml : add CONCAT backward pass (#32151)
Each input's gradient is a view of the output gradient at its offset
along dim, made contiguous.
Mark a and b as params so grad mode checks the op, with grad_nmax raised
to 40000 to keep the default shapes. The output is weighted by random
values in [1, 2], since with a plain sum every gradient is 1 and a wrong
offset would still pass. Transposed-b cases are not grad checked, they
only cover the forward.
Signed-off-by: gianni-cor <gianfranco.cordella@tether.io>
diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index 912e8653a..7da6637bd 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -7404,6 +7404,23 @@ static void ggml_compute_backward(
} //break;
}
} break;
+ case GGML_OP_CONCAT: {
+ const int32_t dim = ggml_get_op_params_i32(tensor, 0);
+ if (src0_needs_grads) {
+ size_t offset = 0;
+ struct ggml_tensor * grad_a = ggml_view_4d(ctx, grad,
+ src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
+ grad->nb[1], grad->nb[2], grad->nb[3], offset);
+ ggml_add_or_set(ctx, cgraph, isrc0, ggml_cont(ctx, grad_a));
+ }
+ if (src1_needs_grads) {
+ size_t offset = src0->ne[dim] * grad->nb[dim];
+ struct ggml_tensor * grad_b = ggml_view_4d(ctx, grad,
+ src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3],
+ grad->nb[1], grad->nb[2], grad->nb[3], offset);
+ ggml_add_or_set(ctx, cgraph, isrc1, ggml_cont(ctx, grad_b));
+ }
+ } break;
case GGML_OP_NONE: {
// noop
} break;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index c9ade8441..84c6cebd4 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -8442,6 +8442,31 @@ struct test_concat : public test_case {
return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
}
+ int64_t grad_nmax() override {
+ return 40000;
+ }
+
+ std::string op_desc(ggml_tensor * t) override {
+ GGML_UNUSED(t);
+ return "CONCAT";
+ }
+
+ // the weighted loss is linear in a and b, so a large eps is exact and keeps f32 rounding small
+ float grad_eps() override {
+ return 1.0f;
+ }
+
+ void initialize_tensors(ggml_context * ctx) override {
+ for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
+ // keep weights away from 0 because a near-zero gradient makes the relative error of the grad check blow up
+ if (strcmp(t->name, "w") == 0) {
+ init_tensor_uniform(t, 1.0f, 2.0f);
+ } else {
+ init_tensor_uniform(t);
+ }
+ }
+ }
+
test_concat(ggml_type type = GGML_TYPE_F32,
std::array<int64_t, 4> ne_a = {10, 5, 5, 5},
int64_t ne_b_d = 5,
@@ -8456,6 +8481,7 @@ struct test_concat : public test_case {
auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
a = ggml_new_tensor(ctx, type, 4, ne.data());
ggml_set_name(a, "a");
+ ggml_set_param(a);
a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
ggml_set_name(a, "view_of_a");
@@ -8463,18 +8489,24 @@ struct test_concat : public test_case {
auto ne = ne_a; ne[2] *= 2; ne[3] *= 4;
a = ggml_new_tensor(ctx, type, 4, ne.data());
ggml_set_name(a, "a");
+ ggml_set_param(a);
a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
ggml_set_name(a, "view_of_a");
} else {
a = ggml_new_tensor(ctx, type, 4, ne_a.data());
ggml_set_name(a, "a");
+ // the transposed b cases target the forward kernel, a grad check of a there takes minutes
+ if (!(v & 16)) {
+ ggml_set_param(a);
+ }
}
ggml_tensor * b;
if (v & 2) {
auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
b = ggml_new_tensor(ctx, type, 4, ne.data());
ggml_set_name(b, "b");
+ ggml_set_param(b);
b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0);
ggml_set_name(b, "view_of_b");
@@ -8482,6 +8514,7 @@ struct test_concat : public test_case {
auto ne = ne_b; ne[2] *= 3; ne[3] *= 2;
b = ggml_new_tensor(ctx, type, 4, ne.data());
ggml_set_name(b, "b");
+ ggml_set_param(b);
b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0);
ggml_set_name(b, "view_of_b");
@@ -8495,11 +8528,21 @@ struct test_concat : public test_case {
} else {
b = ggml_new_tensor(ctx, type, 4, ne_b.data());
ggml_set_name(b, "b");
+ ggml_set_param(b);
}
ggml_tensor * out = ggml_concat(ctx, a, b, dim);
ggml_set_name(out, "out");
+ if (mode == MODE_GRAD) {
+ // with sum(out) as the loss every element has the same gradient, so a wrong view offset in the
+ // backward pass would go unnoticed; random weights make the gradient differ per element
+ ggml_tensor * w = ggml_new_tensor(ctx, type, 4, out->ne);
+ ggml_set_name(w, "w");
+ out = ggml_mul(ctx, out, w);
+ ggml_set_name(out, "out_weighted");
+ }
+
return out;
}
};