Skip to content

docs(2778): land the KV-contents reproducer and its spec - #3077

Merged
inureyes merged 3 commits into
lablup:mainfrom
hephaex:docs/2778-kv-contents-reproducer
Nov 19, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
hephaex:docs/2778-kv-contents-reproducer

Conversation

@hephaex

@hephaex hephaex commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

What

Two deliverables that were prepared for #2778 but never committed, plus the
doc update that becomes correct once the harness is in the tree.

  • examples/kv_history_divergence.rs — the target-only harness the issue asks
    for: it compares logits and KV contents of a plain one-token-append
    history against a block-built history, instead of holding the cache fixed.
    Auto-discovered by Cargo (no manifest change; same as logit_trace.rs).
  • docs/kv-contents-reproducer-spec.md — what a decisive answer requires,
    the three admissible outcomes for block width M vs decode M = 1
    (equal / rounding-equivalent / corrupted), the measurement method, and
    explicit limits on what the numbers license.
  • docs/temperature-0-block-history-flip.md — the harness was previously
    described as "posted on Explain the temperature 0 flip between plain and block-built KV history #2778, not in this tree". It is in this tree now,
    so the bullet is rewritten to say that, to scope this document's own claim
    correctly ("its own evidence never touches the cache"), and to point at the
    spec.

Measured (each figure names its own run)

Width-axis sweep, Qwen3-0.6B-4bit, M1 Pro, 64-token context, 64-token span:

history width traced positions with KV deltas differing bytes max abs logit delta
1 / 2 / 4 / 8 0 0 0
16 4 7,946,151 0.343750
32 2 4,778,963 0.343750
64 1 3,201,507 0.218750

Second run, W = 16, 64-token context, 448-token span: 28 of the 448
positions carry differing KV bytes, kv_rms_rel reaches 0.0455 with up to
73.97% of elements perturbed, and a whole-vocabulary logit delta of
2.757812 at pos 352. Across all 28 diverging positions the top-1 logit
consumes at most 5.455% of its top-2 margin; margin_consumed_positions
is 0.

What this does and does not claim

  • It does not reproduce the reported flip: positions_with_flip is 0 over
    the 448-token span. An absolute reproduction needs the reporter — the
    original Qwen3 copy prompt text, token ids and length are not in the
    issue, and the reported condition is M1 Ultra Metal while these
    measurements are M1 Pro Metal.
  • It does put the issue's reopening trigger, a reproducer showing
    corrupted KV contents, live on the width axis: plain and block-built float
    KV histories are not byte-identical beyond rounding.
  • Open evidentiary gap, stated in the spec: nothing in the 17-column schema
    proves both arms wrote the same token to the same slot.
    arm_repeats_identical proves one arm reproduces itself, and the zero
    row at W <= 8 proves the harness does not manufacture divergence —
    both necessary, neither sufficient.

Note for anyone consuming the trace output: the schema is 17 columns; d_top1
is field 11 and flip moved from 16 to 17.

Refs #2778

Add examples/kv_history_divergence.rs, the target-only harness that compares
logits and KV contents of plain and block-built histories for lablup#2778, and
docs/kv-contents-reproducer-spec.md, which states what a decisive answer
requires and what the measurements so far do and do not license.

Update docs/temperature-0-block-history-flip.md: the harness is now in the
tree, so drop the "not in this tree" note, scope the document's own claim to
"its own evidence never touches the cache", and record the width-axis and
448-token-span results together with their exact scopes.

Refs lablup#2778
@inureyes inureyes added status:review Under review type:docs Documentation improvements or additions priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Nov 19, 2026
…gence

The harness decoded every 2-byte KV element as fp16, so a bf16 cache (the common case for mlx-community checkpoints) produced meaningless kv_max_abs, kv_rms and kv_rms_rel values. It also read int8 as unsigned, compared byte runs of possibly different shapes by truncating zip, and reported "no divergence" when the model owns its caches and the snapshot held no KV at all.

The example now names the dtype from the MLX code and decodes float16, bfloat16, float32 and signed int8, rejecting anything else. It requires matching layer counts, dtypes and shapes in both arms, and errors when no layer exposes readable KV.

Docs: the spec is unwrapped, em dashes removed, stale cache.rs line anchors replaced by symbol names, and its status line now says the harness exists while the dump and comparator are still proposals. Corrected two factual errors: the harness traces every W-th history length (the 28 positions at W = 16 over 448 tokens are all traced positions, not a filtered subset), and the quoted "left open" status comment was not written by the reporter. Non-byte-identical KV rules out outcome 1 but does not trip the reopening trigger by itself; both docs now say so.

Refs lablup#2778
@inureyes

Copy link
Copy Markdown
Member

Hardening summary

Merged current main into the branch (merge commit, no rebase) and pushed 33da75e.

Harness fixes in examples/kv_history_divergence.rs:

  • Every 2-byte KV element was decoded as fp16, so a bf16 cache gave meaningless kv_max_abs / kv_rms / kv_rms_rel. The dtype is now read from the MLX code and decoded as float16, bfloat16, float32 or signed int8; any other code is an error. # kv_dtype prints the name instead of the raw integer.
  • int8 bytes were read as unsigned (-1 vs 0 counted as 255 steps). Now signed.
  • A model that owns its caches (or a packed mode the extractor returns as (None, None)) used to print "no divergence". It now errors. Confirmed on qwen3.5-4b-4bit, which hits that path.
  • Both arms must now agree on layer count, per-layer dtype and K/V shape before bytes are compared; mismatches used to be truncated silently by zip.
  • Stale logit_trace.rs line references replaced by symbol names; dead let _ = &ab; removed.

Doc corrections:

  • The harness traces every W-th history length, so "28 of the 448 positions carry differing bytes" was really 28 of 28 traced positions. Fixed in both docs.
  • KV that is not byte-identical rules out the spec's outcome 1 but does not separate rounding-equivalent from corrupted (outcomes 2 and 3), so it does not by itself trip the reopening trigger. Both docs now say that instead of "trigger live".
  • The quoted "left open" status comment was attributed to the reporter; it was not.
  • The spec status said "No new code"; it now says the harness exists and the dump and comparator are still proposals. Spec prose unwrapped, em dashes removed, stale cache.rs line anchors replaced by symbol names, two redundant paragraphs cut.

Validation (M3 Ultra, Metal, under the GPU lock): example builds and passes clippy -D warnings and fmt. olmoe-1b-7b-0125-4bit (fp16 KV, context 64, span 64): W=1 gives 0 KV diff positions; W=8 gives 8/8, worst logit delta 0.028; W=16 gives 4/4, delta 0.10; no flips, arm_repeats_identical true. On this host and model the first divergence is already at W=8, not W=16 as on Qwen3/M1 Pro, so the threshold is model or hardware dependent. gemma-3n-e2b-it-4bit exercises the bf16 path with plausible values (peak 5.875, max delta 0.54).

This PR delivers the harness, not the criterion: the pos 197 swing is still unreproduced, so it stays Refs #2778.

@inureyes
inureyes merged commit 3c0d74e into lablup:main Nov 19, 2026
32 checks passed
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:medium Medium priority status:review Under review type:docs Documentation improvements or additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants