Train the model.
(model, optimizer, lr_scheduler, forward_step,
train_dataloader, valid_dataloader, end_of_epoch_callback)
| 124 | |
| 125 | |
| 126 | def _train(model, optimizer, lr_scheduler, forward_step, |
| 127 | train_dataloader, valid_dataloader, end_of_epoch_callback): |
| 128 | """Train the model.""" |
| 129 | args = get_args() |
| 130 | timers = get_timers() |
| 131 | |
| 132 | # Turn on training mode which enables dropout. |
| 133 | model.train() |
| 134 | |
| 135 | # Tracking loss. |
| 136 | losses_dict_sum = {} |
| 137 | |
| 138 | # Starting epoch and iteration |
| 139 | start_epoch = args.iteration // args.train_iters_per_epoch |
| 140 | start_iteration = args.iteration % args.train_iters_per_epoch |
| 141 | iteration = args.iteration |
| 142 | |
| 143 | # Memory reporting flag. |
| 144 | report_memory_flag = True |
| 145 | |
| 146 | # For each remaining epoch |
| 147 | timers('interval time').start() |
| 148 | for epoch in range(start_epoch, args.epochs): |
| 149 | print_rank_0('working on epoch {} ...'.format(epoch + 1)) |
| 150 | |
| 151 | # Set the data loader epoch to shuffle the index iterator. |
| 152 | train_dataloader.sampler.set_epoch(args.seed + epoch) |
| 153 | |
| 154 | # For all the batches in the dataset. |
| 155 | for iteration_, batch in enumerate(train_dataloader): |
| 156 | |
| 157 | # Ignore the iterations before starting value |
| 158 | if iteration_ < start_iteration: |
| 159 | continue |
| 160 | # Set to zero so the next epoch does not skip any batches. |
| 161 | start_iteration = 0 |
| 162 | |
| 163 | # Train for one step. |
| 164 | losses_dict, _ = train_step(forward_step, batch, model, |
| 165 | optimizer, lr_scheduler) |
| 166 | iteration += 1 |
| 167 | |
| 168 | # Logging. |
| 169 | report_memory_flag = training_log(losses_dict, losses_dict_sum, |
| 170 | optimizer.param_groups[0]['lr'], |
| 171 | iteration, optimizer.loss_scale, |
| 172 | report_memory_flag) |
| 173 | |
| 174 | # Autoresume |
| 175 | if args.adlr_autoresume and \ |
| 176 | (iteration % args.adlr_autoresume_interval == 0): |
| 177 | check_adlr_autoresume_termination(iteration, model, |
| 178 | optimizer, lr_scheduler) |
| 179 | |
| 180 | # Checkpointing |
| 181 | if args.save and args.save_interval and \ |
| 182 | iteration % args.save_interval == 0: |
| 183 | save_checkpoint(iteration, model, optimizer, lr_scheduler) |
no test coverage detected