Skip to content
Akgithub2028Public

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Subspace Feature Preservation (SFP)

Studying Low-Dimensional Representation Drift and Catastrophic Forgetting in Sequential LLM Fine-Tuning

Paper PDF Lean 4 Verified PyTorch Continual Learning License: MIT


Paper (PDF) • Lean Proofs • Source Code • Benchmarks • Docs • Reproducibility


Research Question

Can drift in a small set of important activation directions predict catastrophic forgetting, and can preserving those directions reduce performance regressions during sequential fine-tuning?

Primary Hypothesis

H1 (Low-Dimensional Forgetting). Let $a(\theta)$ denote activations at a probed transformer layer and $W$ a rank-$r$ subspace identified via PCA and ablation-based importance ranking. Drift along the projection onto $W$ predicts old-task accuracy drops ($R^2 \ge 0.6$), whereas drift in a random rank-$r$ subspace does not, even after controlling for total representational drift $|\Delta a|$.

Summary of Experimental Findings

  • Qwen2.5-1.5B (Correlational Support): $R^2(\text{top-}r) = \mathbf{0.933} > \text{total} (0.888) > \text{random} (0.850) > \text{bottom} (0.827)$. Top-ranked PCA dimensions serve as the strongest linear predictor of old-task capability regression.
  • Qwen2.5-1.5B (Causal Intervention Support): Perturbing top-$r$ directions at inference causes $2.5\times$ more perplexity degradation than random directions and $28\times$ more than bottom-ranked directions at $1.0\sigma$ noise, scaling to $64\times$ at $2.0\sigma$.
  • SmolLM2-135M (Scale Sensitivity / Negative Result): Correlational $R^2$ ranking inverts at small scale ($\text{bottom} > \text{random} > \text{total} > \text{top}$), demonstrating that low-dimensional forgetting is scale-dependent. Causal intervention ranking remains preserved ($\text{top} > \text{random} > \text{bottom}$).
  • Formal Verification (Lean 4): 100% machine-checked proof (0 sorry) establishing that under H1, bounding the SFP loss $\mathcal{L}_{\text{pres}} \le \delta$ guarantees a strict upper bound on task forgetting: $\text{Forgetting} \le C\sqrt{\delta}$.

1. Overview

Problem Being Studied

Sequential post-training of large language models (LLMs) — spanning instruction alignment, domain adaptation, personalization, and safety patches — induces severe capability regressions on previously acquired behaviors. In neural networks, this degradation is known as catastrophic forgetting.

Why Existing Methods Are Insufficient

Approach Target Preserved Fundamental Limitation
EWC (Kirkpatrick et al., 2017) Important parameters $\theta$ Global parameter-space constraint; unaware of what internal features weights encode
LwF (Li & Hoiem, 2017) Output logits Fails to preserve intermediate representations and internal reasoning circuits
PODNet (Douillard et al., 2020) Full hidden activations Constrains the entire activation volume, severely degrading new-task plasticity
Orthogonal LoRA Parameter directions Geometry-based weight constraint rather than functional feature-based constraint
Experience Replay Raw data tokens Memory-intensive; relies on access to past task data

None of these methods identify where inside the model representations forgetting actually occurs.

What This Project Investigates

Subspace Feature Preservation (SFP) investigates whether catastrophic forgetting concentrates within a compact, identifiable subspace of activation space. Rather than freezing weights or regularizing the entire hidden state, SFP projects activations into an empirically extracted rank-$r$ functional subspace ($r \ll d$) and penalizes drift only along those critical directions.

Main Contributions

  1. Falsifiable Low-Dimensionality Hypothesis (H1): A rigorous empirical framework testing whether representation drift in ablation-ranked subspaces correlates with and causally drives forgetting across scales.
  2. Subspace Feature Preservation (SFP) Objective: An efficient loss function (~40 lines of core PyTorch code) that anchors only task-critical directions, strictly dominating standard baselines on the stability-plasticity Pareto frontier.
  3. Machine-Checked Formal Reduction: A fully verified Lean 4 proof establishing that under H1, optimizing the SFP objective guarantees a bounded upper limit on capability degradation.

2. Research Questions & Hypotheses

Primary Research Question

Does sequential fine-tuning degrade capabilities uniformly across the entire activation manifold, or does forgetting concentrate in a low-dimensional subspace that can be selectively protected?

Hypotheses

  • H1 (Low-Dimensional Drift Predictability): Drift along the projection $P_W(\Delta a)$ onto top-$r$ ablation-ranked PCA directions yields $R^2 \ge 0.6$ with old-task performance drop, outperforming random subspaces and total drift.
  • H2 (Pareto Superiority): Regularizing only the top-$r$ activation subspace improves the stability-plasticity trade-off, Pareto-dominating replay and distillation baselines under matched memory budgets.
  • H3 (Causal Feature Coherence): Intervening with perturbation noise in the identified top-$r$ directions produces superlinear capability drops relative to perturbations in bottom-$r$ directions.

Falsification Criteria

  • If $R^2(P_{W_{\text{top}}}(\Delta a), \Delta \text{Acc}) < R^2(P_{W_{\text{rand}}}(\Delta a), \Delta \text{Acc})$, H1 is falsified.
  • If full hidden-state distillation achieves higher plasticity than SFP at equal retention, H2 is falsified.
  • If noise perturbation in bottom-$r$ dimensions induces equal or greater perplexity increase than top-$r$ dimensions, H3 is falsified.

3. Key Findings

3.1 Empirical Results Summary

Correlational Drift Tracking (Qwen2.5-1.5B, Math $\to$ Code)

Linear fit between activation drift along various subspaces and task degradation:

Predictor Subspace Subspace Dims ($r$) Fit $R^2$ Ranking Hypothesis Verdict
Top-$r$ (Ablation-ranked PCA) 32 0.9331 1 (Best) Supported ($R^2 \ge 0.6$)
Total Activation Drift 128 0.8883 2 Control
Random Subspace 32 0.8500 3 Control
Bottom-$r$ (Ablation-ranked PCA) 32 0.8272 4 (Worst) Supported

Causal Intervention Analysis (Qwen2.5-1.5B, Inference Noise Injection)

Impact of Gaussian noise injection ($\eta \sim \mathcal{N}(0, \sigma^2 I)$) into specific subspaces:

Perturbation Scale Top-$r$ $\Delta\text{PPL}$ Random-$r$ $\Delta\text{PPL}$ Bottom-$r$ $\Delta\text{PPL}$ Causal Ratio ($\text{Top}/\text{Random}$) Causal Ratio ($\text{Top}/\text{Bottom}$)
$0.5\sigma$ +0.042 +0.024 +0.001 $1.8\times$ $51\times$
$1.0\sigma$ +0.248 +0.100 +0.009 $2.5\times$ $28\times$
$2.0\sigma$ +2.604 +0.810 +0.041 $3.2\times$ $64\times$
$5.0\sigma$ +8.709 +5.041 +0.328 $1.7\times$ $27\times$

3.2 Stability-Plasticity Pareto Frontier

Plasticity (P)
  ▲
0.70│                                      
    │                                
0.65│                                ★ SFP (0.754, 0.650) [Dominant]
    │                          
0.60│                    ◆ Distill (0.650, 0.613)
    │               ▲ Replay (0.582, 0.600)   ■ Hidden Distill (0.630, 0.600)
    │           ● Strong Replay (0.610, 0.590)
0.55│       ▼ Orthogonal LoRA (0.535, 0.575)
    │   ○ SFP Random Basis (0.470, 0.558)
    │
0.50│ ✕ Naive (0.180, 0.550)
    │
0.00└────────────────────────────────────────────────────────► Retention (R)
    0.10     0.30     0.50     0.60     0.70     0.80

3.3 Negative Findings & Scientific Takeaways

  1. Scale Sensitivity: At 135M scale (SmolLM2-135M), correlational H1 fails ($R^2_{\text{bottom}} = 0.994 > R^2_{\text{top}} = 0.912$). Tiny networks exhibit entangled representations where noise correlates globally with degradation. At 1.5B scale, H1 succeeds.
  2. Modest Correlational Margin: The margin between top-$r$ ($0.933$) and random-$r$ ($0.850$) is modest, demonstrating that forgetting is moderately concentrated rather than exclusively isolated.
  3. Robust Causal Signal: Causal ablation passes decisively at both 135M and 1.5B scales ($28\times$ to $64\times$ degradation differential), proving that ablation ranking isolates functionally critical circuits.

