-
Notifications
You must be signed in to change notification settings - Fork 1
/
experiments.py
333 lines (287 loc) · 10.4 KB
/
experiments.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
# Copyright (c) 2023 Graphcore Ltd. All rights reserved.
"""High-level experiment interface, supporting multiple tasks, models & logging."""
import dataclasses
import datetime
import logging
import multiprocessing
import multiprocessing.pool
import time
import traceback
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Union, cast
import torch
import tqdm
import wandb
from transformers import PreTrainedModel
from transformers.models.gemma.modeling_gemma import GemmaForCausalLM
from transformers.models.gpt_neox.modeling_gpt_neox import GPTNeoXForCausalLM
from transformers.models.llama.modeling_llama import LlamaForCausalLM
from transformers.models.mistral.modeling_mistral import MistralForCausalLM
from . import eval_adapter, utility
from .methods import ann_attention, eviction_attention, sparse_attention
from .models import pipelined_models
from .tasks import bpc, needle, qa, repetition, summarisation
# A dictionary of code changes that may affect the numbers, which is always
# logged alongside experiment results.
CODE_CHANGES: Dict[str, Any] = {
"ann-local-token-for-free": True,
"repetition-ignore-leading-space": True,
"forced-sample-no-specials": True,
"attn-implementation-eager": True,
"attn-scale-queries-before-matmul": True,
}
WANDB_PROJECT = "sparse-attention"
WANDB_URL = "https://wandb.sourcevertex.net"
logger = logging.getLogger(__name__)
# Configuration
TASKS = (
"triviaqa",
"squad",
"squad_train",
"cnn_dailymail",
"wikitext_bpc",
"repetition",
"needle",
)
MODELS = (GPTNeoXForCausalLM, LlamaForCausalLM, MistralForCausalLM, GemmaForCausalLM)
@dataclass
class Task:
name: str # TASKS
shots: int
samples: int
confusion_contexts: int
@dataclass
class Sparsity:
name: str # method name from SparsityMethods
# additional keys are passed to the sparsity method
def __init__(self, name: str, **kwargs: Any):
self.name = name
self.__dict__.update(kwargs)
def __str__(self) -> str:
return str(self.__dict__)
@dataclass
class Execution:
device: str
dtype: str
batch_size: int
pipeline_stages: int
wandb: Union[bool, str] # False | True | "offline"
@classmethod
def auto(cls, batch_size: Optional[int] = None) -> "Execution":
device = "cuda" if torch.cuda.is_available() else "cpu"
return cls(
device=device,
dtype=dict(cpu="float32", cuda="float16")[device],
batch_size=batch_size or dict(cpu=10, cuda=5)[device],
pipeline_stages=1,
wandb=True,
)
@dataclass
class Experiment:
name: str
task: Task
model: str # e.g. EleutherAI/pythia-1b
sparsity: Sparsity
execution: Execution
# This shouldn't be specified directly, and is set via `CODE_CHANGES`
code_changes: Dict[str, Any] = dataclasses.field(
default_factory=lambda: CODE_CHANGES.copy()
)
def to_dict(self) -> Dict[str, Any]:
def convert(obj: Any) -> Any:
if isinstance(obj, dict):
return {k: convert(v) for k, v in obj.items()}
if hasattr(obj, "__dict__"):
return convert(obj.__dict__)
return obj
return cast(Dict[str, Any], convert(self))
Outcome = Dict[str, Any]
# Method
class SparsityMethods:
@classmethod
def apply(cls, sparsity: Sparsity, model: PreTrainedModel) -> PreTrainedModel:
method = getattr(cls, sparsity.name)
return method(
model, **{k: v for k, v in sparsity.__dict__.items() if k != "name"}
)
@staticmethod
def dense(model: PreTrainedModel) -> PreTrainedModel:
return model
@staticmethod
def sparse_v(model: PreTrainedModel, **settings: Any) -> PreTrainedModel:
assert isinstance(model, MODELS)
return sparse_attention.convert(
model, sparse_attention.SparseSettings(**settings)
)
@staticmethod
def local(model: PreTrainedModel, **settings: Any) -> PreTrainedModel:
assert isinstance(model, MODELS)
return sparse_attention.convert(
model, sparse_attention.LocalSettings(**settings)
)
@staticmethod
def eviction(model: PreTrainedModel, **settings: Any) -> PreTrainedModel:
assert isinstance(model, MODELS)
return eviction_attention.convert(
model, eviction_attention.Settings(**settings)
)
@staticmethod
def ann(model: PreTrainedModel, **settings: Any) -> PreTrainedModel:
assert isinstance(model, MODELS)
return ann_attention.convert(model, ann_attention.Settings(**settings))
# Running
def _evaluate(
task: Task, adapter: eval_adapter.Adapter, batch_size: int, progress: bool
) -> Dict[str, Any]:
if task.name in ["triviaqa", "squad", "squad_train"]:
if task.name == "triviaqa":
assert task.confusion_contexts == 0
data = qa.TriviaQA.data(context="wiki")
if task.name == "squad":
data = qa.SQuAD.data(confusion_contexts=task.confusion_contexts)
if task.name == "squad_train":
data = qa.SQuAD.data(
part="train", confusion_contexts=task.confusion_contexts
)
examples = [
qa.add_few_shot_prompt(
data[i],
k=task.shots,
prompt_template=qa.get_default_prompt_template(
adapter.model.config._name_or_path, task.shots
),
)
for i in range(task.samples)
]
evaluate_fn: Any = qa.evaluate
elif task.name == "cnn_dailymail":
assert task.shots == 0 and task.confusion_contexts == 0
data = summarisation.CnnDailymail.data()
examples = [data[i] for i in range(task.samples)]
evaluate_fn = summarisation.evaluate
elif task.name == "wikitext_bpc":
assert task.shots == 0 and task.confusion_contexts == 0
data = bpc.WikiText.data()
examples = [data[i] for i in range(task.samples)]
evaluate_fn = bpc.evaluate
elif task.name == "repetition":
assert task.shots == 0 and task.confusion_contexts == 0
data = repetition.Shakespeare.data()
examples = [data[i] for i in range(task.samples)]
evaluate_fn = repetition.evaluate
elif task.name == "needle":
assert task.shots == 0 and task.confusion_contexts == 0
examples = list(
needle.Dataset.data(
adapter.tokenizer,
lengths=needle.get_default_lengths(adapter.max_length),
depth_steps=task.samples,
)
)
evaluate_fn = needle.evaluate
else:
raise ValueError(f"Task {task.name} not found")
results = list(
evaluate_fn(
adapter=adapter,
examples=examples,
batch_size=batch_size,
progress=progress,
)
)
return dict(
results=results,
count=len(results),
**{
k: sum(x[k] for x in results) / len(results)
for k in [
"prefill_length",
"reference_length",
"match",
"rougeL",
"bpc",
"match_length_char",
]
if k in results[0]
},
)
def run_one(xp: Experiment, progress: bool = True) -> Outcome:
"""Run a single experiment, optionally logging to wandb."""
if xp.execution.wandb:
mode = "offline" if xp.execution.wandb == "offline" else "online"
if mode == "online" and wandb.api.api_url != WANDB_URL:
mode = "offline"
logger.warning(
f"Wandb not logged in to {WANDB_URL}; running in offline mode"
)
wandb.init(
config=xp.to_dict(),
mode=mode,
entity="research",
project=WANDB_PROJECT,
reinit=True,
)
adapter = eval_adapter.Adapter.from_pretrained(
xp.model, dtype=getattr(torch, xp.execution.dtype)
)
if xp.execution.pipeline_stages > 1:
adapter.model = pipelined_models.pipeline_model(
adapter.model, xp.execution.pipeline_stages
)
else:
adapter.model.to(torch.device(xp.execution.device))
adapter.model = SparsityMethods.apply(xp.sparsity, adapter.model)
out = {}
out["parameters"] = sum(p.nelement() for p in adapter.model.parameters())
out["model_config"] = adapter.model.config.to_diff_dict()
t0 = time.time()
try:
out.update(
_evaluate(xp.task, adapter, xp.execution.batch_size, progress=progress)
)
except Exception as error:
out["error"] = repr(error)
out["backtrace"] = traceback.format_exc()
logger.error(f"Error: {error}")
logger.error(traceback.format_exc())
out["duration"] = time.time() - t0
if xp.execution.wandb:
wandb.summary.update(out)
assert wandb.run is not None
out["wandb"] = dict(
id=wandb.run.id, name=wandb.run.name, url=wandb.run.get_url()
)
wandb.finish(exit_code=1 if "error" in out else 0)
return dict(**xp.to_dict(), **out)
def _run_many_task(xp: Experiment) -> Dict[str, Any]:
"""Run an experiment in a non-daemonic subprocess (for sake of wandb)."""
queue: "multiprocessing.Queue[Dict[str, Any]]" = multiprocessing.Queue()
num_threads = torch.get_num_threads()
def _run() -> None:
torch.set_num_threads(num_threads)
queue.put(run_one(xp, progress=False))
p = multiprocessing.get_context("fork").Process(target=_run, daemon=False)
p.start()
try:
return queue.get()
finally:
p.join()
def run_many(
xps: List[Experiment], n_workers: int = 1, out: Optional[Path] = None
) -> None:
"""Run multiple experiments, optionally as a multiprocess sweep."""
if out is None:
out = (
Path("out") / datetime.datetime.now().isoformat(sep="/", timespec="seconds")
).with_suffix(".jsonl")
with utility.jsonlines_writer(out) as writer:
if n_workers >= 2:
with multiprocessing.pool.ThreadPool(n_workers) as pool:
for result in tqdm.tqdm(
pool.map(_run_many_task, xps), total=len(xps), desc="experiments"
):
writer(result)
else:
for xp in tqdm.tqdm(xps, desc="experiments"):
writer(run_one(xp, progress=False))