omrifahn commited on
Commit
88b5d86
·
verified ·
1 Parent(s): d383319

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +130 -0
README.md ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # K-FAC Memorization Suppression
2
+
3
+ Reproduction of ["From Memorization to Reasoning in the Spectrum of Loss Curvature"](https://github.com/goodfire-ai/memorization_kfac) with extended experiments on modified importance formulas.
4
+
5
+ ## Overview
6
+
7
+ This project implements K-FAC (Kronecker-Factored Approximate Curvature) based weight editing to suppress verbatim memorization in language models while preserving general capabilities.
8
+
9
+ **Key insight:** The Fisher Information Matrix, approximated by K-FAC, reveals directions in weight space associated with memorization (low curvature) vs. generalization (high curvature). By removing low-curvature components, we can suppress memorization.
10
+
11
+ ## Project Goal
12
+
13
+ 1. **Reproduce** the paper's K-FAC method on OLMo-2 1B
14
+ 2. **Compare** the original importance formula with a modified version:
15
+ - **Original:** $\Pi_{ij} = \lambda_i \cdot \mu_j$
16
+ - **Modified:** $\Pi_{ij} = \lambda_i \cdot \mu_j \cdot |C_{ij}|^2$
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ pip install -r requirements.txt
22
+ ```
23
+
24
+ ## Project Structure
25
+
26
+ ```
27
+ ├── src/
28
+ │ ├── kfac_collector.py # Collect K-FAC statistics (A, G matrices)
29
+ │ ├── kfac_editor.py # Weight editing via eigendecomposition
30
+ │ ├── evaluate.py # Memorization and perplexity metrics
31
+ │ └── mine_memorized.py # Mine memorized sequences from training data
32
+ ├── notebooks/
33
+ │ ├── 01_collect_kfac.ipynb # Colab: K-FAC collection (~2h on A100)
34
+ │ ├── 02_mine_memorized.ipynb # Colab: Find memorized sequences (~1h)
35
+ │ └── 03_experiments.ipynb # Colab: Run experiments (~2h)
36
+ ├── plans/
37
+ │ └── implementation_plan.md # Detailed implementation plan
38
+ ├── context/
39
+ │ ├── original_paper/ # Paper sections in markdown
40
+ │ └── REPRODUCTION_PLAN.md # Initial reproduction plan
41
+ └── requirements.txt
42
+ ```
43
+
44
+ ## Quick Start
45
+
46
+ ### Local Development
47
+
48
+ ```python
49
+ from src.kfac_collector import KFACCollector, KFACConfig
50
+ from src.kfac_editor import KFACEditor, EditConfig
51
+ from src.evaluate import memorization_score, perplexity
52
+
53
+ # Load model
54
+ from transformers import AutoModelForCausalLM, AutoTokenizer
55
+ model = AutoModelForCausalLM.from_pretrained("allenai/OLMo-2-1124-7B")
56
+ tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-2-1124-7B")
57
+
58
+ # Load pre-collected K-FAC stats
59
+ collector = KFACCollector.load("kfac_statistics.pt", model)
60
+ kfac_stats = collector.get_statistics()
61
+
62
+ # Apply K-FAC editing
63
+ config = EditConfig(energy_threshold=0.6, formula="original")
64
+ editor = KFACEditor(model, kfac_stats, config)
65
+ editor.edit_model()
66
+
67
+ # Evaluate
68
+ result = memorization_score(model, tokenizer, prefixes, suffixes)
69
+ print(f"Strict accuracy: {result.strict_accuracy*100:.1f}%")
70
+ ```
71
+
72
+ ### Running on Colab
73
+
74
+ 1. **01_collect_kfac.ipynb** - Collect K-FAC statistics (~20M tokens, ~2h on A100)
75
+ 2. **02_mine_memorized.ipynb** - Find memorized sequences from training data
76
+ 3. **03_experiments.ipynb** - Run experiments and compare formulas
77
+
78
+ ## Method
79
+
80
+ ### K-FAC Statistics Collection
81
+
82
+ For each target MLP layer, we collect:
83
+ - **A**: Activation covariance matrix (input side)
84
+ - **G**: Gradient covariance matrix (output side)
85
+
86
+ These approximate the Fisher Information Matrix: $F_W \approx G \otimes A$
87
+
88
+ ### Weight Editing
89
+
90
+ 1. **Eigendecompose** A and G matrices
91
+ 2. **Transform** weights to curvature basis: $C = U_G^T W U_A$
92
+ 3. **Compute importance** using either formula
93
+ 4. **Select** top components by cumulative energy (e.g., 60%)
94
+ 5. **Reconstruct** edited weights: $W_{edited} = U_G (C \odot M) U_A^T$
95
+
96
+ ### Importance Formulas
97
+
98
+ | Formula | Definition | Intuition |
99
+ |---------|------------|-----------|
100
+ | Original | $\Pi_{ij} = \lambda_i \cdot \mu_j$ | Pure curvature |
101
+ | Modified | $\Pi_{ij} = \lambda_i \cdot \mu_j \cdot |C_{ij}|^2$ | Curvature weighted by actual weight magnitude |
102
+
103
+ ## Hyperparameters
104
+
105
+ | Parameter | 7B Model | 1B Model (estimated) |
106
+ |-----------|----------|---------------------|
107
+ | Target layers | 23, 24, 25 | 11, 12, 13 |
108
+ | Projections | gate_proj, up_proj | gate_proj, up_proj |
109
+ | Energy threshold | 60% | 60% |
110
+ | K-FAC tokens | ~20M | ~20M |
111
+
112
+ ## Expected Results
113
+
114
+ Based on the paper (OLMo-2 1B):
115
+
116
+ | Metric | Baseline | After K-FAC |
117
+ |--------|----------|-------------|
118
+ | Dolma strict accuracy | ~98% | ~3% |
119
+ | Perplexity (Pile-10k) | ~23 | ~27 |
120
+
121
+ ## References
122
+
123
+ - Paper: [From Memorization to Reasoning in the Spectrum of Loss Curvature](https://github.com/goodfire-ai/memorization_kfac)
124
+ - Original code: https://github.com/goodfire-ai/memorization_kfac
125
+ - Model: [OLMo-2](https://huggingface.co/allenai/OLMo-2-1124-7B)
126
+ - K-FAC: [Martens & Grosse, 2015](https://arxiv.org/abs/1503.05671)
127
+
128
+ ## License
129
+
130
+ MIT