MCPcopy Create free account
hub / github.com/modelscope/FunASR / update_step

Method update_step

funasr/train_utils/trainer_ds.py:713–752  ·  view source on GitHub ↗

Update step. Args: model: Model instance or model name. optim: TODO. scheduler: TODO. scaler: TODO. loss_dict: TODO.

(self, model, optim, scheduler, scaler, loss_dict=None)

Source from the content-addressed store, hash-verified

711 loss.backward()
712
713 def update_step(self, model, optim, scheduler, scaler, loss_dict=None):
714 """Update step.
715
716 Args:
717 model: Model instance or model name.
718 optim: TODO.
719 scheduler: TODO.
720 scaler: TODO.
721 loss_dict: TODO.
722 """
723 batch_idx = loss_dict["batch_idx"]
724 if self.use_deepspeed:
725 model.step()
726 else:
727 if (batch_idx + 1) % self.accum_grad == 0:
728 # Perform gradient clipping if it is set
729 if self.grad_clip > 0:
730 grad_norm = torch.nn.utils.clip_grad_norm_(
731 model.parameters(),
732 max_norm=self.grad_clip,
733 norm_type=self.grad_clip_type,
734 )
735 if not torch.isfinite(grad_norm):
736 logging.warning(
737 f"The grad norm is {grad_norm}. Skipping updating the model."
738 )
739 optim.zero_grad() # Reset gradients
740 return
741
742 # Execute an optimization step (update model parameters)
743 if self.use_ddp or self.use_fsdp:
744 dist.barrier()
745 if scaler:
746 scaler.step(optim)
747 scaler.update()
748 else:
749 optim.step()
750 scheduler.step()
751 # Clear gradients for the next accumulation stage
752 optim.zero_grad(set_to_none=True)
753
754 def validate_epoch(
755 self,

Callers 1

train_epochMethod · 0.95

Calls 3

parametersMethod · 0.80
stepMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected