Chapter 12.1 - Saving Loading Weights
Saving our current model's weights
torch.save(model.state_dict(), "model.pth")
Loading a model's weights
first make sure you have same config as copying model from
GPT_CONFIG_124M = {
"vocab_size": 50257, # Vocabulary size
"context_length": 256, # Shortened context length (orig: 1024)
"emb_dim": 768, # Embedding dimension
"n_heads": 12, # Number of attention heads
"n_layers": 12, # Number of layers
"drop_rate": 0.1, # Dropout rate
"qkv_bias": False # Query-key-value bias
}
next load the weights
model = GPTModel(GPT_CONFIG_124M)
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available():
# Use PyTorch 2.9 or newer for stable mps results
major, minor = map(int, torch.__version__.split(".")[:2])
if (major, minor) >= (2, 9):
device = torch.device("mps")
else:
device = torch.device("cpu")
print("Device:", device)
model.load_state_dict(torch.load("model.pth", map_location=device, weights_only=True))
model.eval();
we can load the model to be loaded on desired device irrespective of what it was saved as using map_location=device