K-Means Memory

Streaming k-means over keys — exact attention below capacity, graceful compression above it.

K-Means Memory

Motivated by the Lattice Memory findings (the no-dynamics ablation was the fidelity ceiling; the best spring constant trended to zero): keep the mass-weighted centroid read, drop the topology entirely, and let the partition be data-adaptive streaming k-means over keys.

Mechanics: greedy fill — while a slot is free, every token births its own centroid, so short contexts are exact attention. Once full, tokens merge into the Euclidean-nearest centroid; a deferred scale factor makes the leak O(1) per token; centroids dying below a mass floor free their slots so births resume as old content fades. The leak doubles as a linear recency bias through the \beta\log(\text{mass}) score term.

Below capacity with zero leak, k-means memory is bit-identical to causal attention: max |Δlogit| = 0.000000 over the full probe. verified
How we know this
computed by evidence/exact-formulations · source code/attention (Java engine) · as of 2026-07-21
implspecmax_dlogit
slidingsliding:2560.000000
kmeans-exactkmeans:l=00.000000
tiered-exacttiered:e=1024;l=0;nerode=00.000000
sink-exactsink:s=16;w=2560.000000

Findings

  1. Dropping the lattice topology lost nothing and (attested) doubled streaming speed, with equal-or-better lexical stats at 16× compression (2048 tokens in 128 centroids per head).
  2. Birth policy matters more than capacity. A DP-means-style relative threshold self-throttled — the mean-distance EMA anchors high and almost nothing births, making capacity settings indistinguishable. Greedy fill fixed it instantly. (The idea returns, rehabilitated, as the surprise gate.)
  3. Deep-stream degradation (~position 1900) persists and is not explained by memory blur alone or RoPE extrapolation alone: position-free scoring (rope=none) made it worse, so positional scoring is protective.
  4. Re-indexed relative RoPE is implemented, verified, and — frozen — refuted. Each centroid tracks a decaying mean insertion position; at read time its key is rotated by its clamped integer age, so scoring uses relative positions that never leave the trained range — StreamingLLM's rolling-position trick generalized to clusters. The read path is exact to float noise (0.000014 max |Δlogit| at zero leak), but deep streams got worse. Diagnosis: raw phase interference is a feature — a position-incoherent cluster's rotated keys partially cancel, automatically suppressing blurry blobs; re-indexing restores their full magnitude at one fake age and lets stale mush outcompete fresh tokens. Position handling and forgetting are coupled. The story reverses with training in the loop — see Training in the Loop.

Measured behaviour

Short-range fidelity on the standard probe, and 3-seed stream health at 2048 tokens (8× trained context):

implspecagreetotalworst_dlogit
kmeans128kmeans:m=12867701.459785
sink4-124sink:s=4;w=12470700.000000
kmeans64kmeans:m=64677015.235278
tiered-nerodetiered:m=64;e=16;nerode=64;tau=0.2;tv=0.0568700.979404
tiered-gatetiered:m=64;e=16;nerode=0;g=0.567700.993266
tiered32-gatetiered:m=32;e=16;g=1.063705.868399
lattice-binninglattice:g=32;it=049709.130353
lattice-bestlattice:rope=raw;k=0.01;l=0.0154709.767433
lattice-underdampedlattice:d=0.12147024.241472
implseedfirstlastmin
tiered-nerode-s110.580.500.38
tiered-nerode-s220.490.500.49
tiered-nerode-s330.540.520.38
sink4-124-s110.560.220.22
sink4-124-s220.540.280.28
sink4-124-s330.570.400.32
kmeans128-s110.630.520.31
kmeans128-s220.550.490.43
kmeans128-s330.480.530.35
kmeans64-s110.580.500.36
kmeans64-s220.430.360.29
kmeans64-s330.520.500.44
tiered32-gate-s110.600.450.40
tiered32-gate-s220.600.430.35
tiered32-gate-s330.450.540.38
lattice-best-s110.550.600.52
lattice-best-s220.550.560.54
lattice-best-s330.390.670.39

kmeans:m=128 holds the distinct-token ratio roughly flat across the stream; at half that budget (m=64) the worst seed sags — the problem Tiered Memory fixes at half the m=128 budget. Frozen (no tuning), the compressed memory pays a real perplexity tax at 2× context: validation CE 4.2283 for raw k-means vs 2.6080 for exact attention with RoPE extrapolation (see Training in the Loop for how tuning closes this).