angerami commited on
Commit
73f3604
Β·
1 Parent(s): 2164f3a

documentation; README still has a lot of boiler plate

Browse files
README.md CHANGED
@@ -1,2 +1,213 @@
1
- # transformer-analysis
2
- Analysis code for analyzing transformers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Transformer Weight Analysis
2
+
3
+ Analysis software developed to inspect weights in transformers-- the $W_{Q}$ and $W_{K}$ matrices and their product, $W_{QK}$-- and analyze the matrix elements as a statistical ensemble.
4
+
5
+ Analyze weight distributions in transformer models - how they differ from normal distributions, vary across attention heads and layers, and evolve during training. Compare statistical properties across model architectures and sizes.
6
+
7
+ Supports systematic checkpoint analysis (Pythia 70M-12B with 154 checkpoints) and cross-model comparison (Pythia, GPT-2, extensible to LLaMA/Mistral).
8
+
9
+ ## Installation
10
+
11
+ **Requirements:**
12
+ - Python 3.8+
13
+ - No GPU required (analysis works on CPU - we inspect weights, not run inference)
14
+
15
+ **Setup:**
16
+ ```bash
17
+ git clone https://github.com/angerami/transformer-analysis.git
18
+ cd transformer-analysis
19
+ pip install -r requirements.txt
20
+ ```
21
+
22
+ **For dashboard only:**
23
+ ```bash
24
+ pip install streamlit plotly pandas
25
+ ```
26
+
27
+ **Authentication (for HuggingFace datasets):**
28
+ ```bash
29
+ export HF_TOKEN="your_token_here"
30
+ ```
31
+ ## Quick Start
32
+
33
+ **Analyze a single model's weights:**
34
+ ```python
35
+ from transformer_analysis.run_weight_analysis import process_model
36
+
37
+ # Single model
38
+ process_model(
39
+ model_name="gpt2",
40
+ out_dir="results/",
41
+ cache_dir="downloads/",
42
+ cleanup_downloads=False,
43
+ )
44
+ ```
45
+
46
+ **View your results:**
47
+ ```bash
48
+ streamlit run dashboards/streamlit_app.py
49
+ # Note: Edit the script to point at your output directory
50
+ ```
51
+
52
+ **Explore published datasets:**
53
+ - **Dashboards:** [HuggingFace Spaces](https://huggingface.co/spaces/angerami/transformer-weights)
54
+ - **Cross-model comparison:** `angerami/weight_study_ana-003`
55
+ - **Checkpoint evolution:** `angerami/pythia-{size}-deduped_weight_evolution_001`
56
+ - Available sizes: 70m, 160m, 410m, 1b, 1.4b, 2.8b, 6.9b, 12b
57
+
58
+ ## Usage
59
+
60
+ **Run analysis:**
61
+ ```python
62
+ from transformer_analysis.run_weight_analysis import process_model
63
+
64
+ process_model(
65
+ model_name="pythia-70m-deduped",
66
+ revision="step3000", # optional, for checkpoints
67
+ out_dir="results/",
68
+ cache_dir="downloads/",
69
+ )
70
+ ```
71
+
72
+ **Load results:**
73
+ ```python
74
+ from datasets import load_from_disk
75
+
76
+ ds = load_from_disk("results/pythia-70m-deduped_step3000/")
77
+ df = ds.to_pandas()
78
+
79
+ # Each row = one attention head
80
+ print(df.info())
81
+ # Columns: weight_type, sum, mean, std, max, min, skew, kurtosis,
82
+ # differential_entropy, entropy, fit_mu, fit_sigma,
83
+ # kl_vs_empirical_normal, head, layer, model, job_id
84
+ # P_w (histogram bins), SVD (singular values), P_sv (SV distribution)
85
+ #
86
+ # Checkpoint datasets add: revision, step
87
+ ```
88
+
89
+ **Visualize:**
90
+ ```bash
91
+ streamlit run dashboards/streamlit_app.py
92
+ # Edit DATASET_PATH to point at your results/
93
+ ```
94
+ ## Data
95
+
96
+ **Published datasets (HuggingFace Hub):**
97
+
98
+ **Cross-model comparison:**
99
+ - `angerami/weight_study_ana-003` - Static weight analysis across GPT-2, Pythia, LLaMA, Mistral
100
+
101
+ **Checkpoint evolution (Pythia suite):**
102
+ - `angerami/pythia-70m-deduped_weight_evolution_001`
103
+ - `angerami/pythia-160m-deduped_weight_evolution_001`
104
+ - `angerami/pythia-410m-deduped_weight_evolution_001`
105
+ - `angerami/pythia-1b-deduped_weight_evolution_001`
106
+ - `angerami/pythia-1.4b-deduped_weight_evolution_001`
107
+ - `angerami/pythia-2.8b-deduped_weight_evolution_001`
108
+ - `angerami/pythia-6.9b-deduped_weight_evolution_001`
109
+ - `angerami/pythia-12b-deduped_weight_evolution_001`
110
+
111
+ Each contains 154 checkpoints spanning full training (steps 0-143000).
112
+
113
+ **Access:**
114
+ ```python
115
+ from datasets import load_dataset
116
+
117
+ ds = load_dataset("angerami/weight_study_ana-003")
118
+ df = ds['train'].to_pandas()
119
+ ```
120
+
121
+ ## Project Structure
122
+ ```
123
+ transformer-analysis/
124
+ β”œβ”€β”€ transformer_analysis/
125
+ β”‚ β”œβ”€β”€ run_weight_analysis.py # Main analysis entry point
126
+ β”‚ β”œβ”€β”€ model_registry.py # Supported model configurations
127
+ β”‚ β”œβ”€β”€ model_loader.py # Model-agnostic weight extraction
128
+ β”‚ β”œβ”€β”€ weight_stats.py # Statistical computations
129
+ β”‚ └── utils/ # Logging, performance monitoring
130
+ β”œβ”€β”€ dashboards/
131
+ β”‚ └── streamlit_app.py # Interactive visualization
132
+ β”œβ”€β”€ results/ # Local output directory
133
+ └── requirements.txt
134
+ ```
135
+
136
+ **Key modules:**
137
+ - `model_registry.py`: ModelConfig definitions for Pythia, GPT-2, LLaMA, Mistral
138
+ - `model_loader.py`: Extracts W_Q, W_K, W_V matrices; handles authentication
139
+ - `weight_stats.py`: Computes distributions, entropy, KL divergence, SVD
140
+ ## Models Supported
141
+
142
+ **Currently implemented:**
143
+ - **Pythia suite** (70M - 12B): Full checkpoint support (154 steps)
144
+ - **GPT-2** (124M, 355M, 774M, 1.5B)
145
+ - **LLaMA 3/3.1** (8B, 70B) - requires authentication
146
+ - **Mistral** (7B v0.1, v0.3)
147
+
148
+ **Adding new models:**
149
+
150
+ Edit `transformer_analysis/model_registry.py`:
151
+ ```python
152
+ ModelConfig(
153
+ name="your-model",
154
+ hf_name="org/model-name",
155
+ num_layers=32,
156
+ num_heads=32,
157
+ d_model=4096,
158
+ weight_names=["q_proj", "k_proj", "v_proj"], # model-specific
159
+ requires_auth=False,
160
+ )
161
+ ```
162
+
163
+ Architecture must use standard attention with separable Q, K, V projection matrices.
164
+
165
+ ## Analysis Types
166
+
167
+ **Weight distributions:**
168
+ - Histograms (configurable bins)
169
+ - Statistical moments (mean, std, skewness, kurtosis)
170
+ - Deviation from normality (KL divergence, fitted ΞΌ/Οƒ)
171
+ - Differential entropy
172
+
173
+ **Singular value decomposition:**
174
+ - Singular value spectra
175
+ - Effective rank analysis
176
+ - Distribution of singular values
177
+
178
+ **Granularity:**
179
+ - Per attention head (W_Q, W_K, W_V)
180
+ - Combined matrices (W_QK = W_Q @ W_K^T)
181
+ - Layer-wise comparisons
182
+ - Cross-model comparisons
183
+
184
+ **Checkpoint evolution (Pythia):**
185
+ - Track all metrics across 154 training checkpoints
186
+ - Correlate with training steps (optionally with loss via W&B integration)
187
+ ## Citation
188
+
189
+ If you use this toolkit in your research, please cite:
190
+ ```bibtex
191
+ @software{transformer_weight_analysis,
192
+ author = {Angerami, Aaron},
193
+ title = {Transformer Weight Analysis: Understanding Weight Distributions and Training Dynamics},
194
+ year = {2025},
195
+ url = {https://github.com/angerami/transformer-analysis}
196
+ }
197
+ ```
198
+ **Related work:**
199
+ - Interactive dashboards: https://huggingface.co/spaces/angerami/transformer-weights
200
+
201
+ ## License
202
+
203
+ [Your license choice - MIT, Apache 2.0, etc.]
204
+
205
+ ## Acknowledgments
206
+
207
+ **Model datasets:**
208
+ - **Pythia:** Biderman et al. (2023) "Pythia: A Suite for Analyzing Large Language Models" [[paper]](https://arxiv.org/abs/2304.01373) [[models]](https://huggingface.co/EleutherAI)
209
+ - **GPT-2:** Radford et al. (2019) "Language Models are Unsupervised Multitask Learners" [[paper]](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) [[models]](https://huggingface.co/openai-community)
210
+ - **LLaMA 3/3.1:** Dubey et al. (2024) "The Llama 3 Herd of Models" [[paper]](https://arxiv.org/abs/2407.21783) [[models]](https://huggingface.co/meta-llama)
211
+ - **Mistral:** Jiang et al. (2023) "Mistral 7B" [[paper]](https://arxiv.org/abs/2310.06825) [[models]](https://huggingface.co/mistralai)
212
+
213
+ **Inspired by:** Sebastian Raschka's "Build a Large Language Model (From Scratch)"
src/transformer_analysis/run_weight_analysis.py CHANGED
@@ -1,5 +1,5 @@
1
  # `run_weight_analysis.py`
2
- # Analysis main for weight analysi
3
  import json
4
  import logging
5
  import uuid
 
1
  # `run_weight_analysis.py`
2
+ # Analysis main for weight analysis
3
  import json
4
  import logging
5
  import uuid