-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtester.py
221 lines (193 loc) · 10.6 KB
/
tester.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
import time
import pandas as pd
from query_pipeline import QueryPipeline
class Tester():
def run_test(self, construction_type, shots, model, construction_format, crfm, queries, needs_instruction, verbose, needs_informative, include_ambiguous_examples, prob_of_ambiguous, togethercomputer, for_finetuning, finetuning_control, salient_task=None):
"""
Runs a single test which consists of a single query to the API with one Prompt
Args:
construction_type (str): type of construction to generate (options included in main)
shots (int): number of examples to include in Prompt (including query)
model (str): model on which to run test (options included in main)
construction_format (str): format of constructions (options included in main)
crfm (bool): if True runs on Stanford's CRFM API, if False runs with OpenAI API
queries (int): number of queries/tests for the current construction_type + construction_format combination
needs_instruction (bool): if True adds instructions to the Prompt
verbose (bool): to print out detailed information on all Prompts while running
needs_informative (bool): if True if needs informative instruction, False otherwise
include_ambiguous_examples (bool): if True includes ambiguous examples in the Prompt, False otherwise
prob_of_ambiguous (float): probability of including an ambiguous example in the Prompt
togethercomputer (bool): if True runs the test with the TogetherComputer API, False otherwise
for_finetuning (bool): if True runs the test for finetuning, False otherwise
finetuning_control (bool): True if running finetuning control tests
salient_task (str): if not None, the salient task for the current test
Returns:
test_df (pd.DataFrame): DataFrame containing all relevant information obtained from running the test
"""
test = QueryPipeline(construction_type, shots, model, construction_format, crfm)
test_df = test.run_pipeline(queries=queries, needs_instruction=needs_instruction, verbose=verbose, needs_informative=needs_informative, include_ambiguous_examples=include_ambiguous_examples, salient_task=salient_task, prob_of_ambiguous=prob_of_ambiguous, togethercomputer=togethercomputer, finetuning_control=finetuning_control, for_finetuning=for_finetuning)
return test_df
def run_two_feature_tests(self, args):
"""
Runs all standard tests which are two-feature tests {'subject_location', 'religious_pronoun', 'propn_negation'}
Args:
args (ArgumentParser.args): command line arguments from main
Returns:
all_tests (pd.DataFrame): DataFrame containg the relevant information from all Prompts queried
"""
all_tests = pd.DataFrame()
construction_formats_list = [args.format_2, args.format_1]
salient_tasks_list = ['subject', 'location', 'religious', 'negation', 'propn', 'pronoun']
construction_types_map = {
'location' : 'subject_location',
'subject' :'subject_location',
'religious' : 'religious_pronoun',
'pronoun' : 'religious_pronoun',
'propn' : 'propn_negation',
'negation' : 'propn_negation'
}
all_tests = pd.DataFrame()
for cf in construction_formats_list:
for st in salient_tasks_list:
for _ in range(3): # need loop in order to not overload API and stay within OpenAI constraints
curr_test = self.run_test(
construction_type=construction_types_map[st],
shots=args.shots,
model=args.model,
construction_format=cf,
crfm=args.crfm,
queries=20,
needs_instruction=args.needs_instruction,
verbose=args.verbose,
needs_informative=args.needs_informative,
include_ambiguous_examples=args.include_ambiguous_examples,
salient_task=st,
prob_of_ambiguous=args.prob_of_ambiguous,
togethercomputer=args.togethercomputer,
for_finetuning=False,
finetuning_control=False
)
all_tests = pd.concat([all_tests, curr_test], ignore_index=True)
if not args.crfm and not args.togethercomputer: time.sleep(60)
return all_tests
def run_two_feature_tests_with_two_set(self, args):
"""
Runs all standard tests which are two-feature tests {'subject_location', 'religious_pronoun', 'propn_negation'}
Args:
args (ArgumentParser.args): command line arguments from main
Returns:
all_tests (pd.DataFrame): DataFrame containg the relevant information from all Prompts queried
"""
all_tests = pd.DataFrame()
construction_types_list = [args.type_1, args.type_2, args.type_3]
construction_formats_list = [args.format_2, args.format_1]
all_tests = pd.DataFrame()
for cf in construction_formats_list:
for ct in construction_types_list:
for _ in range(3): # need loop in order to not overload API and stay within OpenAI constraints
curr_test = self.run_test(
construction_type=ct,
shots=args.shots,
model=args.model,
construction_format=cf,
crfm=args.crfm,
queries=20,
needs_instruction=args.needs_instruction,
verbose=args.verbose,
needs_informative=args.needs_informative,
include_ambiguous_examples=args.include_ambiguous_examples,
salient_task=None,
prob_of_ambiguous=args.prob_of_ambiguous,
togethercomputer=args.togethercomputer,
for_finetuning=False,
finetuning_control=False
)
all_tests = pd.concat([all_tests, curr_test], ignore_index=True)
if not args.crfm: time.sleep(60)
return all_tests
def run_baseline_tests_for_finetuning(self, args):
"""
Runs all standard tests which are two-feature tests {'subject_location', 'religious_pronoun', 'propn_negation'}
Args :
args (ArgumentParser.args): command line arguments from main
Returns:
all_tests (pd.DataFrame): DataFrame containg the relevant information from all Prompts queried
"""
all_tests = pd.DataFrame()
construction_formats_list = [args.format_2, args.format_1]
salient_tasks_list = ['religious', 'pronoun', 'propn', 'negation']
construction_types_map = {
'location' : 'subject_location',
'subject' :'subject_location',
'religious' : 'religious_pronoun',
'pronoun' : 'religious_pronoun',
'propn' : 'propn_negation',
'negation' : 'propn_negation'
}
all_tests = pd.DataFrame()
for cf in construction_formats_list:
for st in salient_tasks_list:
for _ in range(2):
for i in range (3,20): # need loop in order to not overload API and stay within OpenAI constraints
curr_test = self.run_test(
construction_type=construction_types_map[st],
shots=i,
model=args.model,
construction_format=cf,
crfm=args.crfm,
queries=1,
needs_instruction=args.needs_instruction,
verbose=args.verbose,
needs_informative=args.needs_informative,
include_ambiguous_examples=args.include_ambiguous_examples,
salient_task=st,
prob_of_ambiguous=args.prob_of_ambiguous,
togethercomputer=args.togethercomputer,
for_finetuning=True,
finetuning_control=args.finetuning_control
)
all_tests = pd.concat([all_tests, curr_test], ignore_index=True)
return all_tests
def run_finetuned_set(self, args):
"""
Generates finetuning set with two of the six features
Args:
args (ArgumentParser.args): command line arguments from main
Returns:
all_tests (pd.DataFrame): DataFrame containg the relevant information from all Prompts queried
"""
all_tests = pd.DataFrame()
construction_formats_list = [args.format_2, args.format_1]
salient_tasks_list = ['propn', 'negation']
construction_types_map = {
'location' : 'subject_location',
'subject' :'subject_location',
'religious' : 'religious_pronoun',
'pronoun' : 'religious_pronoun',
'propn' : 'propn_negation',
'negation' : 'propn_negation'
}
all_tests = pd.DataFrame()
for cf in construction_formats_list:
for st in salient_tasks_list:
for _ in range(3): # need loop in order to not overload API and stay within OpenAI constraints
curr_test = self.run_test(
construction_type=construction_types_map[st],
shots=20,
model=args.model,
construction_format=cf,
crfm=args.crfm,
queries=20,
needs_instruction=True,
verbose=args.verbose,
needs_informative=False,
include_ambiguous_examples=True,
salient_task=st,
prob_of_ambiguous=args.prob_of_ambiguous,
togethercomputer=args.togethercomputer,
for_finetuning=False,
finetuning_control=False
)
all_tests = pd.concat([all_tests, curr_test], ignore_index=True)
time.sleep(60)
return all_tests