Skip to content

spark-model: fix Marconi warm-replay KV/layout divergence (issue #158) - #171

Closed
AzeezIsh wants to merge 2 commits into
mainfrom
fix/158-marconi-kv
Closed

AzeezIsh wants to merge 2 commits into
mainfrom
fix/158-marconi-kv

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Summary

Fixes the temp-0 divergence between prefix-caching-on and caching-off runs
reported in #158 — the Flash-Next tool round-trip that re-called the tool
instead of answering.

Two independent sources made a warm turn compute a different trajectory:

  1. Single-stream Marconi replay suppressed KV writes below
    cached_prefix_tokens (forward_layers.rs). The replay recomputes hidden
    states under its own pass layout, so attention K/V for [snap_tok, matched) kept values computed under the original layout — an
    inconsistent pair at a near-tied argmax. The batched path already rewrites
    the replayed range (kv_write_start_eff = 0); the single-stream path now
    matches it.
  2. The tail-checkpoint split was gated on prefix_cache.is_active()
    (prefill_b.rs), so caching-off prefills took a single-pass layout while
    caching-on split at floor((N-1)/bs)*bs - bs. Same prompt, two pass
    layouts, temp-0 flips. The split is now a function of absolute token
    position only (SSM models; the checkpoint save still gates on an enabled
    snapshot pool). Cost: +1 small pass on caching-off single-shot prefills —
    the price of cold/warm/on/off sharing one layout.

What this does not cover: a replay anchored deeper than the tail
checkpoint still starts passes at the snapshot position rather than on the
cold chunk grid — the full fixed-grid hardening from the issue's suggestion 1
is follow-up scope if the residual is measurable.

Test plan

  • GB10: serve nvidia/Qwen3.8-Flash-Next-NVFP4 + issue repro (Ottawa
    anchor → Tokyo round-trip) — must answer, not re-call
  • Same repro on the unfixed binary — should still show the bug
  • cargo fmt --check + cargo clippy --workspace clean
  • Benchmark-gate records on the PR head

Verification runs queued on reiner (jobs r158fix2/r158base3). Credit for the
root-cause dissection and the fix direction: the issue authors (Marconi team,
andrew-vogulkin/atlas) — their A1–A4 arm matrix isolated both mechanisms.

Generated with Devin

Two sources made a prefix-cache warm turn compute a different trajectory
than a caching-off run of the same prompt:

1. Single-stream marconi replay suppressed KV writes below
   cached_prefix_tokens, so the replayed tokens' K/V stayed at values
   computed under the original pass layout while the SSM state came from
   the replay layout — an inconsistent pair that flips near-tied argmax
   at temp 0. The batched path already rewrites from 0; match it.

2. The tail-checkpoint split was gated on prefix_cache.is_active(), so
   caching-off prefills took a single-pass layout while caching-on split —
   the same prompt produced different hidden states between the two
   modes. Make the split a function of absolute token position only
   (fire whenever the model has SSM layers; the checkpoint save inside
   already gates on an enabled snapshot pool).
@github-actions

Copy link
Copy Markdown

Thanks for the contribution. Before we can merge, please sign our Contributor License Agreement by replying to this comment with:

I have read the CLA Document and I hereby sign the CLA


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

strix 27B leg for #158 (AzeezStrix, gfx1151, 64 GB, Linux ROCm 7.13): on this box cache-on ≠ cache-off persists with #171. Its effect is on the cache-OFF output only.

Harness: the strix #158 battery (job 175's battery.py). That's 12 tool-call cases behind a ~6.2k-token, 126-rule system prompt with two tools, at temp 0 and max_tokens 160. r1 is the first turn and r2 the turn after the tool result, so 24 responses per arm. Model nvidia/Qwen3.8-27B-NVFP4, ./build-amd.sh, util 0.80, seq 32768, MAX_PREFILL_TOKENS 1024, SSM_SLOTS 4 / ckpt 512, --disable-thinking. Cache ON = --enable-prefix-caching (r2 reuses 6240 cached tokens).

build speculation ON vs OFF identical differing (all r2)
main 7ffd183f2 (job 228) DFlash2 γ=8 21/24 Reykjavik, C-310, D-9
#171 b6a5c17a7 (job 226) DFlash2 γ=8 21/24 C-310, D-9, Nairobi
main-equivalent¹ (job 232) none (NUM_DRAFTS=0) 22/24 Cairo, B-77
#171 b6a5c17a7 (job 233) none 20/24 Cairo, B-77, D-9, Nairobi

¹ #163 in default mode. It was byte-identical to main in both arms under DFlash2 (OFF 24/24, ON 24/24). I did not boot main itself without speculation.

Controls. Identical code paths reproduce byte-for-byte across separate boots and builds:

So the ON/OFF differences are path effects, not run-to-run noise. Every r1 is identical in every arm, and no tool call differs.

What #171 changes here.

  • Cache-ON: output is byte-identical to main in both configs.
  • Cache-OFF: output changes on 4 r2 vs main under DFlash2 (Reykjavik, C-310, D-9, Nairobi) and on 2 vs the main-equivalent without speculation (D-9, Nairobi).

That fits fix (2): the tail split now also fires on caching-off prefills and moves the cold layout. Fix (1) didn't change the warm output on this battery. The net ON==OFF count doesn't improve on this box: 21 → 21 with DFlash2, 22 → 20 without speculation. Since the mismatch survives with speculation off, it sits in the cache path, not the DFlash2 verify path.

Most diffs are wording-level ("October 1" vs "Oct 1", bullet list vs inline ETA). One is not. With speculation off, the cache-ON Cairo r2 is "Cairo: 31°C, sunny.\n\n\n\nassistant\n<tool_response>", while cache-OFF gives "Cairo is currently sunny with a temperature of 31°C.". It's the same text on #171 and on main-equivalent, so it predates this PR, but it looks like a damaged warm state rather than a near-tie.

ON arms log Marconi hits=12 and no SSM snapshot=11 on every build. 0 faults on every arm.

These are observations from one box with one run per arm. Artifacts on strix are under ~/dp4a-ab/out/: bat-main-27b-20261014T0844Z, bat-171-27b-20261014T0737Z, bat-163-ns-27b-20261014T1545Z, bat-171-ns-27b-20261014T1625Z (each has bat-*.json per arm, serve logs and STATUS).

@github-actions github-actions Bot added the needs-rebase Conflicts with main — its green checks are NOT real (GitHub skips merge-commit workflows) label Oct 16, 2026
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Closing in favour of #163 for #158. On the strix 27B battery this change left cache-on != cache-off (21/24 under DFlash2, 20/24 without speculation) and its effect landed on the cache-OFF output; #163's --exact-prefix-cache makes on == off on GB10 (24/24) and is the vehicle to finish. Branch kept for reference.

@AzeezIsh AzeezIsh closed this Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-rebase Conflicts with main — its green checks are NOT real (GitHub skips merge-commit workflows)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants