train.py 14.7 KB
Newer Older
Myle Ott's avatar
Myle Ott committed
1
#!/usr/bin/env python3 -u
Sergey Edunov's avatar
Sergey Edunov committed
2
3
4
5
6
7
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree. An additional grant of patent rights
# can be found in the PATENTS file in the same directory.
Myle Ott's avatar
Myle Ott committed
8
9
10
"""
Train a new model on one or across multiple GPUs.
"""
Sergey Edunov's avatar
Sergey Edunov committed
11

12
import collections
Myle Ott's avatar
Myle Ott committed
13
import itertools
14
import math
Myle Ott's avatar
Myle Ott committed
15
import os
16
17
import random

18
import torch
Sergey Edunov's avatar
Sergey Edunov committed
19

Myle Ott's avatar
Myle Ott committed
20
from fairseq import checkpoint_utils, distributed_utils, options, progress_bar, tasks, utils
21
from fairseq.data import iterators
22
23
from fairseq.trainer import Trainer
from fairseq.meters import AverageMeter, StopwatchMeter
Sergey Edunov's avatar
Sergey Edunov committed
24

Myle Ott's avatar
Myle Ott committed
25

26
def main(args):
Myle Ott's avatar
Myle Ott committed
27
    utils.import_user_module(args)
28

29
30
    if args.max_tokens is None:
        args.max_tokens = 6000
31
32
    print(args)

Myle Ott's avatar
Myle Ott committed
33
34
    if torch.cuda.is_available() and not args.cpu:
        torch.cuda.set_device(args.device_id)
35
36
    torch.manual_seed(args.seed)

Myle Ott's avatar
Myle Ott committed
37
38
    # Setup task, e.g., translation, language modeling, etc.
    task = tasks.setup_task(args)
39

Myle Ott's avatar
Myle Ott committed
40
    # Load dataset splits
41
    load_dataset_splits(args, task)
42

Myle Ott's avatar
Myle Ott committed
43
44
45
    # Build model and criterion
    model = task.build_model(args)
    criterion = task.build_criterion(args)
46
    print(model)
47
    print('| model {}, criterion {}'.format(args.arch, criterion.__class__.__name__))
48
49
50
51
    print('| num. model params: {} (num. trained: {})'.format(
        sum(p.numel() for p in model.parameters()),
        sum(p.numel() for p in model.parameters() if p.requires_grad),
    ))
52
53

    # Build trainer
Myle Ott's avatar
Myle Ott committed
54
    trainer = Trainer(args, task, model, criterion)
55
56
57
58
59
60
61
    print('| training on {} GPUs'.format(args.distributed_world_size))
    print('| max tokens per GPU = {} and max sentences per GPU = {}'.format(
        args.max_tokens,
        args.max_sentences,
    ))

    # Initialize dataloader
62
    epoch_itr = task.get_batch_iterator(
Myle Ott's avatar
Myle Ott committed
63
        dataset=task.dataset(args.train_subset),
64
        max_tokens=args.max_tokens,
65
        max_sentences=args.max_sentences,
Myle Ott's avatar
Myle Ott committed
66
67
68
69
        max_positions=utils.resolve_max_positions(
            task.max_positions(),
            model.max_positions(),
        ),
Myle Ott's avatar
Myle Ott committed
70
        ignore_invalid_inputs=True,
71
        required_batch_size_multiple=args.required_batch_size_multiple,
72
73
        seed=args.seed,
        num_shards=args.distributed_world_size,
Myle Ott's avatar
Myle Ott committed
74
        shard_id=args.distributed_rank,
Myle Ott's avatar
Myle Ott committed
75
        num_workers=args.num_workers,
76
77
78
    )

    # Load the latest checkpoint if one is available
Myle Ott's avatar
Myle Ott committed
79
    load_checkpoint(args, trainer, epoch_itr)
80
81
82
83
84
85
86

    # Train until the learning rate gets too small
    max_epoch = args.max_epoch or math.inf
    max_update = args.max_update or math.inf
    lr = trainer.get_lr()
    train_meter = StopwatchMeter()
    train_meter.start()
Myle Ott's avatar
Myle Ott committed
87
    valid_losses = [None]
88
    valid_subsets = args.valid_subset.split(',')
Myle Ott's avatar
Myle Ott committed
89
    while lr > args.min_lr and epoch_itr.epoch < max_epoch and trainer.get_num_updates() < max_update:
90
        # train for one epoch
Myle Ott's avatar
Myle Ott committed
91
        train(args, trainer, task, epoch_itr)
92

Myle Ott's avatar
Myle Ott committed
93
94
        if epoch_itr.epoch % args.validate_interval == 0:
            valid_losses = validate(args, trainer, task, epoch_itr, valid_subsets)
95
96

        # only use first validation loss to update the learning rate
Myle Ott's avatar
Myle Ott committed
97
        lr = trainer.lr_step(epoch_itr.epoch, valid_losses[0])
98
99

        # save checkpoint
Myle Ott's avatar
Myle Ott committed
100
101
        if epoch_itr.epoch % args.save_interval == 0:
            save_checkpoint(args, trainer, epoch_itr, valid_losses[0])
102
103
104
105
    train_meter.stop()
    print('| done training in {:.1f} seconds'.format(train_meter.sum))


Myle Ott's avatar
Myle Ott committed
106
def train(args, trainer, task, epoch_itr):
107
    """Train the model for one epoch."""
108
    # Update parameters every N batches
Myle Ott's avatar
Myle Ott committed
109
110
    update_freq = args.update_freq[epoch_itr.epoch - 1] \
            if epoch_itr.epoch <= len(args.update_freq) else args.update_freq[-1]
Myle Ott's avatar
Myle Ott committed
111
112
113
114
115
116

    # Initialize data iterator
    itr = epoch_itr.next_epoch_itr(
        fix_batches_to_gpus=args.fix_batches_to_gpus,
        shuffle=(epoch_itr.epoch >= args.curriculum),
    )
117
118
119
120
121
    itr = iterators.GroupedIterator(itr, update_freq)
    progress = progress_bar.build_progress_bar(
        args, itr, epoch_itr.epoch, no_progress_bar='simple',
    )

122
    extra_meters = collections.defaultdict(lambda: AverageMeter())
123
    valid_subsets = args.valid_subset.split(',')
124
    max_update = args.max_update or math.inf
125
126
127
    for i, samples in enumerate(progress, start=epoch_itr.iterations_in_epoch):
        log_output = trainer.train_step(samples)
        if log_output is None:
128
129
130
131
132
            continue

        # log mid-epoch stats
        stats = get_training_stats(trainer)
        for k, v in log_output.items():
133
            if k in ['loss', 'nll_loss', 'ntokens', 'nsentences', 'sample_size']:
134
135
136
137
138
139
                continue  # these are already logged above
            if 'loss' in k:
                extra_meters[k].update(v, log_output['sample_size'])
            else:
                extra_meters[k].update(v)
            stats[k] = extra_meters[k].avg
Myle Ott's avatar
Myle Ott committed
140
        progress.log(stats, tag='train', step=stats['num_updates'])
141
142
143
144
145

        # ignore the first mini-batch in words-per-second calculation
        if i == 0:
            trainer.get_meter('wps').reset()

146
        num_updates = trainer.get_num_updates()
147
        if args.save_interval_updates > 0 and num_updates % args.save_interval_updates == 0 and num_updates > 0:
148
            valid_losses = validate(args, trainer, task, epoch_itr, valid_subsets)
Myle Ott's avatar
Myle Ott committed
149
            save_checkpoint(args, trainer, epoch_itr, valid_losses[0])
150
151

        if num_updates >= max_update:
152
153
154
155
156
157
            break

    # log end-of-epoch stats
    stats = get_training_stats(trainer)
    for k, meter in extra_meters.items():
        stats[k] = meter.avg
Myle Ott's avatar
Myle Ott committed
158
    progress.print(stats, tag='train', step=stats['num_updates'])
159

Myle Ott's avatar
Myle Ott committed
160
    # reset training meters
161
162
163
    for k in [
        'train_loss', 'train_nll_loss', 'wps', 'ups', 'wpb', 'bsz', 'gnorm', 'clip',
    ]:
Myle Ott's avatar
Myle Ott committed
164
165
166
167
        meter = trainer.get_meter(k)
        if meter is not None:
            meter.reset()

168
169
170

def get_training_stats(trainer):
    stats = collections.OrderedDict()
Myle Ott's avatar
Myle Ott committed
171
    stats['loss'] = trainer.get_meter('train_loss')
172
    if trainer.get_meter('train_nll_loss').count > 0:
Myle Ott's avatar
Myle Ott committed
173
174
        nll_loss = trainer.get_meter('train_nll_loss')
        stats['nll_loss'] = nll_loss
175
    else:
Myle Ott's avatar
Myle Ott committed
176
177
178
179
180
181
        nll_loss = trainer.get_meter('train_loss')
    stats['ppl'] = get_perplexity(nll_loss.avg)
    stats['wps'] = trainer.get_meter('wps')
    stats['ups'] = trainer.get_meter('ups')
    stats['wpb'] = trainer.get_meter('wpb')
    stats['bsz'] = trainer.get_meter('bsz')
182
183
    stats['num_updates'] = trainer.get_num_updates()
    stats['lr'] = trainer.get_lr()
Myle Ott's avatar
Myle Ott committed
184
185
186
    stats['gnorm'] = trainer.get_meter('gnorm')
    stats['clip'] = trainer.get_meter('clip')
    stats['oom'] = trainer.get_meter('oom')
187
    if trainer.get_meter('loss_scale') is not None:
Myle Ott's avatar
Myle Ott committed
188
        stats['loss_scale'] = trainer.get_meter('loss_scale')
189
    stats['wall'] = round(trainer.get_meter('wall').elapsed_time)
Myle Ott's avatar
Myle Ott committed
190
    stats['train_wall'] = trainer.get_meter('train_wall')
191
192
193
    return stats


Myle Ott's avatar
Myle Ott committed
194
def validate(args, trainer, task, epoch_itr, subsets):
195
196
197
    """Evaluate the model on the validation set(s) and return the losses."""
    valid_losses = []
    for subset in subsets:
Myle Ott's avatar
Myle Ott committed
198
        # Initialize data iterator
199
        itr = task.get_batch_iterator(
Myle Ott's avatar
Myle Ott committed
200
            dataset=task.dataset(subset),
201
202
            max_tokens=args.max_tokens,
            max_sentences=args.max_sentences_valid,
203
204
205
206
            max_positions=utils.resolve_max_positions(
                task.max_positions(),
                trainer.get_model().max_positions(),
            ),
Myle Ott's avatar
Myle Ott committed
207
            ignore_invalid_inputs=args.skip_invalid_size_inputs_valid_test,
208
            required_batch_size_multiple=args.required_batch_size_multiple,
Myle Ott's avatar
Myle Ott committed
209
            seed=args.seed,
210
            num_shards=args.distributed_world_size,
Myle Ott's avatar
Myle Ott committed
211
            shard_id=args.distributed_rank,
Myle Ott's avatar
Myle Ott committed
212
            num_workers=args.num_workers,
Myle Ott's avatar
Myle Ott committed
213
        ).next_epoch_itr(shuffle=False)
214
        progress = progress_bar.build_progress_bar(
Myle Ott's avatar
Myle Ott committed
215
            args, itr, epoch_itr.epoch,
216
217
218
219
220
221
222
223
224
225
            prefix='valid on \'{}\' subset'.format(subset),
            no_progress_bar='simple'
        )

        # reset validation loss meters
        for k in ['valid_loss', 'valid_nll_loss']:
            meter = trainer.get_meter(k)
            if meter is not None:
                meter.reset()
        extra_meters = collections.defaultdict(lambda: AverageMeter())
Myle Ott's avatar
Myle Ott committed
226

227
228
229
230
        for sample in progress:
            log_output = trainer.valid_step(sample)

            for k, v in log_output.items():
231
                if k in ['loss', 'nll_loss', 'ntokens', 'nsentences', 'sample_size']:
232
233
                    continue
                extra_meters[k].update(v)
234

235
236
237
238
        # log validation stats
        stats = get_valid_stats(trainer)
        for k, meter in extra_meters.items():
            stats[k] = meter.avg
Myle Ott's avatar
Myle Ott committed
239
        progress.print(stats, tag=subset, step=trainer.get_num_updates())
240

Myle Ott's avatar
Myle Ott committed
241
        valid_losses.append(stats['loss'].avg)
242
    return valid_losses
243
244
245
246


def get_valid_stats(trainer):
    stats = collections.OrderedDict()
Myle Ott's avatar
Myle Ott committed
247
    stats['loss'] = trainer.get_meter('valid_loss')
248
    if trainer.get_meter('valid_nll_loss').count > 0:
