-
Notifications
You must be signed in to change notification settings - Fork 11
/
experiment.py
322 lines (271 loc) · 14.3 KB
/
experiment.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import importlib
from itertools import product
from typing import List, Literal, Tuple
from loguru import logger
from core.data.dataloader import DataLoader
from core.data.dataset import Dataset
from core.data.sampler.hard import KeepHardSampler
from core.data.sampler.hierarchical import HierarchicalSampler
from core.data.sampler.kmeans import KMeansSampler
from core.data.sampler.rand import RandomSampler
from core.data.sampler.similarity import SimilaritySampler
from core.evaluator.few_shot import FewShotEvaluator
from core.evaluator.grimoire_classify import GrimoireClassifyEvaluator
from core.evaluator.grimoire_rank import GrimoireRankEvaluator
from core.evaluator.with_grimoire import WithGrimoireEvaluator
from core.llm.base import BaseLLM
from core.load_conf import load_yaml_conf
DATA_CONF_PATH = './configs/data.yaml'
EXPERIMENT_CONF_PATH = './configs/experiment.yaml'
CACHE_GRIMOIRE_DIR = './.cache/grimoire_prompts'
CLASSIFIER_PATH = './.cache/classifier_model.pth'
# SentenceTransformer format!
EMBEDDING_MODEL_NAME = 'sentence-transformers/all-mpnet-base-v2'
EMBEDDINGS_FILENAME = 'ebd_train_all-mpnet-base-v2.pickle'
SIMILARITY_FILENAME = 'sims_all-mpnet-base-v2.pickle'
SEED = 22
EXPERIMENT_BATCH_SIZE = 500
EXPERIMENT_TIMES = 3
PROCESS = 10
def catch_all_exceptions(func):
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
logger.error(repr(e))
return wrapper
@catch_all_exceptions
def expt_few_shot(
model: BaseLLM,
data_name: str,
data_conf: dict,
few_shot_cnt: int,
sample_method: Literal['random', 'hierarchical', 'k-means', 'similarity', 'keep-hard'],
per_class: bool = True,
hard_ratio: float = 0.5,
seed: int = 22,
):
setting = f'{sample_method}-{few_shot_cnt}-shot' \
f'{"-hard-"+str(hard_ratio) if sample_method == "keep-hard" else ""}' \
f'{"-per-class" if per_class else ""}'
logger.info(setting)
if per_class:
if len(data_conf['data_label_list']) == 0:
total_shot_cnt = few_shot_cnt * 4
else:
total_shot_cnt = few_shot_cnt * len(data_conf['data_label_list'])
else:
total_shot_cnt = few_shot_cnt
train_dataset = Dataset(
f'data/{data_name}/train.json', data_name=f'{data_name}_train').load()
test_dataset = Dataset(f'data/{data_name}/test.json', data_name=f'{data_name}_test').resize(
EXPERIMENT_BATCH_SIZE * EXPERIMENT_TIMES).load()
if sample_method == 'random':
train_sampler = RandomSampler(
train_dataset, cnt=total_shot_cnt, cluster=per_class, identical=True, seed=seed)
elif sample_method == 'hierarchical':
ebds_path = f'data/{data_name}/{EMBEDDINGS_FILENAME}'
train_sampler = HierarchicalSampler(
train_dataset, data_conf, ebds_path, num_clusters=few_shot_cnt)
elif sample_method == 'k-means':
ebds_path = f'data/{data_name}/{EMBEDDINGS_FILENAME}'
train_sampler = KMeansSampler(
train_dataset, data_conf, ebds_path, num_clusters=few_shot_cnt, seed=seed)
elif sample_method == 'similarity':
sim_ranks_path = f'data/{data_name}/{SIMILARITY_FILENAME}'
train_sampler = SimilaritySampler(
train_dataset, sim_ranks_path, cnt=total_shot_cnt, cluster=per_class, reverse=False)
elif sample_method == 'keep-hard':
train_sampler = KeepHardSampler(train_dataset, data_conf, model, cnt=total_shot_cnt,
hard_ratio=hard_ratio, cluster=per_class, process_num=PROCESS)
test_dataloader = DataLoader(
test_dataset, batch_size=EXPERIMENT_BATCH_SIZE, shuffle=True, seed=seed)
for batch in test_dataloader:
evaluator = FewShotEvaluator(
model, data_conf, batch, setting, process_num=PROCESS)
evaluator.post_init(few_shot_sampler=train_sampler)
evaluator.run()
@catch_all_exceptions
def expt_with_grimoire(
model: BaseLLM,
data_name: str,
data_conf: dict,
few_shot_cnt: int,
sample_method: Literal['random', 'hierarchical', 'k-means', 'similarity', 'keep-hard'],
per_class: bool = True,
verbose: bool = True,
grimoire_generator: BaseLLM = None,
grimoire_dir: str = CACHE_GRIMOIRE_DIR,
hard_ratio: float = 0.5,
seed: int = 22,
):
setting = f'{sample_method}-{few_shot_cnt}-shot' \
f'{"-hard-"+str(hard_ratio) if sample_method == "keep-hard" else ""}' \
f'{"-per-class" if per_class else ""}' \
f'-with-grimoire'\
f'{"-verbose" if verbose else ""}'
logger.info(setting)
if per_class:
if len(data_conf['data_label_list']) == 0:
total_shot_cnt = few_shot_cnt * 4 # Use 4 as the default number of labels
else:
total_shot_cnt = few_shot_cnt * len(data_conf['data_label_list'])
else:
total_shot_cnt = few_shot_cnt
train_dataset = Dataset(
f'data/{data_name}/train.json', data_name=f'{data_name}_train').load()
test_dataset = Dataset(f'data/{data_name}/test.json', data_name=f'{data_name}_test').resize(
EXPERIMENT_BATCH_SIZE * EXPERIMENT_TIMES).load()
if sample_method == 'random':
train_sampler = RandomSampler(
train_dataset, cnt=total_shot_cnt, cluster=per_class, identical=True, seed=seed)
elif sample_method == 'hierarchical':
ebds_path = f'data/{data_name}/{EMBEDDINGS_FILENAME}'
train_sampler = HierarchicalSampler(
train_dataset, data_conf, ebds_path, num_clusters=few_shot_cnt)
elif sample_method == 'k-means':
ebds_path = f'data/{data_name}/{EMBEDDINGS_FILENAME}'
train_sampler = KMeansSampler(
train_dataset, data_conf, ebds_path, num_clusters=few_shot_cnt, seed=seed)
elif sample_method == 'similarity':
sim_ranks_path = f'data/{data_name}/{SIMILARITY_FILENAME}'
train_sampler = SimilaritySampler(
train_dataset, sim_ranks_path, cnt=total_shot_cnt, cluster=per_class, reverse=False)
elif sample_method == 'keep-hard':
train_sampler = KeepHardSampler(train_dataset, data_conf, model, cnt=total_shot_cnt,
hard_ratio=hard_ratio, cluster=per_class, process_num=PROCESS)
test_dataloader = DataLoader(
test_dataset, batch_size=EXPERIMENT_BATCH_SIZE, shuffle=True, seed=seed)
for batch in test_dataloader:
evaluator = WithGrimoireEvaluator(
model, data_conf, batch, setting, process_num=PROCESS)
evaluator.post_init(few_shot_sampler=train_sampler, verbose=verbose,
grimoire_generator=grimoire_generator, grimoire_dir=grimoire_dir)
evaluator.run()
@catch_all_exceptions
def expt_grimoire_rank(
model: BaseLLM,
data_name: str,
data_conf: dict,
embedding_model_name: str = EMBEDDING_MODEL_NAME,
grimoire_generator: BaseLLM = None,
grimoire_dir: str = CACHE_GRIMOIRE_DIR,
filter_out_contains: List[str] = [],
seed: int = 22,
):
setting = 'grimoire-rank'
logger.info(setting)
test_dataset = Dataset(f'data/{data_name}/test.json', data_name=f'{data_name}_test').resize(
EXPERIMENT_BATCH_SIZE * EXPERIMENT_TIMES).load()
test_dataloader = DataLoader(
test_dataset, batch_size=EXPERIMENT_BATCH_SIZE, shuffle=True, seed=seed)
for batch in test_dataloader:
evaluator = GrimoireRankEvaluator(
model, data_conf, batch, setting, process_num=PROCESS)
evaluator.post_init(embedding_model_name, grimoire_generator,
grimoire_dir, filter_out_contains=filter_out_contains)
evaluator.run()
@catch_all_exceptions
def expt_grimoire_classify(
model: BaseLLM,
data_name: str,
data_conf: dict,
embedding_model_name: str = EMBEDDING_MODEL_NAME,
grimoire_generator: BaseLLM = None,
grimoire_dir: str = CACHE_GRIMOIRE_DIR,
classifier_pth_path: str = CLASSIFIER_PATH,
filter_out_contains: List[str] = [],
seed: int = 22,
):
setting = 'grimoire-classify'
logger.info(setting)
test_dataset = Dataset(f'data/{data_name}/test.json', data_name=f'{data_name}_test').resize(
EXPERIMENT_BATCH_SIZE * EXPERIMENT_TIMES).load()
test_dataloader = DataLoader(
test_dataset, batch_size=EXPERIMENT_BATCH_SIZE, shuffle=True, seed=seed)
for batch in test_dataloader:
evaluator = GrimoireClassifyEvaluator(
model, data_conf, batch, setting, process_num=PROCESS)
evaluator.post_init(embedding_model_name, grimoire_generator,
grimoire_dir, classifier_pth_path, filter_out_contains)
evaluator.run()
def parse_experiment_conf(experiment_conf: dict) -> Tuple[List[BaseLLM], List[str], BaseLLM]:
"""Instantiate LLMs and provide data names based on the provided configuration.
Args:
experiment_conf (dict): The configuration dictionary containing
information about LLMs and datasets.
Returns:
Tuple[List[BaseLLM], List[str], BaseLLM]: A tuple containing a list
of instantiated LLMs, the data names and the LLM used to generate grimoires.
"""
# ─── Get Instantiated Llms ────────────────────────────────────────────
llm_configs = experiment_conf.get('llm', {})
instantiated_llms = []
for llm_type, llm_list in llm_configs.items():
if llm_list is None:
continue
for llm_dict in llm_list:
if llm_dict is None:
continue
generator_name = list(llm_dict.keys())[0]
parameters = list(llm_dict.values())[0]
llm_module_name = f"core.llm.{llm_type}"
llm_module = importlib.import_module(llm_module_name)
generator_class = getattr(llm_module, generator_name)
if parameters is not None:
instantiated_llms.append(generator_class(**parameters))
else:
instantiated_llms.append(generator_class())
# ─── Get Data Names ───────────────────────────────────────────────────
datanames = experiment_conf.get('data')
# ─── Get The Llm Used To Generate Grimoires ───────────────────────────
generator_conf = experiment_conf['grimoire_generator']
generator_type = generator_conf['llm_type']
llm_module = importlib.import_module(f"core.llm.{generator_type}")
generator_params = generator_conf['llm_params'] or {}
generator_class = getattr(llm_module, generator_conf['llm'])
instantiated_generator = generator_class(**generator_params)
return instantiated_llms, datanames, instantiated_generator
if __name__ == '__main__':
experiment_conf = load_yaml_conf(EXPERIMENT_CONF_PATH)
models, datanames, grimoire_generator = parse_experiment_conf(
experiment_conf)
data_confs = load_yaml_conf(DATA_CONF_PATH)
for model, dataname in product(models, datanames):
# ─── Add Experiments Here: ────────────────────────────────────
# Baselines
expt_few_shot(model, dataname, data_confs[dataname], few_shot_cnt=0,
sample_method='random', per_class=True, seed=SEED)
expt_few_shot(model, dataname, data_confs[dataname], few_shot_cnt=4,
sample_method='random', per_class=False, seed=SEED)
# Profound grimoires
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4,
sample_method='k-means', per_class=True, grimoire_generator=grimoire_generator, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4,
sample_method='hierarchical', per_class=True, grimoire_generator=grimoire_generator, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4,
sample_method='random', per_class=True, grimoire_generator=grimoire_generator, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='keep-hard',
per_class=True, grimoire_generator=grimoire_generator, hard_ratio=0.5, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='keep-hard',
per_class=True, grimoire_generator=grimoire_generator, hard_ratio=1.0, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=0,
sample_method='random', grimoire_generator=grimoire_generator, seed=SEED)
# Simple grimoires
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='k-means',
per_class=True, verbose=False, grimoire_generator=grimoire_generator, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='hierarchical',
per_class=True, verbose=False, grimoire_generator=grimoire_generator, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='random',
per_class=True, verbose=False, grimoire_generator=grimoire_generator, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='keep-hard',
per_class=True, verbose=False, grimoire_generator=grimoire_generator, hard_ratio=0.5, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=4, sample_method='keep-hard',
per_class=True, verbose=False, grimoire_generator=grimoire_generator, hard_ratio=1.0, seed=SEED)
expt_with_grimoire(model, dataname, data_confs[dataname], few_shot_cnt=0,
sample_method='random', verbose=False, grimoire_generator=grimoire_generator, seed=SEED)
# Grimoire Selection
expt_grimoire_rank(
model, dataname, data_confs[dataname], grimoire_generator=grimoire_generator, filter_out_contains=[], seed=SEED)
expt_grimoire_classify(
model, dataname, data_confs[dataname], grimoire_generator=grimoire_generator, filter_out_contains=[], seed=SEED)