Commit abca69200 for llama.cpp
commit abca6920035ed77c1d80eaf23d066e9d3a7c5b23
Author: Aman Gupta <amangupta052@gmail.com>
Date: Wed Nov 25 11:54:30 2026 +0800
model : inkling2 graph with fewer ops in mix() and control() (#32948)
Assisted-by: Claude Opus 5.5
diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp
index 73ebe76dd..bdd1f18d6 100644
--- a/src/models/inkling2.cpp
+++ b/src/models/inkling2.cpp
@@ -171,15 +171,33 @@ ggml_tensor * llama_model_inkling2::graph::clip(ggml_tensor * cur, ggml_tensor *
return ggml_scale(ctx0, ggml_l2_norm(ctx0, cur, threshold), threshold);
}
+// the normalized states of list, last first: [norm(l_n-1), ..., norm(l_0)]
+// the stacks are kept per graph, so each block is normalized and copied once and not again for every later mix
+ggml_tensor * llama_model_inkling2::graph::mix_stack(const std::vector<ggml_tensor *> & list) {
+ const auto it = mix_stacks.find(list);
+ if (it != mix_stacks.end()) {
+ return it->second;
+ }
+
+ ggml_tensor * res = ggml_rms_norm(ctx0, ggml_reshape_3d(ctx0, list.back(), n_embd, 1, n_tokens), 1e-6f);
+ if (list.size() > 1) {
+ res = ggml_concat(ctx0, res, mix_stack(std::vector<ggml_tensor *>(list.begin(), list.end() - 1)), 1);
+ }
+
+ return mix_stacks[list] = res;
+}
+
// attention over the depth: mix the earlier block outputs and cur with learned queries
ggml_tensor * llama_model_inkling2::graph::mix(const std::vector<ggml_tensor *> & blocks, ggml_tensor * cur, ggml_tensor * query) {
- ggml_tensor * states = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
- for (ggml_tensor * block : blocks) {
- states = ggml_concat(ctx0, ggml_reshape_3d(ctx0, block, n_embd, 1, n_tokens), states, 1);
+ // [norm(b_n-1), ..., norm(b_0), norm(cur)]
+ ggml_tensor * states = mix_stack({cur});
+ if (!blocks.empty()) {
+ states = ggml_concat(ctx0, mix_stack(blocks), states, 1);
}
- states = ggml_rms_norm(ctx0, states, 1e-6f);
- ggml_tensor * queries = ggml_repeat(ctx0, query, ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_embd, query->ne[1], n_tokens));
+ // one token needs no copy of the queries
+ ggml_tensor * queries = n_tokens == 1 ? query :
+ ggml_repeat(ctx0, query, ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_embd, query->ne[1], n_tokens));
ggml_tensor * scores = ggml_mul_mat(ctx0, states, queries);
scores = ggml_soft_max_ext(ctx0, scores, nullptr, 1.0f/sqrtf(n_embd), 0.0f);
@@ -235,7 +253,9 @@ ggml_tensor * llama_model_inkling2::graph::convolve(
// mix a cross-pass message into a looped layer input: (1 - forget)*cur + gain*message, both gated on cur and message
ggml_tensor * llama_model_inkling2::graph::control(ggml_tensor * cur, ggml_tensor * message, const loop_layer & loop, int ctrl) {
- ggml_tensor * gate = ggml_concat(ctx0, ggml_rms_norm(ctx0, cur, 1e-6f), ggml_rms_norm(ctx0, message, 1e-6f), 0);
+ // cur and message as two rows of one rms_norm, then side by side: [norm(cur); norm(message)]
+ ggml_tensor * gate = ggml_concat(ctx0, ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens), ggml_reshape_3d(ctx0, message, n_embd, 1, n_tokens), 1);
+ gate = ggml_reshape_2d(ctx0, ggml_rms_norm(ctx0, gate, 1e-6f), 2*n_embd, n_tokens);
gate = build_lora_mm(loop.ctrl_down[ctrl], gate);
ggml_tensor * gain = ggml_tanh(ctx0, build_lora_mm(loop.ctrl_gain[ctrl], gate));
@@ -362,7 +382,12 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens
kq_b = ggml_cont(ctx0, ggml_permute(ctx0, kq_b, 2, 0, 1, 3));
cb(kq_b, "kq_b", il);
- sinks = ggml_fill(ctx0, ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, n_head), 0.0f);
+ // one zero tensor per head count for the whole graph
+ ggml_tensor *& zero = zero_sinks[n_head];
+ if (!zero) {
+ zero = ggml_fill(ctx0, ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, n_head), 0.0f);
+ }
+ sinks = zero;
cur = build_attn(inp,
nullptr, nullptr, nullptr,
diff --git a/src/models/models.h b/src/models/models.h
index 23a7b55fd..d61469120 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -2579,6 +2579,12 @@ struct llama_model_inkling2 : public llama_model_base {
ggml_tensor * inp_out_ids = nullptr;
ggml_tensor * tau = nullptr; // log-scaled positions
+
+ // the normalized state stacks of mix(), keyed by their tensors
+ ggml_tensor * mix_stack(const std::vector<ggml_tensor *> & list);
+ std::map<std::vector<ggml_tensor *>, ggml_tensor *> mix_stacks;
+
+ std::map<int64_t, ggml_tensor *> zero_sinks; // by head count
};
std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;