How to get the checkpoint path?

Hi
could you tell me how I can get the checkpoint path in this function? thanks

def on_save_checkpoint(self, checkpoint: Dict[str, Any]) -> None:
      save_path = checkpoint['filepath'] ??? where is the path ???
      self.model.save_pretrained(save_path)
      self.tokenizer.save_pretrained(save_path)

The checkpoint path will be whatever specified by the ModelCheckpoint callback. By default this will be lightning_logs/version_{version number}/epoch_{epoch number}.ckpt.

Hi, I need to define a checkpoint which is called 5 times during the training, how would I know inside the ModelCheckpoint, which iteration number this is ? thanks I appreciate an example, on how to save the model every k steps/epochs

To save a model every k epochs, use the period argument to ModelCheckpoint along with save_top_k=-1. This will save every model every k epochs.

Hi
I need the “path” to the place checkpoint saved inside the callback, could you tell me how to get that checkpoint path? thanks

So when you say “version_{version number}/epoch_{epoch number}” how do you get ecpoh number and version number inside the callback? thanks

FYI. it’s now
lightning_logs/version_{version number}/epoch_{epoch number}-step_{global_step}.ckpt

and to access them:

version_number -> trainer.logger.version
epoch_number -> trainer.current_epoch
global_step -> trainer.global_step

You can explore your Checkpoint attributes, for example, you want best_model_path

In addition to what @goku said, you can get the log directory + version number with: trainer.logger.log_dir, so if you add what you want as a callback:

from pytorch_lightning.callbacks import Callback

class OnCheckpointSomething(Callback):
    def on_save_checkpoint(self, trainer, pl_module):
        save_path = f"{trainer.logger.log_dir}/checkpoints/epoch={trainer.current_epoch}.ckpt"