-
Notifications
You must be signed in to change notification settings - Fork 2
/
derivative_log_prob.py
153 lines (149 loc) · 4.3 KB
/
derivative_log_prob.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
#%%
import einops
import numpy as np
from jaxtyping import Float, Int, Bool
import plotly.express as px
from utils.prompts import get_onesided_datasets
from utils.circuit_analysis import get_log_probs
import torch
from torch import Tensor
from transformer_lens import ActivationCache, HookedTransformer, HookedTransformerConfig, utils
from transformer_lens.hook_points import (
HookedRootModule,
HookPoint,
) # Hooking utilities
from typing import Tuple, Union, List, Optional, Callable
from functools import partial
from collections import defaultdict
from tqdm import tqdm
from utils.store import save_array, load_array
#%% # Model loading
device = torch.device('cpu')
MODEL_NAME = "gpt2-small"
model = HookedTransformer.from_pretrained(
MODEL_NAME,
center_unembed=True,
center_writing_weights=True,
fold_ln=True,
device=device,
)
model.name = MODEL_NAME
model.requires_grad_ = False
#%%
pos_adj = [
' perfect', ' fantastic',' delightful',' cheerful',' marvelous',' good',' remarkable',' wonderful',
' fabulous',' outstanding',' awesome',' exceptional',' incredible',' extraordinary',
' amazing',' lovely',' brilliant',' charming',' terrific',' superb',' spectacular',' great',' splendid',
' beautiful',' joyful',' positive',' excellent',
' breathtaking', ' stunning', ' impressive', ' admirable', ' phenomenal',
' radiant', ' sublime', ' glorious', ' magical', ' sensational', ' pleasing', ' movie',
]
neg_adj = [
' dreadful',' bad',' dull',' depressing',' miserable',' tragic',' nasty',' inferior',' horrific',' terrible',
' ugly',' disgusting',' disastrous',' horrendous',' annoying',' boring',' offensive',' frustrating',' wretched',' dire',
' awful',' unpleasant',' horrible',' mediocre',' disappointing',' inadequate',
' foul', ' vile', ' appalling', ' rotten', ' grim', ' dismal',
' deficient',
' disastrous',
' dismal',
' dreadful',
' hateful',
' hideous',
' inadequate',
' inferior',
' insufficient',
' lousy',
' miserable',
' poor',
' shameful',
' sorry',
' tragic',
' troublesome',
' unbearable',
' uncomfortable',
' unfavorable',
' unfortunate',
' unlucky',
' unpleasant',
' unworthy',
' wretched'
]
neutral_adj = [
' average',' normal',' standard',' typical',' common',' ordinary',
' regular',' usual',' familiar',' generic',' conventional',
' fine', ' okay', ' ok', ' decent', ' fair', ' satisfactory',
' adequate', ' alright',
]
for adj in pos_adj + neg_adj + neutral_adj:
model.to_single_token(adj)
#%%
prompt_return_dict, answer_tokens = get_onesided_datasets(
model,
device,
answer_sentiment='negative',
dataset_sentiments=['positive', 'negative', 'neutral'],
n_answers=5,
positive_adjectives=pos_adj,
negative_adjectives=neg_adj,
neutral_adjectives=neutral_adj,
)
orig_tokens = prompt_return_dict['positive']
neutral_tokens = prompt_return_dict['neutral']
new_tokens = prompt_return_dict['negative']
#%%
all_tokens = torch.cat([orig_tokens, new_tokens, neutral_tokens])
all_tokens.shape
#%%
example_prompt = model.to_str_tokens(orig_tokens[0])
adj_token = example_prompt.index(' perfect')
verb_token = example_prompt.index(' loved')
s2_token = example_prompt.index(' movie', example_prompt.index(' movie') + 1)
end_token = len(example_prompt) - 1
#%%
ACT_NAME = 'blocks.0.hook_resid_post'
_, cache = model.run_with_cache(
all_tokens, names_filter=lambda name: name == ACT_NAME
)
#%%
cache.to(device)
cache[ACT_NAME].requires_grad = True
#%%
#%%
def grad_hook(_: Float[Tensor, "batch pos d_model"], hook: HookPoint):
assert hook.name == ACT_NAME
return cache[ACT_NAME]
#%%
logits = model.run_with_hooks(
all_tokens,
fwd_hooks=[(ACT_NAME, grad_hook)],
)
# %%
log_prob = get_log_probs(
logits, answer_tokens, per_prompt=False
)
#%%
log_prob.backward()
#%%
gradient = einops.reduce(
cache[ACT_NAME].grad[:, (adj_token, verb_token), :],
'batch pos d_model -> d_model',
'mean',
)
# %%
gradient.shape
# %%
km_line = torch.tensor(
load_array('km_line_embed_and_mlp0'),
device=device,
dtype=torch.float32,
)
#%%
cosine_sim = einops.einsum(
gradient / gradient.norm(),
km_line / km_line.norm(),
'd_model, d_model -> ',
)
cosine_sim
# %%
save_array(gradient, 'derivative_log_prob', model)
# %%