diff --git a/include/llama.h b/include/llama.h index 1c0df69798a3..2cc29db52690 100644 --- a/include/llama.h +++ b/include/llama.h @@ -2202,6 +2202,15 @@ 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) 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 // 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..1e0a224cadac 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,9 @@ 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-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..be6938841486 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" @@ -2455,6 +2456,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn( 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 new file mode 100644 index 000000000000..30d6136b9530 --- /dev/null +++ b/src/llama-pinned-experts.cpp @@ -0,0 +1,108 @@ +#include "llama-pinned-experts.h" + +#include "llama-context.h" +#include "llama-cparams.h" +#include "llama-model.h" + +#include + +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 { + 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) && 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]) { + 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]); +} + +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; + } + 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->mask = ggml_new_tensor_2d(graph.ctx0, GGML_TYPE_F32, 1, n_rows); + ggml_set_input(inp->ids); + ggml_set_input(inp->mask); + ggml_tensor * ids = inp->ids; + ggml_tensor * mask = inp->mask; + graph.res->add_input(std::move(inp)); + // 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); +} + +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; + drop_reusable_graphs(); + return true; + } + const auto & hp = model.hparams; + 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 + 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 new file mode 100644 index 000000000000..9bd05b3bf753 --- /dev/null +++ b/src/llama-pinned-experts.h @@ -0,0 +1,51 @@ +#pragma once + +// 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" + +#include +#include + +struct llama_pinned_experts { + 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; + + // 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, 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) {} + 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] + + const llama_pinned_experts & pinned; + const int32_t il; +}; + +// `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 35bc815499d9..e31b299ede0f 100644 --- a/tests/test-walk-exact.cpp +++ b/tests/test-walk-exact.cpp @@ -165,6 +165,232 @@ 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] + 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; + +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 && 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 + r->row0 + 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; +} + +// 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 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, @@ -176,6 +402,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, 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); llama_opt_params lopt{}; @@ -627,6 +856,36 @@ 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")) { + 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; + } + 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));