PyTorch Lightning: RuntimeError missing keys in state_dict
Traceback (most recent call last):
File "eval.py", line 15, in <module>
model.load_state_dict(checkpoint['state_dict'])
RuntimeError: You have missing keys in your state_dict: ['model.layers.0.self_attn.q_proj.weight', 'model.layers.0.self_attn.k_proj.weight']
Immediate Remediation
python
model.load_state_dict(torch.load("checkpoint.pt")["state_dict"], strict=False)Root Cause Analysis
The model architecture defined in code does not match the architecture of the saved checkpoint, leading to missing keys when loading the state dict.
Verification & Guardrails
- Using `strict=False` will randomly initialize missing layers, which may degrade performance; it's better to align the model architecture.
- Check that the checkpoint was saved with the same model class and configuration.
- If the checkpoint was saved with DataParallel or DistributedDataParallel, strip the 'module.' prefix from keys before loading.
Have a custom or uncategorized crash?
Run your trace through our in-memory client privacy sandbox for instant SRE remediation.