Skip to content

fix(kv-cache): audit KV phase policy & multi-turn prefix reuse metrics (#135, #137) - #138

Merged
zeekay merged 1 commit into
mainfrom
fix/kv-phase-audit-135-137
Nov 23, 2026
Merged

zeekay merged 1 commit into
mainfrom
fix/kv-phase-audit-135-137

Conversation

@zeekay

@zeekay zeekay commented Nov 23, 2026

Copy link
Copy Markdown
Member

Summary (2026-11-22)

Fixes #137
Addresses #135

Implements the measurement methodology, portable CPU active-FP8 coverage, and statistical confidence-interval fixes identified in the v1.7.109 evidence audits (#135, #137):

  1. Portable CPU KV-Phase Evaluation (hanzo-engine/src/kv_cache/phase_policy.rs):
    • Fixes evaluate_single_kv_phase_point_portable so bf16_prefill_us, fp8_always_prefill_us, and phase_aware_prefill_us time genuine causal prefill (seq_q = prefill_seq_len > 1 with explicit causal masking) instead of copying single-token decode timings.
    • Adds bench_portable_op_us with 3 untimed warmup passes + median of 5 timed repetitions (matching CUDA's warmed median methodology).
    • Adds decode_speedup_is_native: bool (true on CUDA native FP8 E4M3 paged decode kernel; false on portable CPU where FP8 E4M3 dequantizes to F32) so non-CUDA timing ratios are never conflated with native FP8 bandwidth speedup.
  2. Active FP8 Regime in CPU CI Gate (hanzo-engine/tests/hybrid_prefix_and_kv_phase_eval.rs, Sci: CPU KV-phase gate never tests the FP8-active regime; CPU decode_speedup is a timing artifact #137):
    • Extends hybrid_prefix_and_kv_phase_cpu_gate context grid from &[256, 512] to &[256, 512, 2048] (min_fp8_decode_ctx = 2048).
    • Adds phase_aware_used_fp8: bool to KvPhasePointMetrics and asserts phase_aware_used_fp8 == false below 2048 and phase_aware_used_fp8 == true at ctx = 2048 with fp8_decode_cosine_vs_f32 >= 0.999 and decode_speedup_is_native == false.
  3. Analytic vs Measured KV Footprint Accounting (Evidence audit of 1.7.109 claims: PhasePolicy 2.0x KV memory is analytic, 6.78x TTFT is a single-sample synthetic microbench; log release benchmarks to /v1/research #135):
  4. Multi-Turn Hybrid Prefix Reuse Warmup + 10-Repetition 95% Student-$t$ CIs (Evidence audit of 1.7.109 claims: PhasePolicy 2.0x KV memory is analytic, 6.78x TTFT is a single-sample synthetic microbench; log release benchmarks to /v1/research #135):
    • Adds repetitions: usize to HybridEvalConfig (10 default for CUDA benchmark runs; 3 in the fast CPU gate) and 1 full untimed multi-turn warmup pass before timed repetitions.
    • Adds SummaryStats (n, median, mean, std_dev, ci95_low, ci95_high using exact two-sided 95% Student-$t$ critical values) and per-repetition sample vectors (cold_ms_samples, warm_ms_samples, cumulative_cold_ms_stats, cumulative_warm_ms_stats, cumulative_ttft_speedup_stats), plus is_synthetic_microbench: true.

Verification (2026-11-22, dgx NVIDIA GB10 sm_121 CUDA 13.4)

1. CPU Gate (cargo test -p hanzo-engine --test hybrid_prefix_and_kv_phase_eval & --lib kv_cache::phase_policy::tests)

  • hybrid_prefix_and_kv_phase_cpu_gate ... ok (ctx = [256, 512, 2048], phase_aware_used_fp8 = true at 2048, fp8_decode_cosine_vs_f32 >= 0.999, decode_speedup_is_native = false)
  • kv_cache::phase_policy::tests::kv_phase_policy_parsing_and_phase_selection ... ok
  • kv_cache::phase_policy::tests::cpu_hybrid_prefix_isolation_and_multiturn_parity ... ok

2. 10-Repetition Warmed CUDA Evaluation on GB10 (--release --features cuda)

  • Provenance Isolation: owner_hit = 80, cross_tenant = 0, cross_model = 0, cross_adapter = 0, boundary = 0, post_mutation_max_rel = 0.0
  • Synthetic 5-Turn Hybrid Prefix Reuse (1 warmup sequence + 10 timed sequences):
    • BF16: median_cold = 2588.48 ms (95% CI [2446.54, 2816.26] ms), median_warm = 383.43 ms (95% CI [304.60, 429.54] ms), median_speedup = 6.73x (mean = 7.88x, SD = 3.52x, 95% CI [5.36x, 10.40x]), gdn_state_max_rel = 0.0, conv_state_max_rel = 0.0.
    • F32: median_cold = 3026.50 ms (95% CI [2767.91, 3148.61] ms), median_warm = 385.40 ms (95% CI [320.11, 443.81] ms), median_speedup = 7.55x (mean = 8.13x, SD = 2.07x, 95% CI [6.66x, 9.61x]), attn/gdn/conv max_rel <= 4.73e-6.
  • Phase-Aware KV Precision (head_dim in {64, 128, 256} x ctx in {4096, 8192, 16384, 32768}, 36 layers):
    • All 12 points passed with phase_aware_used_fp8 = true, decode_speedup_is_native = true, fp8_decode_cosine_vs_f32 in [0.999571, 0.999687].
    • At ctx = 32768, d = 128: analytic_kv_bytes_ratio = 2.0, measured_bf16_kv_alloc_bytes = 1,208,549,376 B, measured_fp8_kv_plus_scale_bytes = 604,275,264 B, measured_kv_alloc_bytes_ratio = 1.999998x, decode_speedup = 1.15x (1.75x-2.38x at 4K-16K).

#135, #137)

- Fix portable CPU KV-phase evaluation in phase_policy.rs: measure causal
  prefill (seq_q > 1) separately from single-token decode (seq_q = 1) with
  3 untimed warmup runs + median of 5 timed repetitions (bench_portable_op_us).
- Add explicit provenance/fidelity metadata to KvPhasePointMetrics:
  phase_aware_used_fp8, decode_speedup_is_native (true on CUDA, false on
  portable CPU where FP8 E4M3 dequantizes to F32), analytic_kv_bytes_ratio
  (2.0), measured_bf16_kv_alloc_bytes, measured_fp8_kv_plus_scale_bytes,
  and measured_kv_alloc_bytes_ratio (including scale tensors & 256B CUDA
  allocator rounding).
- Extend CPU CI gate in tests/hybrid_prefix_and_kv_phase_eval.rs to
  contexts &[256, 512, 2048] so PhaseAware (min_fp8_decode_ctx=2048)
  exercises the active FP8 decode path and verifies cosine >= 0.999.
- Upgrade evaluate_multiturn_hybrid_prefix_reuse with 1 full untimed
  warmup sequence + configurable repetitions (10 on CUDA, 3 on CPU gate),
  SummaryStats (median, mean, sample SD, 95% Student-t CI), and explicit
  is_synthetic_microbench = true tagging.

Fixes #137
Addresses #135
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.

Sci: CPU KV-phase gate never tests the FP8-active regime; CPU decode_speedup is a timing artifact

1 participant