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

Method save_checkpoint

funasr/train_utils/trainer_ds.py:171–412  ·  view source on GitHub ↗

Saves a checkpoint containing the model's state, the optimizer's state, and the scheduler's state at the end of the given epoch. This method is intended to be called at the end of each epoch to save the training progress. Args: epoch (int): The epoch num

(
        self,
        epoch,
        step=None,
        model=None,
        optim=None,
        scheduler=None,
        scaler=None,
        step_in_epoch=None,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

169 self.effective_save_name_excludes = effective_save_name_excludes
170
171 def save_checkpoint(
172 self,
173 epoch,
174 step=None,
175 model=None,
176 optim=None,
177 scheduler=None,
178 scaler=None,
179 step_in_epoch=None,
180 **kwargs,
181 ):
182 """
183 Saves a checkpoint containing the model's state, the optimizer's state,
184 and the scheduler's state at the end of the given epoch. This method is
185 intended to be called at the end of each epoch to save the training progress.
186
187 Args:
188 epoch (int): The epoch number at which the checkpoint is being saved.
189 """
190 if self.use_ddp or self.use_fsdp:
191 dist.barrier()
192 step_in_epoch = None if step is None else step_in_epoch
193 if self.use_deepspeed:
194
195 logging.info(f"Save checkpoint: {epoch}, rank: {self.local_rank}\n")
196 # self.step_or_epoch += 1
197 state = {
198 "epoch": epoch,
199 # "state_dict": model.state_dict(),
200 # "optimizer": optim.state_dict(),
201 # "scheduler": scheduler.state_dict(),
202 "saved_ckpts": self.saved_ckpts,
203 "val_acc_step_or_epoch": self.val_acc_step_or_epoch,
204 "val_loss_step_or_epoch": self.val_loss_step_or_epoch,
205 "best_step_or_epoch": self.best_step_or_epoch,
206 "avg_keep_nbest_models_type": self.avg_keep_nbest_models_type,
207 "step": step,
208 "step_in_epoch": step_in_epoch,
209 "data_split_i": kwargs.get("data_split_i", 0),
210 "data_split_num": kwargs.get("data_split_num", 1),
211 "batch_total": self.batch_total,
212 "train_loss_avg": kwargs.get("train_loss_avg", 0),
213 "train_acc_avg": kwargs.get("train_acc_avg", 0),
214 }
215 step = step_in_epoch
216 if hasattr(model, "module"):
217 state["state_dict"] = model.module.state_dict()
218
219 if scaler:
220 state["scaler_state"] = scaler.state_dict()
221 # Create output directory if it does not exist
222 os.makedirs(self.output_dir, exist_ok=True)
223 if step is None:
224 ckpt_name = f"model.pt.ep{epoch}"
225 else:
226 ckpt_name = f"model.pt.ep{epoch}.{step}"
227 filename = os.path.join(self.output_dir, ckpt_name)
228

Callers 1

train_epochMethod · 0.95

Calls 2

keysMethod · 0.80
state_dictMethod · 0.45

Tested by

no test coverage detected