Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(glm53): don't restore a pin over rows another branch rewrote

eni7a52052efa96
diff --git a/c/glm53.c b/c/glm53.c
index ac34e953..6f4fffa7 100644
--- a/c/glm53.c
+++ b/c/glm53.c
@@ -2903,7 +2903,14 @@ static int slot_pin_restore(const GModel *m, KVSlot *slot, const int *tokens, in
while (s >= 0) {
ColiPin *k = &slot->pins.slot[s];
Glm53PinState *st = (Glm53PinState *)k->state;
- if (st && k->len <= slot->session->filled) {
+ /* Lo scatto porta lo stato KDA, non le righe DSA: quelle sono della
+ * sessione, e un ramo rimesso a una profondita' minore le puo' aver
+ * riscritte con altri token. Che gli id combacino con la RICHIESTA
+ * non basta; devono combaciare con la storia dello slot, che e' la
+ * sola descrizione di cosa le righe tengono davvero (come
+ * kv_prefix_holds per gli altri motori, #1650). */
+ if (st && k->len <= slot->session->filled && k->len <= slot->n &&
+ !memcmp(k->ids, slot->tokens, (size_t)k->len * sizeof(int))) {
for (int i = 0; i < c->n_layers; i++) {
GLayerState *ls = &slot->session->layer[i];
if (c->is_full[i] || !ls->kda_state || !st->state[i]) continue;
@@ -2914,7 +2921,7 @@ static int slot_pin_restore(const GModel *m, KVSlot *slot, const int *tokens, in
coli_pin_touch(&slot->pins, s);
return k->len;
}
- k->len = 0; /* lo scatto pretende posizioni che non ci sono */
+ k->len = 0; /* posizioni che non ci sono, o righe che non sono piu' sue */
s = coli_pin_best(&slot->pins, tokens, n);
}
return 0;
diff --git a/c/tests/glm53_pin_branch_harness.py b/c/tests/glm53_pin_branch_harness.py
new file mode 100644
index 00000000..5f2df89e
--- /dev/null
+++ b/c/tests/glm53_pin_branch_harness.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python3
+"""Uno scatto (pin) non si rimette sopra righe che un altro ramo ha riscritto.
+
+Lo scatto di GLM-5.3 salva lo stato ricorrente dei layer KDA, non le righe dei
+layer DSA: quelle restano dove sono, indicizzate per posizione. slot_pin_restore
+controlla che la sessione sia la stessa e che lo scatto non sia piu' profondo
+di `filled`, ma non che le righe fino a quella profondita' siano ancora le sue.
+
+La sequenza che lo rompe resta tutta nella stessa sessione:
+
+ 1. P pin=1 scatto a |P|
+ 2. P+A pin=1 estende, scatto a |P+A|
+ 3. P+B diverge da A: si rimette lo scatto P e si macina B sopra
+ le righe |P|.. -- la sessione e' la stessa, gli scatti restano
+ 4. P+A+X lo scatto P+A combacia con la richiesta ed e' <= filled,
+ quindi si rimette: stato KDA di P+A, righe DSA di P+B
+
+La risposta resta plausibile, quindi la prova e' un confronto a coppie: i
+punteggi (ECHO) della coda X devono essere identici a quelli di un motore
+freddo sullo stesso prompt. Il controllo senza il passo 3 li da' identici; col
+passo 3 no.
+
+Come per gli altri harness glm53, la fixture si genera con
+ python3 tools/make_glm53_multimodal_tiny.py --output <dir>
+"""
+import argparse
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+PROMPT, BRANCH_A, BRANCH_B, TAIL = (
+ "the quick brown", " fox jumps", " cat sleeps all day", " over it")
+
+
+class Engine:
+ def __init__(self, binary, fixture):
+ env = {**os.environ, "SERVE": "1", "SERVE_BATCH": "1", "SNAP": str(fixture),
+ "GLM53_BITS": "32"}
+ self.p = subprocess.Popen([binary], env=env, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
+ while "READY" not in self.line():
+ pass
+
+ def line(self):
+ line = self.p.stdout.readline()
+ if not line:
+ raise AssertionError("il motore ha chiuso lo stream prima del previsto")
+ return line.decode("utf-8", "replace").rstrip("\n")
+
+ def score(self, request_id, text, pin=False):
+ """Legge il prompt senza generare (max_tokens 0, logprobs=1): i punteggi
+ ECHO per posizione, e la riga che chiude la richiesta."""
+ body = text.encode()
+ extension = " logprobs=1" + (" pin=1" if pin else "")
+ self.p.stdin.write(f"SUBMIT {request_id} 0 {len(body)} 0 0 1{extension}\n".encode()
+ + body + b"\n")
+ self.p.stdin.flush()
+ echoes = {}
+ while True:
+ fields = self.line().split()
+ if fields and fields[0] in ("ECHO", "DATA"):
+ self.p.stdout.read(int(fields[2]) + 1)
+ if fields[0] == "ECHO" and fields[4] not in ("nan", "-nan"):
+ echoes[int(fields[3])] = fields[4]
+ if fields and fields[0] in ("DONE", "ERROR"):
+ return " ".join(fields), echoes
+
+ def close(self):
+ self.p.stdin.close()
+ self.p.wait(timeout=60)
+
+
+def tail_scores(binary, fixture, branch_in_between):
+ warm = Engine(binary, fixture)
+ try:
+ warm.score(1, PROMPT, pin=True)
+ warm.score(2, PROMPT + BRANCH_A, pin=True)
+ if branch_in_between:
+ warm.score(3, PROMPT + BRANCH_B)
+ done, scores = warm.score(4, PROMPT + BRANCH_A + TAIL)
+ finally:
+ warm.close()
+ if not done.startswith("DONE 4 "):
+ raise AssertionError(f"richiesta 4 chiusa con {done!r}")
+ return scores
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--binary", required=True)
+ parser.add_argument("--fixture", type=Path, required=True)
+ arguments = parser.parse_args()
+ if not (arguments.fixture / "config.json").exists():
+ print(f"SKIP: manca {arguments.fixture}; generala con\n"
+ f" python3 tools/make_glm53_multimodal_tiny.py --output <dir>")
+ return 2
+ binary = os.path.abspath(arguments.binary)
+
+ cold = Engine(binary, arguments.fixture)
+ try:
+ _, fresh = cold.score(1, PROMPT + BRANCH_A + TAIL)
+ finally:
+ cold.close()
+ tail = sorted(p for p in fresh if p >= len((PROMPT + BRANCH_A).encode()))
+ if not tail:
+ print("FAIL: il motore freddo non ha dato punteggi per la coda")
+ return 1
+
+ for branch_in_between in (False, True):
+ scores = tail_scores(binary, arguments.fixture, branch_in_between)
+ wrong = [p for p in tail if scores.get(p) != fresh[p]]
+ if wrong:
+ where = "con un ramo P+B in mezzo" if branch_in_between else "senza rami"
+ print(f"FAIL: {where}, {len(wrong)} posizioni su {len(tail)} della coda "
+ f"non combaciano con un motore freddo (prima: {wrong[0]}, "
+ f"{scores.get(wrong[0])} contro {fresh[wrong[0]]}) -- uno scatto "
+ f"e' stato rimesso sopra righe che non sono piu' le sue")
+ return 1
+
+ print(f"PASS GLM-5.3 pin: le {len(tail)} posizioni della coda combaciano con un "
+ f"motore freddo, anche dopo un ramo che ha riscritto le righe dello scatto")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/c/tests/test_glm53_oracles.py b/c/tests/test_glm53_oracles.py
index 13226029..2f2bc77a 100644
--- a/c/tests/test_glm53_oracles.py
+++ b/c/tests/test_glm53_oracles.py
@@ -40,6 +40,7 @@ class Glm53HarnessSkipTest(unittest.TestCase):
"chat_template": ["--template", missing],
"tiny": ["--binary", "glm53", "--fixture", missing],
"multimodal_tiny": ["--binary", "glm53", "--fixture", missing],
+ "pin_branch": ["--binary", "glm53", "--fixture", missing],
"serve": ["--binary", "glm53", "--fixture", missing],
"streaming": ["--binary", "glm53", "--quantized", missing,
"--dequantized", missing],