Skip to content

spark-model(hy4): add chunk-aligned prefix caching (P2 #7) and process guards (P3 #10, #11) - #165

Merged
maplepaladin73 merged 13 commits into
mainfrom
devin/hy4-p2-prefix-cache-and-p3-guards
Nov 29, 2026
Merged

maplepaladin73 merged 13 commits into
mainfrom
devin/hy4-p2-prefix-cache-and-p3-guards

Conversation

@maplepaladin73

Copy link
Copy Markdown
Owner

Summary

  • P2 docs: refresh stale README claims against current recipes and code #7 (Chunk-Aligned Cross-Request Prefix Caching for Hy4Model):
    • Adds Hy4Model::enable_prefix_caching() wired from build_model when prefix_cache.is_active() (--enable-prefix-caching) or ATLAS_HY4_PREFIX_CACHE=1 (=0 kill-switch disables).
    • Preserves source.scratch.indices (2048 * 4 bytes) in AttentionCache::copy_committed_state_from alongside latent, pool, tail, len, and first_group_position.
    • Saves and restores 2-slot LRU PrefixCell snapshots strictly at non-final prefill chunk boundaries (sub_end < tokens.len(), exact multiples of the chunk width) so cold and warm executions traverse identical chunk boundaries and preserve circular SWA (576-slot) and DSA IndexPool states bit-for-bit.
    • Synchronizes Rank 0 and Rank 1 in lockstep via PREFIX_SAVE (10) and PREFIX_RESTORE (11) wire commands and records seq.cached_prefix_tokens.
  • P3 SWF(bug): stop item delete reviving expired conversations #10 (ATLAS_HY4_* Environment Variable Registry):
    • Documents all 17 ATLAS_HY4_* environment variables in crates/spark-model/src/layers/hy4/README.md with their default, validation PR, and graduation/expiry decision.
  • P3 SWF(bug): refresh conversation LRU order on writes #11 (Standalone HY4 Live Verification Gate):
    • Adds scripts/dev/verify_hy4_gate.py exercising width >= 512 prompt prefill, forced tool-call grammar (tool_choice="required"), and cross-request prefix-cache cold-vs-warm output parity.

maplepaladin73 and others added 12 commits November 29, 2026 08:12
…h-4096

# Conflicts:
#	crates/spark-model/src/layers/hy4/batch.rs
…-4096

# Conflicts:
#	crates/spark-model/src/layers/hy4/batch.rs
…hunk early-exit, and raise prefill_rows default to 4096
Prevents the scheduler's idle initial_chunk_budget (max_prefill_tokens + 1)
from splitting chunk 0 into 4096 + 1 rows and shifting subsequent chunk
boundaries off the prefill.capacity grid.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Prevents default thinking from consuming the 32/64-token probe budget in
Gate 1 and Gate 3.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Ensures every invocation of verify_hy4_gate.py measures a true cold miss on
request 1 followed by a warm prefix-cache hit on request 2.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@maplepaladin73

Copy link
Copy Markdown
Owner Author

Phase 2 & Phase 3 Live Validation on spark75 + spark74 (TP=2, GB10)

Fingerprint:

  • Image: atlas-hy4-flash:p2-prefix-7b541a11 (7b541a117734c3521e8c1f0eeb860b43ac57d861, code-identical to 9aad49de with script update)
  • Checkpoint: /home/spark/models/hy4/Hy4-flash-NVFP4-BF16ENG
  • Flags: --max-seq-len=65536 --max-prefill-tokens=4096 --max-batch-size=1 --max-num-seqs=1 --kv-cache-dtype=bf16 --attn-proj-dtype=bf16 --enable-prefix-caching=true --dfly-speculative --num-drafts=5
  • Env: ATLAS_HY4_TIMING=0, ATLAS_HY4_ENGRAM_NVMAP=1, ATLAS_HY4_ENGRAM_ROW_CACHE=1, ATLAS_PLE_FAULT_POOL=1, ATLAS_HY4_PREFILL_ROWS=4096

1. P2 #7: Chunk-Aligned Cross-Request Prefix Caching (--enable-prefix-caching=true)

Workload Prompt Tokens Cached Prefix Tokens (cached_tokens) Cold TTFT (eff tok/s) Warm TTFT (eff tok/s) Warm TTFT Speedup Cold == Warm Output Parity
19.4K Long DSA Recall (probe_dsa_long.py 1024 48) 19,379 16,384 (4 chunks) 18.229 s (1,063.1 tok/s) 3.412 s (5,679.7 tok/s) 5.34x (-14.82 s) 100% Bit-Exact (48/48 tokens, 'ANSWER: 342 Perth...')
23.2K OpenClaw Captured Turn (1795901442-000437.c2s) 23,174 20,480 (5 chunks) 22.214 s (1,043.2 tok/s) 3.230 s (7,175.1 tok/s) 6.88x (-18.98 s) 100% Exact (web_search({"query":"Qwen 4 release available Alibaba 2026","count":5.0}))
23.2K OpenClaw Follow-Up Turn (new user message) 23,205 20,480 (5 chunks) — 3.258 s (7,121.7 tok/s) 6.82x Emits valid web_search call (finish=tool_calls)
Gate 3 (verify_hy4_gate.py, 11.7K prompt) 11,671 8,192 (2 chunks) 11.083 s 4.296 s 2.58x 100% Exact ([PASS] Gate 3)

2. P2 #8: SWA Causal Tile Width Sweep (ATLAS_HY4_SWA_TILE_ROWS, bench_prompt_4096.txt 4,182 tok)

ATLAS_HY4_SWA_TILE_ROWS Median TTFT Median Prefill tok/s 3-Trial Rates (tok/s) probe_ids Token Parity
8 (legacy) 4.208 s 993.9 tok/s [996.6, 993.9, 993.3] [29206] (exact)
32 3.799 s 1,100.7 tok/s [1099.9, 1100.7, 1101.4] [29206] (exact)
64 (default) 3.749 s 1,115.6 tok/s [1115.8, 1113.9, 1115.6] [29206] (exact, optimal)
128 3.809 s 1,098.1 tok/s [1098.6, 1095.5, 1098.1] [29206] (exact)
256 3.845 s 1,087.8 tok/s [1092.7, 1087.4, 1087.8] [29206] (exact)

3. P3 #11: Standard HY4 Gate Script (scripts/dev/verify_hy4_gate.py)

[PASS] Gate 1 (wide prefill >= 1024 tok): prompt_tokens=2287, elapsed=2369.6 ms
[PASS] Gate 2 (HY4 tool-call grammar): query_cluster_node({"node_id": "spark-rank-0", "metric": "hbm_free"})
[PASS] Gate 3 (prefix cache parity): prompt_tokens=11671, cached_tokens=8192, cold=11082.7 ms -> warm=4296.2 ms
ALL HY4 GATES PASSED.

…AS_HY4_DSA_TILE_ROWS knob

- Extend HY4 prefix caching with 512-token tail-chunk granularity
  (PREFIX_TAIL_GRANULE = 512, MAX_PREFIX_SNAPSHOTS = 4) on multi-chunk
  prompts so warm turns cache up to 99%+ of the prompt instead of stopping
  at the previous 4096-token boundary, while preserving identical cold/warm
  chunk boundaries.
- Add ATLAS_HY4_DSA_TILE_ROWS (default 64, up to MAX_ROWS = 256) so DSA
  indexed causal tile width can be swept at runtime without rebuilds.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@maplepaladin73

Copy link
Copy Markdown
Owner Author

Phase 4 Update (a4ef2aae): 512-Token Tail Prefix-Cache Granularity, DSA Tile Sweep & DFly Depth Sweep

Fingerprint:

  • Image: atlas-hy4-flash:p4-tail-a4ef2aae (a4ef2aaeb272b059cbdb11b444b733927b29f54d)
  • Checkpoint: /home/spark/models/hy4/Hy4-flash-NVFP4-BF16ENG
  • Hosts: spark75 + spark74 (TP=2, EP=2, GB10)

1. 512-Token Tail Prefix-Cache Granularity (PREFIX_TAIL_GRANULE = 512, MAX_PREFIX_SNAPSHOTS = 4)

Extending prefix snapshots to the last 512-token boundary inside the final chunk (tail_cut = ((end - 1) / 512) * 512 on multi-chunk prompts > 4096 tokens) raises cache coverage on 19K–23K turns to 97.8%–99.4% and brings warm TTFT under 1 second:

Workload Prompt Tokens cached_tokens (%) Cold TTFT (eff tok/s) Warm TTFT (eff tok/s) Warm TTFT Speedup Cold == Warm Parity
23.2K OpenClaw Captured Turn (1795901442-000437.c2s) 23,174 23,040 (99.4%) 22.457 s (1,031.9 tok/s) 0.588 s (39,389 tok/s) 38.2x (-21.87 s) 100% Exact (True: web_search args + reasoning identical)
23.2K OpenClaw Follow-Up Turn (new user turn) 23,205 23,040 (99.3%) — 0.645 s (35,980 tok/s) (wall 4.12 s) 34.8x Emits valid web_search call (finish=tool_calls)
19.4K Long DSA Recall (probe_dsa_long.py 1024 48) 19,379 18,944 (97.8%) 18.428 s (1,051.6 tok/s) 0.904 s (21,437 tok/s) (wall 2.37 s) 20.4x (-17.52 s) 100% Bit-Exact (48/48 tokens, 'ANSWER: 342 Perth...')
Gate 3 (verify_hy4_gate.py, 11.7K prompt) 11,671 11,264 (96.5%) 11.640 s 1.584 s 7.35x 100% Exact ([PASS] Gate 3)
bench_prompt_4096.txt (4,182 tok, Trials 1–3 warm) 4,182 4,096 (97.9%) 3.753 s (1,114.4 tok/s) 0.490 s (8,535 tok/s) 7.66x 100% Bit-Exact (probe_ids = [29206])

2. DSA Causal Tile Width Sweep (ATLAS_HY4_DSA_TILE_ROWS = {64, 128, 256})

ATLAS_HY4_DSA_TILE_ROWS Warm 4.2K Median TTFT Warm 4.2K Effective tok/s probe_ids Token Parity
64 (default) 0.490 s 8,535.0 tok/s [29206] (exact)
128 0.491 s 8,510.6 tok/s [29206] (exact)
256 0.492 s 8,498.4 tok/s [29206] (exact)

3. DFly Speculative Draft Depth Sweep (--num-drafts = {5, 6, 7}, decode_real.py 4 Prompts x 2 Repeats)

--num-drafts Overall Mean tok/s Overall Median tok/s code-limiter code-topk edit-json edit-sql Token Parity vs d=5
5 (default) 31.48 tok/s 31.92 tok/s 31.88 tok/s 24.44 tok/s 37.54 tok/s 32.07 tok/s Baseline (4/4 exact)
6 30.73 tok/s 30.48 tok/s 31.82 tok/s 22.07 tok/s 39.17 tok/s 29.84 tok/s 4/4 bit-exact
7 32.50 tok/s 33.83 tok/s 30.80 tok/s 21.08 tok/s 41.53 tok/s 36.58 tok/s 4/4 bit-exact
  • Decision: Keep --num-drafts=5 as the general-purpose default because it maximizes lower-acceptance code synthesis (code-topk: 24.44 vs 21.08 tok/s), while --num-drafts=7 is validated bit-exact and reaches 41.5 tok/s on structured JSON/SQL edits.

@maplepaladin73
maplepaladin73 merged commit dc314e4 into main Nov 29, 2026
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