forked from peterlai/gpt-circuits
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
330 lines (314 loc) · 12.6 KB
/
models.py
File metadata and controls
330 lines (314 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from dataclasses import dataclass, field
from enum import Enum
from typing import Literal, Optional
from config import Config, map_options
from config.gpt.models import GPTConfig, gpt_options
from collections import namedtuple
import warnings
class HookPoint(str, Enum):
RESID_PRE = "act"
RESID_MID = "residmid"
RESID_POST = "residpost"
MLP_IN = "mlpin"
MLP_OUT = "mlpout"
ACT = "act" #alias for RESID_PRE
ATTN_OUT = "attnout"
ATTN_IN = "attnin"
MLP_ACT_GRAD = "mlpactgrads"
DYT_ACT_GRAD = "normactgrads"
@classmethod
def all(cls) -> list[str]:
return [loc.value for loc in cls]
class SAEVariant(str, Enum):
STANDARD = "standard"
STANDARD_V2 = "standard_v2"
GATED = "gated"
GATED_V2 = "gated_v2"
JUMP_RELU = "jumprelu"
JUMP_RELU_STAIRCASE = "jumprelu_staircase"
TOPK = "topk"
TOPK_STAIRCASE = "topk_staircase"
TOPK_STAIRCASE_DETACH = "topk_staircase_detach"
JSAE = "jsae_layer" # deprecated. Don't use this.
JSAE_LAYER = "jsae_layer"
JSAE_BLOCK = "jsae_block"
STAIRCASE_BLOCK = "staircase_block"
def is_jsae(self) -> bool:
"""Check if this variant is a JSAE type."""
return self in {SAEVariant.JSAE, SAEVariant.JSAE_LAYER, SAEVariant.JSAE_BLOCK}
SAELocations = namedtuple('SAELocations', ['in_loc', 'out_loc'])
location_map: dict[SAEVariant, SAELocations] = {
SAEVariant.JSAE_LAYER: SAELocations(HookPoint.MLP_IN.value, HookPoint.MLP_OUT.value),
SAEVariant.JSAE_BLOCK: SAELocations(HookPoint.RESID_MID.value, HookPoint.RESID_POST.value),
}
def gen_sae_locations(sae_variant: SAEVariant) -> SAELocations:
"""
Generate the location of the SAE weights to train.
"""
if sae_variant not in location_map:
raise ValueError(f"gen_sae_locations: Unsupported SAE variant: {sae_variant}")
return location_map[sae_variant]
@dataclass
class SAEConfig(Config):
gpt_config: GPTConfig = field(default_factory=GPTConfig)
n_features: tuple = () # Number of features in each layer
sae_variant: SAEVariant = SAEVariant.STANDARD
top_k: Optional[tuple[int, ...]] = None # Required for topk variant
sae_keys: Optional[tuple[str, ...]] = None
deprecated: Optional[str] = None
def __post_init__(self):
if self.deprecated is not None:
warnings.warn(self.deprecated, DeprecationWarning, stacklevel=2)
@property
def block_size(self) -> int:
return self.gpt_config.block_size
@staticmethod
def dict_factory(fields: list) -> dict:
"""
Only export n_features and sae_variant.
"""
whitelisted_fields = ("n_features", "sae_variant", "top_k", "sae_keys")
return {k: v for (k, v) in fields if k in whitelisted_fields and v is not None}
def gen_sae_keys(n_features: int, loc : Literal["mlplayer", "mlpblock", "standard"] = 'standard') -> tuple[str, ...]:
match loc:
case "mlplayer":
assert n_features % 2 == 0, "n_features must be even for mlpplayer: " + str(n_features)
return tuple(f"{x}_{y}" for x in range(n_features//2) for y in [HookPoint.MLP_IN.value, HookPoint.MLP_OUT.value])
case "mlpblock":
assert n_features % 2 == 0, "n_features must be even for mlpblock: " + str(n_features)
return tuple(f"{x}_{y}" for x in range(n_features//2) for y in [HookPoint.RESID_MID.value, HookPoint.RESID_POST.value])
case _:
# assume we're using the activations between the transformer blocks
# for a 4 layer transformer, this will be:
# 0_act = 0_residpre (i.e. between the embedding layer and the first transformer block)
# 1_act = 1_residpre (i.e. between the first and second transformer blocks)
# 2_act = 2_residpre (i.e. between the second and third transformer blocks)
# 3_act = 3_residpre (i.e. between the third and fourth transformer blocks)
# 4_act = 3_residpost (i.e. between the fourth/last transformer block and the final layer norm)
return tuple(f"{x}_{HookPoint.ACT.value}" for x in range(n_features))
# SAE configuration options
sae_options: dict[str, SAEConfig] = map_options(
SAEConfig("topk.tblock.gpt2",
gpt_config = gpt_options['gpt2'],
n_features=tuple(768 * n for n in (32,)*13),
sae_variant=SAEVariant.TOPK,
top_k = (32,) * 13,
sae_keys=gen_sae_keys(n_features=13, loc="standard"),
),
SAEConfig(
name="jln.mlpblock.gpt2",
gpt_config = gpt_options['gpt2'],
n_features=tuple(768 * n for n in (32,)*24), # 2 per layer, 12 layers
sae_variant=SAEVariant.JSAE_BLOCK,
top_k = (32,) * 24,
sae_keys=gen_sae_keys(n_features=24, loc="mlpblock"),
),
SAEConfig(
name="jsae.mlp_ln.shk_64x4",
gpt_config = gpt_options['ascii_64x4'],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8, 8, 8, 8)),
sae_variant=SAEVariant.JSAE_BLOCK,
top_k = (10, 10, 10, 10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="mlpblock"),
),
SAEConfig(
name="staircase.tblock.gpt2",
gpt_config=gpt_options['gpt2'],
n_features=tuple(768 * n for n in range(32, 32*13+1, 32)),
sae_variant=SAEVariant.TOPK_STAIRCASE,
top_k = (32,) * 13,
sae_keys=gen_sae_keys(n_features=13, loc="standard"),
),
SAEConfig(
name="mlp.standardx8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8, 8, 8, 8)),
sae_variant=SAEVariant.STANDARD,
sae_keys=gen_sae_keys(n_features=8, loc="mlplayer"),
),
SAEConfig(
name="topk.mlplayer.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8,8,8,8,8,8,8,8)),
sae_variant=SAEVariant.TOPK,
top_k=(10, 10, 10, 10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="mlplayer"),
),
SAEConfig(
name="topk.mlpblock.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8,8,8,8,8,8,8,8)),
sae_variant=SAEVariant.TOPK,
top_k=(10, 10, 10, 10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="mlpblock"),
),
SAEConfig(
name="topk.tblock.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8,8,8,8,8)),
sae_variant=SAEVariant.TOPK,
top_k=(10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="standard"),
),
SAEConfig(
name="staircase-pairsx8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 16, 8, 16, 8, 16, 8, 16)),
sae_variant=SAEVariant.STAIRCASE_BLOCK,
top_k=(10, 10, 10, 10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="mlpblock"),
),
SAEConfig(
name="staircase.mlpblock.gpt2",
gpt_config=gpt_options['gpt2'],
n_features=tuple(768 * n for n in 12*(32,64)),
sae_variant=SAEVariant.STAIRCASE_BLOCK,
top_k = (32,) * 24,
sae_keys=gen_sae_keys(n_features=24, loc="mlpblock"),
),
SAEConfig(
name="mlp_layer.topkx8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8, 8, 8, 8)),
sae_variant=SAEVariant.TOPK,
top_k=(10, 10, 10, 10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="mlplayer"),
deprecated="Use 'topk.mlplayer.shakespeare_64x4' instead",
),
SAEConfig(
name="jsae.topkx8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8, 8, 8, 8)),
sae_variant=SAEVariant.JSAE_LAYER,
top_k=(10, 10, 10, 10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=8, loc="mlplayer"),
),
SAEConfig(
name="standardx16.tiny_32x4",
gpt_config=gpt_options["tiktoken_32x4"],
n_features=tuple(32 * n for n in (16, 16, 16, 16, 16)),
sae_variant=SAEVariant.STANDARD,
sae_keys=gen_sae_keys(n_features=5),
),
# No such thing as tiny_64x2?
# SAEConfig(
# name="standardx8.tiny_64x2",
# gpt_config=gpt_options["tiktoken_64x2"],
# n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
# sae_variant=SAEVariant.STANDARD,
# ),
SAEConfig(
name="standardx8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
sae_variant=SAEVariant.STANDARD,
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="topk-10-x8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
sae_variant=SAEVariant.TOPK,
top_k=(10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="topk-staircase-10-x8-noshare.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 16, 24, 32, 40)),
sae_variant=SAEVariant.TOPK,
top_k=(10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="topk-staircase-10-x8-share.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 16, 24, 32, 40)),
sae_variant=SAEVariant.TOPK_STAIRCASE,
top_k=(10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="topk-staircase-10-x8-detach.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 16, 24, 32, 40)),
sae_variant=SAEVariant.TOPK_STAIRCASE_DETACH,
top_k=(10, 10, 10, 10, 10),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="standardx40.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (40, 40, 40, 40, 40)),
sae_variant=SAEVariant.STANDARD,
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="staircasex8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 16, 24, 32, 40)),
sae_variant=SAEVariant.STANDARD,
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="topk-x8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
sae_variant=SAEVariant.TOPK,
top_k=(10,10,10,10,10),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="top5.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
sae_variant=SAEVariant.TOPK,
top_k=(5,5,5,5,5),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="top20.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
sae_variant=SAEVariant.TOPK,
top_k=(20,20,20,20,20),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="topk-x40.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (40, 40, 40, 40, 40)),
sae_variant=SAEVariant.TOPK,
top_k=(10,10,10,10,10),
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="jumprelu-x8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 8, 8, 8, 8)),
sae_variant=SAEVariant.JUMP_RELU,
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="jumprelu-staircase-x8.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
n_features=tuple(64 * n for n in (8, 16, 24, 32, 40)),
sae_variant=SAEVariant.JUMP_RELU_STAIRCASE,
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="jumprelu-x16.shakespeare_64x4",
gpt_config=gpt_options["ascii_64x4"],
# Only the penultimate layer needs the full x16 expansion factor
n_features=tuple(64 * n for n in (4, 4, 4, 16, 4)),
sae_variant=SAEVariant.JUMP_RELU,
sae_keys=gen_sae_keys(n_features=5),
),
SAEConfig(
name="jumprelu-x32.stories_256x4",
gpt_config=gpt_options["tiktoken_256x4"],
n_features=tuple(256 * n for n in (32, 32, 32, 32, 32)),
sae_variant=SAEVariant.JUMP_RELU,
sae_keys=gen_sae_keys(n_features=5),
),
)