Progress the pipeline to train the next batch of data. The engine will ingest ``self.train_batch_size()`` total samples collectively across all workers. An iterator that over training data should be provided as an argument unless ``deepspeed.initialize()`` was provided a tr
(self, data_iter=None)
| 330 | self.grad_partition_grad_layer_meta_cache = None |
| 331 | |
| 332 | def train_batch(self, data_iter=None): |
| 333 | """Progress the pipeline to train the next batch of data. The engine will ingest |
| 334 | ``self.train_batch_size()`` total samples collectively across all workers. |
| 335 | |
| 336 | |
| 337 | An iterator that over training data should be provided as an argument |
| 338 | unless ``deepspeed.initialize()`` was provided a training set. In that event, |
| 339 | the training data will automatically be read. |
| 340 | |
| 341 | |
| 342 | .. warning:: |
| 343 | A total of ``self.gradient_accumulation_steps()`` entries will be pulled |
| 344 | from ``data_iter`` by each pipeline. There must be sufficient |
| 345 | data left in ``data_iter`` or else a ``StopIteration`` will halt training. |
| 346 | |
| 347 | DeepSpeed provides a convenience class :class:`deepspeed.utils.RepeatingLoader` |
| 348 | that wraps data loaders to automatically restart upon a ``StopIteration``. |
| 349 | |
| 350 | Args: |
| 351 | data_iter (Iterator, optional): Iterator of training data. |
| 352 | |
| 353 | Returns: |
| 354 | The arithmetic mean of the losses computed this batch. |
| 355 | """ |
| 356 | if not torch._C.is_grad_enabled(): |
| 357 | raise RuntimeError('train_batch() requires gradients enabled. Use eval_batch() instead.') |
| 358 | |
| 359 | if data_iter is not None: |
| 360 | self.set_dataiterator(data_iter) |
| 361 | |
| 362 | self.module.train() |
| 363 | self.total_loss = None |
| 364 | self.total_additional_losses = None |
| 365 | self._compute_loss = True |
| 366 | |
| 367 | # Do the work |
| 368 | self.timers(TRAIN_BATCH_TIMER).start() |
| 369 | sched = self._train_schedule() |
| 370 | self._exec_schedule(sched) |
| 371 | |
| 372 | with torch.no_grad(): |
| 373 | self.agg_train_loss = self._aggregate_total_loss() |
| 374 | |
| 375 | self.timers(TRAIN_BATCH_TIMER).stop() |
| 376 | |
| 377 | if self.steps_per_print() is not None and self.global_steps % self.steps_per_print() == 0: |
| 378 | if self.global_rank == 0: |
| 379 | elapsed = self.timers(TRAIN_BATCH_TIMER).elapsed_total(reset=True) / 1000.0 |
| 380 | iter_time = elapsed / self.steps_per_print() |
| 381 | tput = self.train_batch_size() / iter_time |
| 382 | log_str = f'steps: {self.global_steps} loss: {self.agg_train_loss:0.4f} ' |
| 383 | if self.agg_additional_losses is not None: |
| 384 | for loss_name, loss_value in self.agg_additional_losses.items(): |
| 385 | log_str += f'{loss_name}: {loss_value.item():0.4f} ' |
| 386 | log_str += f'iter time (s): {iter_time:0.3f} samples/sec: {tput:0.3f}' |
| 387 | print(log_str) |
| 388 | else: |
| 389 | self.timers(TRAIN_BATCH_TIMER).elapsed(reset=True) |