Myle Ott's avatar
Myle Ott committed
249
250
        nll_loss = trainer.get_meter('valid_nll_loss')
        stats['nll_loss'] = nll_loss
251
    else:
Myle Ott's avatar
Myle Ott committed
252
253
        nll_loss = stats['loss']
    stats['ppl'] = get_perplexity(nll_loss.avg)
Myle Ott's avatar
Nits  
Myle Ott committed
254
255
    stats['num_updates'] = trainer.get_num_updates()
    if hasattr(save_checkpoint, 'best'):
Myle Ott's avatar
Myle Ott committed
256
        stats['best_loss'] = min(save_checkpoint.best, stats['loss'].avg)
257
258
259
260
261
262
263
264
265
266
    return stats


def get_perplexity(loss):
    try:
        return '{:.2f}'.format(math.pow(2, loss))
    except OverflowError:
        return float('inf')


Myle Ott's avatar
Myle Ott committed
267
268
def save_checkpoint(args, trainer, epoch_itr, val_loss):
    if args.no_save or not distributed_utils.is_master(args):
269
        return
Myle Ott's avatar
Myle Ott committed
270
271
272
273

    write_timer = StopwatchMeter()
    write_timer.start()

Myle Ott's avatar
Myle Ott committed
274
275
    epoch = epoch_itr.epoch
    end_of_epoch = epoch_itr.end_of_epoch()
276
277
278
279
    updates = trainer.get_num_updates()

    checkpoint_conds = collections.OrderedDict()
    checkpoint_conds['checkpoint{}.pt'.format(epoch)] = (
Alexei Baevski's avatar
Alexei Baevski committed
280
281
            end_of_epoch and not args.no_epoch_checkpoints and
            epoch % args.save_interval == 0
282
283
    )
    checkpoint_conds['checkpoint_{}_{}.pt'.format(epoch, updates)] = (
Alexei Baevski's avatar
Alexei Baevski committed
284
285
            not end_of_epoch and args.save_interval_updates > 0 and
            updates % args.save_interval_updates == 0
286
287
    )
    checkpoint_conds['checkpoint_best.pt'] = (
Alexei Baevski's avatar
Alexei Baevski committed
288
289
            val_loss is not None and
            (not hasattr(save_checkpoint, 'best') or val_loss < save_checkpoint.best)
290
291
292
    )
    checkpoint_conds['checkpoint_last.pt'] = True  # keep this last so that it's a symlink

Myle Ott's avatar
Myle Ott committed
293
294
295
    prev_best = getattr(save_checkpoint, 'best', val_loss)
    if val_loss is not None:
        save_checkpoint.best = min(val_loss, prev_best)
296
    extra_state = {
Myle Ott's avatar
Myle Ott committed
297
        'train_iterator': epoch_itr.state_dict(),
298
299
        'val_loss': val_loss,
    }
Naman Goyal's avatar
Naman Goyal committed
300
301
    if hasattr(save_checkpoint, 'best'):
        extra_state.update({'best': save_checkpoint.best})
302

303
304
    checkpoints = [os.path.join(args.save_dir, fn) for fn, cond in checkpoint_conds.items() if cond]
    if len(checkpoints) > 0:
305
306
        for cp in checkpoints:
            trainer.save_checkpoint(cp, extra_state)
307

freewym's avatar
freewym committed
308
309
310
311
        write_timer.stop()
        print('| saved checkpoint {} (epoch {} @ {} updates) (writing took {} seconds)'.format(
            checkpoints[0], epoch, updates, write_timer.sum))

312
313
    if not end_of_epoch and args.keep_interval_updates > 0:
        # remove old checkpoints; checkpoints are sorted in descending order
Myle Ott's avatar
Myle Ott committed
314
315
316
        checkpoints = checkpoint_utils.checkpoint_paths(
            args.save_dir, pattern=r'checkpoint_\d+_(\d+)\.pt',
        )
317
        for old_chk in checkpoints[args.keep_interval_updates:]:
Myle Ott's avatar
Myle Ott committed
318
319
320
321
322
            if os.path.lexists(old_chk):
                os.remove(old_chk)

    if args.keep_last_epochs > 0:
        # remove old epoch checkpoints; checkpoints are sorted in descending order
Myle Ott's avatar
Myle Ott committed
323
324
325
        checkpoints = checkpoint_utils.checkpoint_paths(
            args.save_dir, pattern=r'checkpoint(\d+)\.pt',
        )
