Download tests/test_checkpoint.py from ICLRPAT27/PAT-code: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/tests/test_checkpoint.py
- Command line
-
hf download hf://ICLRPAT27/PAT-code/tests/test_checkpoint.py
-
curl -L -o test_checkpoint.py https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/tests/test_checkpoint.py
2.47 kB
| """Checkpoint round trips and integrity failures do not need a GPU.""" | |
| import json | |
| import pytest | |
| import torch | |
| from pat.checkpoint import download_checkpoint, load_checkpoint, save_checkpoint, verify_checkpoint | |
| from pat.config import PATConfig | |
| from pat.model import PATModel | |
| from pat.tokenizer import Tokenizer | |
| def test_weight_round_trip_and_tamper_detection(tmp_path): | |
| model = PATModel(PATConfig(architecture="FA", d_model=80, n_heads=4, n_layers=1, | |
| context_length=64)) | |
| directory = tmp_path / "checkpoint" | |
| save_checkpoint(model, directory, {"training_tokens": 0}) | |
| loaded = load_checkpoint(directory) | |
| assert loaded.lm_head.weight is loaded.token_embed.weight | |
| assert not loaded.training | |
| for name, tensor in model.state_dict().items(): | |
| assert torch.equal(tensor, loaded.state_dict()[name]), name | |
| with pytest.raises(FileExistsError): | |
| save_checkpoint(model, directory) | |
| config = directory / "config.json" | |
| config.write_text(config.read_text().replace('"seed": 42', '"seed": 43')) | |
| with pytest.raises(ValueError, match="SHA-256 mismatch"): | |
| verify_checkpoint(directory) | |
| def test_manifest_rejects_parent_path(tmp_path): | |
| (tmp_path / "manifest.json").write_text(json.dumps({ | |
| "format": "pat-checkpoint-v1", | |
| "files": {name: {} for name in ["config.json", "model.safetensors", "tokenizer.json", | |
| "TOKENIZER_LICENSE.txt", "metadata.json", "../outside"]}, | |
| })) | |
| # Validate the filename before any file content is consulted. | |
| data = json.loads((tmp_path / "manifest.json").read_text()) | |
| data["files"] = {"../outside": {}, **data["files"]} | |
| (tmp_path / "manifest.json").write_text(json.dumps(data)) | |
| with pytest.raises(ValueError, match="basenames"): | |
| verify_checkpoint(tmp_path) | |
| def test_download_requires_immutable_revision(tmp_path): | |
| with pytest.raises(ValueError, match="commit revision"): | |
| download_checkpoint("example/model", "main", tmp_path / "model") | |
| def test_bundled_tokenizer_contract(): | |
| tokenizer = Tokenizer() | |
| text = "Hello, café 日本語.\n x = 3" | |
| assert tokenizer.decode(tokenizer.encode(text)) == text | |
| assert tokenizer.encode("") == [] | |
| for marker in ("[BOS]", "[EOC]", "[PAD]", "<|endoftext|>", "[V64PAD_00]"): | |
| ids = tokenizer.encode(marker) | |
| assert all(token < 50256 for token in ids) | |
| assert tokenizer.decode(ids) == marker | |