Home Knowledge Base Checkpointing

Checkpointing is the practice of periodically saving a model's complete training state — weights, optimizer state, epoch number, learning rate schedule, and training metrics — during training — enabling crash recovery (resume from hour 70 of a 72-hour training run instead of restarting from scratch), best model selection (save the model with the lowest validation loss, which is often NOT the final epoch), and preemption resilience (cloud spot instances can be killed at any time, and checkpoints are insurance against lost compute).

What Is Checkpointing?

What to Save

ComponentWhy It MattersWhat Happens Without It
model.state_dict()Model weights and biasesCan't use or resume the model at all
optimizer.state_dict()Momentum, adaptive learning rates (Adam)Optimizer restarts cold → training diverges
epoch / stepCurrent position in trainingDon't know where to resume
scheduler.state_dict()Learning rate schedule positionLR schedule restarts → wrong learning rate
best_val_metricBest validation score seenCan't determine if new checkpoints are improvements
RNG statesRandom seeds for reproducibilityNon-reproducible training

PyTorch Implementation

# Save checkpoint
def save_checkpoint(model, optimizer, scheduler, epoch, best_val, path):
    torch.save({
        'epoch': epoch,
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict(),
        'scheduler_state_dict': scheduler.state_dict(),
        'best_val_loss': best_val,
    }, path)

# Load checkpoint
checkpoint = torch.load('checkpoint.pt')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
start_epoch = checkpoint['epoch'] + 1

Checkpointing Strategies

StrategyWhen to SaveUse Case
Every N epochsSave every 5 or 10 epochsStandard training — periodic insurance
Best onlySave only when validation metric improvesLong training — disk space efficient
Last + BestKeep most recent + best validationResume from latest OR use best
Top KKeep K best checkpointsModel selection, ensemble from top-K
Every stepSave after every batch/stepVery expensive training (LLMs)

Framework Support

FrameworkCheckpointing API
PyTorchtorch.save() / torch.load() (manual)
PyTorch LightningModelCheckpoint callback (automatic)
KerasModelCheckpoint callback (automatic)
Hugging FaceTrainer(save_strategy="epoch") (automatic)
DeepSpeedBuilt-in distributed checkpointing

Checkpointing is the essential training infrastructure that protects against compute loss — saving the complete training state periodically so that expensive GPU hours are never wasted due to crashes, preemption, or hardware failures, while simultaneously enabling best-model selection by preserving the weights from the optimal validation epoch rather than the final (potentially overfit) epoch.

checkpointsaveresume

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.