Skip to content

Instantly share code, notes, and snippets.

@ChrisLundquist
Last active March 19, 2026 08:02
Show Gist options
  • Select an option

  • Save ChrisLundquist/6f3612d056f14c15c1d16afe117182d3 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisLundquist/6f3612d056f14c15c1d16afe117182d3 to your computer and use it in GitHub Desktop.
kronecker vs SVD

Hypercomplex Structure in Transformer Superposition

Experimental investigation of whether trained neural networks implicitly learn weight matrices that decompose into hypercomplex-like algebraic structures (quaternion, Clifford algebra, Kronecker-factored).

Key Findings

Experiment 3 (The Critical Test): Kronecker vs SVD Surgery

Baseline GPT-2 small perplexity: 29.88 (WikiText-2 test, standard strided evaluation)

Block Size Kronecker PPL SVD PPL Kron Params SVD Params
2 36,853 6,147 42.5M 42.5M
4 29,019 7,118 21.2M 21.3M
8 5,509 5,156 10.6M 10.6M
16 4,487 6,166 5.5M 5.5M
32 4,750 9,037 4.2M 4.3M

All outcomes are Outcome C (both methods catastrophically degrade the model), but the relative ordering is informative:

  • At BS=2 and BS=4: SVD wins decisively. Kronecker decomposition is far worse than SVD at matched parameters. This directly contradicts the "complex-number structure" interpretation from Experiment 1 — if the model had learned block-2 Kronecker structure, BS=2 surgery should outperform SVD, not lose by 6x.

  • At BS=16 and BS=32: Kronecker wins. Kronecker surgery degrades the model less than SVD at matched parameters (4,487 vs 6,166 at BS=16; 4,750 vs 9,037 at BS=32). This suggests genuine medium-block Kronecker structure — the weight matrices are better described by sums of 16x16 or 32x32 Kronecker products than by low-rank approximations.

  • At BS=8: roughly tied (5,509 vs 5,156).

The crossover at BS=8 suggests a structural transition: below BS=8, the weights are better captured by rank (SVD wins); above BS=8, they're better captured by block structure (Kronecker wins).

Selective Surgery

Surgery target PPL (BS=16) Ratio vs baseline
Original model 29.88 1.0x
Attention only 1,973 66x
MLP only 8,020 268x
Early layers (0-5) 6,446 216x
Late layers (6-11) 2,083 70x

Attention layers are more tolerant of Kronecker replacement than MLP layers (66x vs 268x degradation). Late layers are more tolerant than early layers (70x vs 216x). This is the opposite of what we'd naively expect — it suggests late-layer attention has the most Kronecker-compatible structure.

Experiment 1 (Toy Model): Strong Block-2 Signal

In the Anthropic-style toy superposition model (20 bottleneck dims, 80 features):

  • Block size 2 achieves Kronecker error ratios of 0.007-0.08 vs random baselines
  • Ratio is significant at all sparsity levels (S=0.01 to S=1.0)
  • Clifford algebra relations are not satisfied (violations ~1.1-1.3)
  • 2-fold and 4-fold symmetry scores are significant (p < 5%) in roots-of-unity analysis

However, the GPT-2 surgery results show this toy-model signal doesn't transfer: BS=2 Kronecker surgery is much worse than SVD on the real model.

Experiment 2 (GPT-2 Weight Analysis)

  • Attention weights show best Kronecker ratios at BS=2 (early layers: 0.78-0.87) and BS=64 (head dim, later layers)
  • MLP layers show minimal structure (ratios 0.97-0.99)
  • Clifford violations are near the random baseline (~1.1 vs 1.15)

2x2 A-matrix Classification (aI + bJ + cK + dL)

Energy is uniformly distributed across all four basis elements:

  • Scaling (a): 25.5%
  • Rotation (b): 25.8%
  • Diagonal reflection (c): 25.0%
  • Anti-diagonal reflection (d): 23.7%

Complex-number energy (a^2 + b^2) = 51.3% vs random baseline 50.1%. No rotation dominance — the BS=2 structure in the Kronecker decomposition is not specifically about complex multiplication.

Experiment 4 (Rotation Plane Analysis)

GPT-2's orthogonal factor rotation angles are more uniformly distributed than random orthogonal matrices (all k-fold z-scores > 2). Training smooths angular distributions rather than creating roots-of-unity clusters. No per-layer variation detected.

