Repository navigation
Expand file tree
/
Copy patheval.py
More file actions
255 lines (215 loc) · 9.2 KB
/
Copy patheval.py
File metadata and controls
255 lines (215 loc) · 9.2 KB
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
"""
This file defines the base class for evals.
"""
import abc
import asyncio
import logging
import os
import random
from multiprocessing.pool import ThreadPool
from pathlib import Path
from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple, Union
from tqdm import tqdm
from evals.api import CompletionFn
from .data import get_jsonl
from .record import RecorderBase
from .registry import Registry
from .solvers.solver import Solver
from .solvers.utils import maybe_wrap_with_compl_fn, maybe_wrap_with_solver
logger = logging.getLogger(__name__)
SHUFFLE_SEED = 123
_MAX_SAMPLES = None
def _index_samples(samples: List[Any]) -> List[Tuple[Any, int]]:
"""Shuffle `samples` and pair each sample with its index."""
indices = list(range(len(samples)))
random.Random(SHUFFLE_SEED).shuffle(indices)
if _MAX_SAMPLES is not None:
indices = indices[:_MAX_SAMPLES]
logger.info(f"Evaluating {len(indices)} samples")
work_items = [(samples[i], i) for i in indices]
return work_items
def set_max_samples(max_samples: int):
global _MAX_SAMPLES
_MAX_SAMPLES = max_samples
class Eval(abc.ABC):
"""
Evaluation classes generally should override two methods:
`eval_sample`: Takes in a test sample and a random number generator and
records the metrics of interest.
`run`: Takes in a recorder and runs the evaluation. Generally, most `run`
methods will follow this same pattern: loading the data, calling
`eval_all_samples`, and aggregating the recorded results.
"""
def __init__(
self,
completion_fns: list[Union[CompletionFn, Solver]],
eval_registry_path: Path,
seed: int = 20220722,
name: str = "no_name_eval.default",
registry: Optional[Registry] = None,
samples_jsonl: Optional[str] = None,
):
splits = name.split(".")
if len(splits) < 2:
raise ValueError(f"Eval name must at least have <base_eval>.<split>. Got name {name}")
self.completion_fns = [maybe_wrap_with_compl_fn(fn) for fn in completion_fns]
self.eval_registry_path = eval_registry_path
self.seed = seed
self.name = name
self.registry = registry or Registry()
self.samples_jsonl = samples_jsonl
@abc.abstractmethod
def eval_sample(self, sample: Any, rng: random.Random):
raise NotImplementedError()
@property
def completion_fn(self) -> CompletionFn:
"""Helper for more ergonomic access to a single CompletionFn."""
return self.completion_fns[0]
@abc.abstractmethod
def run(self, recorder: RecorderBase) -> Dict[str, float]:
"""Run the evaluation with the corresponding recorder."""
raise NotImplementedError()
async def async_eval_all_samples(
self,
eval_fn: Callable[[Tuple[Any, int]], Awaitable[Tuple[int, Any]]],
samples: List[Any],
concurrency: int = 32,
show_progress: bool = True,
**_kwargs: Any,
):
work_items = _index_samples(samples)
semaphore = asyncio.Semaphore(concurrency)
async def eval_fn_with_semaphore(args):
async with semaphore:
return await eval_fn(args)
futures = [asyncio.ensure_future(eval_fn_with_semaphore(args)) for args in work_items]
for future in tqdm(
asyncio.as_completed(futures), total=len(samples), disable=not show_progress
):
await future
def eval_all_samples(
self,
recorder: RecorderBase,
samples,
show_progress=True,
record_raw_sample=True,
**_kwargs: Any,
):
"""
Evaluate all provided samples in parallel.
"""
work_items = _index_samples(samples)
threads = int(os.environ.get("EVALS_THREADS", "10"))
show_progress = bool(os.environ.get("EVALS_SHOW_EVAL_PROGRESS", show_progress))
def eval_sample(args):
"""
Evaluate a single sample.
"""
sample, idx = args
base_name, split = self.name.split(".")[0:2]
sample_id = f"{base_name}.{split}.{idx}"
with recorder.as_default_recorder(sample_id):
seed = f"{sample_id}:{self.seed}".encode("utf-8")
rng = random.Random(seed)
return idx, self.eval_sample(sample, rng)
with ThreadPool(threads) as pool:
if os.environ.get("EVALS_SEQUENTIAL", "0") in {"1", "true", "yes"}:
logger.info("R
8000
unning in sequential mode!")
iter = map(eval_sample, work_items)
else:
logger.info(f"Running in threaded mode with {threads} threads!")
iter = pool.imap_unordered(eval_sample, work_items)
idx_and_result = list(tqdm(iter, total=len(work_items), disable=not show_progress))
return [r for _, r in sorted(idx_and_result)]
def get_samples(self):
if self.samples_jsonl is None:
raise ValueError(
"To use `get_samples`, you must provide a `samples_jsonl` path." "Got `None`."
)
samples_path = self._get_samples_path()
return get_jsonl(samples_path.as_posix())
def _get_samples_path(self) -> Path:
return self._prefix_registry_path(self.samples_jsonl)
def _prefix_registry_path(self, data_path: str) -> Path:
if os.path.isfile(data_path):
return Path(data_path)
return self.eval_registry_path / "data" / data_path
class SolverEval(Eval):
"""
Compared to Eval, SolverEval supports a single completion_fn which must be
a `Solver` type (see solvers/solver.py). The Solver is what we evaluate,
and Eval code should interact with the Solver instead of the CompletionFn
directly. A new Solver is created for each sample, and the Solver is passed
to eval_sample. This allows Solvers to be stateful (e.g. have a memory)
without interfering with other samples.
Otherwise, this is the same as Eval and requires the same methods to be
implemented:
`eval_sample`: Takes in a Solver, a test sample, and a random number
generator and records the metrics of interest.
`run`: Takes in a recorder and runs the evaluation. Generally, most `run`
methods will follow this same pattern: loading the data, calling
`eval_all_samples`, and aggregating the recorded results.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
assert (
len(self.completion_fns) == 1
), f"{type(self).__name__} supports exactly one completion_fn, got {len(self.completion_fns)}."
# Technically, instead of arg `completion_fns: list[CompletionFn]` we
# should just have `solver: Solver` but we keep the args unchanged for
# compatibility with the existing codebase.
self._solver = maybe_wrap_with_solver(self.completion_fns[0])
@abc.abstractmethod
def eval_sample(self, solver: Solver, sample: Any, rng: random.Random) -> None:
raise NotImplementedError()
def eval_all_samples(
self,
recorder: RecorderBase,
samples,
show_progress=True,
**_kwargs: Any,
):
"""
Evaluate all provided samples in parallel.
"""
work_items = _index_samples(samples)
threads = int(os.environ.get("EVALS_THREADS", "10"))
show_progress = bool(os.environ.get("EVALS_SHOW_EVAL_PROGRESS", show_progress))
def eval_sample(args):
"""
Evaluate a single sample.
"""
sample, idx = args
base_name, split = self.name.split(".")[0:2]
sample_id = f"{base_name}.{split}.{idx}"
with recorder.as_default_recorder(sample_id):
seed = f"{sample_id}:{self.seed}".encode("utf-8")
rng = random.Random(seed)
per_sample_solver = self._solver.copy()
return idx, self.eval_sample(per_sample_solver, sample, rng)
with ThreadPool(threads) as pool:
if os.environ.get("EVALS_SEQUENTIAL", "0") in {"1", "true", "yes"}:
logger.info("Running in sequential mode!")
iter = map(eval_sample, work_items)
else:
logger.info(f"Running in threaded mode with {threads} threads!")
iter = pool.imap_unordered(eval_sample, work_items)
idx_and_result = []
try:
for result in tqdm(iter, total=len(work_items), disable=not show_progress):
idx_and_result.append(result)
except KeyboardInterrupt:
# "Gentle interrupt" allows us to stop early and still get results
gentle_interrupt = os.environ.get("EVALS_GENTLE_INTERRUPT", "0") in {
"1",
"true",
"yes",
}
if gentle_interrupt:
logger.info("Evaluation stopped because of KeyboardInterrupt")
logger.info(
f"Report will be based on {len(idx_and_result)} out of the planned {len(work_items)} samples"
)
else:
raise
return [r for _, r in sorted(idx_and_result)]