From 883dfdc8ecf97f119c5948a39dc08b6b2d502d95 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Fri, 27 Nov 2026 16:01:44 -0600 Subject: [PATCH 1/3] A lesson replays the routing she lived: pinned experts for MoE training graphs A near-tie in a row's top-k picks a different expert when the arithmetic differs slightly, so a tiled walk (64-row graphs) and one 512-row graph trained different functions on the 35B MoE. llama_set_pinned_experts carries each position's chosen experts per layer; build_moe_ffn replays them (one-line hook) for any graph whose rows are all covered, including the last layer's output-only cut. Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M, window 512, all rows labelled, exact walk, 64-row tiles vs one graph (test-walk-exact WALK_PINNED): routed by each graph: cosine 0.3663, norm ratio 3.1704 pinned as lived: cosine 0.9932, norm ratio 0.9709 The remainder is in the early full-attention layers (blk 3, 7 ~0.95), below the linear-attention layers whose chunked arithmetic differs by row count. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- include/llama.h | 7 +++ src/CMakeLists.txt | 1 + src/llama-context.h | 3 ++ src/llama-cparams.h | 2 + src/llama-graph.cpp | 4 ++ src/llama-pinned-experts.cpp | 75 +++++++++++++++++++++++++++++ src/llama-pinned-experts.h | 44 +++++++++++++++++ tests/test-walk-exact.cpp | 91 ++++++++++++++++++++++++++++++++++++ 8 files changed, 227 insertions(+) create mode 100644 src/llama-pinned-experts.cpp create mode 100644 src/llama-pinned-experts.h diff --git a/include/llama.h b/include/llama.h index 1c0df69798a3..b8f9241d5002 100644 --- a/include/llama.h +++ b/include/llama.h @@ -2202,6 +2202,13 @@ extern "C" { // name before anything is decoded (llama_opt_failure). Set before llama_opt_epoch. LLAMA_API void llama_opt_set_walk_host_budget(struct llama_context * lctx, size_t bytes); + // Replay a lesson's routing as it was lived (Continuum): `ids` holds, for each MoE layer and each + // position in [pos0, pos0 + n_pos), its n_used chosen experts ([n_layer][n_pos][n_used]). A graph + // whose rows all lie in that range replays them instead of routing again; n_pos 0 clears. Returns + // false when the shape does not match the model (no layer, or not its experts per token). + LLAMA_API bool llama_set_pinned_experts(struct llama_context * ctx, int32_t n_layer, int32_t n_used, + llama_pos pos0, int32_t n_pos, const int32_t * ids); + // The host bytes the exact walk's largest window so far kept (0 before one ran, or when the // walk is not exact): what a run of this shape needs, from the run's own arithmetic. LLAMA_API size_t llama_opt_walk_host_bytes(struct llama_context * lctx); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1d20f7a4a8ea..b17ee5a17869 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,6 +24,7 @@ set(LLAMA_CORE_SOURCES llama-context-learn.cpp llama-context-learn-tiles.cpp llama-walk.cpp + llama-pinned-experts.cpp llama-cparams.cpp llama-grammar.cpp llama-graph.cpp diff --git a/src/llama-context.h b/src/llama-context.h index f273e7577441..17870edf2f00 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -8,6 +8,7 @@ #include "llama-impl.h" #include "llama-memory.h" #include "llama-moe-cache.h" +#include "llama-pinned-experts.h" #include "ggml-cpp.h" #include "ggml-opt.h" @@ -239,6 +240,8 @@ struct llama_context { llama_context_buffer_bytes buffer_bytes() const; int32_t memory_rows(llama_context_memory_row * out, int32_t n_max) const; // Continuum, llama-context-api.cpp + bool set_pinned_experts(int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids); // Continuum, llama-pinned-experts.cpp + llama_pinned_experts pinned_experts; // Continuum: a lesson's lived routing (llama-pinned-experts.h) llama_memory_breakdown memory_breakdown() const; // diff --git a/src/llama-cparams.h b/src/llama-cparams.h index 134848592c7a..330b1e4de406 100644 --- a/src/llama-cparams.h +++ b/src/llama-cparams.h @@ -34,6 +34,8 @@ struct llama_cparams { uint32_t walk_acc_base = 0; // a later chunk's gradient sits on the recurrent state this chunk leaves bool walk_state_surrogate = false; + // a lesson's routing as it was lived, replayed by the MoE graphs that train its rows (llama-pinned-experts.h) + const struct llama_pinned_experts * pinned_experts = nullptr; uint32_t n_ctx_seq; // context for a single sequence uint32_t n_batch; uint32_t n_ubatch; diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index 45ae8c4ac7fe..c795ebb214d4 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -1,4 +1,5 @@ #include "llama-graph.h" +#include "llama-pinned-experts.h" #include "llama-impl.h" #include "llama-model.h" @@ -2451,6 +2452,9 @@ ggml_tensor * llm_graph_context::build_moe_ffn( // select experts ggml_tensor * selected_experts = selected_experts_in; + if (selected_experts == nullptr) { + selected_experts = llama_build_pinned_experts(*this, il, n_expert_used, n_tokens); // Continuum: the lived routing + } if (selected_experts == nullptr) { selected_experts = ggml_argsort_top_k(ctx0, selection_probs, n_expert_used); // [n_expert_used, n_tokens] cb(selected_experts->src[0], "ffn_moe_argsort", il); diff --git a/src/llama-pinned-experts.cpp b/src/llama-pinned-experts.cpp new file mode 100644 index 000000000000..6cb7c9474598 --- /dev/null +++ b/src/llama-pinned-experts.cpp @@ -0,0 +1,75 @@ +#include "llama-pinned-experts.h" + +#include "llama-cparams.h" + +#include + +bool llama_pinned_experts::covers(const llama_ubatch & ubatch) const { + if (n_pos == 0 || ubatch.pos == nullptr) { + return false; + } + for (uint32_t i = 0; i < ubatch.n_tokens; ++i) { + const llama_pos p = ubatch.pos[i]; + if (p < pos0 || p >= pos0 + n_pos) { + return false; + } + } + return true; +} + +const int32_t * llama_pinned_experts::at(int32_t il, llama_pos pos) const { + return ids.data() + ((size_t) il * (size_t) n_pos + (size_t) (pos - pos0)) * (size_t) n_used; +} + +void llm_graph_input_pinned_experts::set_input(const llama_ubatch * ubatch) { + GGML_ASSERT(ggml_backend_buffer_is_host(ids->buffer)); + int32_t * data = (int32_t *) ids->data; + const bool every_row = ids->ne[1] == (int64_t) ubatch->n_tokens; + GGML_ASSERT(every_row || ubatch->output); + int64_t row = 0; + for (uint32_t i = 0; i < ubatch->n_tokens; ++i) { + if (every_row || ubatch->output[i]) { + std::memcpy(data + (size_t) row++ * pinned.n_used, pinned.at(il, ubatch->pos[i]), sizeof(int32_t) * pinned.n_used); + } + } + GGML_ASSERT(row == ids->ne[1]); +} + +ggml_tensor * llama_build_pinned_experts(const llm_graph_context & graph, int32_t il, int64_t n_used, int64_t n_rows) { + const llama_pinned_experts * pinned = graph.cparams.pinned_experts; + if (pinned == nullptr || pinned->n_used != n_used || il >= pinned->n_layer || !pinned->covers(graph.ubatch)) { + return nullptr; + } + auto inp = std::make_unique(*pinned, il); + inp->ids = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_I32, n_used, n_rows); + ggml_set_input(inp->ids); + ggml_tensor * ids = inp->ids; + graph.res->add_input(std::move(inp)); + return ids; +} + +#include "llama-context.h" +#include "llama-model.h" + +bool llama_context::set_pinned_experts(int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { + if (n_pos == 0) { + pinned_experts = {}; + cparams.pinned_experts = nullptr; + return true; + } + const auto & hp = model.hparams; + if (ids == nullptr || n_pos < 0 || n_layer != (int32_t) hp.n_layer() || n_used != (int32_t) hp.n_expert_used()) { + return false; + } + pinned_experts.n_layer = n_layer; + pinned_experts.n_used = n_used; + pinned_experts.pos0 = pos0; + pinned_experts.n_pos = n_pos; + pinned_experts.ids.assign(ids, ids + (size_t) n_layer * (size_t) n_pos * (size_t) n_used); + cparams.pinned_experts = &pinned_experts; + return true; +} + +bool llama_set_pinned_experts(llama_context * ctx, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { + return ctx && ctx->set_pinned_experts(n_layer, n_used, pos0, n_pos, ids); +} diff --git a/src/llama-pinned-experts.h b/src/llama-pinned-experts.h new file mode 100644 index 000000000000..2810fdc003e2 --- /dev/null +++ b/src/llama-pinned-experts.h @@ -0,0 +1,44 @@ +#pragma once + +// A lesson's routing as it was lived (Continuum): each position's chosen experts, per layer, so a +// mixture-of-experts graph that trains those rows replays them instead of routing again. A near-tie in +// a row's top-k picks a different expert when the arithmetic differs slightly (a tile of 64 rows +// against one of 512, CPU against CUDA), and that discrete choice turns a 1e-4 difference into a +// different function; replaying the lived choice trains the function that was served. + +#include "llama.h" +#include "llama-graph.h" + +#include +#include + +struct llama_pinned_experts { + int32_t n_layer = 0; + int32_t n_used = 0; + llama_pos pos0 = 0; + int32_t n_pos = 0; + // [layer][position - pos0][n_used] + std::vector ids; + + // every row of `ubatch` has a pinned choice: only then does a graph replay it + bool covers(const llama_ubatch & ubatch) const; + const int32_t * at(int32_t il, llama_pos pos) const; +}; + +// The graph input that carries a layer's pinned experts for the rows of a ubatch. +class llm_graph_input_pinned_experts : public llm_graph_input_i { +public: + llm_graph_input_pinned_experts(const llama_pinned_experts & pinned, int32_t il) : pinned(pinned), il(il) {} + virtual ~llm_graph_input_pinned_experts() = default; + + void set_input(const llama_ubatch * ubatch) override; + + ggml_tensor * ids = nullptr; // I32 [n_used, n_rows]: every row of the ubatch, or after the last layer's cut only its outputs, in ubatch order (as out_ids keeps them) + + const llama_pinned_experts & pinned; + const int32_t il; +}; + +// The pinned experts of layer `il` for this graph's rows, or nullptr when nothing is pinned or a row is +// not covered (the router then chooses, as it serves). +ggml_tensor * llama_build_pinned_experts(const llm_graph_context & graph, int32_t il, int64_t n_used, int64_t n_rows); diff --git a/tests/test-walk-exact.cpp b/tests/test-walk-exact.cpp index 35bc815499d9..c7fead136373 100644 --- a/tests/test-walk-exact.cpp +++ b/tests/test-walk-exact.cpp @@ -165,6 +165,68 @@ static std::vector> adapter_layout(const llama_ad } static std::vector> g_layout; + +// A lesson's routing as it was lived: every layer's chosen experts for each position of the window, +// read from the graph's own ffn_moe_topk tensors during one served forward of the window. +struct lived_routing { + int32_t n_layer = 0; + int32_t n_used = 0; + int32_t n_pos = 0; + std::vector ids; // [layer][position][n_used] +}; +static const lived_routing * g_pinned = nullptr; + +static bool routing_eval(ggml_tensor * t, bool ask, void * user_data) { + const char * name = ggml_get_name(t); + const bool topk = std::strncmp(name, "ffn_moe_topk-", 13) == 0; + if (ask) { + return topk; + } + if (!topk) { + return true; + } + auto * r = (lived_routing *) user_data; + const int32_t il = std::atoi(name + 13); + const int64_t n_used = t->ne[0], n_tokens = t->ne[1]; + if (r->n_used == 0) { + r->n_used = (int32_t) n_used; + r->ids.assign((size_t) r->n_layer * r->n_pos * n_used, 0); + } + GGML_ASSERT(n_used == r->n_used && n_tokens == r->n_pos && il < r->n_layer); + for (int64_t i = 0; i < n_tokens; ++i) { // a view of the argsort: read row by row + ggml_backend_tensor_get(t, &r->ids[((size_t) il * r->n_pos + i) * n_used], i * t->nb[1], n_used * sizeof(int32_t)); + } + return true; +} + +static lived_routing record_routing(common_params params, llama_model * model, const std::string & init, + const std::vector & tokens) { + lived_routing r; + r.n_layer = llama_model_n_layer(model); + r.n_pos = (int32_t) g_window; + params.cb_eval = routing_eval; + params.cb_eval_user_data = &r; + llama_adapter_lora * adapter = llama_adapter_lora_init(model, init.c_str()); + GGML_ASSERT(adapter != nullptr); + llama_context * ctx = make_ctx(params, model, g_window); + float scale = 1.0f; + GGML_ASSERT(llama_set_adapters_lora(ctx, &adapter, 1, &scale) == 0); + llama_batch batch = llama_batch_init((int32_t) g_window, 0, 1); + for (uint32_t i = 0; i < g_window; ++i) { + batch.token[i] = tokens[i]; + batch.pos[i] = (llama_pos) i; + batch.n_seq_id[i] = 1; + batch.seq_id[i][0] = 0; + batch.logits[i] = true; // every row an output, so the last layer routes them all too + } + batch.n_tokens = (int32_t) g_window; + GGML_ASSERT(llama_decode(ctx, batch) == 0); + llama_batch_free(batch); + llama_free(ctx); + llama_adapter_lora_free(adapter); + GGML_ASSERT(r.n_used > 0 && "the model has no MoE layer to pin"); + return r; +} // one SGD step from the fresh adapter: the step itself, -lr * the gradient it took static std::vector step_delta(const common_params & params, llama_model * model, const std::string & init, const std::vector & tokens, const std::vector & labelled, @@ -176,6 +238,9 @@ static std::vector step_delta(const common_params & params, llama_model * g_layout = adapter_layout(adapter); llama_context * ctx = make_ctx(params, model, chunk); + if (g_pinned) { // the lesson's routing as it was lived, replayed by every graph that trains its rows + GGML_ASSERT(llama_set_pinned_experts(ctx, g_pinned->n_layer, g_pinned->n_used, 0, g_pinned->n_pos, g_pinned->ids.data())); + } float scale = 1.0f; GGML_ASSERT(llama_set_adapters_lora(ctx, &adapter, 1, &scale) == 0); llama_opt_params lopt{}; @@ -627,6 +692,32 @@ int main(int argc, char ** argv) { return 0; } + // WALK_PINNED: on a mixture-of-experts base, is the tiled walk's disagreement with one graph the + // walk, or routing flips between row shapes? Both are measured: each graph routing for itself, then + // both replaying the routing the window was served with. If pinning restores agreement, routing + // was the cause, and the walk replays the lived routing by design. + if (getenv("WALK_PINNED")) { + std::vector all(WINDOW + 1, 1); + all[0] = 0; + const uint32_t tile = getenv("WALK_PINNED_TILE") ? (uint32_t) atoi(getenv("WALK_PINNED_TILE")) : 64; + const auto ref_routed = step_delta(params, model, init, tokens, all, WINDOW, true); + const auto tile_routed = step_delta(params, model, init, tokens, all, tile, true); + compare("routed by each graph: tiles vs one graph", tile_routed, ref_routed); + const lived_routing lived = record_routing(params, model, init, tokens); + g_pinned = &lived; + const auto ref_pinned = step_delta(params, model, init, tokens, all, WINDOW, true); + const auto tile_pinned = step_delta(params, model, init, tokens, all, tile, true); + g_pinned = nullptr; + const agreement pinned = compare("routing pinned as lived: tiles vs one graph", tile_pinned, ref_pinned); + if (pinned.cosine < 0.997) { + worst_tensors(tile_pinned, ref_pinned, 12); + } + std::remove(init.c_str()); + llama_model_free(model); + llama_backend_free(); + return pinned.cosine < 0.997 ? 1 : 0; + } + const bool recompute_only = getenv("WALK_RECOMPUTE_ONLY") != nullptr; const bool live_only = getenv("WALK_LIVE_ONLY") != nullptr; GGML_ASSERT(!(live_only && recompute_only)); From 83d4edd521231bca2a402c679978bdb5bef80045 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Fri, 27 Nov 2026 16:32:37 -0600 Subject: [PATCH 2/3] Pinned experts are per row and per sequence, validated on ingress (Codex on #167) The pin was context-wide and keyed only by position: a neighbour decoding the same positions got the lesson's experts, and one uncovered row sent the whole ubatch back to the router. Now the pin names its sequence; the hook runs after the router and replaces only rows of that sequence inside the range (routed + mask * (pinned - routed), exact in F32 for expert ids), every other row routes as it serves. llama_set_pinned_experts refuses, setting nothing, an id that is not one of the model's experts. Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M (test-walk-exact WALK_PINNED): two sequences at positions [0,64) in one ubatch, seq 0 pinned on [32,64) to deliberately wrong ids: 0 of 128 rows x 40 layers differ from (seq 0 [32,64) replayed, seq 0 [0,32) and seq 1 routed as unpinned) tiles vs one graph, pinned as lived: cosine 0.9932, norm ratio 0.9709 (unchanged); the case still exits 1 against its 0.997 gate: the remainder in blk 3/7 attention adapters is the next bisect Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- include/llama.h | 10 ++-- src/llama-context.h | 2 +- src/llama-graph.cpp | 4 +- src/llama-pinned-experts.cpp | 81 ++++++++++++++++++++------------ src/llama-pinned-experts.h | 38 ++++++++------- tests/test-walk-exact.cpp | 90 +++++++++++++++++++++++++++++++++++- 6 files changed, 171 insertions(+), 54 deletions(-) diff --git a/include/llama.h b/include/llama.h index b8f9241d5002..2cc29db52690 100644 --- a/include/llama.h +++ b/include/llama.h @@ -2203,10 +2203,12 @@ extern "C" { LLAMA_API void llama_opt_set_walk_host_budget(struct llama_context * lctx, size_t bytes); // Replay a lesson's routing as it was lived (Continuum): `ids` holds, for each MoE layer and each - // position in [pos0, pos0 + n_pos), its n_used chosen experts ([n_layer][n_pos][n_used]). A graph - // whose rows all lie in that range replays them instead of routing again; n_pos 0 clears. Returns - // false when the shape does not match the model (no layer, or not its experts per token). - LLAMA_API bool llama_set_pinned_experts(struct llama_context * ctx, int32_t n_layer, int32_t n_used, + // position in [pos0, pos0 + n_pos) of sequence `seq`, its n_used chosen experts + // ([n_layer][n_pos][n_used]). Per row: a row of `seq` alone inside the range replays its experts; + // every other row in the same ubatch (a neighbour's, or `seq` outside the range) routes as it + // serves. n_pos 0 clears. Returns false, setting nothing, when the shape is not the model's (its + // layers and experts per token) or an id is not one of its experts. + LLAMA_API bool llama_set_pinned_experts(struct llama_context * ctx, llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids); // The host bytes the exact walk's largest window so far kept (0 before one ran, or when the diff --git a/src/llama-context.h b/src/llama-context.h index 17870edf2f00..aef57164d0ba 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -240,7 +240,7 @@ struct llama_context { llama_context_buffer_bytes buffer_bytes() const; int32_t memory_rows(llama_context_memory_row * out, int32_t n_max) const; // Continuum, llama-context-api.cpp - bool set_pinned_experts(int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids); // Continuum, llama-pinned-experts.cpp + bool set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids); // Continuum, llama-pinned-experts.cpp llama_pinned_experts pinned_experts; // Continuum: a lesson's lived routing (llama-pinned-experts.h) llama_memory_breakdown memory_breakdown() const; diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index c795ebb214d4..be6938841486 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -2452,13 +2452,11 @@ ggml_tensor * llm_graph_context::build_moe_ffn( // select experts ggml_tensor * selected_experts = selected_experts_in; - if (selected_experts == nullptr) { - selected_experts = llama_build_pinned_experts(*this, il, n_expert_used, n_tokens); // Continuum: the lived routing - } if (selected_experts == nullptr) { selected_experts = ggml_argsort_top_k(ctx0, selection_probs, n_expert_used); // [n_expert_used, n_tokens] cb(selected_experts->src[0], "ffn_moe_argsort", il); } + selected_experts = llama_pin_experts(*this, il, selected_experts); // Continuum: a lesson's rows replay the routing she lived cb(selected_experts, "ffn_moe_topk", il); if (arch == LLM_ARCH_GROVEMOE && n_expert != hparams.n_expert) { diff --git a/src/llama-pinned-experts.cpp b/src/llama-pinned-experts.cpp index 6cb7c9474598..c907eee295a8 100644 --- a/src/llama-pinned-experts.cpp +++ b/src/llama-pinned-experts.cpp @@ -1,20 +1,14 @@ #include "llama-pinned-experts.h" +#include "llama-context.h" #include "llama-cparams.h" +#include "llama-model.h" #include -bool llama_pinned_experts::covers(const llama_ubatch & ubatch) const { - if (n_pos == 0 || ubatch.pos == nullptr) { - return false; - } - for (uint32_t i = 0; i < ubatch.n_tokens; ++i) { - const llama_pos p = ubatch.pos[i]; - if (p < pos0 || p >= pos0 + n_pos) { - return false; - } - } - return true; +bool llama_pinned_experts::pinned(const llama_ubatch & ubatch, uint32_t i) const { + return n_pos > 0 && ubatch.n_seq_id[i] == 1 && ubatch.seq_id[i][0] == seq && + ubatch.pos[i] >= pos0 && ubatch.pos[i] < pos0 + n_pos; } const int32_t * llama_pinned_experts::at(int32_t il, llama_pos pos) const { @@ -22,54 +16,83 @@ const int32_t * llama_pinned_experts::at(int32_t il, llama_pos pos) const { } void llm_graph_input_pinned_experts::set_input(const llama_ubatch * ubatch) { - GGML_ASSERT(ggml_backend_buffer_is_host(ids->buffer)); - int32_t * data = (int32_t *) ids->data; + GGML_ASSERT(ggml_backend_buffer_is_host(ids->buffer) && ggml_backend_buffer_is_host(mask->buffer)); + int32_t * id = (int32_t *) ids->data; + float * m = (float *) mask->data; const bool every_row = ids->ne[1] == (int64_t) ubatch->n_tokens; GGML_ASSERT(every_row || ubatch->output); int64_t row = 0; for (uint32_t i = 0; i < ubatch->n_tokens; ++i) { - if (every_row || ubatch->output[i]) { - std::memcpy(data + (size_t) row++ * pinned.n_used, pinned.at(il, ubatch->pos[i]), sizeof(int32_t) * pinned.n_used); + if (!every_row && !ubatch->output[i]) { + continue; + } + const bool p = pinned.pinned(*ubatch, i); + m[row] = p ? 1.0f : 0.0f; + if (p) { + std::memcpy(id + (size_t) row * pinned.n_used, pinned.at(il, ubatch->pos[i]), sizeof(int32_t) * pinned.n_used); + } else { + std::memset(id + (size_t) row * pinned.n_used, 0, sizeof(int32_t) * pinned.n_used); } + ++row; } GGML_ASSERT(row == ids->ne[1]); } -ggml_tensor * llama_build_pinned_experts(const llm_graph_context & graph, int32_t il, int64_t n_used, int64_t n_rows) { +ggml_tensor * llama_pin_experts(const llm_graph_context & graph, int32_t il, ggml_tensor * routed) { const llama_pinned_experts * pinned = graph.cparams.pinned_experts; - if (pinned == nullptr || pinned->n_used != n_used || il >= pinned->n_layer || !pinned->covers(graph.ubatch)) { - return nullptr; + if (pinned == nullptr || pinned->n_used != routed->ne[0] || il >= pinned->n_layer) { + return routed; } + bool any = false; + for (uint32_t i = 0; i < graph.ubatch.n_tokens && !any; ++i) { + any = pinned->pinned(graph.ubatch, i); + } + if (!any) { + return routed; + } + const int64_t n_used = routed->ne[0], n_rows = routed->ne[1]; auto inp = std::make_unique(*pinned, il); - inp->ids = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_I32, n_used, n_rows); + inp->ids = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_I32, n_used, n_rows); + inp->mask = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_F32, 1, n_rows); ggml_set_input(inp->ids); - ggml_tensor * ids = inp->ids; + ggml_set_input(inp->mask); + ggml_tensor * ids = inp->ids; + ggml_tensor * mask = inp->mask; graph.res->add_input(std::move(inp)); - return ids; + // per row: routed + mask * (pinned - routed), in F32 (expert ids are exact far below 2^24) + ggml_context * ctx = graph.ctx0; + ggml_tensor * r = ggml_cast(ctx, routed, GGML_TYPE_F32); + ggml_tensor * p = ggml_cast(ctx, ids, GGML_TYPE_F32); + ggml_tensor * mixed = ggml_add(ctx, r, ggml_mul(ctx, ggml_sub(ctx, p, r), mask)); + return ggml_cast(ctx, mixed, GGML_TYPE_I32); } -#include "llama-context.h" -#include "llama-model.h" - -bool llama_context::set_pinned_experts(int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { +bool llama_context::set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { if (n_pos == 0) { pinned_experts = {}; cparams.pinned_experts = nullptr; return true; } const auto & hp = model.hparams; - if (ids == nullptr || n_pos < 0 || n_layer != (int32_t) hp.n_layer() || n_used != (int32_t) hp.n_expert_used()) { + if (ids == nullptr || seq < 0 || n_pos < 0 || n_layer != (int32_t) hp.n_layer() || n_used != (int32_t) hp.n_expert_used()) { return false; } + const size_t n = (size_t) n_layer * (size_t) n_pos * (size_t) n_used; + for (size_t k = 0; k < n; ++k) { + if (ids[k] < 0 || ids[k] >= (int32_t) hp.n_expert) { + return false; // not an expert of this model + } + } + pinned_experts.seq = seq; pinned_experts.n_layer = n_layer; pinned_experts.n_used = n_used; pinned_experts.pos0 = pos0; pinned_experts.n_pos = n_pos; - pinned_experts.ids.assign(ids, ids + (size_t) n_layer * (size_t) n_pos * (size_t) n_used); + pinned_experts.ids.assign(ids, ids + n); cparams.pinned_experts = &pinned_experts; return true; } -bool llama_set_pinned_experts(llama_context * ctx, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { - return ctx && ctx->set_pinned_experts(n_layer, n_used, pos0, n_pos, ids); +bool llama_set_pinned_experts(llama_context * ctx, llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { + return ctx && ctx->set_pinned_experts(seq, n_layer, n_used, pos0, n_pos, ids); } diff --git a/src/llama-pinned-experts.h b/src/llama-pinned-experts.h index 2810fdc003e2..2773d08a47ce 100644 --- a/src/llama-pinned-experts.h +++ b/src/llama-pinned-experts.h @@ -1,10 +1,12 @@ #pragma once -// A lesson's routing as it was lived (Continuum): each position's chosen experts, per layer, so a -// mixture-of-experts graph that trains those rows replays them instead of routing again. A near-tie in -// a row's top-k picks a different expert when the arithmetic differs slightly (a tile of 64 rows -// against one of 512, CPU against CUDA), and that discrete choice turns a 1e-4 difference into a -// different function; replaying the lived choice trains the function that was served. +// A lesson's routing as it was lived (Continuum): each position's chosen experts, per layer, for ONE +// sequence, so a mixture-of-experts graph that trains those rows replays them instead of routing +// again. A near-tie in a row's top-k picks a different expert when the arithmetic differs slightly (a +// tile of 64 rows against one of 512, CPU against CUDA), and that discrete choice turns a 1e-4 +// difference into a different function; replaying the lived choice trains the function that was +// served. The pin is per row: a row of the pinned sequence inside its range replays, every other row +// (a neighbour's, or the lesson's own outside the range) routes as it serves, in the same ubatch. #include "llama.h" #include "llama-graph.h" @@ -13,19 +15,22 @@ #include struct llama_pinned_experts { - int32_t n_layer = 0; - int32_t n_used = 0; - llama_pos pos0 = 0; - int32_t n_pos = 0; + llama_seq_id seq = -1; + int32_t n_layer = 0; + int32_t n_used = 0; + llama_pos pos0 = 0; + int32_t n_pos = 0; // [layer][position - pos0][n_used] std::vector ids; - // every row of `ubatch` has a pinned choice: only then does a graph replay it - bool covers(const llama_ubatch & ubatch) const; + // row i of `ubatch` replays: it belongs to the pinned sequence alone and its position is covered + bool pinned(const llama_ubatch & ubatch, uint32_t i) const; const int32_t * at(int32_t il, llama_pos pos) const; }; -// The graph input that carries a layer's pinned experts for the rows of a ubatch. +// The graph input that carries, for the rows a layer sees, each pinned row's experts and a mask (1 +// pinned, 0 routed). Rows are the ubatch's, or after the last layer's cut only its outputs, in ubatch +// order (as out_ids keeps them). class llm_graph_input_pinned_experts : public llm_graph_input_i { public: llm_graph_input_pinned_experts(const llama_pinned_experts & pinned, int32_t il) : pinned(pinned), il(il) {} @@ -33,12 +38,13 @@ class llm_graph_input_pinned_experts : public llm_graph_input_i { void set_input(const llama_ubatch * ubatch) override; - ggml_tensor * ids = nullptr; // I32 [n_used, n_rows]: every row of the ubatch, or after the last layer's cut only its outputs, in ubatch order (as out_ids keeps them) + ggml_tensor * ids = nullptr; // I32 [n_used, n_rows], 0 on a routed row + ggml_tensor * mask = nullptr; // F32 [1, n_rows] const llama_pinned_experts & pinned; const int32_t il; }; -// The pinned experts of layer `il` for this graph's rows, or nullptr when nothing is pinned or a row is -// not covered (the router then chooses, as it serves). -ggml_tensor * llama_build_pinned_experts(const llm_graph_context & graph, int32_t il, int64_t n_used, int64_t n_rows); +// `routed` ([n_used, n_rows], the router's choice) with every pinned row of this graph replaced by its +// lived choice; `routed` itself when no row of the ubatch is pinned. +ggml_tensor * llama_pin_experts(const llm_graph_context & graph, int32_t il, ggml_tensor * routed); diff --git a/tests/test-walk-exact.cpp b/tests/test-walk-exact.cpp index c7fead136373..96365365bfd4 100644 --- a/tests/test-walk-exact.cpp +++ b/tests/test-walk-exact.cpp @@ -227,6 +227,90 @@ static lived_routing record_routing(common_params params, llama_model * model, c GGML_ASSERT(r.n_used > 0 && "the model has no MoE layer to pin"); return r; } + +// Two residents in one ubatch at the same positions, the lesson's range covering only part of hers: +// routing per row, every row an output, rows in batch order (seq 0's, then seq 1's). +static lived_routing route_two(const common_params & params_in, llama_model * model, const std::vector & tokens, + uint32_t n, const lived_routing * pin, llama_pos pin0, int32_t pin_n) { + common_params params = params_in; + lived_routing r; + r.n_layer = llama_model_n_layer(model); + r.n_pos = (int32_t) (2 * n); + params.cb_eval = routing_eval; + params.cb_eval_user_data = &r; + auto cparams = common_context_params_to_llama(params); + cparams.n_ctx = 4 * n; + cparams.n_batch = 2 * n; + cparams.n_ubatch = 2 * n; + cparams.n_seq_max = 2; + cparams.kv_unified = true; + llama_context * ctx = llama_init_from_model(model, cparams); + GGML_ASSERT(ctx != nullptr); + if (pin) { + // the lesson's lived ids for positions [pin0, pin0 + pin_n) of seq 0, from its rows of `pin` + std::vector ids((size_t) pin->n_layer * pin_n * pin->n_used); + for (int32_t il = 0; il < pin->n_layer; ++il) { + for (int32_t p = 0; p < pin_n; ++p) { + std::memcpy(&ids[((size_t) il * pin_n + p) * pin->n_used], + &pin->ids[((size_t) il * pin->n_pos + pin0 + p) * pin->n_used], sizeof(int32_t) * pin->n_used); + } + } + GGML_ASSERT(llama_set_pinned_experts(ctx, 0, pin->n_layer, pin->n_used, pin0, pin_n, ids.data())); + } + llama_batch batch = llama_batch_init((int32_t) (2 * n), 0, 2); + for (uint32_t s = 0; s < 2; ++s) { + for (uint32_t i = 0; i < n; ++i) { + const uint32_t k = s * n + i; + batch.token[k] = tokens[s * n + i]; // a neighbour with its own text at the same positions + batch.pos[k] = (llama_pos) i; + batch.n_seq_id[k] = 1; + batch.seq_id[k][0] = (llama_seq_id) s; + batch.logits[k] = true; + } + } + batch.n_tokens = (int32_t) (2 * n); + GGML_ASSERT(llama_decode(ctx, batch) == 0); + llama_batch_free(batch); + llama_free(ctx); + GGML_ASSERT(r.n_used > 0 && "the model has no MoE layer to pin"); + return r; +} + +// the model's expert count, from its own metadata (.expert_count) +static int32_t model_n_expert(const llama_model * model) { + char arch[64], val[32]; + GGML_ASSERT(llama_model_meta_val_str(model, "general.architecture", arch, sizeof(arch)) > 0); + GGML_ASSERT(llama_model_meta_val_str(model, (std::string(arch) + ".expert_count").c_str(), val, sizeof(val)) > 0); + return (int32_t) std::atoi(val); +} +// what this catches (Codex on fork #167): a context-wide pin that hands a neighbour the lesson's experts +// at the same positions, or drops the whole ubatch to the router when one row is uncovered. +static int pinned_rows_are_per_sequence(const common_params & params, llama_model * model, const std::vector & tokens) { + const uint32_t n = 64; + const llama_pos pin0 = 32; + const int32_t pin_n = 32; + const lived_routing served = route_two(params, model, tokens, n, nullptr, 0, 0); + const int32_t n_expert = model_n_expert(model); + lived_routing fake = served; // deliberately not what routing picks, so a replay is unmistakable + for (auto & e : fake.ids) { + e = (e + 1) % n_expert; + } + const lived_routing pinned = route_two(params, model, tokens, n, &fake, pin0, pin_n); + int wrong = 0; + for (int32_t il = 0; il < served.n_layer; ++il) { + for (uint32_t row = 0; row < 2 * n; ++row) { + const size_t o = ((size_t) il * served.n_pos + row) * served.n_used; + const bool covered = row < n && (llama_pos) row >= pin0 && (llama_pos) row < pin0 + pin_n; + const auto & want = covered ? fake.ids : served.ids; + if (std::memcmp(&pinned.ids[o], &want[o], sizeof(int32_t) * served.n_used) != 0) { + ++wrong; + } + } + } + printf(" pinned per sequence: %d of %u rows x %d layers differ from (seq 0 [%d,%d) replayed, the rest routed)\n", + wrong, 2 * n, served.n_layer, pin0, pin0 + pin_n); + return wrong == 0 ? 0 : 1; +} // one SGD step from the fresh adapter: the step itself, -lr * the gradient it took static std::vector step_delta(const common_params & params, llama_model * model, const std::string & init, const std::vector & tokens, const std::vector & labelled, @@ -239,7 +323,7 @@ static std::vector step_delta(const common_params & params, llama_model * llama_context * ctx = make_ctx(params, model, chunk); if (g_pinned) { // the lesson's routing as it was lived, replayed by every graph that trains its rows - GGML_ASSERT(llama_set_pinned_experts(ctx, g_pinned->n_layer, g_pinned->n_used, 0, g_pinned->n_pos, g_pinned->ids.data())); + GGML_ASSERT(llama_set_pinned_experts(ctx, 0, g_pinned->n_layer, g_pinned->n_used, 0, g_pinned->n_pos, g_pinned->ids.data())); } float scale = 1.0f; GGML_ASSERT(llama_set_adapters_lora(ctx, &adapter, 1, &scale) == 0); @@ -697,6 +781,10 @@ int main(int argc, char ** argv) { // both replaying the routing the window was served with. If pinning restores agreement, routing // was the cause, and the walk replays the lived routing by design. if (getenv("WALK_PINNED")) { + if (pinned_rows_are_per_sequence(params, model, tokens) != 0) { + fprintf(stderr, "FAILED: a pinned row is not its own sequence's (Codex on fork #167)\n"); + return 1; + } std::vector all(WINDOW + 1, 1); all[0] = 0; const uint32_t tile = getenv("WALK_PINNED_TILE") ? (uint32_t) atoi(getenv("WALK_PINNED_TILE")) : 64; From 17e10774f1f58bd1789eefc8aa4b6f87450a35f5 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Fri, 27 Nov 2026 16:43:03 -0600 Subject: [PATCH 3/3] A pin survives graph reuse: one topology while it is set, reuse only under the same pin (Codex on #167) A graph built with no covered row had no pin input, and a later covered ubatch of the same shape reused it and skipped the pin; one built before the pin was set was reused after. Now, while a pin is set, every MoE layer carries the pin input (its mask is all zeros when no row is covered), the input reuses a graph only while the same pin is set, and setting or clearing a pin drops the cached graphs. Measured on the M5, CPU-only, Qwen3.6-35B-A3B Q4_K_M (test-walk-exact WALK_PINNED), one context, decodes [0,32) then [32,64) (same shape, reuse eligible), [32,64) pinned to deliberately wrong ids: pin set before the first decode: 0 of 64 rows x 40 layers differ from ([0,32) routed, [32,64) replayed) pin set between the decodes: 0 of 64 rows x 40 layers differ per sequence (unchanged): 0 of 128 rows x 40 layers differ tiles vs one graph pinned: cosine 0.9932 (unchanged; the case exits 1 against its 0.997 gate) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo --- src/llama-context.h | 1 + src/llama-pinned-experts.cpp | 24 +++++++--- src/llama-pinned-experts.h | 1 + tests/test-walk-exact.cpp | 86 ++++++++++++++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 10 deletions(-) diff --git a/src/llama-context.h b/src/llama-context.h index aef57164d0ba..1e0a224cadac 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -241,6 +241,7 @@ struct llama_context { llama_context_buffer_bytes buffer_bytes() const; int32_t memory_rows(llama_context_memory_row * out, int32_t n_max) const; // Continuum, llama-context-api.cpp bool set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids); // Continuum, llama-pinned-experts.cpp + void drop_reusable_graphs(); // Continuum: no cached graph is reused across a change it cannot see llama_pinned_experts pinned_experts; // Continuum: a lesson's lived routing (llama-pinned-experts.h) llama_memory_breakdown memory_breakdown() const; diff --git a/src/llama-pinned-experts.cpp b/src/llama-pinned-experts.cpp index c907eee295a8..30d6136b9530 100644 --- a/src/llama-pinned-experts.cpp +++ b/src/llama-pinned-experts.cpp @@ -38,18 +38,19 @@ void llm_graph_input_pinned_experts::set_input(const llama_ubatch * ubatch) { GGML_ASSERT(row == ids->ne[1]); } +bool llm_graph_input_pinned_experts::can_reuse(const llm_graph_params & params) { + // the same pin is still set (setting or clearing one also drops the cached graphs) + return params.cparams.pinned_experts == &pinned; +} + ggml_tensor * llama_pin_experts(const llm_graph_context & graph, int32_t il, ggml_tensor * routed) { const llama_pinned_experts * pinned = graph.cparams.pinned_experts; + // While a pin is set, every MoE layer carries it, covered rows or not: the graph's shape does not + // depend on which rows a ubatch covers, so a reused graph never skips a pin (Codex on #167); its + // input sets each row's mask per ubatch. if (pinned == nullptr || pinned->n_used != routed->ne[0] || il >= pinned->n_layer) { return routed; } - bool any = false; - for (uint32_t i = 0; i < graph.ubatch.n_tokens && !any; ++i) { - any = pinned->pinned(graph.ubatch, i); - } - if (!any) { - return routed; - } const int64_t n_used = routed->ne[0], n_rows = routed->ne[1]; auto inp = std::make_unique(*pinned, il); inp->ids = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_I32, n_used, n_rows); @@ -71,6 +72,7 @@ bool llama_context::set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_ if (n_pos == 0) { pinned_experts = {}; cparams.pinned_experts = nullptr; + drop_reusable_graphs(); return true; } const auto & hp = model.hparams; @@ -90,9 +92,17 @@ bool llama_context::set_pinned_experts(llama_seq_id seq, int32_t n_layer, int32_ pinned_experts.n_pos = n_pos; pinned_experts.ids.assign(ids, ids + n); cparams.pinned_experts = &pinned_experts; + drop_reusable_graphs(); // a graph built without this pin has no input to carry it return true; } +void llama_context::drop_reusable_graphs() { + for (auto & res : gf_res_prev) { + res->reset(); + } + gf_res_prev_active = nullptr; +} + bool llama_set_pinned_experts(llama_context * ctx, llama_seq_id seq, int32_t n_layer, int32_t n_used, llama_pos pos0, int32_t n_pos, const int32_t * ids) { return ctx && ctx->set_pinned_experts(seq, n_layer, n_used, pos0, n_pos, ids); } diff --git a/src/llama-pinned-experts.h b/src/llama-pinned-experts.h index 2773d08a47ce..9bd05b3bf753 100644 --- a/src/llama-pinned-experts.h +++ b/src/llama-pinned-experts.h @@ -37,6 +37,7 @@ class llm_graph_input_pinned_experts : public llm_graph_input_i { virtual ~llm_graph_input_pinned_experts() = default; void set_input(const llama_ubatch * ubatch) override; + bool can_reuse(const llm_graph_params & params) override; ggml_tensor * ids = nullptr; // I32 [n_used, n_rows], 0 on a routed row ggml_tensor * mask = nullptr; // F32 [1, n_rows] diff --git a/tests/test-walk-exact.cpp b/tests/test-walk-exact.cpp index 96365365bfd4..e31b299ede0f 100644 --- a/tests/test-walk-exact.cpp +++ b/tests/test-walk-exact.cpp @@ -173,6 +173,7 @@ struct lived_routing { int32_t n_used = 0; int32_t n_pos = 0; std::vector ids; // [layer][position][n_used] + int32_t row0 = 0; // the row the next decode's first row records at (several decodes, one record) }; static const lived_routing * g_pinned = nullptr; @@ -192,9 +193,9 @@ static bool routing_eval(ggml_tensor * t, bool ask, void * user_data) { r->n_used = (int32_t) n_used; r->ids.assign((size_t) r->n_layer * r->n_pos * n_used, 0); } - GGML_ASSERT(n_used == r->n_used && n_tokens == r->n_pos && il < r->n_layer); + GGML_ASSERT(n_used == r->n_used && r->row0 + n_tokens <= r->n_pos && il < r->n_layer); for (int64_t i = 0; i < n_tokens; ++i) { // a view of the argsort: read row by row - ggml_backend_tensor_get(t, &r->ids[((size_t) il * r->n_pos + i) * n_used], i * t->nb[1], n_used * sizeof(int32_t)); + ggml_backend_tensor_get(t, &r->ids[((size_t) il * r->n_pos + r->row0 + i) * n_used], i * t->nb[1], n_used * sizeof(int32_t)); } return true; } @@ -311,6 +312,85 @@ static int pinned_rows_are_per_sequence(const common_params & params, llama_mode wrong, 2 * n, served.n_layer, pin0, pin0 + pin_n); return wrong == 0 ? 0 : 1; } + +// One sequence decoded as two same-shape ubatches, [0, n) then [n, 2n), on one context (the second may +// reuse the first's graph). `when` says when the pin (of positions [n, 2n), from `pin`) is set: never, +// before the first decode, or between the two. +enum class pin_when { never, before, between }; +static lived_routing route_halves(const common_params & params_in, llama_model * model, const std::vector & tokens, + uint32_t n, const lived_routing * pin, pin_when when) { + common_params params = params_in; + lived_routing r; + r.n_layer = llama_model_n_layer(model); + r.n_pos = (int32_t) (2 * n); + params.cb_eval = routing_eval; + params.cb_eval_user_data = &r; + auto cparams = common_context_params_to_llama(params); + cparams.n_ctx = 4 * n; + cparams.n_batch = n; + cparams.n_ubatch = n; + cparams.kv_unified = true; + llama_context * ctx = llama_init_from_model(model, cparams); + GGML_ASSERT(ctx != nullptr); + auto set_pin = [&]() { + std::vector ids((size_t) pin->n_layer * n * pin->n_used); + for (int32_t il = 0; il < pin->n_layer; ++il) { + std::memcpy(&ids[(size_t) il * n * pin->n_used], &pin->ids[((size_t) il * pin->n_pos + n) * pin->n_used], + sizeof(int32_t) * n * pin->n_used); + } + GGML_ASSERT(llama_set_pinned_experts(ctx, 0, pin->n_layer, pin->n_used, (llama_pos) n, (int32_t) n, ids.data())); + }; + if (when == pin_when::before) { + set_pin(); + } + llama_batch batch = llama_batch_init((int32_t) n, 0, 1); + for (uint32_t half = 0; half < 2; ++half) { + if (half == 1 && when == pin_when::between) { + set_pin(); + } + for (uint32_t i = 0; i < n; ++i) { + batch.token[i] = tokens[half * n + i]; + batch.pos[i] = (llama_pos) (half * n + i); + batch.n_seq_id[i] = 1; + batch.seq_id[i][0] = 0; + batch.logits[i] = true; + } + batch.n_tokens = (int32_t) n; + r.row0 = (int32_t) (half * n); + GGML_ASSERT(llama_decode(ctx, batch) == 0); + } + llama_batch_free(batch); + llama_free(ctx); + return r; +} + +// what this catches (Codex on fork #167 at 83d4edd52): a graph built with no covered row, reused for a +// covered ubatch of the same shape, skipping the pin; and one built before the pin was set, reused after. +static int pinned_rows_survive_graph_reuse(const common_params & params, llama_model * model, const std::vector & tokens) { + const uint32_t n = 32; + const lived_routing served = route_halves(params, model, tokens, n, nullptr, pin_when::never); + const int32_t n_expert = model_n_expert(model); + lived_routing fake = served; + for (auto & e : fake.ids) { + e = (e + 1) % n_expert; + } + int failed = 0; + for (const pin_when when : { pin_when::before, pin_when::between }) { + const lived_routing got = route_halves(params, model, tokens, n, &fake, when); + int wrong = 0; + for (int32_t il = 0; il < served.n_layer; ++il) { + for (uint32_t row = 0; row < 2 * n; ++row) { + const size_t o = ((size_t) il * served.n_pos + row) * served.n_used; + const auto & want = row >= n ? fake.ids : served.ids; + wrong += std::memcmp(&got.ids[o], &want[o], sizeof(int32_t) * served.n_used) != 0; + } + } + printf(" pinned across graph reuse (pin set %s): %d of %u rows x %d layers differ from ([0,%u) routed, [%u,%u) replayed)\n", + when == pin_when::before ? "before the first decode" : "between the decodes", wrong, 2 * n, served.n_layer, n, n, 2 * n); + failed += wrong != 0; + } + return failed; +} // one SGD step from the fresh adapter: the step itself, -lr * the gradient it took static std::vector step_delta(const common_params & params, llama_model * model, const std::string & init, const std::vector & tokens, const std::vector & labelled, @@ -781,7 +861,7 @@ int main(int argc, char ** argv) { // both replaying the routing the window was served with. If pinning restores agreement, routing // was the cause, and the walk replays the lived routing by design. if (getenv("WALK_PINNED")) { - if (pinned_rows_are_per_sequence(params, model, tokens) != 0) { + if (pinned_rows_are_per_sequence(params, model, tokens) != 0 || pinned_rows_survive_graph_reuse(params, model, tokens) != 0) { fprintf(stderr, "FAILED: a pinned row is not its own sequence's (Codex on fork #167)\n"); return 1; }