Mechanism Investigation: Why Does Kronecker Beat SVD on PPL?

The paradox: at BS=16, Kronecker surgery gives worse reconstruction error than SVD by every per-layer metric, yet produces better perplexity (4,487 vs 6,166). We tested four hypotheses:

Hypothesis 1: Activation-Subspace Alignment — REJECTED

Kronecker might concentrate error in directions the network doesn't use. Tested by computing ||xW - xW_approx|| using real WikiText-2 activations.

Result: SVD wins on activation-weighted error even MORE decisively than on Frobenius error (0/48 layers favor Kronecker). The errors are not in unused subspaces.

Hypothesis 2: Cross-Layer Error Cancellation — REJECTED (but subtle)

Kronecker errors might cancel across layers while SVD errors compound. Tested via single-layer surgery (replace one layer at a time, evaluate PPL).

Result: Kronecker wins 11/12 single-layer attention surgeries. The advantage exists per-layer, not just globally. But the per-layer effects are massively non-additive (see hybrid surgery below).

Hypothesis 3: Spectral Conditioning — PARTIAL

SVD truncation creates rank-deficient matrices; Kronecker preserves full rank.

Method Median Condition Number (BS=16)
Original weights 83.8
Kronecker approx 52.5
SVD approx 2.9 × 10^16 (numerically singular)

SVD at BS=16 keeps only ~37 of 768 singular values — the approximated matrices are effectively rank-37. Kronecker preserves all 768 dimensions with effective rank ~768.

Nullspace noise test: Injecting random signal into SVD's nullspace does NOT close the PPL gap (best gap reduction: 4%). The hard spectral cutoff is a contributing factor but not the primary mechanism.

Hypothesis 4: Error Type Consistency — CONFIRMED (the real mechanism)

Hybrid surgery (the decisive test):

Configuration PPL Description
All Kronecker 4,487 Best — consistent block-structure errors
Reverse hybrid 5,125 SVD on attention + Kronecker on MLP
All SVD 6,166 Consistent rank-truncation errors
Hybrid 11,725 Worst — Kron on attention + SVD on MLP

The prediction was that hybrid (Kronecker on attention, SVD on MLP) should beat both uniform methods, since per-layer data shows Kronecker winning on attention and SVD winning on MLP. Instead, it's the worst of all four configurations — nearly 3x worse than all-Kronecker.

Per-layer gap analysis (PPL_SVD − PPL_Kron for single-layer surgery):

Layer type Gap pattern Interpretation
Attention c_attn +0.3 to +14.0 Kronecker slightly better
Attention c_proj −0.9 to +1.9 Mixed, negligible
MLP c_fc −141,886 to −55 SVD vastly better
MLP c_proj −693 to −0.5 SVD moderately better

If per-layer effects were additive, the MLP penalty sum (−146K) would overwhelm the attention benefit (+40), predicting all-SVD should win. Yet all-Kronecker (4,487) beats all-SVD (6,166). The effects are non-additive by over five orders of magnitude.

Mechanism: Why Consistency Wins

The real mechanism is cross-layer error coherence:

  1. Rank preservation: Kronecker matrices maintain full rank (condition ~52) while SVD creates rank-37 approximations (condition ~10^16). Full-rank errors can be partially absorbed by downstream layers; rank-deficient projections permanently destroy information in 731/768 dimensions.

  2. Error type mixing is catastrophic: Within each transformer block, the data flows through attention → residual → MLP → residual. When attention uses Kronecker (block-structured errors) and MLP uses SVD (rank-truncation errors), the error types are incompatible — neither downstream component can compensate for the other's error structure. This creates the worst outcome (11,725 PPL).

  3. Consistent Kronecker errors are self-compensating: When all layers use Kronecker, the block-structured errors become the "language" of the model. Downstream Kronecker-approximated layers process Kronecker-approximated inputs, and the consistent error structure allows partial compensation. Each layer's errors are somewhat aligned with what the next layer expects.

  4. Single-layer catastrophes disappear in full surgery: Kronecker on h.0.mlp.c_fc alone causes ~141K PPL damage, but this disappears when all layers are changed simultaneously. The catastrophe comes from the mismatch between one approximated layer and 47 exact layers, not from the Kronecker error itself.

