-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
291 lines (222 loc) · 8.55 KB
/
util.py
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
# %%
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
import functools
from pathlib import Path
from typing import Optional
import pandas as pd
import torch as t
from pydantic_settings import BaseSettings
import nnsight
from tqdm.auto import tqdm
from openai import OpenAI
project_root = Path(__file__).parents[2]
plots_dir = project_root / "plots"
tqdm.pandas()
# %%
class Settings(BaseSettings):
"""
Settings for the project.
NNSIGHT_API_TOKEN is only required if REMOTE_MODE is True. This is obtained
from https://login.ndif.us/
"""
HF_API_TOKEN: str
NNSIGHT_API_TOKEN: str = None
OPENAI_API_TOKEN: str = None
REMOTE_MODE: bool = False
class Config:
env_file = str(project_root / ".env")
env_file_encoding = "utf-8"
settings = Settings()
device = t.device("cuda" if t.cuda.is_available() else "cpu")
if settings.REMOTE_MODE:
nnsight.CONFIG.set_default_api_key(settings.NNSIGHT_API_TOKEN)
# %%
def vectorize(func, *, out_type="list", threaded=False, pbar=False):
def wrapper(first_arg, *args, **kwargs):
first_arg = pd.Series(first_arg)
# Function to be executed in parallel
apply_func = functools.partial(func, *args, **kwargs)
# Use ThreadPoolExecutor to map the function in parallel
if threaded:
with ThreadPoolExecutor() as executor:
if pbar:
results_list = list(
tqdm(executor.map(apply_func, first_arg), total=len(first_arg))
)
else:
results_list = list(executor.map(apply_func, first_arg))
else:
if pbar:
results_list = list(first_arg.progress_map(apply_func))
else:
results_list = list(first_arg.map(apply_func))
if out_type == "tensor":
results = t.stack(results_list, dim=0)
elif out_type == "series":
results = pd.Series(results_list, index=first_arg.index)
elif out_type == "list":
pass # results is already a list
else:
raise ValueError(f"Invalid out_type: {out_type}")
return results
return wrapper
# ===
# Interventions
# ===
@dataclass
class Intervention:
magnitude: float
@classmethod
def batch_learn(cls, model, pos_prompts, neg_prompts, magnitudes, **kwargs): ...
@classmethod
def learn(cls, model, pos_prompts, neg_prompts, magnitude=1.0, **kwargs): ...
def apply(self, model, prompt): ...
@dataclass
class ResidualStreamIntervention(Intervention):
layer: int
magnitude: float
vector: t.Tensor
def with_magnitude(self, magnitude):
return ResidualStreamIntervention(
layer=self.layer, magnitude=magnitude, vector=self.vector
)
@classmethod
def batch_learn(cls, model, pos_prompts, neg_prompts, layers, magnitudes):
get_residuals = vectorize(last_token_residual_stream, out_type="tensor")
pos_vectors = get_residuals(pos_prompts, model=model).mean(0)
neg_vectors = get_residuals(neg_prompts, model=model).mean(0)
function_vecs = pos_vectors - neg_vectors
return {
(layer, magnitude): cls(
layer=layer, vector=function_vecs[layer], magnitude=magnitude
)
for layer in layers
for magnitude in magnitudes
}
@classmethod
def learn(cls, model, pos_prompts, neg_prompts, layer, magnitude=1.0):
interventions = cls.batch_learn(
model, pos_prompts, neg_prompts, [layer], [magnitude]
)
return interventions[(layer, magnitude)]
def apply(self, model):
model.model.layers[self.layer].output[0][:, -1, :] += (
self.vector * self.magnitude
)
# ===
# Model Inference
# ===
@t.inference_mode()
def next_logits(prompt: str, *, model, intervention: Optional[Intervention] = None):
with model.trace(prompt, remote=settings.REMOTE_MODE):
if intervention is not None:
intervention.apply(model)
log_probs = model.lm_head.output[..., -1, :].save()
return log_probs.value.squeeze()
@t.inference_mode()
def next_token_str(prompt: str, *, model, intervention: Optional[Intervention] = None):
logits = next_logits(prompt, model=model, intervention=intervention)
return model.tokenizer.decode(logits.argmax(), skip_special_tokens=False)
@t.inference_mode()
def last_token_residual_stream(
prompt: str, *, model, intervention: Optional[Intervention] = None
):
saves = []
with model.trace(prompt, remote=settings.REMOTE_MODE):
if intervention is not None:
intervention.apply(model)
for _, layer in enumerate(model.model.layers):
saves.append(layer.output[0][:, -1, :].save())
return t.stack([save.value for save in saves])
@t.inference_mode()
def continue_text(
prompt: str,
*,
model,
intervention: Optional[Intervention] = None,
intervention_pos: str = "last_input_token",
max_new_tokens=50,
skip_special_tokens=True,
):
if intervention_pos not in ["last_input_token", "all_tokens"]:
raise ValueError(f"Invalid intervention_pos: {intervention_pos}")
with model.generate(
max_new_tokens=max_new_tokens, remote=settings.REMOTE_MODE
) as generator:
with generator.invoke(prompt):
if intervention is not None:
intervention.apply(model)
for _ in range(max_new_tokens):
model.next()
if intervention is not None and intervention_pos == "all_tokens":
intervention.apply(model)
all_tokens = model.generator.output.save()
complete_string = model.tokenizer.batch_decode(
all_tokens.value, skip_special_tokens=False
)[0]
# Find the first occurrence of the original prompt
prompt_index = complete_string.find(prompt)
assert prompt_index != -1, "Original prompt not found in the completion"
# Ensure it's the only occurrence
assert (
complete_string.count(prompt) == 1
), "Multiple occurrences of the original prompt found"
# Keep only the text coming after the prompt
complete_string = complete_string[prompt_index + len(prompt) :]
if skip_special_tokens:
# Re-encode and decode the completion to remove special tokens
tokens = model.tokenizer.encode(complete_string)
complete_string = model.tokenizer.decode(tokens, skip_special_tokens=True)
return complete_string
@t.inference_mode()
def batch_continue_text(
prompts,
*,
model,
intervention: Optional[Intervention] = None,
max_new_tokens=50,
skip_special_tokens=True,
):
with model.generate(
max_new_tokens=max_new_tokens, remote=settings.REMOTE_MODE
) as generator:
with generator.invoke(list(prompts)):
if intervention is not None:
intervention.apply(model)
for _ in range(max_new_tokens):
model.next()
all_tokens = model.generator.output.save()
complete_strings = model.tokenizer.batch_decode(
all_tokens.value, skip_special_tokens=False
)
processed_completions = []
# Find the first occurrence of the original prompt
for prompt, complete_string in zip(prompts, complete_strings):
prompt_index = complete_string.find(prompt)
assert prompt_index != -1, "Original prompt not found in the completion"
# Ensure it's the only occurrence
assert (
complete_string.count(prompt) == 1
), "Multiple occurrences of the original prompt found"
# Keep only the text coming after the prompt
complete_string = complete_string[prompt_index + len(prompt) :]
if skip_special_tokens:
# Re-encode and decode the completion to remove special tokens
tokens = model.tokenizer.encode(complete_string)
complete_string = model.tokenizer.decode(tokens, skip_special_tokens=True)
processed_completions.append(complete_string)
return processed_completions
openai_client = OpenAI(api_key=settings.OPENAI_API_TOKEN)
def call_openai(prompt, return_type, model="gpt-4o-2024-08-06"):
completion = openai_client.beta.chat.completions.parse(
model=model,
messages=[
{"role": "user", "content": prompt},
],
response_format=return_type,
)
value = completion.choices[0].message.parsed
return value
def append(df: pd.DataFrame, row: dict) -> pd.DataFrame:
return pd.concat([df, pd.DataFrame([row])], ignore_index=True)