mindchain commited on
Commit
fd46017
Β·
verified Β·
1 Parent(s): a0067a3

Update README with detailed specs

Browse files
Files changed (1) hide show
  1. README.md +149 -0
README.md ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ library_name: sae-lens
4
+ tags:
5
+ - sae
6
+ - sparse-autoencoder
7
+ - t5gemma
8
+ - t5gemma2
9
+ - google-t5gemma
10
+ - interpretability
11
+ - mechanistic-interpretability
12
+ - sparse-autoencoders
13
+ ---
14
+
15
+ # T5Gemma 2-270M Sparse Autoencoders (All 36 Layers)
16
+
17
+ Sparse Autoencoders (SAEs) trained on all layers of `google/t5gemma-2-270m-270m`.
18
+
19
+ ## Model Specifications
20
+
21
+ | Property | Value |
22
+ |----------|-------|
23
+ | **Base Model** | `google/t5gemma-2-270m-270m` |
24
+ | **Architecture** | T5 Encoder-Decoder |
25
+ | **Encoder Parameters** | ~270M |
26
+ | **Decoder Parameters** | ~270M |
27
+ | **Total Parameters** | ~540M |
28
+ | **Encoder Layers** | 18 |
29
+ | **Decoder Layers** | 18 |
30
+ | **Hidden Size (d_model)** | 640 |
31
+ | **FFN Dimension** | 2,560 |
32
+ | **Attention Heads** | 10 |
33
+ | **Vocabulary Size** | 32,128 |
34
+
35
+ ## SAE Configuration
36
+
37
+ | Property | Value |
38
+ |----------|-------|
39
+ | **SAE Input Dimension (d_in)** | 640 |
40
+ | **SAE Hidden Dimension (d_sae)** | 4,096 |
41
+ | **Expansion Factor** | 6.4Γ— |
42
+ | **L1 Coefficient** | 0.01 |
43
+ | **Training Epochs** | 5 |
44
+ | **Batch Size** | 2 |
45
+ | **Learning Rate** | 1e-4 |
46
+ | **Optimizer** | Adam |
47
+ | **Activation Hook** | `self_attn.o_proj` |
48
+ | **Precision** | float16 (model), float32 (SAE) |
49
+
50
+ ## Coverage
51
+
52
+ | Component | Layers | Count |
53
+ |-----------|--------|-------|
54
+ | Encoder | 0-17 | 18 SAEs |
55
+ | Decoder | 0-17 | 18 SAEs |
56
+ | **Total** | **36** | **36 SAEs** |
57
+
58
+ ## Usage
59
+
60
+ ```python
61
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
62
+ import torch
63
+ from huggingface_hub import hf_hub_download
64
+
65
+ # Load base model
66
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/t5gemma-2-270m-270m")
67
+ tokenizer = AutoTokenizer.from_pretrained("google/t5gemma-2-270m-270m")
68
+
69
+ # Load SAE for a specific layer
70
+ sae_path = hf_hub_download(
71
+ repo_id="mindchain/t5gemma2-sae-all-layers",
72
+ filename="encoder/sae_encoder_00.pt"
73
+ )
74
+ sae = torch.load(sae_path, map_location="cpu")
75
+
76
+ # SAE forward pass
77
+ def forward_sae(sae, activations):
78
+ features = torch.relu(activations @ sae['W_enc'] + sae['b_enc'])
79
+ reconstructed = features @ sae['W_dec'] + sae['b_dec']
80
+ return reconstructed, features
81
+
82
+ # Example: Extract activations and run through SAE
83
+ inputs = tokenizer("Translate to German: Hello world", return_tensors="pt")
84
+ with torch.no_grad():
85
+ # Get encoder layer 0 output (you'd need to hook this)
86
+ activations = ... # hook self_attn.o_proj of encoder layer 0
87
+ recon, features = forward_sae(sae, activations)
88
+ ```
89
+
90
+ ## SAE Checkpoint Structure
91
+
92
+ Each checkpoint file contains:
93
+
94
+ ```python
95
+ {
96
+ 'model_name': 'google/t5gemma-2-270m-270m',
97
+ 'layer_type': 'encoder' or 'decoder',
98
+ 'layer_idx': int,
99
+ 'd_in': 640,
100
+ 'd_sae': 4096,
101
+ 'W_enc': Tensor([640, 4096]), # Encoder weights
102
+ 'b_enc': Tensor([4096]), # Encoder bias
103
+ 'W_dec': Tensor([4096, 640]), # Decoder weights
104
+ 'b_dec': Tensor([640]), # Decoder bias
105
+ 'history': {
106
+ 'loss': [...], # Loss per epoch
107
+ 'l0': [...] # Sparsity (active features) per epoch
108
+ }
109
+ }
110
+ ```
111
+
112
+ ## Training Details
113
+
114
+ - **Dataset**: 1500 diverse text samples, repeated across epochs
115
+ - **Hook Point**: Self-attention output projection (`model.{encoder,decoder}.layers.{N}.self_attn.o_proj`)
116
+ - **Training Duration**: ~2 hours on single GPU
117
+ - **Final Metrics** (example, layer 0):
118
+ - Loss: ~0.0014
119
+ - L0 (active features): ~1367/4096 (~33% sparsity)
120
+
121
+ ## Files
122
+
123
+ ```
124
+ encoder/
125
+ β”œβ”€β”€ sae_encoder_00.pt # Encoder layer 0
126
+ β”œβ”€β”€ sae_encoder_01.pt # Encoder layer 1
127
+ β”œβ”€β”€ ...
128
+ └── sae_encoder_17.pt # Encoder layer 17
129
+
130
+ decoder/
131
+ β”œβ”€β”€ sae_decoder_00.pt # Decoder layer 0
132
+ β”œβ”€β”€ sae_decoder_01.pt # Decoder layer 1
133
+ β”œβ”€β”€ ...
134
+ └── sae_decoder_17.pt # Decoder layer 17
135
+ ```
136
+
137
+ ## License
138
+
139
+ MIT
140
+
141
+ ## Credits
142
+
143
+ Trained by [mindchain](https://huggingface.co/mindchain)
144
+
145
+ ## References
146
+
147
+ - [T5Gemma 2 Model Card](https://huggingface.co/google/t5gemma-2-270m-270m)
148
+ - [SAELens](https://github.com/decoderesearch/SAELens)
149
+ - [TransformerLens](https://github.com/TransformerLensOrg/TransformerLens)