From a72d05b8b839d8da3f05dd4c6a5cf743e3779ae6 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:35:04 +0000 Subject: [PATCH] inkling2: read the conv state in place, copy row views only when they are not contiguous Assisted-by: Claude Opus 5.5 --- src/llama-graph.cpp | 2 ++ src/models/inkling2.cpp | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index e71731c056e9..033bc501d14e 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -1497,6 +1497,8 @@ bool llm_graph_input_mem_hybrid_iswa_rel::can_reuse(const llm_graph_params & par res &= inp_rs->head == mctx->get_recr()->get_head(); res &= inp_rs->rs_z == mctx->get_recr()->get_rs_z(); + // build_rs_in_place() reads the states through a view when the copy is the identity + res &= inp_rs->s_copy_identity == mctx->get_recr()->is_s_copy_identity(params.ubatch.n_seqs); return res; } diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp index 23dacd52b01c..8b147b8a89b8 100644 --- a/src/models/inkling2.cpp +++ b/src/models/inkling2.cpp @@ -321,13 +321,19 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens const int64_t n_embd_head = hparams.n_embd_head_k(il); const int64_t n_embd_k_gqa = n_embd_head*n_head_kv; + // a view of rows, copied only when it is not contiguous (several tokens) + auto rows = [&](ggml_tensor * view) { + return ggml_is_contiguous(view) ? view : ggml_cont(ctx0, view); + }; + // the message of controller ctrl, [n_embd, n_tokens] auto message = [&](int ctrl) { - return ggml_cont(ctx0, ggml_view_2d(ctx0, messages, n_embd, n_tokens, messages->nb[2], ctrl*messages->nb[1])); + return rows(ggml_view_2d(ctx0, messages, n_embd, n_tokens, messages->nb[2], ctrl*messages->nb[1])); }; ggml_tensor * state = recr->mctx->get_r_l(il); - ggml_tensor * history = build_rs(recr, state, hparams.n_embd_r(), ubatch.n_seqs); + // each convolution reads its slice of the state before it writes it back, the state can be read in place + ggml_tensor * history = build_rs_in_place(recr, state, hparams.n_embd_r(), ubatch.n_seqs); history = ggml_reshape_3d(ctx0, history, hparams.n_shortconv_l_cache - 1, hparams.n_sconv_channels, ubatch.n_seqs); ggml_tensor * inpSA = cur; @@ -337,7 +343,7 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens if (layer.attn_res_query) { ggml_tensor * mixed = mix(blocks, cur, layer.attn_res_query); for (int j = 0; j < 3; ++j) { - inputs[j] = ggml_cont(ctx0, ggml_view_2d(ctx0, mixed, n_embd, n_tokens, mixed->nb[2], (is_swa ? 0 : j)*mixed->nb[1])); + inputs[j] = rows(ggml_view_2d(ctx0, mixed, n_embd, n_tokens, mixed->nb[2], (is_swa ? 0 : j)*mixed->nb[1])); } }