File size: 2,471 Bytes
643267a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | """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
|