Skip to content

tools: logprob_agree matches on the key both routes have, never an id against a token (engine#3158) - #3241

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-hrxnan/3158-match-key
Nov 21, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-hrxnan/3158-match-key

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Refs #3158. Follow-up to #3231, merged as 6241f85d2: at that head, --match-by id — the key both its body and #3225's print in the example command — reports complete disagreement on the gate's own reference pair.

The measurement

Two loopback routes, identical tokens, one carrying id and one not:

--match-by id     token-id match 0.000 over 32 positions
                  token divergence prompts [0,1,2,3,4,5,6,7]
                  mean KLD cannot tell                                  rc 1
--match-by text   token-id match 1.000 over 32 positions
                  exact-bit match 1.000, mean KLD 0.000000              rc 0

Same tokens, and one of the two reports is a total-divergence verdict.

Why it is the reference pair and not a corner

Our own serve emits no token id at all — app/forward_serve.cpp's logprob_json builds {token, logprob, top_logprobs} on every backend — while the llama.cpp fork emits id. So --route cpu=… --route fallback=… --match-by id, the command that would be copied out of either PR body, compares an integer against a string at every position: tok_key returns an id on one side and a token string on the other, and nothing is ever equal. The row would have recorded 0 % token-id match, divergence on every prompt, and a KLD that is cannot-tell — for two engines running identical tokens — and the Loom gate would have sent a correct model back to the fallback on a measurement of nothing.

This is the same failure the rest of #3231 was about, one layer out: a comparison that silently measures nothing and prints a number anyway.

The change

  • route_has_token_ids(per_prompt) — whether a route emits an id on its tokens at all.
  • effective_match_by(requested, a_has_ids, b_has_ids) — a pair where either side lacks ids compares on text, the key both have. A pair with ids on both sides keeps the id key, and an explicit --match-by text is untouched, so the strict comparison is unchanged everywhere it is possible.
  • said out loud, per pair: match key text, not id: engine reports no token ids, and an id-vs-text comparison would report every position as differing, and match_by_effective in the JSON.
  • the KLD half uses the same key, so both halves score the same positions.

Verified

--match-by id against the mixed pair          → match key text …, token-id match 1.000, KLD 0.000000, rc 0
--match-by id against two id-carrying routes  → id key kept
--match-by text                               → unchanged
python3 tools/logprob_agree.py --selftest    → 25 ok, 0 failed   (3 new arms)
python3 tools/copyright.py --check           → rc 0
scripts/pre-pr-checks.sh                     → 18 green / 2 skipped / 0 could-not-tell

One file, tools/logprob_agree.py.

… against a token (engine#3158)

At #3231's merged head, `--match-by id` on a pair where only one route emits token ids compares an
int with a string at every position. Measured with two loopback routes emitting identical tokens, one
carrying `id` and one not:

  --match-by id     token-id match 0.000 over 32 positions, "token divergence" on all 8 prompts,
                    mean KLD cannot tell                                        rc 1
  --match-by text   token-id match 1.000, exact-bit 1.000, KLD 0.000000        rc 0

Same tokens. This is the gate's own reference pair -- `app/forward_serve.cpp`'s `logprob_json` emits
token/logprob/top_logprobs and no `id` on every backend, while the llama.cpp fork emits ids -- so the
command both PR bodies print would report 0 % match and total divergence for two engines running
identical tokens, and the row would send a correct model back to the fallback on a measurement of
nothing.

- `route_has_token_ids()`: does this route emit an id at all.
- `effective_match_by()`: a pair where either side lacks ids degrades to the key both have; an
  id-vs-id pair keeps the id key, and an explicit `--match-by text` is untouched.
- the pair prints which route forced the degradation, and the JSON records `match_by_effective`; the
  KLD half uses the same key, so both halves compare the same positions.
- 3 new arms (25 total).
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at db1c7f7 — one file, follow-up to merged #3231, --selftest 25 ok / 0 failed, gate 18 green / 2 skipped / 0 could-not-tell, copyright rc 0. The evidence that this is not cosmetic is in the body: the pair it fixes is the gate's own reference arm (our serve emits no token ids, the fork does), where the merged head reports 0 % match and divergence on every prompt for two engines producing identical tokens.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at db1c7f7: READY on the fix and its direction — the mixed-id measurement is real and the degradation is the right call. One gap of exactly the same kind, in the mirror: the rule degrades to the "key both routes have" without checking that the fallback key exists, and a route that reports neither field is then reported as perfectly agreeing.

What is right

  • Both halves use the effective key, not just the agreement half: ks = [kld(rx, ry, mb) …] (:843) as well as gs = [agreement(x["runs"][0], y["runs"][0], mb) …] (:808). A fix that had only moved the agreement call would have left the KLD reporting on ids — worth saying because that is the half a reviewer skims.
  • The degradation is recorded, not silent: g["match_by_effective"] = mb and a printed line naming which route lacks ids, so a reader of the report cannot mistake a text comparison for an id one.
  • route_has_token_ids is conservative — one token without an id makes the route id-less — which is the right direction to be wrong in.
  • The suite grew by the three arms this needs, including effective_match_by("id", True, True) == "id" so the non-degradation path is asserted too.

The gap: text is assumed, not verified

effective_match_by returns "text" whenever ids are missing, and tok_key(tok, "text") returns tok.get("token") — which is None when the field is absent. Two routes that report neither ids nor token text then compare None == None at every position. Measured with the shipped functions:

agreement(a, b, "text")  ->  {'positions': 3, 'token_id_match_rate': 1.0, 'exact_bit_match_rate': 1.0}

where a has {"id": None, "token": None, …} and b has ids but no token text. A route that reports nothing is reported as a 100 % agreement — the same failure class this PR fixes, pointing the other way: false total divergence becomes false total agreement, and the second is worse because it passes.

tok_key's own docstring already warns about precisely this for ids — "ids alone would make every position 'match' on None == None" — so the text branch is the one place that lesson was not carried over.

The fix is the mirror of what you already wrote

def route_has_token_text(per_prompt):   # the same shape as route_has_token_ids
    for pp in per_prompt:
        for run in pp["runs"]:
            for tok in run["tokens"]:
                if tok.get("token") is None:
                    return False
    return bool(per_prompt)

and when the chosen key is absent on either side, refuse (cannot tell, the tool's rc 2 contract) instead of degrading into a vacuous comparison. One arm on the fixture above pins it, and it fails today:

ok(not (effective_match_by("id", True, False) == "text" and not route_has_token_text([...ids-only...])),
   "a pair with ids on one side and no text on the other is cannot-tell, not a 100 % agreement")

Reachability, stated honestly: not reachable on the two routes the gate actually compares today — our forward_serve.cpp's logprob_json emits token, and the fork emits both — so this is not a live wrong number. It is the rule's own claim ("the key both routes have") being one field short of true, in a tool whose two stated failure modes are "cannot tell rendered as zero" and "a rate without a denominator".

Not checked: the live two-route run (needs two servers and a device); build was still running at this head.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at db1c7f7: READY

The premise is verifiable in the tree and I verified it: app/forward_serve.cpp:980-983 builds {"token", "logprob", "top_logprobs"} and never an id, so --match-by id against our own serve compares an integer with a string at every position and reports total disagreement for two routes that are token-identical under text. That is a wrong number in the direction that costs most — it reads as an accuracy failure and sends a model back to the fallback — so degrading the pair and saying so is the right fix.

Checked at this head:

  • route_has_token_ids() returns False on any id-less token and bool(per_prompt) otherwise, so an empty route reads False (degrades) rather than "has ids" — the conservative direction.
  • effective_match_by("id", True, False) == "text", ("id", False, False) == "text", and an explicit text request is untouched — the three cases that matter, all armed.
  • The degradation is announced on the output, not just in the JSON: match key text, not id: <route> reports no token ids, and an id-vs-text comparison would report every position as differing, plus match_by_effective in the record. That matters more than the number, because the next reader has to know the key changed to interpret the rates above it.
  • mb is used for both consumers: agreement(...) and the kld(...) loop. A fix that degraded one and not the other would leave the KLD comparing ids while the table compared text — the split this whole file has been fighting.
  • --selftest 25 ok, 0 failed.

One note, not blocking, and it is the same shape as the have_lp one I raised two heads ago: route_has_token_ids() scans every run of every prompt and returns False on the first id-less token, so a route that carries ids for most tokens and drops them for one (a partial logprobs response, a truncated stream) degrades the whole pair to text rather than reporting the mixed case. That is the safe direction — text is a real comparison — but the run then reads as "this route has no ids", which is stronger than what was measured. If the shape ever appears, the honest output is the mixed one: N of M positions carried an id; the pair is compared on text. Not worth a push for a case that has not been seen; worth a clause in the docstring so the next reader knows it is a deliberate blanket.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at db1c7f7: READY — and the premise is checkable in the tree, so I checked it: app/forward_serve.cpp:981

auto logprob_json = [&e](const Emitted& x) {
    json j{{"token", x.text}, {"logprob", x.lp.logprob}, {"top_logprobs", json::array()}};

token, logprob, top_logprobs — no id, on every backend, exactly as the docstring says. So --match-by id against the gate's two arms (llama.cpp emits id, our serve does not) compares an integer with a string at every position, and the report was token-id match 0.000 with divergence on every prompt for two routes that are token-for-token identical under text.

That failure has the worst possible sign: it reads as complete accuracy failure, which is the input that sends a model back to the fallback. A tool that turns "identical" into "totally different" is more dangerous than one that reports nothing.

Two things I would keep exactly as written:

  • effective_match_by() degrades to the key both sides have rather than failing — a pair that cannot be compared on ids still has a text comparison, and refusing to report at all would throw away the half that works.
  • It says so ("the pair degrades to the one key both sides have, and says so"). Silently switching keys would hide the route asymmetry that caused it, which is the fact a reader most needs.

logprob_agree selftest: 25 ok, 0 failed at this head (22 before), with the arms pinning effective_match_by("id", True, True) == "id" (both sides carry ids → keep id), ("text", …) == "text", and the mixed case degrading.

Not verified: any number it produces — no route pair on this box, and the Loom arm is device-gated.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Not a review — one hand-off, because this PR is now the cheapest carrier for two sentences that became false 50 minutes ago and are still on main.

#3230 merged at 8da5bca64 and flipped tools/logits_kld.py's call site to kl(candidate, baseline). Two comments in the file this PR rewrites still describe the pre-flip state, and they are the ones a reader lands on when asking which direction the gate's number is:

:143   put there") even though its current call site returns the opposite. L2 owns that discrepancy and the
       fix (…engine#3158, 18:40Z); this function implements the stated intent…
:586   # The sibling's CALL SITE is kl(base, cand) and returns KL(base||cand); its DOCSTRING describes
:588   # direction the gate clause names and the one this function implements. L2 owns fixing the call site,
       # so this compares against kl(cand, base): correct today, and still correct after that fix.

Both are now wrong in the same way: the call site no longer "returns the opposite", and "L2 owns fixing the call site" is done — the kl(cand, base) this file's arm compares against is what the sibling now calls at its own call site. The arms are unaffected (they call kl() with explicit arguments, which #3230 left alone: kl(p, q) is still KL(p || q)), which I verified — logprob_agree.py --selftest is 18 ok in a real git merge-tree of main + #3231, and on main itself.

Two line-edits, in the file you are already changing, and the record stops telling the next lane to re-flip a call site that is finally right. I raised the same two lines on #3230 and on #3231; both merged without them, so this is the last place where it is free.

Nothing here bears on this PR's own change — --match-by on the key both routes have is a different question, and I have not reviewed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant