Download src/tinyllm/checkpoint.py from razor5050/tinyllm-75m-openwebtext-chat: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/razor5050/tinyllm-75m-openwebtext-chat/resolve/main/src/tinyllm/checkpoint.py
- Command line
-
hf download hf://razor5050/tinyllm-75m-openwebtext-chat/src/tinyllm/checkpoint.py
-
curl -L -o checkpoint.py https://huggingface.co/razor5050/tinyllm-75m-openwebtext-chat/resolve/main/src/tinyllm/checkpoint.py
1.08 kB
| from pathlib import Path | |
| import torch, time, shutil | |
| def save_checkpoint(path, model, optimizer=None, scheduler=None, step=0, epoch=0, metrics=None, config=None): | |
| path=Path(path); path.parent.mkdir(parents=True, exist_ok=True) | |
| tmp=path.with_suffix(path.suffix+'.tmp') | |
| state={'model':model.state_dict(),'step':step,'epoch':epoch,'metrics':metrics or {},'saved_at':time.time(),'config':config} | |
| if optimizer is not None: state['optimizer']=optimizer.state_dict() | |
| if scheduler is not None: state['scheduler']=scheduler.state_dict() | |
| torch.save(state,tmp); tmp.replace(path) | |
| latest=path.parent/'latest.pt' | |
| if latest != path: shutil.copy2(path, latest) | |
| def load_checkpoint(path, model, optimizer=None, scheduler=None, map_location='cpu'): | |
| state=torch.load(path,map_location=map_location) | |
| model.load_state_dict(state['model']) | |
| if optimizer is not None and 'optimizer' in state: optimizer.load_state_dict(state['optimizer']) | |
| if scheduler is not None and 'scheduler' in state: scheduler.load_state_dict(state['scheduler']) | |
| return state | |