MCPcopy Index your code
hub / github.com/MoonInTheRiver/DiffSinger / run_training_batch

Method run_training_batch

utils/pl_utils.py:1436–1562  ·  view source on GitHub ↗
(self, batch, batch_idx)

Source from the content-addressed store, hash-verified

1434 model.on_epoch_end()
1435
1436 def run_training_batch(self, batch, batch_idx):
1437 # track grad norms
1438 grad_norm_dic = {}
1439
1440 # track all metrics for callbacks
1441 all_callback_metrics = []
1442
1443 # track metrics to log
1444 all_log_metrics = []
1445
1446 if batch is None:
1447 return 0, grad_norm_dic, {}
1448
1449 # hook
1450 if self.is_function_implemented('on_batch_start'):
1451 model_ref = self.get_model()
1452 response = model_ref.on_batch_start(batch)
1453
1454 if response == -1:
1455 return -1, grad_norm_dic, {}
1456
1457 splits = [batch]
1458 self.hiddens = None
1459 for split_idx, split_batch in enumerate(splits):
1460 self.split_idx = split_idx
1461
1462 # call training_step once per optimizer
1463 for opt_idx, optimizer in enumerate(self.optimizers):
1464 if optimizer is None:
1465 continue
1466 # make sure only the gradients of the current optimizer's paramaters are calculated
1467 # in the training step to prevent dangling gradients in multiple-optimizer setup.
1468 if len(self.optimizers) > 1:
1469 for param in self.get_model().parameters():
1470 param.requires_grad = False
1471 for group in optimizer.param_groups:
1472 for param in group['params']:
1473 param.requires_grad = True
1474
1475 # wrap the forward step in a closure so second order methods work
1476 def optimizer_closure():
1477 # forward pass
1478 output = self.training_forward(
1479 split_batch, batch_idx, opt_idx, self.hiddens)
1480
1481 closure_loss = output[0]
1482 progress_bar_metrics = output[1]
1483 log_metrics = output[2]
1484 callback_metrics = output[3]
1485 self.hiddens = output[4]
1486 if closure_loss is None:
1487 return None
1488
1489 # accumulate loss
1490 # (if accumulate_grad_batches = 1 no effect)
1491 closure_loss = closure_loss / self.accumulate_grad_batches
1492
1493 # backward pass

Callers 1

run_training_epochMethod · 0.95

Calls 9

get_modelMethod · 0.95
print_nan_gradientsMethod · 0.95
clip_gradientsMethod · 0.95
on_batch_startMethod · 0.80
grad_normMethod · 0.80
on_batch_endMethod · 0.80
updateMethod · 0.80
optimizer_stepMethod · 0.45

Tested by

no test coverage detected