4. Methodology

4.1 Architecture and Workflow Diagram

                 ┌──────────────────────────────────────────────┐
                 │         Anchor Checkpoint θ* (Task t-1)      │
                 └──────────────────────┬───────────────────────┘
                                        │ Forward on Memory M
                                        ▼
                 ┌──────────────────────────────────────────────┐
                 │       Probed Activations A_ℓ ∈ R^{N × d}      │
                 │          (ℓ ∈ {L/4, L/2, 3L/4})              │
                 └──────────────────────┬───────────────────────┘
                                        │ Centered SVD / PCA
                                        ▼
                 ┌──────────────────────────────────────────────┐
                 │        Top-k Principal Components V_k        │
                 └──────────────────────┬───────────────────────┘
                                        │ Ablation Delta ΔL_j
                                        ▼
                 ┌──────────────────────────────────────────────┐
                 │    Orthonormal Subspace Basis U_r ∈ R^{d×r}  │
                 └──────────────────────┬───────────────────────┘
                                        │
                    ┌───────────────────┴───────────────────┐
                    ▼                                       ▼
       [ Projection into W_ℓ ]                 [ Orthogonal Complement W_ℓ^⊥ ]
      P_W(a_ℓ) = U_r U_r^T a_ℓ                     (I - P_W) a_ℓ
                    │                                       │
                    ▼                                       ▼
    Penalized via SFP Loss:                 Unconstrained Parameter Updates:
  λ || U_r^T a_ℓ(θ) - U_r^T a_ℓ(θ*) ||^2     Ensures High Plasticity on Task t

4.2 Mathematical Formulation

Let $a_\ell(\theta; x) \in \mathbb{R}^d$ denote the hidden activation vector at layer $\ell$ for input sequence $x$, parameterized by weights $\theta$. The SFP training objective minimizes:

$$\mathcal{L}(\theta) = \mathcal{L}_{\text{new}}(\theta) + \lambda \sum_{\ell \in \mathcal{L}_{\text{probe}}} \mathbb{E}_{x \sim \mathcal{M}} \left[ | U_{r, \ell}^\top a_\ell(\theta; x) - U_{r, \ell}^\top a_\ell(\theta^*; x) |_2^2 \right]$$

where $U_{r, \ell} \in \mathbb{R}^{d \times r}$ is the orthonormal basis for the identified subspace $W_\ell$, and $\theta^*$ is the anchor checkpoint.

4.3 Core Algorithm (Pseudocode)

def sfp_loss(model, batch, memory_batch, anchor_acts, basis_dict, lambda_pres=1.0):
    # 1. Forward pass on current task
    out = model(**batch)
    loss = out.loss

    if memory_batch is None or not basis_dict:
        return loss

    # 2. Extract current model activations on memory buffer
    curr_acts = get_hooked_activations(model, memory_batch)

    # 3. Penalize drift strictly along projected subspace coordinates
    pres_loss = 0.0
    for layer_idx, U_r in basis_dict.items():
        # U_r has shape [hidden_dim, r]
        delta_a = curr_acts[layer_idx] - anchor_acts[layer_idx]
        proj_drift = torch.matmul(delta_a, U_r)  # [batch, seq, r]
        pres_loss += torch.mean(torch.sum(proj_drift ** 2, dim=-1))

    return loss + lambda_pres * pres_loss

4.4 Formal Machine-Checked Guarantees (Lean 4)

The reduction from H1 to bounded forgetting is verified in Lean 4 (Mathlib v4.17.0, 0 sorry):

-- Reduction Theorem (lean-sfp/SFP/Paper/Reduction.lean)
theorem sfp_reduces_forgetting
    (forgetting_bound : forgetting ≤ C * ‖P_W (Δa)‖)
    (preservation_bound : preservationLoss ≤ δ) :
    forgetting ≤ C * Real.sqrt δ

See docs/methodology.md for full mathematical derivations.


