-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference_updates.py
166 lines (151 loc) · 5.73 KB
/
inference_updates.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
import argparse
import collections
import json
import os
from itertools import chain
import numpy as np
import torch
import wandb
from datasets import load_dataset
from tqdm import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
from inference import get_generation_config, get_scores, prepare_prompt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def main(args):
experiment_dir = os.path.join(args.output_dir, args.model_name)
os.makedirs(experiment_dir, exist_ok=True)
model = AutoModelForCausalLM.from_pretrained(args.model_name_or_path).to(device)
use_fast = True
if (
"alpaca" in args.model_name_or_path
or "llama" in args.model_name_or_path.lower()
):
# the fact tokenizer causes issues with protobuf and tokenizers libraries
use_fast = False
tokenizer = AutoTokenizer.from_pretrained(
args.model_name_or_path, use_fast=use_fast
)
config = get_generation_config(tokenizer)
ds = load_dataset(f"coastalcph/fm-updates-{args.model_name}")
templates_ds = load_dataset("coastalcph/fm_templates")["train"]
outputs = {key: [] for key in ["raw_predictions", "predictions"]}
updated_counts_mutability = collections.defaultdict(int)
rng = np.random.default_rng(42)
for ex_i, ex in enumerate(tqdm(chain(*[ds[split] for split in args.splits]))):
relation = ex["relation"]
subject = ex["query"]["label"]
prompt = ex["prediction"]["query"].replace(subject, "[X]")
templates = set(
[
t.replace("[Y].", "").replace("[Y] .", "").strip()
for t in templates_ds[relation][0]["templates"]
]
)
if prompt not in templates:
print("prompt", prompt)
print("templates", templates)
raise Exception("prompt not in templates")
templates.remove(prompt)
context = list(templates)[rng.choice(len(templates), 1)[0]]
new_target = ex["updates"][0]
query = "Imagine that {} {}. Then, {}".format(
context.replace("[X]", subject), new_target, prompt.replace("[X]", subject)
)
with torch.no_grad():
prompt = prepare_prompt(
query, args.model_name_or_path, args.instruction, args.template
)
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
model_output = model.generate(
input_ids, generation_config=config, output_scores=True
)
answer, token_scores, first_token_score, perplexity = get_scores(
model_output, input_ids, prompt, query, tokenizer
)
outputs["raw_predictions"].append(
{
"index": ex_i,
"query": query,
"predictions": [
{
"output_ids": model_output["sequences"][0].cpu().tolist(),
"answer": tokenizer.decode(model_output["sequences"][0]),
}
],
}
)
outputs["predictions"].append(
{
"index": ex_i,
"query": query,
"new_target": new_target,
"predictions": [
{
"answer": answer,
"per_token_probability": token_scores,
"first_token_probability": first_token_score,
"perplexity": perplexity,
}
],
}
)
if answer.startswith(new_target):
updated_counts_mutability[f"{ex['type']}/succ"] += 1
updated_counts_mutability[f"{ex['type']}/total"] += 1
if ex_i % 100 == 0:
print(updated_counts_mutability)
print("query", query)
print("new_target", new_target)
print("answer", answer)
for mutability in list(
set([k.split("/")[0] for k in updated_counts_mutability.keys()])
):
total = updated_counts_mutability[f"{mutability}/total"]
succ = updated_counts_mutability[f"{mutability}/succ"]
wandb.run.summary[f"{mutability}_total"] = total
wandb.run.summary[f"{mutability}_succ"] = succ
wandb.run.summary[f"{mutability}_succ_rate"] = succ / total
print("Writing outputs")
for key in outputs:
with open(os.path.join(experiment_dir, key + ".json"), "w") as outfile:
for i, item in enumerate(outputs[key]):
outfile.write(json.dumps(item))
if i != len(outputs[key]) - 1:
outfile.write("\n")
with open(os.path.join(experiment_dir, "args.json"), "w") as f:
json.dump(args.__dict__, f, indent=2)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Inference")
parser.add_argument(
"--template",
type=str,
default="query_in_response",
help="query_in_instructions, query_in_response or query_in_input",
)
parser.add_argument(
"--instruction",
type=str,
default="Complete the fact in as few words as possible",
)
parser.add_argument(
"--output_dir",
type=str,
default="output",
help="Dir where model outputs will be stored",
)
parser.add_argument("--model_name", type=str, required=True, help="")
parser.add_argument(
"--model_name_or_path",
type=str,
required=True,
help="Model name or path",
)
parser.add_argument("--splits", nargs="+", default=["test"], help="")
args = parser.parse_args()
project_name = "prompt_updates"
wandb.init(
project=project_name,
name=" ".join([args.model_name]),
config=args,
)
main(args)