# Offline native clustering qualification

This kit runs prebuilt native e-means/o-means/p-means correctness tests and matched
synthetic CPU/CUDA measurements. It needs Python 3.10+ from the host, Linux
x86_64, and an existing compatible NVIDIA driver with `nvidia-smi`. It does
not download, compile, install, request root, contact a service, change a
driver, or stop another user's workload. There are no LLM, model-weight,
LibTorch or package-manager dependencies.

**Preparing the L40 package does not qualify L40 execution.** A completed
campaign still needs independent evidence review. The package must contain newly
V100-qualified portable binaries, their original build/dependency records,
runtime libraries, source identity inputs, and the hashed M2 comparison
reference. The original build record describes the actual compiler, sysroot,
CUDA toolkit and `70;89` GPU code. Compiler observations made later on the
execution host describe that host, not the compiler used to build the binaries.
`source/` contains the inputs used to verify the compiled source fingerprint,
not a complete rebuildable source distribution or SDK. Execution uses the
included prebuilt binaries. Aner's root LICENSE/NOTICE files, when present,
and the staged dependency redistribution notices are included unchanged.

## Prepare from existing artifacts

Stage the three ELF binaries `aner_clustering_tests`,
`aner_clustering_cuda_tests`, and `aner_clustering_benchmark` in one directory.
Stage their runtime dependency closure in another, with every filename/SONAME
alias copied as a regular file. Do not include `libcuda`, NVIDIA driver
libraries, libc or the ELF loader. Keep redistribution notices and the exact
build/ELF/dependency records in a separate provenance directory. The supplied
`--identity` file must be the actual benchmark's `--identity` JSON output.
An optional regular-text `CMakeCache.txt` beside the staged benchmark is also
copied beside the packaged benchmark and hashed, so its recorder can retain
the actual original build settings. Its bytes must match the provenance copy
when both are provided; it is never treated as an ELF executable.

```sh
python3 tools/clustering_qualification/package.py \
  --source /local/frozen-source --binaries /local/stage/bin \
  --runtime /local/stage/runtime/lib --provenance /local/stage/provenance --licenses /local/stage/licenses \
  --identity /local/stage/build-identity.json --reference /local/m2.json \
  --algorithms e-means o-means p-means --seeds 42 43 44 \
  --cases small medium large-k large --output /local/new-l40-kit \
  --archive /local/new-l40-kit.tar.gz
```

The default comparison now contains all three methods, seeds 42/43/44 and
five repeated fits after one warm-up per configuration. Every repetition
retains its final loss. Optional `wide-512` and `wide-1024` cases add 8,192
rows and 128 centers at the named dimension; `all` in the recorder continues
to mean the original four shapes. These are fixed-epoch synthetic checks,
not a reproduction of the upstream H100 time-to-target study.

To add all-four-method evidence, explicitly request `--lloyd-supplement` and
include `small` among the cases. A separate final CPU phase runs legacy Lloyd
on the same small input with seeds 42/43/44, five timed fits after warm-up and a
three-iteration cap. It uses the newly completed `benchmark-small.json` as its
reference, verifies the actual target binary hash, and retains a separate
`lloyd-small.json`. Lloyd computes Float64 distances/means and may stop before
the cap; this is not GPU Lloyd, an epoch budget or an equal-quality comparison.
The supplemental report corrects the frozen harness's unreported/unused
placeholder fields explicitly. Main e/o/p recipes and reports are unchanged.

Only request cases, algorithms and seeds present in the reference. The M2
report must have the same version and compiled source identity as the new
binaries; incomplete or failed reports are rejected. Old M2/V100 reports
and the dev.4 L40 package remain historical artifacts, not references for
a changed p-means binary. Use a new package and result directory for every
source identity. Do not update, stop, or replace an already launched batch.