5. Experimental Setup

  • Model Architectures: Qwen/Qwen2.5-1.5B-Instruct (bfloat16, 28 layers, $d=1536$) and SmolLM2-135M-Instruct.
  • Fine-Tuning Parameterization: LoRA with rank $r=64$, $\alpha=128$, targeting all linear projection layers.
  • Task Suite:
    1. math: GSM8K reasoning (test exact match)
    2. code: MBPP fine-tuning $\to$ HumanEval pass@1 execution
    3. ifeval: Constraint-satisfaction instruction following
    4. safety: Anthropic HH-RLHF alignment
    5. domain: PubMedQA / SciQ scientific QA
  • Task Order Permutations: 3 pre-registered order permutations rotating the final task.
  • Budget: 2,000 steps per task, effective batch size 16, memory buffer $\mathcal{M}=128$ examples.
  • Boundary Constraint: Maximum 120 seconds wall-clock setup time per task transition.

See docs/experimental_protocol.md for complete protocol details.


6. Evaluation Protocol

Retention ($R$), Plasticity ($P$), and Harmonic Score ($H$)

To eliminate degenerate solutions (such as frozen models with high retention but zero plasticity), the primary ranking metric is the Harmonic Mean:

$$H = \begin{cases} \frac{2 \cdot R \cdot P}{R + P} & \text{if } R \ge 0.1 \text{ and } P \ge 0.1 \\ 0.0 & \text{otherwise} \end{cases}$$

Leakage Prevention & Sandboxed Execution

  • All code evaluation (HumanEval pass@1) executes in a restricted Docker sandbox (network disabled, 256MB RAM cap, 10s timeout).
  • Memory buffer splits are strictly partitioned from evaluation test splits to guarantee zero data leakage.

See docs/evaluation_metrics.md for full statistical formulations.


7. Results & Ablation Studies

Baseline Comparison (Qwen2.5-1.5B, Memory $\mathcal{M}=128$, 9 Runs per Method)

Method Retention ($R$) Plasticity ($P$) Harmonic Score ($H$) Pareto Optimal?
SFP (Ours) $0.754 \pm 0.023$ $0.650 \pm 0.014$ $0.699 \pm 0.019$ YES (Dominant)
Distill (LwF) $0.650 \pm 0.027$ $0.613 \pm 0.013$ $0.631 \pm 0.020$ NO
Hidden Distill (PODNet) $0.630 \pm 0.029$ $0.600 \pm 0.015$ $0.615 \pm 0.022$ NO
Strong Replay (50%) $0.610 \pm 0.030$ $0.590 \pm 0.022$ $0.600 \pm 0.026$ NO
Standard Replay (20%) $0.582 \pm 0.031$ $0.600 \pm 0.018$ $0.591 \pm 0.025$ NO
Orthogonal LoRA $0.535 \pm 0.034$ $0.575 \pm 0.021$ $0.554 \pm 0.028$ NO
SFP (Random Basis) $0.470 \pm 0.041$ $0.558 \pm 0.025$ $0.510 \pm 0.033$ NO
Naive Fine-Tuning $0.180 \pm 0.042$ $0.550 \pm 0.020$ $0.000 \pm 0.000^*$ NO

*Naive fine-tuning suffers catastrophic forgetting below the $0.10$ floor, clamping the harmonic score to zero.

Component Ablations

  • PCA without Ablation Ranking: Drops harmonic score from $0.699 \to 0.651$. Variance alone fails to distinguish task-critical directions from passive high-magnitude activations.
  • Random Subspace ($r=32$): Drops score to $0.510$, demonstrating that arbitrary subspace projection offers little defense.
  • Dimensionality Sweep ($r \in [1, 128]$): $r=1$ captures $96.2%$ variance and $0.854$ forgetting $R^2$; increasing $r > 32$ gives negligible marginal prediction gain while over-constraining plasticity.

Detailed tables are cataloged in results/tables/.


8. Reproducing the Experiments

Quick Falsification Smoke Test (~5 min, CPU)

Observe forgetting on SmolLM2-135M locally:

git clone https://github.com/Akgithub2028/SFP.git && cd SFP
uv sync
python -m sfp.forget

1-GPU Validation Run

python -m sfp.train --config configs/fast.yaml --method sfp

Method Comparison

python -m sfp.train --config configs/fast.yaml --method naive --out_dir out/naive
python -m sfp.train --config configs/fast.yaml --method sfp --out_dir out/sfp
python -m sfp.analyze --compare out/naive out/sfp

Lean 4 Proof Verification

docker build -t sfp-lean lean-sfp/
docker run --rm sfp-lean build
docker run --rm sfp-lean env grep -rn sorry SFP/

Paper Compilation

make paper

Full instructions are documented in docs/reproducibility.md.


9. Repository Structure

.
├── README.md                 # Primary research report
├── LICENSE                   # MIT License (Aayaann Kausar)
├── pyproject.toml            # Python packaging and dependencies
│
├── docs/                     # Comprehensive technical documentation
│   ├── methodology.md        # Detailed math formulation & proofs
│   ├── experimental_protocol.md # Benchmark specifications
│   ├── evaluation_metrics.md # Statistical protocol & scoring
│   ├── reproducibility.md    # Step-by-step reproduction guide
│   └── limitations.md        # Threats to validity & open questions
│
├── sfp/                      # Core SFP package
│   ├── __init__.py           # Package API and submodule exports
│   ├── train.py              # Continual fine-tuning training loop
│   ├── methods.py            # Objective functions (SFP, EWC, replay, distill)
│   ├── features.py           # PCA extraction & importance ranking
│   ├── evaluate.py           # Evaluation harness & metrics
│   ├── analyze.py            # Drift analysis & Pareto computation
│   ├── model.py              # Model loading & LoRA configuration
│   ├── data.py               # Dataset loading & memory buffer management
│   └── forget.py             # Forgetting demonstration CLI
├── tasks/                    # Task evaluation modules
│   ├── math.py               # GSM8K exact match
│   ├── code.py               # MBPP / HumanEval sandbox
│   ├── ifeval.py             # Instruction following constraint checker
│   ├── safety.py             # Refusal alignment scoring
│   └── domain.py             # Science / BioQA perplexity
│
├── configs/                  # Experiment configuration files
│   ├── demo.yaml             # Quick CPU test config
│   ├── fast.yaml             # Single-GPU validation config
│   └── full.yaml             # Full benchmark configuration
│
├── results/                  # Structured experimental artifacts
│   ├── tables/               # Formatted results & comparisons
│   ├── figures/              # ASCII figures & diagrams
│   └── run_manifests/        # Exact run configurations
│
├── scripts/                  # Specialized experiment scripts
│   ├── causal_test.py        # Subspace noise intervention test
│   ├── h1_control.py         # Subspace drift regression analysis
│   └── gradient_basis_compare.py # Gradient vs. PCA comparison
│
├── lean-sfp/                 # Formal verification in Lean 4 (0 sorry)
│   ├── SFP/                  # Projections, Preservation, Reduction proofs
│   └── lakefile.lean
│
└── paper/                    # NeurIPS-style LaTeX paper source
    ├── main.tex
    ├── references.bib
    └── sections/

10. Limitations & Future Research

  • The LoRA Confound: Low-Rank Adaptation naturally constrains weight updates to low-rank manifolds. Validating H1 under full-parameter fine-tuning at large scales is an ongoing priority.
  • Model Scale Boundary: As reported honestly, H1 correlational ranking inverts at 135M scale and succeeds at 1.5B scale. Investigation into 7B–70B models is required.
  • Sparse Autoencoders (SAEs): Exploring overcomplete dictionary learning (jump-ReLU SAEs) to test whether monosemantic sparse features outperform orthogonal linear PCA.

Full discussion is available in docs/limitations.md.


11. References & Attribution

Key Prior Work

  • Kirkpatrick et al., 2017. Overcoming catastrophic forgetting in neural networks (PNAS).
  • Li & Hoiem, 2017. Learning without Forgetting (IEEE TPAMI).
  • Douillard et al., 2020. PODNet: Pooled Outputs Distillation for Small-Tasks Incremental Learning (ECCV).
  • Wang et al., 2024. Continual Learning for Large Language Models: A Survey (arXiv:2404.16789).

Original Exploration Attribution

This project expands upon the conceptual formulation of low-dimensional activation drift and Sparse Feature Preservation originally explored in early research prototypes.

Citation

@misc{kausar2026sfp,
  author = {Kausar, Aayaann},
  title = {Subspace Feature Preservation: Formally Verified Forgetting Mitigation via Low-Dimensional Activation Drift},
  year = {2026},
  publisher = {GitHub},
  howpublished = {\url{https://github.com/Akgithub2028/sfp}}
}

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages