-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_baselines.py
505 lines (441 loc) · 14.8 KB
/
run_baselines.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"""
Run all baselines on all tasks and save the results to a Pandas dataframe.
"""
import argparse
import json
import inspect
import logging
import numpy as np
import pandas as pd
from collections import defaultdict
from pathlib import Path
from cik_benchmark.baselines.direct_prompt import DirectPrompt
from cik_benchmark.baselines.lag_llama import lag_llama
from cik_benchmark.baselines.chronos import ChronosForecaster
from cik_benchmark.baselines.moirai import MoiraiForecaster
from cik_benchmark.baselines.llm_processes import LLMPForecaster
from cik_benchmark.baselines.timellm import TimeLLMForecaster
from cik_benchmark.baselines.unitime import UniTimeForecaster
from cik_benchmark.baselines.timegen import timegen1
from cik_benchmark.baselines.naive import oracle_baseline, random_baseline
from cik_benchmark.baselines.statsmodels import (
ExponentialSmoothingForecaster,
)
from cik_benchmark.baselines.r_forecast import R_ETS, R_Arima
from cik_benchmark.evaluation import evaluate_all_tasks
from cik_benchmark.config import RESULT_CACHE_PATH
logging.basicConfig(level=logging.INFO)
pd.set_option("display.max_columns", None)
pd.set_option("display.max_rows", None)
def experiment_naive(
n_samples, output_folder, max_parallel=None, skip_cache_miss=False
):
"""
Naive baselines (random and oracle)
"""
results = []
results.append(
(
"random",
evaluate_all_tasks(
random_baseline,
n_samples=n_samples,
output_folder=f"{output_folder}/random/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
)
)
results.append(
(
"oracle",
evaluate_all_tasks(
oracle_baseline,
n_samples=n_samples,
output_folder=f"{output_folder}/oracle/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
)
)
return results, {}
def experiment_lag_llama(
n_samples, output_folder, max_parallel=10, skip_cache_miss=False
):
"""
Lag LLAMA baseline
"""
results = evaluate_all_tasks(
lag_llama,
n_samples=n_samples,
output_folder=f"{output_folder}/lag_llama/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
)
return results, {}
def experiment_chronos(
model_size, n_samples, output_folder, max_parallel=1, skip_cache_miss=False
):
"""
Chronos baselines
"""
results = evaluate_all_tasks(
ChronosForecaster(model_size=model_size),
n_samples=n_samples,
output_folder=f"{output_folder}/chronos/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
)
return results, {}
def experiment_moirai(
model_size, n_samples, output_folder, max_parallel=1, skip_cache_miss=False
):
"""
Moirai baselines
"""
results = evaluate_all_tasks(
MoiraiForecaster(model_size=model_size),
n_samples=n_samples,
output_folder=f"{output_folder}/moirai/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
)
return results, {}
def experiment_statsmodels(
n_samples, output_folder, max_parallel=None, skip_cache_miss=False
):
"""
Statsmodels baselines (Exponential Smoothing)
"""
return (
evaluate_all_tasks(
ExponentialSmoothingForecaster(),
n_samples=n_samples,
output_folder=f"{output_folder}/exp_smoothing/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
{},
)
def experiment_r_ets(
n_samples, output_folder, max_parallel=None, skip_cache_miss=False
):
"""
Baseline using the R "forecast" package: ETS
"""
return (
evaluate_all_tasks(
R_ETS(),
n_samples=n_samples,
output_folder=f"{output_folder}/r_ets/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
{},
)
def experiment_r_arima(
n_samples, output_folder, max_parallel=None, skip_cache_miss=False
):
"""
Baseline using the R "forecast" package: Arima
"""
return (
evaluate_all_tasks(
R_Arima(),
n_samples=n_samples,
output_folder=f"{output_folder}/r_arima/",
max_parallel=1, # Hardcoded as it's buggy with None
skip_cache_miss=skip_cache_miss,
),
{},
)
def experiment_directprompt(
llm,
use_context,
n_samples,
output_folder,
max_parallel=1,
skip_cache_miss=False,
batch_size=None,
batch_size_on_retry=5,
n_retries=3,
temperature=1.0,
):
"""
DirectPrompt baselines
"""
# Costs per 1000 tokens
openai_costs = {
"gpt-4o": {"input": 0.005, "output": 0.015}, # Same price Azure and OpenAI
"gpt-35-turbo": {"input": 0.002, "output": 0.002},
"gpt-3.5-turbo": {"input": 0.003, "output": 0.006}, # OpenAI API
"gpt-4o-mini": {"input": 0.00015, "output": 0.0006}, # OpenAI API
"llama-3.1-405b": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-3.1-405b-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-2-7B": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-2-70B": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-3-8B": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-3-8B-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-3-70B": {"input": 0.0, "output": 0.0}, # Toolkit
"llama-3-70B-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
"mixtral-8x7B": {"input": 0.0, "output": 0.0}, # Toolkit
"mixtral-8x7B-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
"phi-3-mini-128k-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
"gemma-2-9B": {"input": 0.0, "output": 0.0}, # Toolkit
"gemma-2-9B-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
"gemma-2-27B": {"input": 0.0, "output": 0.0}, # Toolkit
"gemma-2-27B-instruct": {"input": 0.0, "output": 0.0}, # Toolkit
}
if not llm.startswith("openrouter-") and llm not in openai_costs:
raise ValueError(f"Invalid model: {llm} -- Not in cost dictionary")
dp_forecaster = DirectPrompt(
model=llm,
use_context=use_context,
token_cost=openai_costs[llm] if not llm.startswith("openrouter-") else None,
batch_size=batch_size,
batch_size_on_retry=batch_size_on_retry,
n_retries=n_retries,
temperature=temperature,
dry_run=skip_cache_miss,
)
results = evaluate_all_tasks(
dp_forecaster,
n_samples=n_samples,
output_folder=f"{output_folder}/{dp_forecaster.cache_name}",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
)
total_cost = dp_forecaster.total_cost
del dp_forecaster
return results, {"total_cost": total_cost}
def experiment_timellm(
use_context,
dataset,
pred_len,
n_samples,
output_folder,
max_parallel=1,
skip_cache_miss=False,
):
"""
TimeLLM baselines
Doesn't use n_samples as it is not implemented in the TimeLLMForecaster
"""
timellm_forecaster = TimeLLMForecaster(
use_context=use_context,
dataset=dataset,
pred_len=pred_len,
dry_run=skip_cache_miss,
)
return (
evaluate_all_tasks(
timellm_forecaster,
n_samples=n_samples,
output_folder=f"{output_folder}/{timellm_forecaster.cache_name}",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
{},
)
def experiment_unitime(
use_context,
pred_len,
n_samples,
output_folder,
dataset="",
per_dataset_checkpoint=False,
max_parallel=1,
skip_cache_miss=False,
):
"""
TimeLLM baselines
Doesn't use n_samples as it is not implemented in the TimeLLMForecaster
"""
unitime_forecaster = UniTimeForecaster(
use_context=use_context,
dataset=dataset,
pred_len=pred_len,
per_dataset_checkpoint=per_dataset_checkpoint,
dry_run=skip_cache_miss,
)
return (
evaluate_all_tasks(
unitime_forecaster,
n_samples=n_samples,
output_folder=f"{output_folder}/{unitime_forecaster.cache_name}",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
{},
)
def experiment_timegen1(
n_samples, output_folder, max_parallel=10, skip_cache_miss=False
):
"""
Nixtla TimeGEN-1 baseline
"""
results = evaluate_all_tasks(
timegen1,
n_samples=n_samples,
output_folder=f"{output_folder}/timegen1/",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
)
return results, {}
def experiment_llmp(
llm, use_context, n_samples, output_folder, max_parallel=1, skip_cache_miss=False
):
"""
LLM Process baselines
"""
llmp_forecaster = LLMPForecaster(
llm_type=llm, use_context=use_context, dry_run=skip_cache_miss
)
return (
evaluate_all_tasks(
llmp_forecaster,
n_samples=n_samples,
output_folder=f"{output_folder}/{llmp_forecaster.cache_name}",
max_parallel=max_parallel,
skip_cache_miss=skip_cache_miss,
),
{},
)
def compile_results(results, cap=None):
# Compile results into Pandas dataframe
errors = defaultdict(list)
missing = defaultdict(list)
results_ = {
"Task": [task for task in list(results.values())[0]],
}
for method, method_results in results.items():
_method_results = []
for task in results_["Task"]:
task_results = []
for seed_res in method_results[task]:
# Keep track of exceptions and missing results
seed_res["task"] = task
if "error" in seed_res:
if "cache miss" in seed_res["error"].lower():
missing[method].append(seed_res)
else:
errors[method].append(seed_res)
else:
if cap == None:
score = seed_res["score"]
else:
score = min(seed_res["score"], cap)
task_results.append(score)
mean = np.mean(task_results)
std = np.std(task_results, ddof=1)
stderr = std / np.sqrt(len(task_results))
_method_results.append(f"{mean.round(3): .3f} ± {stderr.round(3) :.3f}")
results_[method] = _method_results
results = pd.DataFrame(results_).sort_values("Task").set_index("Task")
del results_
return results, missing, errors
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--n-samples",
type=int,
default=25,
help="Number of samples to evaluate",
)
parser.add_argument(
"--output",
type=str,
default="./benchmark_results/",
help="Output folder for results",
)
parser.add_argument(
"--exp-spec",
type=str,
help="Experiment specification file",
)
parser.add_argument(
"--list-exps",
action="store_true",
help="List available experiments and their parameters",
)
parser.add_argument(
"--skip-cache-miss",
action="store_true",
help="Skip tasks that have not already been computed",
)
parser.add_argument(
"--cap",
type=float,
help="Cap value to cap each instance's metric",
)
args = parser.parse_args()
output_folder = Path(args.output)
# List all available experiments
if args.list_exps:
print("Available experiments:")
# Filter globals to only include functions that start with "experiment_"
exp_funcs = [
v
for k, v in globals().items()
if k.startswith("experiment_") and inspect.isfunction(v)
]
# Print each experiment function name with a list of its parameters
for func in exp_funcs:
# Get the function signature
signature = inspect.signature(func)
# List of parameters excluding 'n_samples', 'output_folder', and 'skip_cache_miss'
params = [
name
for name, param in signature.parameters.items()
if name not in ["n_samples", "output_folder", "skip_cache_miss"]
]
# Print the function name and its parameters
print(f"\t{func.__name__}({', '.join(params)})")
exit()
# Run all experiments
all_results = {}
extra_infos = {}
# ... load specifications
with open(args.exp_spec, "r") as f:
exp_spec = json.load(f)
# ... run each experiment
for exp in exp_spec:
current_results = {}
print(f"Running experiment: {exp['label']}")
exp_label = exp["label"]
# ... extract configuration
config = {k: v for k, v in exp.items() if k != "method" and k != "label"}
config["n_samples"] = args.n_samples
config["output_folder"] = output_folder / exp_label
config["skip_cache_miss"] = args.skip_cache_miss
print(f"\tConfig: {config}")
# ... do it!
function = globals().get(f"experiment_{exp['method']}")
# ... process results
res, extra_info = function(**config)
if isinstance(res, list):
all_results.update({f"{exp_label}_{k}": v for k, v in res})
current_results.update({f"{exp_label}_{k}": v for k, v in res})
else:
all_results[exp_label] = res
current_results[exp_label] = res
extra_infos[exp_label] = extra_info
# Compile results
current_results, missing, errors = compile_results(
current_results, cap=args.cap
)
print(current_results)
print("Number of missing results:", {k: len(v) for k, v in missing.items()})
print("Number of errors:", {k: len(v) for k, v in errors.items()})
# Save results to CSV. Note: Saved in output_folder / exp_label, not output_folder as it is exp-specific result
filename = "results.csv" if not args.cap else f"results-cap-{args.cap}.csv"
print(f"Saving results to {output_folder/exp_label}/{filename}")
current_results.to_csv(output_folder / exp_label / filename)
print(f"Saving missing results to {output_folder/exp_label}/missing.json")
with open(output_folder / exp_label / "missing.json", "w") as f:
json.dump(missing, f)
print(f"Saving errors to {output_folder/exp_label}/errors.json")
with open(output_folder / exp_label / "errors.json", "w") as f:
json.dump(errors, f)
if __name__ == "__main__":
main()