Files
exp-model-parameter-similarity/crossqkv/README.md
T

107 lines
6.2 KiB
Markdown

# Cross-layer QKV CPU study
This phase targets learned QKV functions across different layers, independent of cost.
The six full-attention layers (indices 3/7/11/15/19/23) form 15 unordered pairs.
No within-layer Q/K/V substitution, training, continuous R fitting, GPU, or new model download.
Read [fixed primary protocol](../docs/CROSS_LAYER_QKV_PROTOCOL.md) and
[findings with evidence limits](../docs/CROSS_LAYER_QKV_FINDINGS.md).
The original main protocol had no passing candidate. A later fixed one-source audit
showed near-self-token retrieval and small readout errors, but failed strict fresh
mean-JS/row-JS-P95/row-TV-P95 criteria; it is not relabeled as a primary pass.
- `core.py`: exact discrete head assignment, actual attention parts, common-space head
decomposition, fixed recipient V/gate/O transplants and temporal-cache controls.
- `protocol.py`, `fresh_samples.json`: predeclared layer/unit/gate scope and untouched fresh texts.
- `run.py`, `fresh.py`: calibration, frozen selection, old validation and gated fresh/network steps.
- `conditional.py`: separately registered post-screen metric audit on one fixed H23 candidate;
no fallback selection or later threshold changes.
- `audit_case.py`: recompute selected fresh probabilities, self mass, shared top5 mass and
actual-H3 versus same-H23 difference; no full-network intervention after failure.
- `report.py`, `audit.py`: saved-data scientific plots, raw-count/matching/probability audits,
SHA256 and explicit small Git delivery with external large-cache metadata.
Use the authorized remote CPU environment with the retained source checkpoint/cache:
```sh
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 venv/bin/python -B -m unittest tests.test_crossqkv -v
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 timeout 7200 venv/bin/python -B -m crossqkv.run --source runs/cpu-qwen35-2b --root runs/NEW_QKV_DIRECTORY
```
The main run requires a fresh directory and never overwrites prior protocol evidence.
The recorded execution used `runs/cross-layer-qkv-v2` after fixing a controls-variable
shadowing error in the first attempt; old logs and its profile cache are retained.
The first attempt took about 10.3 seconds, then a 60-second reserve was deducted from
the next 7140-second run limit. The main study finished in about 74.8 seconds.
The fixed conditional audit used only remaining shared budget and took about 105.3 seconds.
Final tests/plots/audits fit comfortably within the two-hour computation budget.
All weights and caches stay on the server until explicit user deletion. Git contains
all figure tables and the selected small probability matrices; large caches have exact
paths/sizes/hashes in `external-caches.json`. Old validation texts were used in earlier
experiments; the four fresh texts were fixed before the new cross-layer measurements.
## LiSA-style single-layer pilot
The pilot reuses layer 19's actual pre-softmax scores at layer 23, with the
recipient's own values, output gate and output projection. It compares native
attention, direct reuse, a frozen one-to-one head assignment, linear mixing
of donor heads, a 32-coordinate recipient-only control, and joint donor/recipient
mixing. The linear variants use a closed-form ridge fit to the native scores,
centred within each causal key row. The recipient branch takes a fixed subset of
post-RoPE coordinates rather than learning a low-rank Q/K projection. The
calibration uses 12 synthetic prompts without answer labels. All checkpoint
weights are frozen.
Evaluation scores the four candidate option tokens on 24 disjoint retrieval
prompts, including 12 with more table entries than calibration. Choice NLL
normalizes over those four tokens. Run against the retained checkpoint on the
remote CPU environment:
```sh
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 venv/bin/python -B -m crossqkv.lisa_pilot \
--source runs/cpu-qwen35-2b --out runs/NEW_LISA_DIRECTORY
```
The diagnostic forward still computes native full Q/K and does not measure
latency or parameter savings. Refer to LiSA (arXiv:2408.01890, §5.1) for its
learned nonlinear head aligner and learned low-rank Q/K projections.
## Forward-only calibration and search
This follow-up keeps the final-layer intervention at 19→23 and changes the
task to generating the latest value of a key. Some prompts contain two
conflicting records for that key. The fit, validation and test splits contain
12, 8 and 16 prompts, respectively. The test set adds longer lists. All
checkpoints and grid settings are selected using validation loss before test
replay. Each prompt's final-query scores, values, gate and residual are captured
from a single native forward. Substitutions replay only the final query of the
last layer and check the native replay against full-prefix activations.
The conditions cover direct sharing, a closed-form linear layer over the donor
heads, a linear layer with a small current-layer Q/K branch, a two-coefficient
diagonal grid, and two forward-only optimizers. The latter each start at
direct sharing and run three fixed seeds. One proposes additive Gaussian
perturbations and keeps improvements. The other proposes a bounded update
from a two-sided directional estimate and applies the same acceptance rule.
These methods fit only the small adapter. The backbone is frozen, and there
is no autograd. Search traces expose forward-call budgets and selected steps.
From the confirmed remote workspace containing the code and retained weights:
```sh
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 venv/bin/python -B -m crossqkv.forward_search capture \
--source runs/cpu-qwen35-2b --out runs/NEW_SEARCH_CACHE
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 venv/bin/python -B -m crossqkv.forward_search_fit \
--source runs/cpu-qwen35-2b --cache runs/NEW_SEARCH_CACHE --out runs/NEW_SEARCH_EVAL
```
The main optimization metric is nine-digit NLL. Evaluation also reports
full-vocabulary greedy exact digit at the first answer token, and strata by
list length and duplicate key. Each perturbation reuses fixed lower-layer
activations, so its forward objective computes the exact affected final query
without repeatedly forwarding the whole backbone. For interrupted captures,
`--resume` checks source and code fingerprints and keeps finished batches.
This is a single-site
comparison, not an evaluation of full-model fine-tuning or measured speedup.