Files
leleka/scripts/test-keyless-routing.py
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

75 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Independent identities for the paper's effective query projection."""
import importlib.util
from pathlib import Path
import unittest
import numpy as np
spec = importlib.util.spec_from_file_location("routing", Path(__file__).with_name("prepare-keyless-routing.py"))
routing = importlib.util.module_from_spec(spec)
spec.loader.exec_module(routing)
calibration_spec = importlib.util.spec_from_file_location("calibration", Path(__file__).with_name("calibrate-keyless-routing.py"))
calibration = importlib.util.module_from_spec(calibration_spec)
calibration_spec.loader.exec_module(calibration)
class FrozenRoutingCalibration(unittest.TestCase):
def test_positional_attention_gradient_against_finite_difference(self):
rng = np.random.default_rng(9023)
q, v, raw_v = rng.normal(size=(2,3,256)), rng.normal(size=(2,4,256)), rng.normal(size=(2,4,256))
qp=np.array([31,63,95]); mask=(np.array([0,16,32,80])[None,:]<=qp[:,None])[None,:,:]
teacher=rng.normal(size=(2,3,4)); target=calibration.softmax(teacher,mask)
data=(q,qp,v,raw_v,mask,teacher,target)
matrix=rng.normal(size=(2,256,256))*.01
loss, gradient=calibration.objective_gradient(matrix,data)
direction=rng.normal(size=matrix.shape); direction/=np.linalg.norm(direction)
epsilon=1e-4
plus=calibration.objective_gradient(matrix+epsilon*direction,data)[0].sum()
minus=calibration.objective_gradient(matrix-epsilon*direction,data)[0].sum()
self.assertAlmostEqual(float((plus-minus)/(2*epsilon)),float(np.sum(gradient*direction)),places=7)
x=rng.normal(size=(2,3,256))
np.testing.assert_allclose(calibration.rotate(calibration.rotate(x,qp),qp,inverse=True),x,rtol=2e-7,atol=2e-7)
class QueryRouting(unittest.TestCase):
def test_square_value_projection_preserves_logits(self):
rng = np.random.default_rng(4102)
q, k, v = (rng.normal(size=(7, 7)) for _ in range(3))
x, y = rng.normal(size=(19, 7)), rng.normal(size=(13, 7))
a, report = routing.query_routing(q, k, v)
teacher = (x @ q.T) @ (y @ k.T).T
candidate = ((x @ q.T) @ a) @ (y @ v.T).T
np.testing.assert_allclose(candidate, teacher, rtol=1e-10, atol=1e-10)
self.assertLess(report["score_weight_relative_l2"], 1e-11)
def test_rectangular_equivalence_under_subspace_condition(self):
rng = np.random.default_rng(319)
q, v = rng.normal(size=(4, 17)), rng.normal(size=(4, 17))
k = rng.normal(size=(4, 4)) @ v
a, report = routing.query_routing(q, k, v)
np.testing.assert_allclose(q.T @ a @ v, q.T @ k, rtol=1e-11, atol=1e-11)
self.assertLess(report["key_subspace_relative_l2"], 1e-11)
def test_out_of_subspace_residual_is_reported(self):
rng = np.random.default_rng(521)
q, k, v = (rng.normal(size=(4, 17)) for _ in range(3))
a, report = routing.query_routing(q, k, v)
direct = np.linalg.norm(q.T @ k - q.T @ a @ v) / np.linalg.norm(q.T @ k)
self.assertAlmostEqual(report["score_weight_relative_l2"], direct, places=12)
self.assertGreater(report["score_weight_relative_l2"], .1)
# The residual must be orthogonal to every value-space basis vector.
np.testing.assert_allclose((k - a @ v) @ v.T, 0, atol=1e-11)
def test_rank_deficiency_is_explicit(self):
v = np.array([[1., 0., 0.], [2., 0., 0.]])
_, report = routing.query_routing(v, v, v)
self.assertEqual(report["value_rank"], 1)
with self.assertRaises(ValueError):
routing.query_routing(v, v, np.zeros_like(v))
if __name__ == "__main__":
unittest.main()