diff --git a/crossqkv/lisa_rank_residuals.py b/crossqkv/lisa_rank_residuals.py new file mode 100644 index 0000000..fe0f87d --- /dev/null +++ b/crossqkv/lisa_rank_residuals.py @@ -0,0 +1,86 @@ +"""Measure how much native attention score error extra rank can remove on frozen fit captures.""" +import argparse +import hashlib +import io +import json +from itertools import permutations +from pathlib import Path + +import numpy as np + + +RECEIPT_SHA = '682bc035a5bed9f9245f589cacf3e9616300e8bf5e85d3743b1264a56360fa55' +RANKS = (32, 40, 48) + + +def matrices(directory, record): + data = (directory / record['file']).read_bytes() + if hashlib.sha256(data).hexdigest() != record['sha256']: + raise ValueError(f"Capture changed for {record['id']}") + with np.load(io.BytesIO(data), allow_pickle=False) as capture: + donor = capture['donor'][0].astype(np.float64) + teachers = capture['teacher'][:, 0].astype(np.float64) + length = capture['ids'].shape[1] + start = int(capture['start'][0]) + if start != record['input_tokens'] or length - start != record['answer_and_eos_tokens']: + raise ValueError(f"Unexpected token counts for {record['id']}") + if teachers.shape != (3, 8, length, length) or donor.shape != teachers.shape[1:]: + raise ValueError(f"Unexpected score shapes for {record['id']}") + features = donor.reshape(8, -1).T + features = np.column_stack((features, np.ones(length * length))) + targets = teachers.reshape(24, -1).T + return features, targets, length + + +def measure(prior): + raw = (prior / 'verified-fit-capture.json').read_bytes() + if hashlib.sha256(raw).hexdigest() != RECEIPT_SHA: + raise ValueError('Expected the verified 24-example native score capture') + receipt = json.loads(raw) + records = receipt['records'] + sites = receipt['recipients'] + directory = prior / 'fit-capture' + # Fit one affine donor-head map on each half of the training examples. + gram = np.zeros((2, 9, 9)) + cross = np.zeros((2, 9, 24)) + for i, record in enumerate(records): + x, y, _ = matrices(directory, record) + gram[i % 2] += x.T @ x + cross[i % 2] += x.T @ y + coefficients = [np.linalg.solve(g, c) for g, c in zip(gram, cross)] + + rows = [] + for i, record in enumerate(records): + x, y, length = matrices(directory, record) + residual = (y - x @ coefficients[1 - i % 2]).T.reshape(3, 8, length, length) + singular_values = np.linalg.svd(residual, compute_uv=False) + energy = singular_values**2 + np.testing.assert_allclose(energy.sum(-1), (residual**2).sum(axis=(-1, -2)), rtol=1e-10) + rows.append({ + 'id': record['id'], 'length': length, + 'mse': {str(site): {str(rank): float(energy[j, :, rank:].sum() / (8 * length**2)) + for rank in (0, *RANKS)} for j, site in enumerate(sites)} + }) + + means = {str(site): {str(rank): float(np.mean([r['mse'][str(site)][str(rank)] for r in rows])) + for rank in (0, *RANKS)} for site in sites} + uniform = np.array([np.mean([row['mse'][str(site)]['40'] for site in sites]) for row in rows]) + configs = [] + for ranks in ((40, 40, 40), *permutations(RANKS)): + errors = [np.mean([row['mse'][str(site)][str(rank)] for site, rank in zip(sites, ranks)]) + for row in rows] + configs.append({'ranks': list(ranks), 'mean_mse': float(np.mean(errors)), + 'examples_below_uniform_mse': int(np.sum(errors < uniform))}) + return { + 'source_receipt_sha256': RECEIPT_SHA, 'split': 'fit', 'sites': sites, + 'donor_layer': 11, 'donor_fit': 'affine, opposite index parity', + 'metric': 'mean per-example, per-layer, per-head, all token-pair squared raw score residual', + 'mean_mse': means, 'configurations': configs, 'examples': rows + } + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('prior_final', type=Path) + args = parser.parse_args() + print(json.dumps(measure(args.prior_final), indent=2, allow_nan=False)) diff --git a/docs/LISA_COMBINATIONS_RESULTS.md b/docs/LISA_COMBINATIONS_RESULTS.md index c2b3aad..1119d45 100644 --- a/docs/LISA_COMBINATIONS_RESULTS.md +++ b/docs/LISA_COMBINATIONS_RESULTS.md @@ -48,6 +48,20 @@ 如果逐题任选敏感度分秩或学生前缀蒸馏的回答,16 题里最多能答对 3 道。再允许选原 LiSA 的回答,上限是 5 道。两条路线在这批验证题上都没有贡献原 LiSA 答错的新题。预定的 32 对新留出题用于检验这一结果能否推广。 +### 同一笔秩预算在注意力分数上能换来多少改善 + +取 24 道训练题的原生注意力分数,把题号按奇偶分半。在一半题上用第 11 层的 8 个头线性拟合三个接收层的分数(含截距),在另一半逐题计算剩余误差,两半互换。对每一题、每一层、每一个头,直接用奇异值分解求剩余分数矩阵的最佳秩近似。下表是所有 token 对的均方误差,各题等权。这项分析可用旧缓存复跑,不需要加载底座。 [计算脚本](../crossqkv/lisa_rank_residuals.py) · [逐题误差与七种分配](../results/lisa-combinations/fit-rank-residuals.json) + +| 接收层 | 秩 32 | 秩 40 | 秩 48 | 从 40 增至 48 减少的误差 | +|---|---:|---:|---:|---:| +| 15 | 0.03952 | 0.02078 | 0.01084 | 0.00994 | +| 19 | 0.04404 | 0.02256 | 0.01152 | 0.01104 | +| 23 | 0.04279 | 0.02261 | 0.01179 | 0.01082 | + +第 19 层从 40 增到 48,平均误差下降 0.01104。为它让出这 8 个秩,第 23 层从 40 降到 32,误差上升 0.02018。三层平均误差由均匀秩的 0.02198 升到敏感度分秩的 0.02503,增加 13.9%。对每一道训练题,把 32、40、48 任意分给三层的六种排列,其误差都高于三层各取 40。另用 QR 分解拟合供给头映射,显式重建各秩的矩阵,288 个逐题、逐层误差(含秩 0)与奇异值计算的最大绝对差小于 2×10⁻¹⁴。 + +这里逐题近似时可以看到目标矩阵。实际 LiSA 须从隐藏状态预测分数,并经非线性网络融合多个头。这个比较说明简单增秩分配在训练分数近似上的代价。其对完整答案的影响由训练后的生成结果检验。 + ## 两种蒸馏前缀的完整答案 两种蒸馏在同一批 16 道验证题上都丢掉了原 LiSA 答对的题,也都没有救回原本答错的题。学生前缀组比标准答案前缀组少答对 1 道。三组共 48 个回答均正常结束,可解析出完整整数。 [起始 LiSA 答卷](../results/lisa-combinations/validation-prior-lisa.json) · [学生前缀答卷](../results/lisa-combinations/trajectory-student/validation-student.json) · [标准答案前缀答卷](../results/lisa-combinations/trajectory-reference/validation-reference.json) diff --git a/results/lisa-combinations/fit-rank-residuals.json b/results/lisa-combinations/fit-rank-residuals.json new file mode 100644 index 0000000..462d166 --- /dev/null +++ b/results/lisa-combinations/fit-rank-residuals.json @@ -0,0 +1,675 @@ +{ + "source_receipt_sha256": "682bc035a5bed9f9245f589cacf3e9616300e8bf5e85d3743b1264a56360fa55", + "split": "fit", + "sites": [ + 15, + 19, + 23 + ], + "donor_layer": 11, + "donor_fit": "affine, opposite index parity", + "metric": "mean per-example, per-layer, per-head, all token-pair squared raw score residual", + "mean_mse": { + "15": { + "0": 3.0491509033035786, + "32": 0.0395217815948953, + "40": 0.020783290371489474, + "48": 0.010840281262399469 + }, + "19": { + "0": 3.857443153106972, + "32": 0.04404012339850357, + "40": 0.022555703253053838, + "48": 0.011519385659275065 + }, + "23": { + "0": 3.0209357835593145, + "32": 0.04279017456139522, + "40": 0.022611030031209806, + "48": 0.01179076608337388 + } + }, + "configurations": [ + { + "ranks": [ + 40, + 40, + 40 + ], + "mean_mse": 0.021983341218584373, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 32, + 40, + 48 + ], + "mean_mse": 0.024622750310441005, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 32, + 48, + 40 + ], + "mean_mse": 0.024550732428460054, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 40, + 32, + 48 + ], + "mean_mse": 0.025538059951122308, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 40, + 48, + 32 + ], + "mean_mse": 0.02503095019738659, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 48, + 32, + 40 + ], + "mean_mse": 0.02583047823070428, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 48, + 40, + 32 + ], + "mean_mse": 0.02539538635894951, + "examples_below_uniform_mse": 0 + } + ], + "examples": [ + { + "id": "fit-lookup-000", + "length": 105, + "mse": { + "15": { + "0": 3.088659967440397, + "32": 0.042362592505534986, + "40": 0.022141397788788385, + "48": 0.011514037850336351 + }, + "19": { + "0": 3.9943518537218665, + "32": 0.04591974433321144, + "40": 0.024057801139955103, + "48": 0.012400994145531046 + }, + "23": { + "0": 3.043062165797575, + "32": 0.043840611411928194, + "40": 0.023782948692401322, + "48": 0.012654655382485485 + } + } + }, + { + "id": "fit-lookup-001", + "length": 106, + "mse": { + "15": { + "0": 3.043755284271191, + "32": 0.04146037910762712, + "40": 0.021973328312789527, + "48": 0.011547387907239248 + }, + "19": { + "0": 3.9598790566955886, + "32": 0.045888230039258966, + "40": 0.023175579379109502, + "48": 0.011920069339334243 + }, + "23": { + "0": 3.0228768421518657, + "32": 0.04389074185450007, + "40": 0.02298613226861967, + "48": 0.01201903802540863 + } + } + }, + { + "id": "fit-lookup-002", + "length": 103, + "mse": { + "15": { + "0": 3.1611634458593403, + "32": 0.039321211199099954, + "40": 0.020199787980594377, + "48": 0.010459417590053765 + }, + "19": { + "0": 3.7504174288845213, + "32": 0.04261753426778668, + "40": 0.021543287068594312, + "48": 0.010728145288211992 + }, + "23": { + "0": 3.0404045066898244, + "32": 0.04148977907847477, + "40": 0.021596132424008038, + "48": 0.01100976489064943 + } + } + }, + { + "id": "fit-lookup-003", + "length": 106, + "mse": { + "15": { + "0": 3.0409953314068368, + "32": 0.03762918598377197, + "40": 0.019843534700772446, + "48": 0.010574553412406835 + }, + "19": { + "0": 3.688879228913018, + "32": 0.04392129860493001, + "40": 0.022968772572630233, + "48": 0.011682937781274223 + }, + "23": { + "0": 2.9696612315728035, + "32": 0.04365312259554772, + "40": 0.023110205181705423, + "48": 0.012251188178660657 + } + } + }, + { + "id": "fit-lookup-004", + "length": 106, + "mse": { + "15": { + "0": 3.021519909278273, + "32": 0.040987579290442194, + "40": 0.0218709069889447, + "48": 0.011580689428509915 + }, + "19": { + "0": 3.8603281075397944, + "32": 0.043717190016046695, + "40": 0.022459706664211883, + "48": 0.011489303457101622 + }, + "23": { + "0": 3.0533279734811867, + "32": 0.0429414877038192, + "40": 0.022566893434764704, + "48": 0.011829068992343534 + } + } + }, + { + "id": "fit-lookup-005", + "length": 106, + "mse": { + "15": { + "0": 3.033023410308657, + "32": 0.04048812757096736, + "40": 0.021191326763191305, + "48": 0.011073238036764433 + }, + "19": { + "0": 3.7947891522935953, + "32": 0.046712646076783715, + "40": 0.02369760775856494, + "48": 0.012093032569584385 + }, + "23": { + "0": 2.9266299087225995, + "32": 0.04418105299845767, + "40": 0.023223134094120208, + "48": 0.012197499278392794 + } + } + }, + { + "id": "fit-lookup-006", + "length": 106, + "mse": { + "15": { + "0": 3.013331118405758, + "32": 0.04131749410468121, + "40": 0.022042406028810024, + "48": 0.011636497469555753 + }, + "19": { + "0": 3.823321135345336, + "32": 0.043444020469542224, + "40": 0.023039572744899546, + "48": 0.012151077808294013 + }, + "23": { + "0": 3.0123366474248527, + "32": 0.043892186138404905, + "40": 0.023768489833834446, + "48": 0.012493576292187758 + } + } + }, + { + "id": "fit-lookup-007", + "length": 106, + "mse": { + "15": { + "0": 2.9797496554592016, + "32": 0.040086780980416785, + "40": 0.021471205229520056, + "48": 0.011298897793875544 + }, + "19": { + "0": 3.826629680630135, + "32": 0.046253373518954505, + "40": 0.02394178399044027, + "48": 0.012263827559098354 + }, + "23": { + "0": 3.0080892417871485, + "32": 0.04416354613658148, + "40": 0.02347209773759818, + "48": 0.01214014313596462 + } + } + }, + { + "id": "fit-lookup-008", + "length": 106, + "mse": { + "15": { + "0": 3.0195279869049405, + "32": 0.04027418538529785, + "40": 0.021116286802696546, + "48": 0.010885256441784051 + }, + "19": { + "0": 3.9049296662519, + "32": 0.044775724860484895, + "40": 0.023020902804934695, + "48": 0.01181115860807368 + }, + "23": { + "0": 3.022332594278084, + "32": 0.04271748172881601, + "40": 0.022841955815524173, + "48": 0.011960985252592796 + } + } + }, + { + "id": "fit-lookup-009", + "length": 105, + "mse": { + "15": { + "0": 3.109396586760397, + "32": 0.03755905434822013, + "40": 0.019840372604972886, + "48": 0.010344465475210856 + }, + "19": { + "0": 3.889074040521854, + "32": 0.042862804080481424, + "40": 0.021958251337402295, + "48": 0.011303882000132073 + }, + "23": { + "0": 3.036374564918723, + "32": 0.042727702436651936, + "40": 0.022601026341831074, + "48": 0.011770845282018036 + } + } + }, + { + "id": "fit-lookup-010", + "length": 107, + "mse": { + "15": { + "0": 3.0311224516019055, + "32": 0.04091108401342758, + "40": 0.0218021374757848, + "48": 0.01159018725724504 + }, + "19": { + "0": 3.9363469267180835, + "32": 0.04567514334168894, + "40": 0.02376781635912793, + "48": 0.012322024685530862 + }, + "23": { + "0": 3.014115868352978, + "32": 0.044461534712689404, + "40": 0.023592730803344734, + "48": 0.012495188587781836 + } + } + }, + { + "id": "fit-lookup-011", + "length": 106, + "mse": { + "15": { + "0": 2.978140786774898, + "32": 0.0387811816167493, + "40": 0.02037526802543315, + "48": 0.0106736184315463 + }, + "19": { + "0": 3.844477663585858, + "32": 0.045556113791662724, + "40": 0.023331656132232113, + "48": 0.011906942552667195 + }, + "23": { + "0": 2.9601191176649757, + "32": 0.043240836434076536, + "40": 0.02260293695313534, + "48": 0.0119007769151467 + } + } + }, + { + "id": "fit-lookup-012", + "length": 107, + "mse": { + "15": { + "0": 3.0087156260860324, + "32": 0.039908258905094175, + "40": 0.02095174812968115, + "48": 0.010945949375037204 + }, + "19": { + "0": 3.8500214677028963, + "32": 0.04244640193219456, + "40": 0.021656130249074597, + "48": 0.011118828008838963 + }, + "23": { + "0": 3.012859888829719, + "32": 0.04171247094948818, + "40": 0.0222639528061427, + "48": 0.011602191423433644 + } + } + }, + { + "id": "fit-lookup-013", + "length": 105, + "mse": { + "15": { + "0": 3.090885110032237, + "32": 0.03762876468904888, + "40": 0.019838887261469686, + "48": 0.010323019538322325 + }, + "19": { + "0": 3.8526016885782073, + "32": 0.04267441235679496, + "40": 0.021965440593552564, + "48": 0.011204493115001445 + }, + "23": { + "0": 2.9829829723700807, + "32": 0.04183375644979849, + "40": 0.022193649610459963, + "48": 0.0116555170847707 + } + } + }, + { + "id": "fit-lookup-014", + "length": 105, + "mse": { + "15": { + "0": 3.1381995988564113, + "32": 0.0394204188617157, + "40": 0.02086813289927566, + "48": 0.010854347550869876 + }, + "19": { + "0": 3.96491581298912, + "32": 0.044485907588952385, + "40": 0.022877678884423304, + "48": 0.01175281372917842 + }, + "23": { + "0": 3.056772542795872, + "32": 0.04255880849165352, + "40": 0.022612492155258415, + "48": 0.011914870920016675 + } + } + }, + { + "id": "fit-lookup-015", + "length": 104, + "mse": { + "15": { + "0": 3.069267210462956, + "32": 0.037746196997427636, + "40": 0.01989923322963685, + "48": 0.01029592139206914 + }, + "19": { + "0": 3.8705993446575504, + "32": 0.042685739359091625, + "40": 0.021382466243261136, + "48": 0.01075929182481224 + }, + "23": { + "0": 3.0856817422797613, + "32": 0.041619385601778976, + "40": 0.021868313984371644, + "48": 0.0112163979206816 + } + } + }, + { + "id": "fit-lookup-016", + "length": 106, + "mse": { + "15": { + "0": 3.0007538969450036, + "32": 0.03916077842933743, + "40": 0.02049354544660005, + "48": 0.010682154227783142 + }, + "19": { + "0": 3.870593473010357, + "32": 0.04499375247587852, + "40": 0.023060264604825936, + "48": 0.011894306441044961 + }, + "23": { + "0": 3.0078244299495527, + "32": 0.04343119038793748, + "40": 0.02305271005274073, + "48": 0.012061257691392924 + } + } + }, + { + "id": "fit-lookup-017", + "length": 104, + "mse": { + "15": { + "0": 3.097493153149072, + "32": 0.03850921447926982, + "40": 0.019769321682123944, + "48": 0.010122230466840933 + }, + "19": { + "0": 3.820263396381217, + "32": 0.04169650174393219, + "40": 0.020890787003452566, + "48": 0.010491055604303079 + }, + "23": { + "0": 3.055860371881066, + "32": 0.04156449816886682, + "40": 0.02182786872676137, + "48": 0.011057894501810384 + } + } + }, + { + "id": "fit-lookup-018", + "length": 108, + "mse": { + "15": { + "0": 3.0013828310609028, + "32": 0.03971017933680604, + "40": 0.021048825625806546, + "48": 0.011115656621268686 + }, + "19": { + "0": 3.8060033369100768, + "32": 0.04542715302568046, + "40": 0.023526612657494325, + "48": 0.012077680832697587 + }, + "23": { + "0": 3.0269971279675176, + "32": 0.04410912108278284, + "40": 0.023608312332425496, + "48": 0.012556557224355341 + } + } + }, + { + "id": "fit-lookup-019", + "length": 103, + "mse": { + "15": { + "0": 3.0112006462653227, + "32": 0.037640117940643265, + "40": 0.019441597324216846, + "48": 0.009988085038926717 + }, + "19": { + "0": 3.8768517398640947, + "32": 0.04158191015113058, + "40": 0.020643499621951257, + "48": 0.01039837221786275 + }, + "23": { + "0": 3.065298751963704, + "32": 0.040825522024696886, + "40": 0.021289032661542927, + "48": 0.010876842781099974 + } + } + }, + { + "id": "fit-lookup-020", + "length": 106, + "mse": { + "15": { + "0": 3.0499784330668804, + "32": 0.04122577355978493, + "40": 0.021592165464748375, + "48": 0.011222263753869931 + }, + "19": { + "0": 3.8824985292132834, + "32": 0.04492968673719023, + "40": 0.022922977828289975, + "48": 0.011503317232916728 + }, + "23": { + "0": 3.033711254655971, + "32": 0.043830061367554726, + "40": 0.02317944512208468, + "48": 0.012154593458581674 + } + } + }, + { + "id": "fit-lookup-021", + "length": 105, + "mse": { + "15": { + "0": 3.0001907173024827, + "32": 0.03697409357661978, + "40": 0.019188918824031547, + "48": 0.009750811653384558 + }, + "19": { + "0": 3.766616992263308, + "32": 0.042624045486801626, + "40": 0.02124433468816115, + "48": 0.010587855522900326 + }, + "23": { + "0": 2.9766873470897104, + "32": 0.041481534529769944, + "40": 0.02121486238795562, + "48": 0.010788750140809036 + } + } + }, + { + "id": "fit-lookup-022", + "length": 103, + "mse": { + "15": { + "0": 3.1429597973216787, + "32": 0.03808212093085823, + "40": 0.019726271041694728, + "48": 0.010127914275230436 + }, + "19": { + "0": 3.8598970499663743, + "32": 0.04248202181605006, + "40": 0.0214628237211103, + "48": 0.010853348943230156 + }, + "23": { + "0": 3.049645123879466, + "32": 0.039979464021990575, + "40": 0.02080617789667549, + "48": 0.010634926145447776 + } + } + }, + { + "id": "fit-lookup-023", + "length": 105, + "mse": { + "15": { + "0": 3.048208724265116, + "32": 0.041337984464644754, + "40": 0.022112363284163796, + "48": 0.011560149309456157 + }, + "19": { + "0": 3.8843489019292843, + "32": 0.043591605489556345, + "40": 0.02274112402559217, + "48": 0.011750496554981203 + }, + "23": { + "0": 3.0388065889185087, + "32": 0.04281829316721918, + "40": 0.022603219431728962, + "48": 0.01173585649494112 + } + } + } + ] +}