Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ·
220f2ae
1
Parent(s): 17a7cd2
swap README for HF
Browse files
README.md
CHANGED
|
@@ -1,206 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
- Python 3.8+
|
| 11 |
-
- No GPU required (analysis works on CPU - no training or inference)
|
| 12 |
-
|
| 13 |
-
**Setup:**
|
| 14 |
-
```bash
|
| 15 |
-
git clone https://github.com/angerami/transformer-analysis.git
|
| 16 |
-
cd transformer-analysis
|
| 17 |
-
pip install -e .
|
| 18 |
-
```
|
| 19 |
-
|
| 20 |
-
**Authentication (for HuggingFace datasets):**
|
| 21 |
-
```bash
|
| 22 |
-
export HF_TOKEN="your_token_here"
|
| 23 |
-
```
|
| 24 |
-
## Quick Start
|
| 25 |
-
|
| 26 |
-
**Analyze a single model's weights:**
|
| 27 |
-
```python
|
| 28 |
-
from transformer_analysis.weight_analysis import process_model
|
| 29 |
-
|
| 30 |
-
# Single model
|
| 31 |
-
process_model(
|
| 32 |
-
model_name="gpt2",
|
| 33 |
-
out_dir="results/",
|
| 34 |
-
cache_dir="downloads/",
|
| 35 |
-
cleanup_downloads=False,
|
| 36 |
-
)
|
| 37 |
-
```
|
| 38 |
-
|
| 39 |
-
**View your results:**
|
| 40 |
-
```bash
|
| 41 |
-
streamlit run dashboards/streamlit_app.py
|
| 42 |
-
# Note: Edit the script to point at your output directory
|
| 43 |
-
```
|
| 44 |
-
|
| 45 |
-
**Explore published datasets:**
|
| 46 |
-
- **Dashboards:** [HuggingFace Spaces](https://huggingface.co/spaces/angerami/transformer-weights)
|
| 47 |
-
- **Cross-model comparison:** `angerami/weight_study_ana-003`
|
| 48 |
-
- **Checkpoint evolution:** `angerami/pythia-{size}-deduped_weight_evolution_001`
|
| 49 |
-
- Available sizes: 70m, 160m, 410m, 1b, 1.4b, 2.8b, 6.9b, 12b
|
| 50 |
-
|
| 51 |
-
## Usage
|
| 52 |
-
|
| 53 |
-
**Run analysis:**
|
| 54 |
-
```python
|
| 55 |
-
from transformer_analysis.weight_analysis import process_model
|
| 56 |
-
|
| 57 |
-
process_model(
|
| 58 |
-
model_name="pythia-70m-deduped",
|
| 59 |
-
revision="step3000", # optional, for checkpoints
|
| 60 |
-
out_dir="results/",
|
| 61 |
-
cache_dir="downloads/",
|
| 62 |
-
)
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
-
**Load results:**
|
| 66 |
-
```python
|
| 67 |
-
from datasets import load_from_disk
|
| 68 |
-
|
| 69 |
-
ds = load_from_disk("results/pythia-70m-deduped_step3000/")
|
| 70 |
-
df = ds.to_pandas()
|
| 71 |
-
|
| 72 |
-
# Each row = one attention head
|
| 73 |
-
print(df.info())
|
| 74 |
-
# Columns: weight_type, sum, mean, std, max, min, skew, kurtosis,
|
| 75 |
-
# differential_entropy, entropy, fit_mu, fit_sigma,
|
| 76 |
-
# kl_vs_empirical_normal, head, layer, model, job_id
|
| 77 |
-
# P_w (histogram bins), SVD (singular values), P_sv (SV distribution)
|
| 78 |
-
#
|
| 79 |
-
# Checkpoint datasets add: revision, step
|
| 80 |
-
```
|
| 81 |
-
|
| 82 |
-
**Visualize:**
|
| 83 |
-
```bash
|
| 84 |
-
streamlit run dashboards/streamlit_app.py
|
| 85 |
-
# Edit DATASET_PATH to point at your results/
|
| 86 |
-
```
|
| 87 |
-
## Data
|
| 88 |
-
|
| 89 |
-
**Published datasets (HuggingFace Hub):**
|
| 90 |
-
|
| 91 |
-
**Cross-model comparison:**
|
| 92 |
-
- `angerami/weight_study_ana-003` - Static weight analysis across GPT-2, Pythia, LLaMA, Mistral
|
| 93 |
-
|
| 94 |
-
**Checkpoint evolution (Pythia suite):**
|
| 95 |
-
- `angerami/pythia-70m-deduped_weight_evolution_001`
|
| 96 |
-
- `angerami/pythia-160m-deduped_weight_evolution_001`
|
| 97 |
-
- `angerami/pythia-410m-deduped_weight_evolution_001`
|
| 98 |
-
- `angerami/pythia-1b-deduped_weight_evolution_001`
|
| 99 |
-
- `angerami/pythia-1.4b-deduped_weight_evolution_001`
|
| 100 |
-
- `angerami/pythia-2.8b-deduped_weight_evolution_001`
|
| 101 |
-
- `angerami/pythia-6.9b-deduped_weight_evolution_001`
|
| 102 |
-
- `angerami/pythia-12b-deduped_weight_evolution_001`
|
| 103 |
-
|
| 104 |
-
Each contains 154 checkpoints spanning full training (steps 0-143000).
|
| 105 |
-
|
| 106 |
-
**Access:**
|
| 107 |
-
```python
|
| 108 |
-
from datasets import load_dataset
|
| 109 |
-
|
| 110 |
-
ds = load_dataset("angerami/weight_study_ana-003")
|
| 111 |
-
df = ds['train'].to_pandas()
|
| 112 |
-
```
|
| 113 |
-
|
| 114 |
-
## Project Structure
|
| 115 |
-
```
|
| 116 |
-
transformer-analysis/
|
| 117 |
-
├── transformer_analysis/
|
| 118 |
-
│ ├── weight_analysis.py # Main analysis entry point
|
| 119 |
-
│ ├── model_registry.py # Supported model configurations
|
| 120 |
-
│ ├── model_loader.py # Model-agnostic weight extraction
|
| 121 |
-
│ ├── weight_stats.py # Statistical computations
|
| 122 |
-
│ └── utils/ # Logging, performance monitoring
|
| 123 |
-
├── dashboards/
|
| 124 |
-
│ └── streamlit_app.py # Interactive visualization
|
| 125 |
-
├── results/ # Local output directory
|
| 126 |
-
└── requirements.txt
|
| 127 |
-
```
|
| 128 |
-
|
| 129 |
-
**Key modules:**
|
| 130 |
-
- `model_registry.py`: ModelConfig definitions for Pythia, GPT-2, LLaMA, Mistral
|
| 131 |
-
- `model_loader.py`: Extracts W_Q, W_K, W_V matrices; handles authentication
|
| 132 |
-
- `weight_stats.py`: Computes distributions, entropy, KL divergence, SVD
|
| 133 |
-
## Models Supported
|
| 134 |
-
|
| 135 |
-
**Currently implemented:**
|
| 136 |
-
- **Pythia suite** (70M - 12B): Full checkpoint support (154 steps)
|
| 137 |
-
- **GPT-2** (124M, 355M, 774M, 1.5B)
|
| 138 |
-
- **LLaMA 3/3.1** (8B, 70B) - requires authentication
|
| 139 |
-
- **Mistral** (7B v0.1, v0.3)
|
| 140 |
-
|
| 141 |
-
**Adding new models:**
|
| 142 |
-
|
| 143 |
-
Edit `transformer_analysis/model_registry.py`:
|
| 144 |
-
```python
|
| 145 |
-
ModelConfig(
|
| 146 |
-
name="your-model",
|
| 147 |
-
hf_name="org/model-name",
|
| 148 |
-
num_layers=32,
|
| 149 |
-
num_heads=32,
|
| 150 |
-
d_model=4096,
|
| 151 |
-
weight_names=["q_proj", "k_proj", "v_proj"], # model-specific
|
| 152 |
-
requires_auth=False,
|
| 153 |
-
)
|
| 154 |
-
```
|
| 155 |
-
|
| 156 |
-
Architecture must use standard attention with separable Q, K, V projection matrices.
|
| 157 |
-
|
| 158 |
-
## Analysis Types
|
| 159 |
-
|
| 160 |
-
**Weight distributions:**
|
| 161 |
-
- Histograms (configurable bins)
|
| 162 |
-
- Statistical moments (mean, std, skewness, kurtosis)
|
| 163 |
-
- Deviation from normality (KL divergence, fitted μ/σ)
|
| 164 |
-
- Differential entropy
|
| 165 |
-
|
| 166 |
-
**Singular value decomposition:**
|
| 167 |
-
- Singular value spectra
|
| 168 |
-
- Effective rank analysis
|
| 169 |
-
- Distribution of singular values
|
| 170 |
-
|
| 171 |
-
**Granularity:**
|
| 172 |
-
- Per attention head (W_Q, W_K, W_V)
|
| 173 |
-
- Combined matrices (W_QK = W_Q @ W_K^T)
|
| 174 |
-
- Layer-wise comparisons
|
| 175 |
-
- Cross-model comparisons
|
| 176 |
-
|
| 177 |
-
**Checkpoint evolution (Pythia):**
|
| 178 |
-
- Track all metrics across 154 training checkpoints
|
| 179 |
-
- Correlate with training steps (optionally with loss via W&B integration)
|
| 180 |
-
## Citation
|
| 181 |
-
|
| 182 |
-
If you use this toolkit in your research, please cite:
|
| 183 |
-
```bibtex
|
| 184 |
-
@software{transformer_weight_analysis,
|
| 185 |
-
author = {Angerami, Aaron},
|
| 186 |
-
title = {Transformer Weight Analysis: Understanding Weight Distributions and Training Dynamics},
|
| 187 |
-
year = {2025},
|
| 188 |
-
url = {https://github.com/angerami/transformer-analysis}
|
| 189 |
-
}
|
| 190 |
-
```
|
| 191 |
-
**Related work:**
|
| 192 |
-
- Interactive dashboards: https://huggingface.co/spaces/angerami/transformer-weights
|
| 193 |
-
|
| 194 |
-
## License
|
| 195 |
-
|
| 196 |
-
MIT License - see [LICENSE](LICENSE) for details
|
| 197 |
-
|
| 198 |
-
## Acknowledgments
|
| 199 |
-
|
| 200 |
-
**Model datasets:**
|
| 201 |
-
- **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)
|
| 202 |
-
- **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)
|
| 203 |
-
- **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)
|
| 204 |
-
- **Mistral:** Jiang et al. (2023) "Mistral 7B" [[paper]](https://arxiv.org/abs/2310.06825) [[models]](https://huggingface.co/mistralai)
|
| 205 |
-
|
| 206 |
-
**Inspired by:** Sebastian Raschka's "Build a Large Language Model (From Scratch)"
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Transformer Weights
|
| 3 |
+
emoji: 🌊
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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