Commit 111f42974 for llama.cpp
commit 111f429748689473e3c7250278cb1e45e2b44892
Author: Aman Gupta <amangupta052@gmail.com>
Date: Wed Dec 2 22:14:05 2026 +0800
CUDA: allow the short conv fusion when past is the state cell (#33504)
Since #30139 the recurrent states are read in place from the cache, so the
conv past of Inkling 2 is a view of the same state rows the store writes.
The overlap check rejected that, and the fusion never matched in decode.
A thread reads the K - 1 past values of its channel into registers before
it stores the new state at the same addresses, so the exact alias is safe
when one thread runs the whole sequence (Ts <= 8 or cur in place) and only
slot 0 is stored. Add SHORT_CONV cases with past aliasing the cell.
Fixes #33424
Assisted-by: Claude Opus 5.5
diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu
index cc32837fe..0a31c2187 100644
--- a/ggml/src/ggml-cuda/fusion.cu
+++ b/ggml/src/ggml-cuda/fusion.cu
@@ -1876,6 +1876,13 @@ static int ggml_cuda_try_fuse_short_conv(ggml_backend_cuda_context * cuda_ctx, g
// and cur at a token right before it writes dst there, so dst may be either of them in place when one thread has
// the whole sequence; any other overlap is rejected
const bool cur_in_place = cur->data == dst->data;
+ // one thread runs the whole sequence of a channel, so it reads all its inputs before it writes any output
+ const bool one_run = cur_in_place || Ts <= 8;
+ // with the recurrent states read in place from the cache, past is the cell itself: a thread reads the K - 1 past
+ // values of its channel into registers before it stores the new ones at the same addresses, so the exact alias is
+ // safe when one thread has the whole sequence
+ const bool past_is_cell = one_run && kw == 1 && past->data == cell->data &&
+ past->nb[1] == (size_t) ch_s*sizeof(float) && past->nb[2] == (size_t) seq_s*sizeof(float);
if ((cur_in_place && Ts > 64) || (!cur_in_place && ggml_cuda_tensors_overlap(cur, dst)) ||
(resid && resid->data != dst->data && ggml_cuda_tensors_overlap(resid, dst)) ||
ggml_cuda_tensors_overlap(cell, dst) || (resid && ggml_cuda_tensors_overlap(resid, cell))) {
@@ -1883,6 +1890,9 @@ static int ggml_cuda_try_fuse_short_conv(ggml_backend_cuda_context * cuda_ctx, g
}
for (const ggml_tensor * t : { cell, (const ggml_tensor *) dst }) {
for (const ggml_tensor * src : { past, w, dyn }) {
+ if (t == cell && src == past && past_is_cell) {
+ continue;
+ }
if (src && ggml_cuda_tensors_overlap(t, src)) {
return 0;
}
@@ -1908,7 +1918,7 @@ static int ggml_cuda_try_fuse_short_conv(ggml_backend_cuda_context * cuda_ctx, g
args.state_ch_s = ch_s;
args.state_seq_s = seq_s;
args.kw = kw;
- args.run = cur_in_place || Ts <= 8 ? Ts : 1;
+ args.run = one_run ? Ts : 1;
args.C = C;
args.K = K;
args.Ts = Ts;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 895f3cc7b..c0c29a0ee 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -4945,11 +4945,13 @@ struct test_l2_clip : public test_case {
// the causal short conv of Inkling 2 as convolve() builds it: CONCAT(past, cur^T), the state store (a CPY of the last
// columns, or SET_ROWS of the windows of n_slots rollback slots), SSM_CONV, the optional input-dependent taps of H heads,
-// the residual ADD and SCALE, and an optional residual add after it
+// the residual ADD and SCALE, and an optional residual add after it. with in_place, past is read from the state cell it
+// is stored to, as when the recurrent states are read in place from the cache
struct test_short_conv : public test_case {
const int64_t C, Ts, S, H, n_slots;
const bool res;
const int64_t K; // the conv width
+ const bool in_place;
std::vector<ggml_tensor *> verify;
@@ -4962,11 +4964,12 @@ struct test_short_conv : public test_case {
std::vector<ggml_tensor *> fusion_test_nodes() override { return verify; }
std::string vars() override {
- return VARS_TO_STR7(C, Ts, S, H, n_slots, res, K);
+ return VARS_TO_STR8(C, Ts, S, H, n_slots, res, K, in_place);
}
- test_short_conv(int64_t C = 1024, int64_t Ts = 1, int64_t S = 1, int64_t H = 0, int64_t n_slots = 0, bool res = false, int64_t K = 4)
- : C(C), Ts(Ts), S(S), H(H), n_slots(n_slots), res(res), K(K) {}
+ test_short_conv(int64_t C = 1024, int64_t Ts = 1, int64_t S = 1, int64_t H = 0, int64_t n_slots = 0, bool res = false, int64_t K = 4,
+ bool in_place = false)
+ : C(C), Ts(Ts), S(S), H(H), n_slots(n_slots), res(res), K(K), in_place(in_place) {}
// the inputs only: the rollback windows are an overlapping view
void initialize_tensors(ggml_context * ctx) override {
@@ -4983,7 +4986,6 @@ struct test_short_conv : public test_case {
const int64_t T = Ts*S;
ggml_tensor * hist = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, K1, C + 64, S);
- ggml_tensor * past = ggml_view_3d(ctx, hist, K1, C, S, hist->nb[1], hist->nb[2], 32*hist->nb[1]);
ggml_tensor * cur = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, C, T), 0.5f);
ggml_tensor * w = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, K, C);
ggml_set_name(hist, "hist");
@@ -5001,6 +5003,10 @@ struct test_short_conv : public test_case {
ggml_set_name(state, "state");
const size_t row_size = state->nb[1];
+ ggml_tensor * past = in_place ?
+ ggml_view_3d(ctx, state, K1, C, S, K1*sizeof(float), row_size, 32*K1*sizeof(float)) :
+ ggml_view_3d(ctx, hist, K1, C, S, hist->nb[1], hist->nb[2], 32*hist->nb[1]);
+
ggml_tensor * x = ggml_concat(ctx, past, ggml_transpose(ctx, ggml_reshape_3d(ctx, cur, C, Ts, S)), 0);
ggml_tensor * st;
@@ -12314,6 +12320,14 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_short_conv(256, 5, 2, 4, 0, true, K));
test_cases.emplace_back(new test_short_conv(256, 3, 1, 0, 6, false, K));
}
+ // past read in place from the state cell
+ for (int64_t H : { 0, 8 }) {
+ for (int64_t Ts : { 1, 3, 33 }) {
+ for (int64_t S : { 1, 2 }) {
+ test_cases.emplace_back(new test_short_conv(1024, Ts, S, H, 0, true, 4, true));
+ }
+ }
+ }
for (int64_t T : { 1, 3 }) {
for (int64_t Q : { 1, 3, 4 }) {
for (int64_t B : { 1, 3, 12 }) {