Overall Interpretation

The hypercomplex structure hypothesis receives mixed support:

Evidence for:

  1. At BS=16 and BS=32, Kronecker surgery preserves model quality better than SVD at matched parameter count. This means the weight matrices have genuine block structure that SVD cannot capture.
  2. Toy superposition models show strong BS=2 Kronecker decomposability vs random matrices.
  3. Attention weights consistently show more structure than MLP weights.

Evidence against:

  1. At BS=2 and BS=4 (the quaternion/complex-number regime), SVD dominates Kronecker. The model did not learn complex-number multiplication.
  2. Clifford algebra relations are not satisfied at any block size.
  3. 2x2 A-matrix classification shows no rotation dominance — structure is generic block decomposability, not hypercomplex algebra.
  4. Rotation angles are uniformly distributed, not clustered at roots of unity.
  5. All surgery outcomes are Outcome C (catastrophic degradation), meaning neither method preserves the model at these compression ratios.

What the model did learn: Medium-block (16x16, 32x32) Kronecker structure that outperforms rank-based approximation. This is a weaker but real structural claim: GPT-2's weights organize into block patterns that are more Kronecker-like than low-rank. The structure is concentrated in attention rather than MLP, and in late layers rather than early layers.

What the model did not learn: Quaternion multiplication, Clifford algebra, complex rotations, or any specific hypercomplex algebraic structure.

The deeper finding: Kronecker's PPL advantage is not about per-layer reconstruction quality — SVD is better on every per-layer metric. The advantage comes from error type consistency: Kronecker approximation preserves full rank and creates block-structured errors that downstream Kronecker-approximated layers can partially absorb. Mixing approximation methods within the attention→MLP pipeline is catastrophic, worse than either method applied uniformly. This implies that weight matrix structure in transformers is best understood not layer-by-layer but as a system-level property where cross-layer error coherence dominates.

File Structure

src/
  kronecker.py            Kronecker decomposition (Van Loan & Pitsianis)
  toy_model.py            Experiment 1: toy superposition model
  analyze_pretrained.py   Experiment 2: GPT-2 weight probing
  kronecker_surgery.py    Experiment 3: weight replacement + perplexity eval
  rotation_analysis.py    Experiment 4: rotation plane analysis
  classify_2x2.py         2x2 A-matrix aI+bJ+cK+dL classification
  activation_error.py     Mechanism: activation-weighted error + single-layer surgery
  spectral_mechanism.py   Mechanism: conditioning, nullspace noise, per-layer correlation

results/exp{1,2,3,4}/     Numerical results (.npz) and summaries (.txt)
figures/                   All plots

Reproduction

Requirements: torch (ROCm/CUDA), numpy, matplotlib, scipy, einops, transformers, datasets

python src/toy_model.py           # Exp 1 (CPU, ~2 min)
python src/rotation_analysis.py   # Exp 4 (CPU, ~5 min)
python src/analyze_pretrained.py  # Exp 2 (CPU, ~15 min)
python src/classify_2x2.py        # 2x2 analysis (CPU, ~2 min)
python src/kronecker_surgery.py   # Exp 3 (GPU recommended, ~30 min)
python src/activation_error.py    # Mechanism analysis (GPU recommended, ~20 min)
python src/spectral_mechanism.py  # Spectral analysis (GPU recommended, ~2 hr)

The Elevator Pitch

We asked: do neural networks secretly learn algebraic structure in their weights? Specifically, do transformer weight matrices decompose into hypercomplex algebras (quaternions, Clifford algebras) — structured number systems that generalize complex numbers?

The short answer: no, but we found something more interesting.


What we tested

We took GPT-2 (a 124M-parameter language model) and tried replacing its weight matrices with structured approximations at matched parameter counts:

  • Kronecker approximation: Decomposes W ≈ A⊗B (block-structured, preserves all dimensions but imposes grid pattern)
  • SVD approximation: Decomposes W ≈ UΣV^T (low-rank, keeps the most important directions but throws away the rest)

Same parameter budget, different inductive bias. Then we measured which broken model is less broken.

The headline result

At block size 16, Kronecker wins: 4,487 PPL vs 6,166 PPL (both catastrophic vs the original 29.88, but the relative ordering matters). This means GPT-2's weights have more block structure than rank structure at that scale.

