diff --git a/crossqkv/lisa_rank_residuals.py b/crossqkv/lisa_rank_residuals.py index fe0f87d..e35b237 100644 --- a/crossqkv/lisa_rank_residuals.py +++ b/crossqkv/lisa_rank_residuals.py @@ -32,7 +32,17 @@ def matrices(directory, record): return features, targets, length -def measure(prior): +def layer_designs(donor, teachers, adjacent_native): + for layer, target in enumerate(np.hsplit(teachers, 3)): + if adjacent_native and layer: + start = (layer - 1) * 8 + features = np.column_stack((teachers[:, start:start + 8], donor[:, -1])) + else: + features = donor + yield features, target + + +def measure(prior, adjacent_native=False): 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') @@ -41,18 +51,22 @@ def measure(prior): 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)) + gram = np.zeros((2, 3, 9, 9)) + cross = np.zeros((2, 3, 9, 8)) 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)] + donor, teachers, _ = matrices(directory, record) + for layer, (x, y) in enumerate(layer_designs(donor, teachers, adjacent_native)): + gram[i % 2, layer] += x.T @ x + cross[i % 2, layer] += x.T @ y + coefficients = np.linalg.solve(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) + donor, teachers, length = matrices(directory, record) + errors = [(y - x @ coef).T.reshape(8, length, length) + for (x, y), coef in zip(layer_designs(donor, teachers, adjacent_native), + coefficients[1 - i % 2])] + residual = np.stack(errors) 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) @@ -73,7 +87,8 @@ def measure(prior): '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', + **({'native_donor_layers': [11, 15, 19]} if adjacent_native else {'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 } @@ -82,5 +97,7 @@ def measure(prior): if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('prior_final', type=Path) + parser.add_argument('--adjacent-native', action='store_true', + help='Use each previous native full-attention layer as the donor') args = parser.parse_args() - print(json.dumps(measure(args.prior_final), indent=2, allow_nan=False)) + print(json.dumps(measure(args.prior_final, args.adjacent_native), indent=2, allow_nan=False)) diff --git a/docs/LISA_COMBINATIONS_RESULTS.md b/docs/LISA_COMBINATIONS_RESULTS.md index 1119d45..a365dca 100644 --- a/docs/LISA_COMBINATIONS_RESULTS.md +++ b/docs/LISA_COMBINATIONS_RESULTS.md @@ -60,7 +60,9 @@ 第 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 须从隐藏状态预测分数,并经非线性网络融合多个头。这个比较说明简单增秩分配在训练分数近似上的代价。其对完整答案的影响由训练后的生成结果检验。 +这项近似用同一个第 11 层预测三个接收层,并可逐题看到原生目标矩阵。实际 LiSA 依次把第 11、15、19 层的分数传给第 15、19、23 层,后两次传递的是上一接收层的融合输出。模型还须从隐藏状态预测局部分数。13.9% 衡量的是固定第 11 层时的矩阵近似误差,实际链式共享的效果由完整答案检验。 [训练前向](../crossqkv/lisa_tail.py) · [贪心生成前向](../crossqkv/lisa_generate.py) + +把供给改成上一全注意力层的**原生分数**后,均匀秩的三层平均误差为 0.01682,敏感度分秩为 0.01901(增加 13.0%),反向分秩为 0.01885(增加 12.1%)。六种非均匀排列在每道题上的误差仍然都更高,但两种分秩的相对顺序随供给层而改变。固定第 11 层时,敏感度分秩在 23/24 题上优于反向分秩。使用相邻层原生分数时,这一数字降至 2/24。两种分析都支持均匀秩在这七个配置里的分数近似优势,要判断多给第 19 层还是第 23 层更有利于完整答案,仍须比较训练后的链式模型。脚本选项为 `--adjacent-native`。独立 QR 拟合及逐头矩阵重建复核了 288 个误差值,最大绝对差小于 2×10⁻¹⁴。 [相邻原生供给的逐题数据](../results/lisa-combinations/fit-rank-adjacent-native.json) ## 两种蒸馏前缀的完整答案 diff --git a/results/lisa-combinations/fit-rank-adjacent-native.json b/results/lisa-combinations/fit-rank-adjacent-native.json new file mode 100644 index 0000000..6ee594a --- /dev/null +++ b/results/lisa-combinations/fit-rank-adjacent-native.json @@ -0,0 +1,679 @@ +{ + "source_receipt_sha256": "682bc035a5bed9f9245f589cacf3e9616300e8bf5e85d3743b1264a56360fa55", + "split": "fit", + "sites": [ + 15, + 19, + 23 + ], + "native_donor_layers": [ + 11, + 15, + 19 + ], + "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.039521781594895264, + "40": 0.020783290371489457, + "48": 0.010840281262399451 + }, + "19": { + "0": 2.512677766820607, + "32": 0.028017079240270398, + "40": 0.014575690475241961, + "48": 0.0075188286924900765 + }, + "23": { + "0": 2.32949672919509, + "32": 0.028727581321570477, + "40": 0.015100597622509863, + "48": 0.007748733860780121 + } + }, + "configurations": [ + { + "ranks": [ + 40, + 40, + 40 + ], + "mean_mse": 0.016819859489747093, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 32, + 40, + 48 + ], + "mean_mse": 0.02061540197697245, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 32, + 48, + 40 + ], + "mean_mse": 0.020713735969965067, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 40, + 32, + 48 + ], + "mean_mse": 0.018849701157513325, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 40, + 48, + 32 + ], + "mean_mse": 0.019009900128516666, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 48, + 32, + 40 + ], + "mean_mse": 0.017985986041726573, + "examples_below_uniform_mse": 0 + }, + { + "ranks": [ + 48, + 40, + 32 + ], + "mean_mse": 0.018047851019737295, + "examples_below_uniform_mse": 0 + } + ], + "examples": [ + { + "id": "fit-lookup-000", + "length": 105, + "mse": { + "15": { + "0": 3.088659967440397, + "32": 0.04236259250553502, + "40": 0.022141397788788392, + "48": 0.011514037850336353 + }, + "19": { + "0": 2.60677435406026, + "32": 0.030297211627820866, + "40": 0.01598283395379536, + "48": 0.008219940043388664 + }, + "23": { + "0": 2.3563735679615907, + "32": 0.02892025119959655, + "40": 0.015145857942858488, + "48": 0.007854447730351124 + } + } + }, + { + "id": "fit-lookup-001", + "length": 106, + "mse": { + "15": { + "0": 3.0437552842711924, + "32": 0.041460379107627, + "40": 0.02197332831278948, + "48": 0.01154738790723923 + }, + "19": { + "0": 2.6002811453342303, + "32": 0.029788866923491097, + "40": 0.015212847384373162, + "48": 0.007800509698369113 + }, + "23": { + "0": 2.3453950294400725, + "32": 0.028961945449738232, + "40": 0.015314327377369019, + "48": 0.007819546227552078 + } + } + }, + { + "id": "fit-lookup-002", + "length": 103, + "mse": { + "15": { + "0": 3.1611634458593403, + "32": 0.039321211199099995, + "40": 0.020199787980594405, + "48": 0.010459417590053765 + }, + "19": { + "0": 2.4157809408598623, + "32": 0.02655148904575669, + "40": 0.013735592399044141, + "48": 0.007057144763423892 + }, + "23": { + "0": 2.325402166282838, + "32": 0.027055553748214518, + "40": 0.013891678650954479, + "48": 0.006962328419010828 + } + } + }, + { + "id": "fit-lookup-003", + "length": 106, + "mse": { + "15": { + "0": 3.040995331406837, + "32": 0.03762918598377188, + "40": 0.01984353470077239, + "48": 0.010574553412406797 + }, + "19": { + "0": 2.3597098591507217, + "32": 0.02773534955136491, + "40": 0.014466135600102365, + "48": 0.0074571195122997225 + }, + "23": { + "0": 2.3234745384230875, + "32": 0.029123405193316097, + "40": 0.015470646972640318, + "48": 0.008016444188761525 + } + } + }, + { + "id": "fit-lookup-004", + "length": 106, + "mse": { + "15": { + "0": 3.0215199092782723, + "32": 0.040987579290442236, + "40": 0.02187090698894471, + "48": 0.011580689428509917 + }, + "19": { + "0": 2.573071094425272, + "32": 0.02882784504735971, + "40": 0.015108305637581228, + "48": 0.007925058462351522 + }, + "23": { + "0": 2.340310773344382, + "32": 0.028890642640557497, + "40": 0.015253046125958373, + "48": 0.007854863994071565 + } + } + }, + { + "id": "fit-lookup-005", + "length": 106, + "mse": { + "15": { + "0": 3.0330234103086564, + "32": 0.040488127570967286, + "40": 0.021191326763191246, + "48": 0.011073238036764401 + }, + "19": { + "0": 2.454760989612247, + "32": 0.02946771758936749, + "40": 0.01545794859144716, + "48": 0.00802543841461683 + }, + "23": { + "0": 2.2967207477889273, + "32": 0.029778415586498647, + "40": 0.01600304961677946, + "48": 0.0083111870016772 + } + } + }, + { + "id": "fit-lookup-006", + "length": 106, + "mse": { + "15": { + "0": 3.013331118405758, + "32": 0.04131749410468123, + "40": 0.022042406028810038, + "48": 0.011636497469555758 + }, + "19": { + "0": 2.528745937676687, + "32": 0.02826800264634013, + "40": 0.014733505683104612, + "48": 0.0076718514475501865 + }, + "23": { + "0": 2.3072629450831816, + "32": 0.0289161816884407, + "40": 0.015426171607648538, + "48": 0.008021220293678933 + } + } + }, + { + "id": "fit-lookup-007", + "length": 106, + "mse": { + "15": { + "0": 2.9797496554592016, + "32": 0.04008678098041669, + "40": 0.021471205229520018, + "48": 0.011298897793875518 + }, + "19": { + "0": 2.524372130523825, + "32": 0.02903602273670119, + "40": 0.015166059667194653, + "48": 0.007834174908611297 + }, + "23": { + "0": 2.3610797089906277, + "32": 0.029450602936166363, + "40": 0.015499606337721955, + "48": 0.00805269819015974 + } + } + }, + { + "id": "fit-lookup-008", + "length": 106, + "mse": { + "15": { + "0": 3.019527986904939, + "32": 0.040274185385297874, + "40": 0.021116286802696557, + "48": 0.010885256441784055 + }, + "19": { + "0": 2.523656269236793, + "32": 0.02772895841962885, + "40": 0.014412311378240944, + "48": 0.007384325810057883 + }, + "23": { + "0": 2.320639636723277, + "32": 0.027954408126430304, + "40": 0.014598363754756497, + "48": 0.007555480787878869 + } + } + }, + { + "id": "fit-lookup-009", + "length": 105, + "mse": { + "15": { + "0": 3.1093965867603957, + "32": 0.03755905434822006, + "40": 0.019840372604972834, + "48": 0.010344465475210821 + }, + "19": { + "0": 2.505387553072129, + "32": 0.027001747506091436, + "40": 0.013899598887412037, + "48": 0.007008295926413987 + }, + "23": { + "0": 2.3448651884655374, + "32": 0.029100983555730472, + "40": 0.015379827992887235, + "48": 0.007858481884474376 + } + } + }, + { + "id": "fit-lookup-010", + "length": 107, + "mse": { + "15": { + "0": 3.0311224516019064, + "32": 0.04091108401342763, + "40": 0.02180213747578483, + "48": 0.011590187257245059 + }, + "19": { + "0": 2.5713354258542362, + "32": 0.029039653431551533, + "40": 0.01531556329140523, + "48": 0.007899517545272823 + }, + "23": { + "0": 2.3511743607212607, + "32": 0.03012251758846437, + "40": 0.016076320232085935, + "48": 0.008327212573692013 + } + } + }, + { + "id": "fit-lookup-011", + "length": 106, + "mse": { + "15": { + "0": 2.978140786774899, + "32": 0.03878118161674924, + "40": 0.020375268025433116, + "48": 0.01067361843154627 + }, + "19": { + "0": 2.545974192767094, + "32": 0.02905582052089718, + "40": 0.01503920798962135, + "48": 0.007825902631264234 + }, + "23": { + "0": 2.3154443153866144, + "32": 0.029736139916476785, + "40": 0.015719287229754325, + "48": 0.00816518344542446 + } + } + }, + { + "id": "fit-lookup-012", + "length": 107, + "mse": { + "15": { + "0": 3.0087156260860324, + "32": 0.039908258905094224, + "40": 0.020951748129681164, + "48": 0.0109459493750372 + }, + "19": { + "0": 2.5004236680399994, + "32": 0.027530296227075, + "40": 0.014297426111298323, + "48": 0.0073502099933688055 + }, + "23": { + "0": 2.3037441781455907, + "32": 0.028300959976612527, + "40": 0.014905029499610128, + "48": 0.0076801742973075585 + } + } + }, + { + "id": "fit-lookup-013", + "length": 105, + "mse": { + "15": { + "0": 3.090885110032237, + "32": 0.037628764689048785, + "40": 0.01983888726146963, + "48": 0.010323019538322292 + }, + "19": { + "0": 2.4559173768588263, + "32": 0.027772754396836425, + "40": 0.014524390215698612, + "48": 0.007483559779230743 + }, + "23": { + "0": 2.287404497389505, + "32": 0.02819735934494258, + "40": 0.014912927963379, + "48": 0.007726565200995596 + } + } + }, + { + "id": "fit-lookup-014", + "length": 105, + "mse": { + "15": { + "0": 3.13819959885641, + "32": 0.039420418861715725, + "40": 0.020868132899275662, + "48": 0.010854347550869874 + }, + "19": { + "0": 2.5679901885805516, + "32": 0.028098421308328406, + "40": 0.014312423513410482, + "48": 0.00732004539511427 + }, + "23": { + "0": 2.3466282966243877, + "32": 0.02880736726984329, + "40": 0.014808088738714507, + "48": 0.00750811978348572 + } + } + }, + { + "id": "fit-lookup-015", + "length": 104, + "mse": { + "15": { + "0": 3.0692672104629573, + "32": 0.037746196997427546, + "40": 0.019899233229636805, + "48": 0.01029592139206912 + }, + "19": { + "0": 2.4838282610330245, + "32": 0.026826048708958952, + "40": 0.013808543684409204, + "48": 0.006989747600969923 + }, + "23": { + "0": 2.322057578321556, + "32": 0.02797636928934322, + "40": 0.014399908661816594, + "48": 0.007212087854706693 + } + } + }, + { + "id": "fit-lookup-016", + "length": 106, + "mse": { + "15": { + "0": 3.0007538969450036, + "32": 0.03916077842933745, + "40": 0.020493545446600054, + "48": 0.010682154227783146 + }, + "19": { + "0": 2.5872858743785674, + "32": 0.028776526081457792, + "40": 0.015233754140328008, + "48": 0.007916572897216386 + }, + "23": { + "0": 2.347073684588352, + "32": 0.029756068268149995, + "40": 0.015805893856473047, + "48": 0.00809506288386048 + } + } + }, + { + "id": "fit-lookup-017", + "length": 104, + "mse": { + "15": { + "0": 3.097493153149071, + "32": 0.03850921447926972, + "40": 0.019769321682123874, + "48": 0.010122230466840895 + }, + "19": { + "0": 2.4503922223577725, + "32": 0.025405658587984905, + "40": 0.013039908579401138, + "48": 0.006659946143533161 + }, + "23": { + "0": 2.3199401227863863, + "32": 0.02735269039309821, + "40": 0.014341351722532756, + "48": 0.007256346435349317 + } + } + }, + { + "id": "fit-lookup-018", + "length": 108, + "mse": { + "15": { + "0": 3.0013828310609036, + "32": 0.03971017933680606, + "40": 0.02104882562580656, + "48": 0.011115656621268687 + }, + "19": { + "0": 2.5217980667349007, + "32": 0.030615218969447338, + "40": 0.01612161718714934, + "48": 0.008545605297330143 + }, + "23": { + "0": 2.326397701784687, + "32": 0.03073856758564035, + "40": 0.016339969467014808, + "48": 0.008568277707684677 + } + } + }, + { + "id": "fit-lookup-019", + "length": 103, + "mse": { + "15": { + "0": 3.0112006462653227, + "32": 0.03764011794064314, + "40": 0.019441597324216794, + "48": 0.009988085038926689 + }, + "19": { + "0": 2.543471885532161, + "32": 0.02619589976045457, + "40": 0.013660075369000754, + "48": 0.007015486171297309 + }, + "23": { + "0": 2.3365870614142663, + "32": 0.027970465568334695, + "40": 0.014406812634280746, + "48": 0.007354861080531025 + } + } + }, + { + "id": "fit-lookup-020", + "length": 106, + "mse": { + "15": { + "0": 3.0499784330668795, + "32": 0.04122577355978497, + "40": 0.021592165464748382, + "48": 0.011222263753869933 + }, + "19": { + "0": 2.4914495907758174, + "32": 0.028387678466645947, + "40": 0.014732006285955, + "48": 0.007600996890895566 + }, + "23": { + "0": 2.333302869555968, + "32": 0.028901195362459362, + "40": 0.015146381684210501, + "48": 0.007708436189913433 + } + } + }, + { + "id": "fit-lookup-021", + "length": 105, + "mse": { + "15": { + "0": 3.000190717302482, + "32": 0.036974093576619696, + "40": 0.019188918824031505, + "48": 0.009750811653384532 + }, + "19": { + "0": 2.504255342893704, + "32": 0.02679008610350141, + "40": 0.013656397393850609, + "48": 0.007080721321304438 + }, + "23": { + "0": 2.343114821525361, + "32": 0.027011591312553825, + "40": 0.014172719903089378, + "48": 0.007187917055432581 + } + } + }, + { + "id": "fit-lookup-022", + "length": 103, + "mse": { + "15": { + "0": 3.1429597973216787, + "32": 0.03808212093085827, + "40": 0.019726271041694738, + "48": 0.010127914275230446 + }, + "19": { + "0": 2.5344581089967053, + "32": 0.02599389167658157, + "40": 0.013376992897619809, + "48": 0.006852960444486019 + }, + "23": { + "0": 2.291104635624609, + "32": 0.02783449426763895, + "40": 0.01437474325606307, + "48": 0.007167743463725208 + } + } + }, + { + "id": "fit-lookup-023", + "length": 105, + "mse": { + "15": { + "0": 3.048208724265116, + "32": 0.04133798446464467, + "40": 0.022112363284163734, + "48": 0.011560149309456123 + }, + "19": { + "0": 2.453145924939178, + "32": 0.027218736432846176, + "40": 0.01452312556436353, + "48": 0.007526757521394901 + }, + "23": { + "0": 2.362423074310092, + "32": 0.028603775449443824, + "40": 0.015022331711637587, + "48": 0.007704925968997894 + } + } + } + ] +}