diff --git a/tools/kernel-ab/README.md b/tools/kernel-ab/README.md index d0cd499c..a799fbbd 100644 --- a/tools/kernel-ab/README.md +++ b/tools/kernel-ab/README.md @@ -12,8 +12,11 @@ production, and Python is used offline only (`verify_manifest.py`, standard libr | `cpp/avxvnni_probe.cpp` | the engine's own `STRATA_HAVE_AVXVNNI_INTRINSICS` probe, run by the Makefile | | `verify_manifest.py` | the KAB1 reader rule (exit 0 / 1 / 2) | | `smoke.sh` | build, gen, verify, negative tests, poolcheck in one command; last line `SMOKE OK` | +| `cpp/kab_time.cpp` | the `time` sub-command (STRA-317); `#include`d by `kab.cpp`, one translation unit | +| `aa.sh` | the C++ A/A control: N rounds of two fresh `kab time` processes, per-round ratios only (STRA-317) | +| `time_smoke.sh` | STRA-317 acceptance in one command (time, verify, A/A); last line `TIME SMOKE OK` | -The `time` mode (resident / streamed regimes, A/A, clock calibration) is **not** here: it is STRA-317 (S0.8a2). +The `time` mode (resident / streamed regimes, clock calibration, A/A control) is STRA-317 (S0.8a2): section "Timing" below. The Rust half (`examples/kernel_ab.rs`, reader, comparison, alternation) is STRA-289. ## Why a Makefile and not CMake @@ -122,6 +125,45 @@ the C++ `ExpertJobMulti.nt`; the dispatch keys on `nt` (`mt_min`, #152), so the ## Not covered here -* Timing, A/A, clock calibration, regimes: STRA-317. Rust reader, comparison, alternation: STRA-289. MSVC build: STRA-255. +* Rust reader, comparison, alternation: STRA-289; the Rust `time` mode: STRA-318. MSVC build: STRA-255. The A/A class (slower/equal/faster): STRA-313. * Row types outside the KAB1 list (the K-quants, NVFP4, the trellis types, IQ2_XXS / IQ2_XS / IQ1_*): not generated. * CMake itself was not run (see above). `GGML_NATIVE=ON` ties the ggml-cpu half to the build host's CPU. + +## Timing (`kab time`, `aa.sh`; STRA-317 / S0.8a2) + +``` +/kab time --run-dir D --case --slices N --pin --out F.json [--slice-ms 60] [--regime both|resident|streamed] [--llc-bytes B] +bash tools/kernel-ab/aa.sh D # env: E2E_ROOT, KAB, AA_CELL=avx512|avx2, AA_SLICES, AA_SLICE_MS, AA_OUT +bash tools/kernel-ab/time_smoke.sh # time + verify_manifest + A/A (TS_ROUNDS shortens the A/A) +``` + +The command line is the same as the Rust half's (STRA-318), so one runner can alternate both programs. `D` is never written to +(`verify_manifest.py D` still prints `OK` after a run); `F.json` and the A/A output go next to it, outside the repository. + +* **What is timed.** One routed expert for the case's `rows` tokens: the activation quantization, the gate/up rows + SwiGLU, the hidden + quantization and the down rows, i.e. the stage sequence of `compose_expert()` (same code path, kernels unchanged) with all scratch + pre-allocated. `selfcheck_bitwise_equal_compose_expert` in the output proves, before any slice, that its output equals the reference + composition's on the resident copy and the first/last streamed copy. Pool = 1 worker: one thread, pinned with `sched_setaffinity` to + `--pin` (verified with `sched_getcpu` before every slice; a migrated slice makes the run exit 3). Buffers and scratch are 4096-aligned. +* **Regimes** (both in one run by default, labelled `RESIDENT` / `STREAMED` in `regimes[]`): RESIDENT cycles one copy of the expert; + STREAMED cycles K distinct 4096-aligned copies with K x stride >= 2 x the last-level cache of the pinned CPU, read at run time from + `/sys/devices/system/cpu/cpu/cache/index*/size` (the highest-level data/unified cache; `--llc-bytes` overrides and is recorded). + The factor 2 is a design choice recorded in the file, not a drop threshold. STREAMED does one untimed pass over all copies first. +* **Slices.** Each slice runs calls for `--slice-ms` (default 60, `tq_cpu_parity.cpp:461`), at least 2, and records monotonic ns (`CLOCK_MONOTONIC`) + and TSC ticks (lfence-serialised `rdtsc`) per call, plus the dependent-add core-clock calibration before and after. +* **Dropped slices.** The method and the constant are `tq_cpu_parity --bench`'s: `core_ghz()` (`tq_cpu_parity.cpp` lines 273-284) and + `time_slice()` (lines 290-304); a slice is dropped when `std::fabs(g1 - g0) > 0.05 * g0`, i.e. the clock moved by more than 5 percent + (**`engine/strata/src/kernels/cpu/tq_cpu_parity.cpp`, line 303**, copied, no new threshold). `slices[]` holds the kept slices, `dropped[]` + the dropped ones, `dropped_count` and `slices_kept` the counts; the median of the kept slices is in `median_ns_per_call` / `median_tsc_per_call`. +* **Recorded in `F.json`**: `manifest_sha256` and the case's `input_sha256` (checked against the manifest before use; a mismatch refuses the run), `tree` + (`avx512` | `avx2`, from `cpu_avx512_ok()`) with its probes, `effective_env` (every `STRATA_*` / `LELEKA_*` variable the process saw), + the pin (CPU, `thread_siblings_list`, sibling CPU, the sibling's and the pin's busy fraction from `/proc/stat` over the run, governor, boost, load average at start and end), + `clock_calibration` (method, the cited drop rule with file and line, GHz at start and end, `tsc_invariant`), and the build record of `kab`. +* **The runner scrubs the environment**, not `kab time`: `aa.sh` removes every `STRATA_*` and `LELEKA_*` variable and sets only what the cell needs + (`AA_CELL=avx2` sets `STRATA_FORCE_AVX2=1`, which `expert_layout.cpp cpu_avx512_ok()` honours; the default cell `avx512` sets nothing). +* **Exit codes**: 0 ok; 1 usage / refused input; 3 a regime kept no slice, or a slice migrated off the pin; 4 the final output differs from the reference. +* **A/A (`aa.sh`).** A seeded schedule (balanced a-first / b-first, SplitMix64 Fisher-Yates) is written to `schedule.tsv` before the first run; each round runs two + fresh `kab time` processes in that order; `ratios.tsv` holds exactly `` data lines (`#` lines are comments) with, per regime, the ratio of the + median slice ns of process b over process a. No verdict and no class: the A/A class belongs to the S0.10b classifier (STRA-313). +* **Noise.** On a shared host the numbers are not evidence by themselves; every record carries the load average and the SMT sibling's busy fraction. + The leaf's acceptance is correctness (builds, runs, shape of the records), not a timing claim. diff --git a/tools/kernel-ab/aa.sh b/tools/kernel-ab/aa.sh new file mode 100755 index 00000000..83b556c7 --- /dev/null +++ b/tools/kernel-ab/aa.sh @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +# tools/kernel-ab/aa.sh - STRA-317 (S0.8a2): the C++ A/A control of the CPU kernel A/B timing. +# +# bash tools/kernel-ab/aa.sh D +# +# D a KAB1 run directory written by `kab gen` (STRA-248); never written to (an extra file would make verify_manifest.py refuse it) +# case case id, e.g. 18-20-r1 +# rounds number of rounds (30 for the A/A control of the plan) +# seed integer; writes the order schedule BEFORE the first run (balanced: half of the rounds "a first", half "b first", shuffled by the seed) +# cpu the logical CPU both processes are pinned to (a physical core whose SMT sibling is idle) +# +# One round = two FRESH `kab time` processes of the SAME binary, labelled a and b, run one after the other in the scheduled order, +# both pinned to , STRATA_* / LELEKA_* scrubbed from their environment (the cell avx2 sets only STRATA_FORCE_AVX2=1). Per round +# and regime the ratio is median slice ns of b / median slice ns of a (the same statistic the Rust/C++ comparison uses). Only the +# ratios are written: NO verdict and no class (the A/A class is computed later by the S0.10b classifier, STRA-313). +# +# Output (default: next to D, never inside it): /aa---s/ +# schedule.tsv written first: roundfirst (a|b)pincell +# rounds/rNN-a.json, rNN-b.json the full `kab time` records +# ratios.tsv "#" comment lines (the column header is one of them), then EXACTLY data lines: +# round first ratio_resident ratio_streamed median_ns_a_resident median_ns_b_resident median_ns_a_streamed median_ns_b_streamed +# kept_a_resident kept_b_resident kept_a_streamed kept_b_streamed dropped_total +# +# Environment: KAB (binary; default $E2E_ROOT/build/kernel-ab/kab) E2E_ROOT (default $HOME/work/leleka-e2e) +# AA_CELL avx512|avx2 (default avx512) AA_SLICES (default 50) AA_SLICE_MS (default: kab's 60) AA_OUT (output dir) +# PYTHON (default python3; stdlib only, offline arithmetic and JSON reading) +# CPU only, no GPU, nothing installed. Timing taken on a shared host is noisy and is not evidence by itself: each record carries the +# load average and the SMT sibling's busy fraction. +set -euo pipefail + +if [ "$#" -ne 5 ]; then + echo "usage: bash tools/kernel-ab/aa.sh D " >&2 + exit 2 +fi +d="${1%/}"; cas="$2"; rounds="$3"; seed="$4"; cpu="$5" +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo="$(cd "${here}/../.." && pwd)" +root="${E2E_ROOT:-${HOME}/work/leleka-e2e}" +kab="${KAB:-${root}/build/kernel-ab/kab}" +cell="${AA_CELL:-avx512}" +slices="${AA_SLICES:-50}" +py="${PYTHON:-python3}" +export PYTHONDONTWRITEBYTECODE=1 + +case "${rounds}" in ''|*[!0-9]*) echo "aa.sh: rounds must be a positive integer" >&2; exit 2;; esac +case "${seed}" in ''|*[!0-9]*) echo "aa.sh: seed must be a non-negative integer" >&2; exit 2;; esac +case "${cpu}" in ''|*[!0-9]*) echo "aa.sh: cpu must be a logical CPU number" >&2; exit 2;; esac +[ "${rounds}" -ge 1 ] || { echo "aa.sh: rounds must be >= 1" >&2; exit 2; } +case "${cell}" in avx512|avx2) ;; *) echo "aa.sh: AA_CELL must be avx512 or avx2" >&2; exit 2;; esac +[ -f "${d}/manifest.json" ] || { echo "aa.sh: ${d} has no manifest.json (not a complete KAB1 run directory)" >&2; exit 2; } +[ -f "${d}/case-${cas}.in" ] || { echo "aa.sh: ${d}/case-${cas}.in does not exist" >&2; exit 2; } +[ -x "${kab}" ] || { echo "aa.sh: ${kab} is not an executable (build with make -C tools/kernel-ab/cpp BUILD=...)" >&2; exit 2; } + +dabs="$(cd "${d}" && pwd)" +out="${AA_OUT:-$(dirname "${dabs}")/aa-$(basename "${dabs}")-${cas}-s${seed}}" +case "${out}/" in "${repo}/"*) echo "aa.sh: output directory ${out} is inside the repository" >&2; exit 1;; esac +case "${out}/" in "${dabs}/"*) echo "aa.sh: output directory ${out} is inside the KAB1 run directory" >&2; exit 1;; esac +[ ! -e "${out}" ] || { echo "aa.sh: ${out} exists; remove it or set AA_OUT" >&2; exit 1; } +mkdir -p "${out}/rounds" + +# ---- the schedule, written BEFORE the first run ------------------------------------------------------------------------------- +"${py}" -I - "${rounds}" "${seed}" "${cpu}" "${cell}" > "${out}/schedule.tsv" <<'PY' +import sys +rounds, seed, cpu, cell = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3], sys.argv[4] +M = (1 << 64) - 1 +state = seed & M +def nxt(): # SplitMix64: the same sequence on every host and Python version + global state + state = (state + 0x9E3779B97F4A7C15) & M + z = state + z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & M + z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & M + return z ^ (z >> 31) +order = ["a"] * ((rounds + 1) // 2) + ["b"] * (rounds // 2) # balanced +for i in range(len(order) - 1, 0, -1): # Fisher-Yates + j = nxt() % (i + 1) + order[i], order[j] = order[j], order[i] +print("# seed=%d rounds=%d (balanced: %d a-first, %d b-first)" % (seed, rounds, order.count("a"), order.count("b"))) +print("round\tfirst\tpin\tcell") +for r, f in enumerate(order, 1): + print("%d\t%s\t%s\t%s" % (r, f, cpu, cell)) +PY +echo "aa: schedule written to ${out}/schedule.tsv" + +# ---- the scrubbed environment of the cell ------------------------------------------------------------------------------------- +scrub=() +while IFS= read -r v; do scrub+=(-u "$v"); done < <(compgen -e | grep -E '^(STRATA_|LELEKA_)' || true) +cellenv=() +[ "${cell}" = "avx2" ] && cellenv=(STRATA_FORCE_AVX2=1) +extra=() +[ -z "${AA_SLICE_MS:-}" ] || extra=(--slice-ms "${AA_SLICE_MS}") + +run_one() { #