-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdrop.py
201 lines (147 loc) · 6.22 KB
/
drop.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
import json
import random
from pathlib import Path
from typing import List
from datasets import load_dataset
from fire import Fire
from pydantic import BaseModel
from tqdm import tqdm
from modeling import select_model, EvalModel
class DropAnswer(BaseModel):
spans: List[str]
types: List[str]
class DropSample(BaseModel):
section_id: str
query_id: str
passage: str
question: str
answers_spans: DropAnswer
def get_answers(self) -> List[str]:
return [text.strip() for text in self.answers_spans.spans]
def as_prompt(self, include_answer=True):
prompt = self.passage.strip() + " " + self.question.strip()
prompt += "\nAnswer:"
if include_answer:
answer = self.get_answers()[0]
prompt = f"{prompt} {answer}\n\n"
return prompt
class DropData(BaseModel):
samples: List[DropSample]
@classmethod
def load_from_huggingface(cls, path: str = "drop", split: str = "validation"):
data = load_dataset(path, split=split)
samples = [DropSample(**raw) for raw in tqdm(data, desc=str((path, split)))]
return cls(samples=samples)
@classmethod
def load(cls, path: str):
with open(path) as f:
samples = [DropSample(**json.loads(line)) for line in f]
return cls(samples=samples)
def save(self, path: str):
Path(path).parent.mkdir(exist_ok=True, parents=True)
with open(path, "w") as f:
for s in self.samples:
print(s.json(), file=f)
def analyze(self):
info = dict(samples=len(self.samples))
print(json.dumps(info, indent=2))
def train_test_split(self, num_train: int, seed: int = 0):
random.seed(seed)
samples_train = random.sample(self.samples, k=num_train)
texts_train = set(s.passage for s in samples_train)
samples_test = [s for s in self.samples if s.passage not in texts_train]
print(dict(samples_train=len(samples_train), samples_test=len(samples_test)))
return DropData(samples=samples_train), DropData(samples=samples_test)
def test_data(path_out: str = "data/drop.json"):
data = DropData.load_from_huggingface()
data.analyze()
data.save(path_out)
def gen_prompt(data: DropData, k=-1):
prompt = ""
if k == -1:
k = len(data.samples)
for sample in data.samples[:k]:
prompt += sample.as_prompt()
return prompt
def filter_dataset(data: DropData) -> DropData:
samples = []
for s in data.samples:
spans = s.get_answers()
if len(set(spans)) == 1:
samples.append(s)
print(dict(orig=len(data.samples), after_filter=len(samples)))
return DropData(samples=samples)
def evaluate(model: EvalModel, data: DropData, ntrain: int) -> dict:
data = filter_dataset(data)
data_train, data_test = data.train_test_split(num_train=ntrain)
is_correct = []
score = 0
progress = tqdm(data_test.samples)
sample: DropSample
for sample in progress:
# get prompt and make sure it fits
k = int(ntrain)
prompt_end = sample.as_prompt(include_answer=False)
train_prompt = gen_prompt(data_train, k)
prompt = train_prompt + prompt_end
while not model.check_valid_length(prompt) and k > 0:
k -= 1
train_prompt = gen_prompt(data_train, k)
prompt = train_prompt + prompt_end
label = sample.get_answers()[0]
pred = model.run(prompt).strip()
is_correct.append(pred.startswith(label))
score = sum(is_correct) / len(is_correct)
progress.set_postfix(score=score)
print(dict(prompt=prompt, label=label, pred=pred))
return dict(score=score)
def main(data_dir: str = "drop", ntrain: int = 3, **kwargs):
model = select_model(max_input_length=2048, max_output_length=8, **kwargs)
print(locals())
all_results = []
data = DropData.load_from_huggingface()
result = evaluate(model, data, ntrain=ntrain)
print(result)
return result["score"]
"""
python main.py drop --model_name seq_to_seq --model_path google/flan-t5-xl
{'score': 0.5632458233890215}
python main.py drop --model_name llama --model_path eachadea/vicuna-13b --load_8bit
{'score': 0.3291964996022275}
python main.py drop --model_name llama --model_path chavinlo/alpaca-native
{'score': 0.2638027048528242}
python main.py drop --model_name llama --model_path decapoda-research/llama-13b-hf --load_8bit
{'score': 0.35338106603023073}
python main.py drop --model_name llama --model_path TheBloke/koala-13B-HF --load_8bit
{'score': 0.28353221957040575}
python main.py drop --model_name llama --model_path decapoda-research/llama-7b-hf
{'score': 0.27653142402545744}
python main.py drop --model_name chatglm --model_path THUDM/chatglm-6b
{'score': 0.44200477326968973}
python main.py drop --model_name seq_to_seq --model_path declare-lab/flan-alpaca-gpt4-xl
{'score': 0.28496420047732696}
python main.py drop --model_name seq_to_seq --model_path google/flan-t5-xl --lora_path declare-lab/flan-alpaca-xl-lora
{'score': 0.36945107398568017}
python main.py drop --model_name llama --model_path TheBloke/wizardLM-7B-HF
{'score': 0.2587112171837709}
python main.py drop --model_name seq_to_seq --model_path google/flan-t5-xxl --load_8bit
{'score': 0.6728719172633254}
python main.py drop --model_name causal --model_path Salesforce/codegen-6B-mono
{'score': 0.1280827366746221}
python main.py drop --model_name seq_to_seq --model_path bigscience/T0pp --load_8bit
{'score': 0.016070007955449484}
python main.py drop --model_name llama --model_path TheBloke/OpenAssistant-SFT-7-Llama-30B-HF --load_8bit
{'score': 0.46046141607000796}
python main.py drop --model_name causal --model_path stabilityai/stablelm-tuned-alpha-7b
{'score': 0.1271280827366746}
python main.py drop --model_name causal --model_path bigscience/bloomz-7b1
{'score': 0.23182179793158314}
python main.py drop --model_name llama --model_path huggyllama/llama-30b --load_8bit
{'score': 0.45425616547334924}
python main.py drop --model_name causal --model_path facebook/opt-iml-30b --load_8bit
{'score': 0.4758949880668258}
python main.py drop --model_name seq_to_seq --model_path declare-lab/flan-alpaca-xxl --load_8bit
{'score': 0.6233890214797136}
"""
if __name__ == "__main__":
Fire()