Skip to content

Commit 3663e39

Browse files
Mac K2.A reviewer: split research A/B vs product-shape smoke (per user directive)
Per user directive 2026-06-09: '在Mac mini上的k2.a.1的测试脚本严重浪费时间。跑大量无用用例, 而且完全不顾及真是的用户对时延的体验。按照上边的建议进行修改。' Diagnosis the user reached: K2.A.1's product question is binary correctness — does KL ON preserve recall? Is the bf16 dtype crash fixed? — not a statistical research question. The Mac M4 A/B reviewer (review_pr_k2a1_integration_on_mac.sh) takes 7-9h to answer the wrong question for the product: * 20 samples ≠ 1 user request * KL OFF / oracle / v0.3 baselines are research-only — they are NOT on the production path (KL ON + K2.A.2 stateful) * mean throughput across 20 samples masks first-token latency that users actually feel User's product-aligned shape (verbatim): * Don't run oracle / v0.3 / A/B; only one production arm * Don't run 20 samples; single request * K2.A.2 stateful caching, no per-step recompression * Eventually MLX/Metal — not the current PyTorch MPS research harness (acknowledged this is a K3 deliverable) * Long history: incremental update only; do not rebuild full context every step Three changes in this PR: 1. NEW scripts/review_pr_k2a_production_smoke_on_mac.sh ~120 LOC. Single-request K2.A production-shape smoke. Wraps scripts/research/k1e_niah_validation.py with the product-aligned constraint set: --n-samples 1 single request, NOT 20 --skip-oracle no oracle arm --skip-v03 no v0.3 arm --kl-on KL compression on (production path) --stateful K2.A.2 incremental decode (default ON) Default 5.6k context (CTX_LINES=280); ~3-5 min on Mac M4 24 GB. Reports recall hit/miss, prefill latency, first-token latency, mean tok/s, peak resident memory. Time budget listed for 1.4k / 5.6k / 16k context rungs so the user can pick what matches their request distribution. Explicitly documents what it does NOT validate: - statistical recall delta (that's the A/B's job) - throughput parity vs full-attention oracle (gate (c) work for K2.A.2 + K3) - MLX/Metal native latency — this is PyTorch MPS, the conservative upper bound; K3 MLX path will be faster 2. AMENDED scripts/review_pr_k2a1_integration_on_mac.sh Two banner additions: (a) Header docstring ('READ THIS BEFORE RUNNING'): explains this is a 7-9h RESEARCH A/B for PR-K2.A.1 binding gate (b) recall delta evidence — NOT a product-latency signal. Points users to review_pr_k2a_production_smoke_on_mac.sh if that's what they wanted. (b) Runtime banner with 10-second sleep: prints the same warning as the script starts running, with a Ctrl-C window so users who launched the wrong script can abort before they've burned hours of M4 time. The A/B itself is unchanged in semantics — it remains the binding evidence collector for PR-K2.A.1 merge (gate (b) recall delta ≤ 1pp at every rung, ADR §11.8 criterion 1a). Cannot be deleted; can be made impossible to run by accident. 3. AMENDED docs/adr/0008-session-bound-runtime-and-grpc-protocol.md §11.11 K2.A.1 reviewer-scripts subsection now lists three reviewer scripts (vast A/B, Mac A/B, Mac product smoke) with a 'scope split' table making explicit: A/B script binding gate (b) evidence ~7-9h production smoke user-facing latency + recall ~3-5min Plus a paragraph (recorded as the user's directive) stating that the A/B's KL OFF / oracle / v0.3 arms are research-only and 'must not be conflated' with product validation. Future contributors can read this section instead of relearning the lesson. Tests: tests/inference_engine/v04/: 253 passed (no v04 code changes). bash -n syntax check on both Mac reviewer scripts: OK. Compat: * No code-path changes. K1.E runner (k1e_niah_validation.py) is untouched; both scripts use its existing flags (--n-samples / --skip-oracle / --skip-v03 / --kl-on / --stateful / --kl-lattice / --kl-q-range). * The A/B reviewer's default behaviour is unchanged — same flags, same time budget. Only the banner differs. * The new product smoke script is additive; it cannot affect the A/B's binding gate evidence path. Stack: Off main, parallel to (does not depend on, does not block): AgentMemory/v04-pr-k3-block-b-prereqs-1-4-dflash-loader-8e7f (K3 prereq 1+4 work, currently stashed; will resume after this) Net effect: Mac M4 reviewers now answer two distinct questions cleanly. Users wanting the 'does this work for my latency' answer get it in ~3-5 min instead of being told to run a 7-9h script whose stated purpose is statistical research evidence. Co-authored-by: FluffyAIcode <[email protected]>
1 parent e8136a8 commit 3663e39

3 files changed

Lines changed: 294 additions & 5 deletions

File tree

docs/adr/0008-session-bound-runtime-and-grpc-protocol.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,10 +2293,40 @@ scripts + tests).** What it delivers:
22932293
schema bumps 4 → 5 to record the KL config block.
22942294
* Reviewer scripts:
22952295
- `scripts/review_pr_k2a1_integration_on_vast.sh` — vast.ai
2296-
CUDA A/B at the §11.12 ladder.
2296+
CUDA A/B at the §11.12 ladder. **Research evidence
2297+
collector** (statistical, ~hours).
22972298
- `scripts/review_pr_k2a1_integration_on_mac.sh` — Mac M4
22982299
(PyTorch MPS) A/B at the small-end §11.12 rungs (1.4k +
2299-
5.6k by default).
2300+
5.6k by default). **Research evidence collector**
2301+
(statistical, ~7-9h). Banner at runtime warns users who
2302+
ran it expecting product-shape latency.
2303+
- `scripts/review_pr_k2a_production_smoke_on_mac.sh`
2304+
**product-shape smoke** (added 2026-06-09 per user
2305+
directive). Single request, KL ON + K2.A.2 stateful only,
2306+
no oracle / v0.3 / KL OFF arms, no statistical averaging.
2307+
Reports first-token latency, recall hit/miss, peak resident
2308+
memory. ~3-5 min @ 5.6k context on Mac M4 24 GB.
2309+
2310+
**Scope split (recorded 2026-06-09)**: research A/B and
2311+
product-shape smoke answer different questions and **must
2312+
not be conflated**:
2313+
2314+
| Script | Question | Time |
2315+
|---|---|---|
2316+
| `..._k2a1_integration_on_mac.sh` | Statistical recall delta (ADR §11.8 1a binding gate) | ~7-9h |
2317+
| `..._k2a_production_smoke_on_mac.sh` | User-facing first-token latency + recall hit + dtype crash | ~3-5min |
2318+
2319+
The A/B is necessary for PR-K2.A.1 merge evidence (binding
2320+
gate (b) recall delta ≤ 1pp at every rung needs sample
2321+
distribution). The product smoke is necessary for honest
2322+
release-readiness signal — it answers "if a user sends one
2323+
request through this stack on Mac, what do they wait for".
2324+
Mean throughput across 20 samples masks first-token latency
2325+
that users actually feel; the A/B's KL OFF / oracle / v0.3
2326+
arms are not on the production path; running them as a
2327+
proxy for product validation **wastes time and does not
2328+
produce the answer the question is asking**. Per the user's
2329+
directive: do not use the A/B as a product-experience signal.
23002330

23012331
K2.A.1 acceptance gates (per §11.11.5 above): **gate (a)
23022332
round-trip identity** is closed by the K2.A.0 Mac smoke

scripts/review_pr_k2a1_integration_on_mac.sh

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
#!/usr/bin/env bash
2-
# Mac M4 reviewer aid for PR-K2.A.1 — KakeyaLattice integration
3-
# A/B at the §11.12 ladder rungs Mac M4 24 GB can handle.
2+
# Mac M4 RESEARCH A/B for PR-K2.A.1 KakeyaLattice integration —
3+
# §11.12 ladder rungs Mac M4 24 GB can handle.
4+
#
5+
# ============================================================
6+
# READ THIS BEFORE RUNNING (added 2026-06-09 per user directive)
7+
# ============================================================
8+
#
9+
# This is a research evidence collector with a 7-9h time budget.
10+
# It runs 20 samples × 2 context rungs × {KL OFF, KL ON} × {oracle,
11+
# v0.3, v0.4} arms because the BINDING gate for PR-K2.A.1 merge
12+
# (recall delta ≤ 1pp at every rung — see ADR §11.8 criterion 1a)
13+
# needs statistical signal across a sample distribution.
14+
#
15+
# It deliberately does NOT reflect user-facing latency:
16+
# * 20 samples ≠ 1 user request
17+
# * KL OFF / oracle / v0.3 baselines are research-only — the
18+
# production path is KL ON + K2.A.2 stateful (incremental
19+
# decode), not stateless A/B
20+
# * mean throughput across 20 samples masks first-token latency
21+
#
22+
# If you want to measure user-facing latency for the K2.A
23+
# production path, run instead:
24+
#
25+
# bash scripts/review_pr_k2a_production_smoke_on_mac.sh
26+
#
27+
# (single request, KL ON + stateful, ~3-5 min on Mac M4 24 GB at
28+
# 5.6k context. Report includes first-token latency and dtype-crash
29+
# regression check.)
30+
#
31+
# Continue reading only if you actually need the A/B research
32+
# evidence for PR-K2.A.1 merge (gate (b) recall delta).
33+
# ============================================================
434
#
535
# Mirror of scripts/review_pr_k2a1_integration_on_vast.sh, but
636
# scoped to Mac M4 budget: default ladder '70 280' (~1.4k +
@@ -166,7 +196,20 @@ run_one_rung() {
166196
fi
167197
}
168198