But here's the thing — SVD gives better reconstruction error by every metric we tested. Per-layer Frobenius error, activation-weighted error, you name it. SVD is closer to the original weights. Yet the model works better with the worse approximation.

The paradox, and what it reveals

We spent most of the investigation chasing this paradox through four hypotheses. The answer turned out to be about error consistency across layers, not per-layer quality:

  1. Kronecker preserves full rank (all 768 dimensions survive, condition number ~52). SVD at this compression ratio keeps only ~37 of 768 singular values — the matrix becomes effectively rank-37, condition number ~10^16.

  2. Consistent error type matters more than error magnitude. We tested a hybrid: Kronecker on attention layers (where it wins per-layer) + SVD on MLP layers (where it wins per-layer). This "best of both worlds" approach should dominate — instead it was the worst configuration, nearly 3× worse than all-Kronecker. Using the same approximation everywhere beats using the best approximation per-layer.

  3. Per-layer effects are non-additive by five orders of magnitude. Summing the individual layer advantages predicts SVD should win by ~146,000 PPL. In reality Kronecker wins by ~1,700. The transformer's cross-layer interactions completely overwhelm per-layer reconstruction quality.

The takeaway

Transformers are systems, not stacks of independent layers. When you perturb weights, what matters isn't how close each layer is to its original — it's whether the perturbations are mutually coherent so downstream layers can partially absorb upstream errors. A full-rank approximation that's farther from the original but preserves the information flow beats a closer approximation that creates dimensional bottlenecks.

As for the original question: GPT-2 didn't learn quaternions or Clifford algebras. No specific algebraic structure emerged. But it did learn weights with medium-block (16×16) Kronecker-like organization, concentrated in attention layers — and the reason that matters has less to do with the structure itself and more to do with how errors propagate through 12 layers of residual connections.

Both tasks are complete. Here's a summary of the results and what they mean:


Results Summary

Hybrid Surgery — The Decisive Test

The hybrid surgery results spectacularly contradict the "attention-specific block structure" hypothesis:

Configuration PPL
All Kronecker 4,487 (best)
Rev hybrid (SVD-attn + Kron-MLP) 5,125
All SVD 6,166
Hybrid (Kron-attn + SVD-MLP) 11,725 (worst!)

If the effect were simply "Kronecker respects attention head boundaries, SVD doesn't," then Kron-on-attention + SVD-on-MLP should have been the best combination. Instead it's the worst — nearly 3× worse than all-Kronecker.

The Real Mechanism: Error Type Consistency

The per-layer data makes this stark:

  • Per-layer, SVD wins on MLP by 5 orders of magnitude (e.g., −141,886 gap on h.0.mlp.c_fc)
  • Per-layer, Kronecker wins on attention by ~10 PPL
  • If these were additive, all-SVD should win by ~146K. But all-Kronecker actually wins (4,487 vs 6,166)

The effects are massively non-additive. The mechanism is cross-layer error coherence:

  1. Kronecker preserves full rank (condition ~52) while SVD creates rank-37 matrices (condition ~10^16). Full-rank errors can flow through downstream layers; rank-deficient projections permanently destroy information in 731/768 dimensions.

  2. Mixing error types within the attention→MLP pipeline is catastrophic. Kronecker block-structured errors and SVD rank-truncation errors are incompatible — neither downstream component can compensate for the other's error structure.

  3. Consistent Kronecker errors are self-compensating. When all layers use the same approximation, downstream layers process inputs with the same error structure they were approximated to handle.

  4. Single-layer catastrophes vanish in full surgery. The −141K damage from Kronecker on one MLP layer comes from the mismatch between one approximated layer and 47 exact layers, not from the Kronecker error itself.

Updated README

The README now includes the full mechanism investigation section with all four hypotheses (activation-subspace: rejected, cross-layer cancellation: subtle, spectral conditioning: partial, error consistency: confirmed), the hybrid surgery table, and the deeper finding about system-level weight structure.

The spectral mechanism per-layer correlation test is still running in the background (task babaf79) but the partial data already confirms the attention-vs-MLP split clearly.

The hybrid surgery task has completed (we already captured its results). The spectral mechanism per-layer test (babaf79) is still grinding through layers in the background. Let me check if it's made more progress:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment