Download tests/test_generation.py from ICLRPAT27/PAT-code: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/tests/test_generation.py
- Command line
-
hf download hf://ICLRPAT27/PAT-code/tests/test_generation.py
-
curl -L -o test_generation.py https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/tests/test_generation.py
1.66 kB
| """Sampling respects the public vocabulary and explicit sampling settings.""" | |
| from types import SimpleNamespace | |
| import pytest | |
| import torch | |
| from pat.generate import generate, sample | |
| from pat.tokenizer import EOD_ID | |
| def test_internal_ids_cannot_win_greedy_selection(): | |
| logits = torch.zeros(50304) | |
| logits[EOD_ID + 1:] = 1000 | |
| logits[17] = 10 | |
| assert sample(logits, 0, 0.95, None) == 17 | |
| logits[EOD_ID] = 20 | |
| assert sample(logits, 0, 0.95, None) == EOD_ID | |
| def test_small_nucleus_keeps_highest_probability_token(): | |
| logits = torch.tensor([3., 2., 1.]) | |
| generator = torch.Generator().manual_seed(42) | |
| for _ in range(10): | |
| assert sample(logits, 1, 0.1, generator) == 0 | |
| def test_invalid_sampling_settings_are_rejected(temperature, top_p): | |
| with pytest.raises(ValueError): | |
| sample(torch.zeros(3), temperature, top_p, None) | |
| def test_nonfinite_logits_are_rejected(): | |
| with pytest.raises(FloatingPointError): | |
| sample(torch.tensor([0., float('nan')]), 0, .9, None) | |
| def test_generation_requires_a_nonempty_text_prompt(): | |
| tokenizer = SimpleNamespace(encode=lambda _: []) | |
| with pytest.raises(ValueError, match="at least one text token"): | |
| generate(None, "", tokenizer=tokenizer) | |
| def test_generation_context_budget_includes_bos(): | |
| tokenizer = SimpleNamespace(encode=lambda _: [1, 2, 3]) | |
| model = SimpleNamespace(config=SimpleNamespace(context_length=4)) | |
| with pytest.raises(ValueError, match="context length"): | |
| generate(model, "three tokens", tokenizer=tokenizer, max_new_tokens=1) | |