169-
echo "==> PR-K2.A.1 KakeyaLattice integration A/B — Mac M4"
199+
cat <<'BANNER'
200+
==> PR-K2.A.1 KakeyaLattice integration A/B — Mac M4
201+
+------------------------------------------------------+
202+
| This is a RESEARCH A/B (~7-9h on Mac M4 24 GB). |
203+
| It does NOT reflect user-facing latency. |
204+
| |
205+
| For product-shape latency on Mac, run instead: |
206+
| bash scripts/review_pr_k2a_production_smoke_on_mac.sh |
207+
| |
208+
| Press Ctrl-C in the next 10 s to abort if you meant |
209+
| the production smoke. |
210+
+------------------------------------------------------+
211+
BANNER
212+
sleep 10
170213
echo " Model: google/gemma-3-1b-it"
171214
echo " Samples / arm: $N_SAMPLES"
172215
echo " Sink x window: ${SINK} x ${WINDOW}"
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/usr/bin/env bash
2+
# Mac M4 K2.A production-shape smoke (per user directive 2026-06-09).
3+
#
4+
# This script answers the **product-aligned** question: under a
5+
# realistic single-user single-request shape, does the K2.A KL ON +
6+
# K2.A.2 stateful path deliver acceptable behaviour on Mac M4?
7+
#
8+
# Specifically:
9+
#
10+
# * Recall hits the needle once per request (not statistical
11+
# averaging over 20 samples).
12+
# * Per-step dtype crash (the bf16 / fp32 round-trip failure
13+
# fixed by PR #87) does NOT recur on MPS bf16.
14+
# * First-token latency is recorded so we can see what the user
15+
# actually waits for, not just a research mean throughput.
16+
#
17+
# What this script does NOT validate (deliberately):
18+
#
19+
# * Statistical recall delta over a sample distribution. That's
20+
# a research A/B question, answered by
21+
# scripts/review_pr_k2a1_integration_on_mac.sh (~7-9h, BINDING
22+
# gate evidence for PR-K2.A.1 merge).
23+
# * Throughput parity vs full-attention oracle (no oracle arm
24+
# run here — gate (c) work that K2.A.2 + K3 close).
25+
# * MLX/Metal native path latency. v0.4 today is PyTorch MPS
26+
# research harness; the user-facing latency floor will only
27+
# drop when K3 ships the MLX path. This smoke nonetheless
28+
# records the PyTorch MPS number as a CONSERVATIVE upper bound
29+
# on what users experience: anything K3 ships will be faster.
30+
#
31+
# Usage
32+
# -----
33+
#
34+
# Default — single 5.6k-context request, KL ON + stateful, ~3-5 min
35+
# on Mac M4 24 GB:
36+
#
37+
# bash scripts/review_pr_k2a_production_smoke_on_mac.sh
38+
#
39+
# Single 1.4k request (~30-60 s):
40+
#
41+
# CTX_LINES=70 bash scripts/review_pr_k2a_production_smoke_on_mac.sh
42+
#
43+
# Wider context (slower, may OOM on 24 GB):
44+
#
45+
# CTX_LINES=800 bash scripts/review_pr_k2a_production_smoke_on_mac.sh
46+
#
47+
# Env knobs (defaults):
48+
#
49+
# CTX_LINES (280) padding-line count; 70=1.4k, 280=5.6k,
50+
# 800=16k tokens
51+
# SINK (4)
52+
# WINDOW (64)
53+
# MAX_NEW_TOKENS (24)
54+
# SEED (42)
55+
# KL_LATTICE (D4) D4 (v1.4) or E8 (v1.5)
56+
# KL_Q_RANGE (38) canonical D4 operating point
57+
# ATTN_IMPL (sdpa) memory-efficient; required for >=4k Mac M4
58+
# STATEFUL (1) 1 = K2.A.2 (default; production path);
59+
# 0 = K2.A.1 stateless (each-step recompute,
60+
# research-only — DO NOT ship to users)
61+
#
62+
# Output
63+
# ------
64+
#
65+
# Single JSON report at:
66+
#
67+
# results/research/k2a_production_smoke_mac_ctx<CTX_LINES>_<stamp>.json
68+
#
69+
# JSON contains: recall (hit/miss for the single request), prefill
70+
# latency, first-token latency, mean tok/s, peak resident memory,
71+
# effective attention window, dtype/numerics warnings if any.
72+
#
73+
# Pre-flight
74+
# ----------
75+
#
76+
# kakeyalattice must be installed (for KL ON arm). PyTorch with MPS
77+
# support must be importable. HF_TOKEN must be exported (Gemma 3-1B
78+
# is gated):
79+
#
80+
# export HF_TOKEN=hf_xxx
81+
# pip install kakeyalattice
82+
#
83+
# Lineage (why this script exists alongside the A/B script)
84+
# ---------------------------------------------------------
85+
#
86+
# The A/B reviewer scripts/review_pr_k2a1_integration_on_mac.sh is a
87+
# **research evidence collector** for PR-K2.A.1 merging — it takes
88+
# 7-9h, runs 20 samples × 2 context rungs × {KL OFF, KL ON} × {oracle,
89+
# v0.3, v0.4} arms because the BINDING gate for that PR (recall delta
90+
# ≤ 1pp at every rung) needs statistical signal.
91+
#
92+
# That A/B shape is wrong for the **product** question — users send
93+
# one request, expect one answer, and care about the latency of that
94+
# one request. Per the user's 2026-06-09 directive ("严重浪费时间..."):
95+
# do not run oracle / v0.3 / A/B for product-experience verification;
96+
# run a single production arm (KL ON + stateful), single request,
97+
# report user-facing latency.
98+
#
99+
# Both scripts have a place — research signal vs product signal.
100+
# This file is the product-signal entry point.
101+
102+
set -euo pipefail
103+
104+
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
105+
cd "$ROOT"
106+
107+
CTX_LINES="${CTX_LINES:-280}"
108+
SINK="${SINK:-4}"
109+
WINDOW="${WINDOW:-64}"
110+
MAX_NEW_TOKENS="${MAX_NEW_TOKENS:-24}"
111+
SEED="${SEED:-42}"
112+
ATTN_IMPL="${ATTN_IMPL:-sdpa}"
113+
KL_LATTICE="${KL_LATTICE:-D4}"
114+
KL_Q_RANGE="${KL_Q_RANGE:-38}"
115+
STATEFUL="${STATEFUL:-1}"
116+
117+
stamp="$(date +%s)"
118+
out_dir="results/research"
119+
log_dir="${out_dir}/logs"
120+
mkdir -p "$out_dir" "$log_dir"
121+
122+
# ±15 % range around the target line count — same formula as A/B
123+
# script so the haystack length distribution matches research evidence.
124+
lo=$(( (CTX_LINES * 85 + 50) / 100 ))
125+
hi=$(( (CTX_LINES * 115 + 50) / 100 ))
126+
if [[ $lo -lt 10 ]]; then lo=10; fi
127+
if [[ $hi -lt $((lo + 1)) ]]; then hi=$((lo + 1)); fi
128+
129+
report="${out_dir}/k2a_production_smoke_mac_ctx${CTX_LINES}_${stamp}.json"
130+
log="${log_dir}/k2a_production_smoke_mac_ctx${CTX_LINES}_${stamp}.log"
131+
132+
echo "==> Mac M4 K2.A production-shape smoke (single request)"
133+
echo " Model: google/gemma-3-1b-it"
134+
echo " Context: ~$(( CTX_LINES * 20 )) tokens (lines [$lo, $hi])"
135+
echo " Sink x window: ${SINK} x ${WINDOW}"
136+
echo " Attn impl: $ATTN_IMPL"
137+
echo " Lattice: $KL_LATTICE (Q=$KL_Q_RANGE)"
138+
echo " Stateful (K2.A.2): $STATEFUL"
139+
echo " Samples: 1 (product shape, NOT 20)"
140+
echo " Arms: v0.4 KL ON only (NO oracle, NO v0.3, NO KL OFF)"
141+
echo " Time budget: ~30 s @ 1.4k, ~3-5 min @ 5.6k, ~12-18 min @ 16k"
142+
echo " Report: $report"
143+
echo
144+
145+
echo "==> Pre-flight: kakeyalattice availability"
146+
if PYTHONPATH=.:sdks/python python3 -c "import kakeyalattice; print(' kakeyalattice', kakeyalattice.__name__, 'OK')" 2>/dev/null; then
147+
echo " kakeyalattice installed."
148+
else
149+
echo " ERROR: kakeyalattice NOT installed. Install:"
150+
echo " pip install kakeyalattice"
151+
exit 1
152+
fi
153+
echo
154+
155+
# Build the K1.E runner invocation. Production-shape constraints:
156+
#
157+
# --n-samples 1 single request, not 20
158+
# --skip-oracle no oracle arm (research-only baseline)
159+
# --skip-v03 no v0.3 arm (research-only baseline)
160+
# --kl-on production arm uses KL compression
161+
# (no --kl-on toggle for KL OFF)
162+
# --stateful K2.A.2 incremental decode path (production
163+
# path; without this each step recompresses
164+
# the whole resident window, which is what
165+
# the user's directive specifically forbids)
166+
#
167+
flags=(
168+
--model google/gemma-3-1b-it
169+
--device auto
170+
--attn-impl "$ATTN_IMPL"
171+
--n-samples 1
172+
--haystack-min-lines "$lo"
173+
--haystack-max-lines "$hi"
174+
--sink-size "$SINK"
175+
--window-size "$WINDOW"
176+
--max-new-tokens "$MAX_NEW_TOKENS"
177+
--seed "$SEED"
178+
--skip-oracle
179+
--skip-v03
180+
--kl-on
181+
--kl-lattice "$KL_LATTICE"
182+
--kl-q-range "$KL_Q_RANGE"
183+
--output "$report"
184+
)
185+
[[ "$STATEFUL" == "1" ]] && flags+=(--stateful)
186+
187+
echo "==> Running v0.4 KL ON $([[ "$STATEFUL" == "1" ]] && echo "+ stateful (K2.A.2)") on a single request"
188+
PYTHONPATH=.:sdks/python python3 scripts/research/k1e_niah_validation.py \
189+
"${flags[@]}" 2>&1 | tee "$log"
190+
exit_code=${PIPESTATUS[0]}
191+
192+
echo
193+
if [[ "$exit_code" -eq 0 ]]; then
194+
echo "==> Production smoke OK."
195+
echo " Report: $report"
196+
echo " Log: $log"
197+
echo
198+
echo "Inspect first-token latency / recall:"
199+
echo " python3 -c 'import json,sys;r=json.load(open(\"$report\"));"
200+
echo " v=r[\"per_config\"].get(\"v0.4\");"
201+
echo " print(\"recall:\", v[\"recall\"]);"
202+
echo " print(\"first_token_s:\", v.get(\"first_token_seconds\"));"
203+
echo " print(\"mean_tok_s:\", v.get(\"mean_throughput_tokens_per_sec\"));"
204+
echo " print(\"peak_mem_GB:\", v.get(\"peak_memory_bytes\",0)/1e9)'"
205+
echo
206+
echo "Commit (only if recall=hit AND no dtype crash):"
207+
echo " git add $report $log"
208+
echo " git commit -m 'Mac M4 K2.A production-shape smoke evidence'"
209+
echo " git push"
210+
else
211+
echo "==> Production smoke FAILED (exit=$exit_code)."
212+
echo " Report (partial): $report"
213+
echo " Log: $log"
214+
fi
215+
216+
exit "$exit_code"

0 commit comments

Comments
 (0)