-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain.py
468 lines (411 loc) · 14.7 KB
/
train.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
import itertools
import json
import math
import os
import random
from collections import defaultdict
from collections.abc import Sized
from itertools import chain, repeat
from pathlib import Path
from typing import (
TYPE_CHECKING,
List,
Optional,
Tuple,
)
import spacy
import torch
from accelerate import Accelerator
from confit import Cli
from confit.utils.random import set_seed
from rich_logger import RichTablePrinter
from torch.utils.data import DataLoader
from tqdm import tqdm
from eds_pseudo.adapter import PseudoReader
from eds_pseudo.scorer import PseudoScorer
from edsnlp.core.pipeline import Pipeline
from edsnlp.core.registries import registry
from edsnlp.optimization import LinearSchedule, ScheduledOptimizer
from edsnlp.pipes.trainable.embeddings.transformer.transformer import Transformer
from edsnlp.utils.collections import batchify
from edsnlp.utils.typing import AsList
app = Cli(pretty_exceptions_show_locals=False)
BASE_DIR = Path.cwd()
LOGGER_FIELDS = {
"step": {},
"(.*loss)": {
"goal": "lower_is_better",
"format": "{:.2e}",
"goal_wait": 2,
"name": r"\1",
},
"Token Scores / micro / (.*)": {
"goal": "higher_is_better",
"format": "{:.2%}",
"goal_wait": 2,
},
"Token Scores / micro / Precision": {"name": "p"},
"Token Scores / micro / Recall": {"name": "r"},
"Token Scores / micro / F1": {"name": "f1"},
"Token Scores / micro / Redact": {"name": "redact"},
"Token Scores / micro / Redact Full": {"name": "full"},
"lr": {"format": "{:.2e}"},
"labels": {"format": "{:.2f}"},
}
class BatchSizeArg:
"""
Batch size argument validator / caster for confit/pydantic
Examples
--------
```python
def fn(batch_size: BatchSizeArg):
return batch_size
print(fn("10 samples"))
# Out: (10, "samples")
print(fn("10 words"))
# Out: (10, "words")
print(fn(10))
# Out: (10, "samples")
```
"""
@classmethod
def validate(cls, value, config=None):
value = str(value)
parts = value.split()
num = int(parts[0])
unit = parts[1] if len(parts) == 2 else "samples"
if len(parts) == 2 and str(num) == parts[0] and unit in ("words", "samples"):
return num, unit
raise Exception(f"Invalid batch size: {value}, must be <int> samples|words")
@classmethod
def __get_validators__(cls):
yield cls.validate
if TYPE_CHECKING:
BatchSizeArg = Tuple[int, str] # noqa: F811
class LengthSortedBatchSampler:
"""
Batch sampler that sorts the dataset by length and then batches
sequences of similar length together. This is useful for transformer
models that can then be padded more efficiently.
Parameters
----------
dataset: Iterable
The dataset to sample from (can be a generator or a fixed size collection)
batch_size: int
The batch size
batch_unit: str
The unit of the batch size, either "words" or "samples"
noise: int
The amount of noise to add to the sequence length before sorting
(uniformly sampled in [-noise, noise])
drop_last: bool
Whether to drop the last batch if it is smaller than the batch size
buffer_size: Optional[int]
The size of the buffer to use to shuffle the batches. If None, the buffer
will be approximately the size of the dataset.
"""
def __init__(
self,
dataset,
batch_size: int,
batch_unit: str,
noise=1,
drop_last=True,
buffer_size: Optional[int] = None,
):
self.dataset = dataset
self.batch_size = batch_size
self.batch_unit = batch_unit
self.noise = noise
self.drop_last = drop_last
self.buffer_size = buffer_size
def __iter__(self):
# Shuffle the dataset
if self.batch_unit == "words":
def sample_len(idx, noise=True):
count = sum(
len(x)
for x in next(
v
for k, v in self.dataset[idx].items()
if k.endswith("word_lengths")
)
)
if not noise:
return count
return count + random.randint(-self.noise, self.noise)
else:
sample_len = lambda idx, noise=True: 1 # noqa: E731
def make_batches():
total = 0
batch = []
for seq_size, idx in sorted_sequences:
if total and total + seq_size > self.batch_size:
yield batch
total = 0
batch = []
total += seq_size
batch.append(idx)
# Shuffle the batches in buffer that contain approximately
# the full dataset to add more randomness
if isinstance(self.dataset, Sized):
total_count = sum(sample_len(i, False) for i in range(len(self.dataset)))
assert (
isinstance(self.dataset, Sized) or self.buffer_size is not None
), "Dataset must have a length or buffer_size must be specified"
buffer_size = self.buffer_size or math.ceil(total_count / self.batch_size)
# Sort sequences by length +- some noise
sorted_sequences = chain.from_iterable(
sorted((sample_len(i), i) for i in range(len(self.dataset)))
for _ in repeat(None)
)
# Batch sorted sequences
batches = make_batches()
buffers = batchify(batches, buffer_size)
for buffer in buffers:
random.shuffle(buffer)
yield from buffer
class SubBatchCollater:
"""
Collater that splits batches into sub-batches of a maximum size
Parameters
----------
nlp: Pipeline
The pipeline object
embedding: Transformer
The transformer embedding pipe
grad_accumulation_max_tokens: int
The maximum number of tokens (word pieces) to accumulate in a single batch
"""
def __init__(self, nlp, embedding, grad_accumulation_max_tokens):
self.nlp = nlp
self.embedding: Transformer = embedding
self.grad_accumulation_max_tokens = grad_accumulation_max_tokens
def __call__(self, seq):
total = 0
mini_batches = [[]]
for sample_features in seq:
num_tokens = sum(
math.ceil(len(p) / self.embedding.stride) * self.embedding.window
for key in sample_features
if key.endswith("/input_ids")
for p in sample_features[key]
)
if total + num_tokens > self.grad_accumulation_max_tokens:
print(
f"Mini batch size was becoming too large: {total} > "
f"{self.grad_accumulation_max_tokens} so it was split"
)
total = 0
mini_batches.append([])
total += num_tokens
mini_batches[-1].append(sample_features)
return [self.nlp.collate(b) for b in mini_batches if len(b)]
@app.command(name="train", registry=registry)
def train(
*,
nlp: Pipeline,
train_data: AsList[PseudoReader],
val_data: PseudoReader,
seed: int = 42,
data_seed: int = 42,
max_steps: int = 1000,
batch_size: BatchSizeArg = 2000,
embedding_lr: float = 5e-5,
task_lr: float = 3e-4,
validation_interval: int = 10,
grad_max_norm: float = 5.0,
grad_accumulation_max_tokens: int = 96 * 128,
scorer: PseudoScorer,
output_dir: Optional[Path] = None,
cpu: bool = False,
):
"""
Train a model on a dataset.
Parameters
----------
nlp: Pipeline
The edsnlp pipeline object
train_data: AsList[PseudoReader]
The training data, can be a PseudoReader or a list of PseudoReaders
val_data: PseudoReader
The validation data
seed: int
The seed to use for random number generators when initializing the model
data_seed: int
The seed to use for random number generators when shuffling the data
max_steps: int
The maximum number of training steps
batch_size: BatchSizeArg
The batch size to use for training, support the following units:
- <int> samples: the number of samples per batch
- <int> words: the number of words per batch
embedding_lr: float
The learning rate for the transformer embedding
task_lr: float
The learning rate for the task (NER) head
validation_interval: int
The number of steps between each validation
grad_max_norm: float
The maximum gradient norm to use for gradient clipping
grad_accumulation_max_tokens: int
The maximum number of tokens to accumulate in a single batch
scorer: PseudoScorer
The scorer object to use for validation
output_dir: Optional[Path]
The output directory to save the model and training metrics.
cpu: bool
Whether to force the training to run on CPU (useful for M1 chips for which
all the ops of transformers are not yet supported)
Returns
-------
Pipeline
The model (trained in place). The artifacts are saved in `artifacts`
and `artifacts/train_metrics.json`.
"""
trf_pipe = next(
module
for name, pipe in nlp.torch_components()
for module_name, module in pipe.named_component_modules()
if isinstance(module, Transformer)
)
if nlp.has_pipe("dates-normalizer"):
nlp.select_pipes(disable=["dates-normalizer"])
output_dir = Path(output_dir or BASE_DIR / "artifacts")
set_seed(seed)
with set_seed(data_seed):
train_docs: List[spacy.tokens.Doc] = list(
chain.from_iterable(td(nlp) for td in train_data)
)
val_docs: List[spacy.tokens.Doc] = list(val_data(nlp))
model_path = output_dir / "model-last"
train_metrics_path = output_dir / "train_metrics.json"
os.makedirs(output_dir, exist_ok=True)
# Initialize pipeline with training documents
nlp.post_init(train_docs)
# Preprocessing training data
print("Preprocessing data")
preprocessed = list(
nlp.preprocess_many(train_docs, supervision=True).set_processing(
show_progress=True
)
)
print(f"TRAINING DATASET SIZE: {len(preprocessed)}")
dataloader = DataLoader(
preprocessed,
batch_sampler=LengthSortedBatchSampler(
preprocessed,
batch_size=batch_size[0],
batch_unit=batch_size[1],
),
collate_fn=SubBatchCollater(
nlp,
trf_pipe,
grad_accumulation_max_tokens=grad_accumulation_max_tokens,
),
)
trained_pipes = nlp.torch_components()
print("Training", ", ".join([name for name, c in trained_pipes]))
trf_params = set(trf_pipe.parameters())
params = set(nlp.parameters())
optimizer = ScheduledOptimizer(
torch.optim.AdamW(
[
{
"params": list(params - trf_params),
"lr": task_lr,
"schedules": [
LinearSchedule(
total_steps=max_steps,
warmup_rate=0.1,
start_value=task_lr,
path="lr",
)
],
},
{
"params": list(trf_params),
"lr": embedding_lr,
"schedules": [
LinearSchedule(
total_steps=max_steps,
warmup_rate=0.1,
start_value=0,
path="lr",
)
],
},
]
)
)
grad_params = {p for group in optimizer.param_groups for p in group["params"]}
print(
"Optimizing:"
+ "".join(
f"\n - {len(group['params'])} params "
f"({sum(p.numel() for p in group['params'])} total)"
for group in optimizer.param_groups
)
)
print(f"Not optimizing {len(params - grad_params)} params")
accelerator = Accelerator(cpu=cpu)
trained_pipes = dict(nlp.torch_components())
print("Device:", accelerator.device)
[dataloader, optimizer, *accelerated_pipes] = accelerator.prepare(
dataloader,
optimizer,
*trained_pipes.values(),
)
trained_pipes = list(zip(trained_pipes.keys(), accelerated_pipes))
del accelerated_pipes
cumulated_data = defaultdict(lambda: 0.0, count=0)
iterator = itertools.chain.from_iterable(itertools.repeat(dataloader))
all_metrics = []
nlp.train(True)
set_seed(seed)
with RichTablePrinter(LOGGER_FIELDS, auto_refresh=False) as logger:
# Training loop
with tqdm(
range(max_steps + 1),
"Training model",
leave=True,
mininterval=5.0,
) as bar:
for step in bar:
if (step % validation_interval) == 0:
metrics = {m["name"]: m["value"] for m in scorer(nlp, val_docs)}
cumulated_data = defaultdict(lambda: 0.0, count=0)
all_metrics.append(
{
"step": step,
"lr": optimizer.param_groups[0]["lr"],
**metrics,
**cumulated_data,
}
)
logger.log_metrics(all_metrics[-1])
train_metrics_path.write_text(json.dumps(all_metrics, indent=2))
nlp.to_disk(model_path)
if step == max_steps:
break
mini_batches = next(iterator)
optimizer.zero_grad()
for mini_batch in mini_batches:
loss = torch.zeros((), device=accelerator.device)
with nlp.cache():
for name, pipe in trained_pipes:
output = pipe.module_forward(mini_batch[name])
if "loss" in output:
loss += output["loss"]
for key, value in output.items():
if key.endswith("loss"):
cumulated_data[key] += float(value)
accelerator.backward(loss)
torch.nn.utils.clip_grad_norm_(
(p for g in optimizer.param_groups for p in g["params"]),
grad_max_norm,
)
optimizer.step()
return nlp
if __name__ == "__main__":
app()