Myle Ott's avatar
Myle Ott committed
326
327
328
        for old_chk in checkpoints[args.keep_last_epochs:]:
            if os.path.lexists(old_chk):
                os.remove(old_chk)
329
330


Myle Ott's avatar
Myle Ott committed
331
332
def load_checkpoint(args, trainer, epoch_itr):
    """Load a checkpoint and replay dataloader to match."""
333
334
335
336
337

    # Only rank 0 should attempt to create the required dir
    if args.distributed_rank == 0:
        os.makedirs(args.save_dir, exist_ok=True)

338
339
340
341
    if os.path.isabs(args.restore_file):
        checkpoint_path = args.restore_file
    else:
        checkpoint_path = os.path.join(args.save_dir, args.restore_file)
342
    if os.path.isfile(checkpoint_path):
343
344
        extra_state = trainer.load_checkpoint(checkpoint_path, args.reset_optimizer, args.reset_lr_scheduler,
                                              eval(args.optimizer_overrides))
345
        if extra_state is not None:
Myle Ott's avatar
Myle Ott committed
346
347
348
349
350
            # replay train iterator to match checkpoint
            epoch_itr.load_state_dict(extra_state['train_iterator'])

            print('| loaded checkpoint {} (epoch {} @ {} updates)'.format(
                checkpoint_path, epoch_itr.epoch, trainer.get_num_updates()))
alexeib's avatar
alexeib committed
351

Myle Ott's avatar
Myle Ott committed
352
353
            trainer.lr_step(epoch_itr.epoch)
            trainer.lr_step_update(trainer.get_num_updates())
354
            if 'best' in extra_state and not args.reset_optimizer:
355
                save_checkpoint.best = extra_state['best']
356
        return True
357
358
    else:
        print('| no existing checkpoint found {}'.format(checkpoint_path))
359
    return False
360

361

362
363
364
365
366
367
368
369
370
371
372
def load_dataset_splits(args, task):
    task.load_dataset(args.train_subset, combine=True)
    for split in args.valid_subset.split(','):
        for k in itertools.count():
            split_k = split + (str(k) if k > 0 else '')
            try:
                task.load_dataset(split_k, combine=False)
            except FileNotFoundError as e:
                if k > 0:
                    break
                raise e
Sergey Edunov's avatar
Sergey Edunov committed
373

Myle Ott's avatar
Myle Ott committed
374

Myle Ott's avatar
Myle Ott committed
375
376
377
378
def distributed_main(i, args):
    args.device_id = i
    if args.distributed_rank is None:  # torch.multiprocessing.spawn
        args.distributed_rank = i
379
    main(args)
Myle Ott's avatar
Myle Ott committed
380
381


Myle Ott's avatar
Myle Ott committed
382
def cli_main():
Myle Ott's avatar
Myle Ott committed
383
384
    parser = options.get_training_parser()
    args = options.parse_args_and_arch(parser)
385

Myle Ott's avatar
Myle Ott committed
386
387
    if args.distributed_init_method is None:
        distributed_utils.infer_init_method(args)
388

Myle Ott's avatar
Myle Ott committed
389
390
391
    if args.distributed_init_method is not None:
        # distributed training
        distributed_main(args.device_id, args)
392
    elif args.distributed_world_size > 1:
Myle Ott's avatar
Myle Ott committed
393
        # fallback for single node with multiple GPUs
394
395
        port = random.randint(10000, 20000)
        args.distributed_init_method = 'tcp://localhost:{port}'.format(port=port)
Myle Ott's avatar
Myle Ott committed
396
        args.distributed_rank = None  # set based on device id
Myle Ott's avatar
Myle Ott committed
397
398
        if max(args.update_freq) > 1 and args.ddp_backend != 'no_c10d':
            print('| NOTE: you may get better performance with: --ddp-backend=no_c10d')
Myle Ott's avatar
Myle Ott committed
399
400
401
402
403
        torch.multiprocessing.spawn(
            fn=distributed_main,
            args=(args, ),
            nprocs=args.distributed_world_size,
        )
404
    else:
Myle Ott's avatar
Myle Ott committed
405
        # single GPU training
406
        main(args)
Myle Ott's avatar
Myle Ott committed
407
408
409
410


if __name__ == '__main__':
    cli_main()