Keep the emitted archive SHA256
and manifest SHA256 separately. Verify the archive hash before extracting to
a **new directory on the target's local disk**, outside VPN/network mounts.
The immutable manifest records every payload file's SHA256, size and
executable mode. Verification rejects symlinks and unexpected files. Runtime
libraries use the package-local loader path; the host supplies its driver.
`--target v100` creates a distinct sm70/V100 rehearsal manifest. A successful
rehearsal remains V100 evidence and cannot qualify the default L40 target.

## Launch once, disconnect, inspect later

Set `KIT`, `OUT` and `SHA` to the extracted absolute kit directory, a fresh
absolute results directory outside it, and the separately retained manifest
SHA256. These variables are local paths/identity, not credentials.

```sh
python3 "$KIT/kit/runner.py" verify --manifest "$KIT/manifest.json" --manifest-sha256 "$SHA"
python3 "$KIT/kit/launch.py" --manifest "$KIT/manifest.json" --manifest-sha256 "$SHA" --output "$OUT"
python3 "$KIT/kit/runner.py" status --output "$OUT"
```

The launcher closes stdin, redirects both output streams to private local
logs, and starts a new session. The worker ignores SIGHUP. After launch, close
the original connection, reconnect, and observe **two advancing heartbeats**
with a matching live process identity before releasing the VPN. Detachment
survives an ordinary launching-shell exit; it does not override a site's
logout-kill policy or survive reboot, power loss, or shutdown. No persistent
service or login policy is installed or changed.

The runner checks the exact GPU model and compute capability, requires an
idle device, and holds a per-user GPU lock plus a result-directory lock.
Other users remain free to schedule work: a newly busy GPU defers the next
phase rather than interrupting it. Phases execute serially: CPU correctness,
mandatory CUDA correctness, then one recorder invocation per requested case.
A CUDA skip/exit 77 fails qualification. Each phase, log file and campaign
has a bound; defaults are 30 minutes per benchmark case and two hours overall.
For a larger admitted matrix, set `--benchmark-timeout` and
`--campaign-timeout` during preparation from observed comparable durations;
the whole-campaign ceiling is six hours. These bounds do not change sample
counts, seeds or worker policies. Every phase has
its own command record, log, outcome and artifact hashes. Heartbeats and
status updates are atomic. `status` detects dead/reused process IDs and stale
heartbeats; it never relabels a stale process as a completed run.

## Stop and explicitly retry

```sh
python3 "$KIT/kit/runner.py" request-stop --output "$OUT"
# After reviewing a failed, stopped, deferred or stale attempt:
python3 "$KIT/kit/launch.py" --manifest "$KIT/manifest.json" --manifest-sha256 "$SHA" --output "$OUT" --retry
```

Stopping terminates only the currently owned phase process group. Retry is a
**fresh complete attempt**, not checkpoint restoration or phase reuse. Old
attempts and logs remain unchanged. At most three attempts are permitted in
one result directory. A successful campaign requires a new output directory
for another measurement. Fixing the immutable payload requires a new package
and new result directory; do not edit a manifest to bypass a failed check.

## Collect a small receipt

After the attempt is terminal, or its owner is demonstrably dead:

```sh
python3 "$KIT/kit/collect.py" --manifest "$KIT/manifest.json" --manifest-sha256 "$SHA" \
  --output "$OUT" --archive /local/new-clustering-receipt.zip
```

Collection includes the package manifest, original build provenance, bounded
status/command records, logs, and benchmark JSON. It excludes executables,
libraries, source trees, CUDA caches and work directories. It refuses to
overwrite a receipt and reports the archive SHA256. Retain and verify that
hash after transfer. Receipts are private evidence and can contain local
paths, process identities and GPU UUIDs; review before sharing publicly.

The lifecycle tests use clearly marked fixture processes and mocked GPU
inventory. They test hash failures, owned locks, disconnect survival,
heartbeats, stop/timeout, explicit retries and collection. They do not claim
GPU qualification: `python3 tests/test_clustering_qualification.py`.
