Check rank allocation against adjacent native donor scores

This commit is contained in:
2026-11-26 13:37:16 +08:00
parent 63b0705353
commit 6baff1af4a
3 changed files with 710 additions and 12 deletions
+28 -11
View File
@@ -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))