Download tests/test_checkpoint.py from Amitkumar001/Law_Slm: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/Amitkumar001/Law_Slm/resolve/main/tests/test_checkpoint.py
- Command line
-
hf download hf://Amitkumar001/Law_Slm/tests/test_checkpoint.py
-
curl -L -o test_checkpoint.py https://huggingface.co/Amitkumar001/Law_Slm/resolve/main/tests/test_checkpoint.py
1.48 kB
| """ | |
| Unit tests for CheckpointManager saving and loading functionality. | |
| """ | |
| import os | |
| import torch | |
| import pytest | |
| from slm.config.model_config import ModelConfig | |
| from slm.config.train_config import TrainConfig | |
| from slm.model.transformer_lm import SLMForCausalLM | |
| from slm.checkpoint.manager import CheckpointManager | |
| def test_checkpoint_manager_save_and_load(tmp_path): | |
| ckpt_dir = str(tmp_path / "checkpoints") | |
| manager = CheckpointManager(output_dir=ckpt_dir) | |
| model_config = ModelConfig(vocab_size=100, d_model=32, n_heads=2, n_layers=1) | |
| train_config = TrainConfig(output_dir=ckpt_dir) | |
| model = SLMForCausalLM(model_config) | |
| optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3) | |
| ckpt_path = manager.save_checkpoint( | |
| model=model, | |
| optimizer=optimizer, | |
| scheduler=None, | |
| step=10, | |
| epoch=1, | |
| loss=2.5, | |
| model_config=model_config, | |
| train_config=train_config, | |
| is_best=True | |
| ) | |
| assert os.path.exists(ckpt_path) | |
| # Test loading into a new model instance | |
| new_model = SLMForCausalLM(model_config) | |
| loaded_meta = manager.load_checkpoint(ckpt_path, new_model) | |
| assert loaded_meta["step"] == 10 | |
| assert loaded_meta["epoch"] == 1 | |
| assert loaded_meta["loss"] == 2.5 | |
| assert loaded_meta["model_config"]["vocab_size"] == 100 | |
| # Verify weight restoration match | |
| for p1, p2 in zip(model.parameters(), new_model.parameters()): | |
| assert torch.equal(p1, p2) | |