-
Notifications
You must be signed in to change notification settings - Fork 32
/
prompting_with_steering.py
216 lines (201 loc) · 7.42 KB
/
prompting_with_steering.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
"""
Use CAA to steer the model
Usage:
python prompting_with_steering.py --behaviors sycophancy --layers 10 --multipliers 0.1 0.5 1 2 5 10 --type ab --use_base_model --model_size 7b
"""
import json
from llama_wrapper import LlamaWrapper
import os
from dotenv import load_dotenv
import argparse
from typing import List, Dict, Optional
from tqdm import tqdm
from utils.helpers import get_a_b_probs
from utils.tokenize import E_INST
from steering_settings import SteeringSettings
from behaviors import (
get_open_ended_test_data,
get_steering_vector,
get_system_prompt,
get_truthful_qa_data,
get_mmlu_data,
get_ab_test_data,
ALL_BEHAVIORS,
get_results_dir,
)
load_dotenv()
HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
def process_item_ab(
item: Dict[str, str],
model: LlamaWrapper,
system_prompt: Optional[str],
a_token_id: int,
b_token_id: int,
) -> Dict[str, str]:
question: str = item["question"]
answer_matching_behavior = item["answer_matching_behavior"]
answer_not_matching_behavior = item["answer_not_matching_behavior"]
model_output = model.get_logits_from_text(
user_input=question, model_output="(", system_prompt=system_prompt
)
a_prob, b_prob = get_a_b_probs(model_output, a_token_id, b_token_id)
return {
"question": question,
"answer_matching_behavior": answer_matching_behavior,
"answer_not_matching_behavior": answer_not_matching_behavior,
"a_prob": a_prob,
"b_prob": b_prob,
}
def process_item_open_ended(
item: Dict[str, str],
model: LlamaWrapper,
system_prompt: Optional[str],
a_token_id: int,
b_token_id: int,
) -> Dict[str, str]:
question = item["question"]
model_output = model.generate_text(
user_input=question, system_prompt=system_prompt, max_new_tokens=100
)
return {
"question": question,
"model_output": model_output.split(E_INST)[-1].strip(),
"raw_model_output": model_output,
}
def process_item_tqa_mmlu(
item: Dict[str, str],
model: LlamaWrapper,
system_prompt: Optional[str],
a_token_id: int,
b_token_id: int,
) -> Dict[str, str]:
prompt = item["prompt"]
correct = item["correct"]
incorrect = item["incorrect"]
category = item["category"]
model_output = model.get_logits_from_text(
user_input=prompt, model_output="(", system_prompt=system_prompt
)
a_prob, b_prob = get_a_b_probs(model_output, a_token_id, b_token_id)
return {
"question": prompt,
"correct": correct,
"incorrect": incorrect,
"a_prob": a_prob,
"b_prob": b_prob,
"category": category,
}
def test_steering(
layers: List[int], multipliers: List[int], settings: SteeringSettings, overwrite=False
):
"""
layers: List of layers to test steering on.
multipliers: List of multipliers to test steering with.
settings: SteeringSettings object.
"""
save_results_dir = get_results_dir(settings.behavior)
if not os.path.exists(save_results_dir):
os.makedirs(save_results_dir)
process_methods = {
"ab": process_item_ab,
"open_ended": process_item_open_ended,
"truthful_qa": process_item_tqa_mmlu,
"mmlu": process_item_tqa_mmlu,
}
test_datasets = {
"ab": get_ab_test_data(settings.behavior),
"open_ended": get_open_ended_test_data(settings.behavior),
"truthful_qa": get_truthful_qa_data(),
"mmlu": get_mmlu_data(),
}
model = LlamaWrapper(
HUGGINGFACE_TOKEN,
size=settings.model_size,
use_chat=not settings.use_base_model,
override_model_weights_path=settings.override_model_weights_path,
)
a_token_id = model.tokenizer.convert_tokens_to_ids("A")
b_token_id = model.tokenizer.convert_tokens_to_ids("B")
model.set_save_internal_decodings(False)
test_data = test_datasets[settings.type]
for layer in layers:
name_path = model.model_name_path
if settings.override_vector_model is not None:
name_path = settings.override_vector_model
if settings.override_vector is not None:
vector = get_steering_vector(settings.behavior, settings.override_vector, name_path, normalized=True)
else:
vector = get_steering_vector(settings.behavior, layer, name_path, normalized=True)
if settings.model_size != "7b":
vector = vector.half()
vector = vector.to(model.device)
for multiplier in multipliers:
result_save_suffix = settings.make_result_save_suffix(
layer=layer, multiplier=multiplier
)
save_filename = os.path.join(
save_results_dir,
f"results_{result_save_suffix}.json",
)
if os.path.exists(save_filename) and not overwrite:
print("Found existing", save_filename, "- skipping")
continue
results = []
for item in tqdm(test_data, desc=f"Layer {layer}, multiplier {multiplier}"):
model.reset_all()
model.set_add_activations(
layer, multiplier * vector
)
result = process_methods[settings.type](
item=item,
model=model,
system_prompt=get_system_prompt(settings.behavior, settings.system_prompt),
a_token_id=a_token_id,
b_token_id=b_token_id,
)
results.append(result)
with open(
save_filename,
"w",
) as f:
json.dump(results, f, indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--layers", nargs="+", type=int, required=True)
parser.add_argument("--multipliers", nargs="+", type=float, required=True)
parser.add_argument(
"--behaviors",
type=str,
nargs="+",
default=ALL_BEHAVIORS
)
parser.add_argument(
"--type",
type=str,
required=True,
choices=["ab", "open_ended", "truthful_qa", "mmlu"],
)
parser.add_argument("--system_prompt", type=str, default=None, choices=["pos", "neg"], required=False)
parser.add_argument("--override_vector", type=int, default=None)
parser.add_argument("--override_vector_model", type=str, default=None)
parser.add_argument("--use_base_model", action="store_true", default=False)
parser.add_argument("--model_size", type=str, choices=["7b", "13b"], default="7b")
parser.add_argument("--override_model_weights_path", type=str, default=None)
parser.add_argument("--overwrite", action="store_true", default=False)
args = parser.parse_args()
steering_settings = SteeringSettings()
steering_settings.type = args.type
steering_settings.system_prompt = args.system_prompt
steering_settings.override_vector = args.override_vector
steering_settings.override_vector_model = args.override_vector_model
steering_settings.use_base_model = args.use_base_model
steering_settings.model_size = args.model_size
steering_settings.override_model_weights_path = args.override_model_weights_path
for behavior in args.behaviors:
steering_settings.behavior = behavior
test_steering(
layers=args.layers,
multipliers=args.multipliers,
settings=steering_settings,
overwrite=args.overwrite,
)