Download tests/test_data.py from ICLRPAT27/PAT-code: direct link, hf CLI and curl.
- Browser
- Download file 3.5 kB
-
https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/tests/test_data.py
- Command line
-
hf download hf://ICLRPAT27/PAT-code/tests/test_data.py
-
curl -L -o test_data.py https://huggingface.co/ICLRPAT27/PAT-code/resolve/main/tests/test_data.py
3.5 kB
| """Document boundaries and deterministic token-budget batches.""" | |
| import json | |
| from types import SimpleNamespace | |
| import numpy as np | |
| import pytest | |
| from pat.data import PieceDataset, training_batches, validation_batches | |
| from pat.prepare import prepare | |
| from pat.tokenizer import EOD_ID, Tokenizer | |
| def test_preparation_preserves_complete_content_and_final_boundary(tmp_path): | |
| tokenizer = Tokenizer() | |
| documents = ["one two three four five six seven eight nine ten " * 5, "second document"] | |
| source = tmp_path / "input.jsonl" | |
| source.write_text("".join(json.dumps({"text": text}) + "\n" for text in documents)) | |
| destination = tmp_path / "prepared" | |
| prepare(source, destination, None, context_length=16) | |
| dataset = PieceDataset(destination) | |
| recovered, current = [], [] | |
| for piece in dataset: | |
| ids = piece.tolist() | |
| assert EOD_ID not in ids[:-1] | |
| is_final = ids[-1] == EOD_ID | |
| current.extend(ids[:-1] if is_final else ids) | |
| if is_final: | |
| recovered.append(current) | |
| current = [] | |
| assert not current | |
| assert recovered == [tokenizer.encode(text) for text in documents] | |
| assert sum(dataset.lexical_lengths) == sum(map(len, recovered)) | |
| batches = list(validation_batches(dataset, 20)) | |
| assert sorted(index for batch in batches for index in batch) == list(range(len(dataset))) | |
| def test_prepared_data_hash_detects_changed_token_array(tmp_path): | |
| source = tmp_path / "input.jsonl" | |
| source.write_text(json.dumps({"text": "An ordinary document."}) + "\n") | |
| destination = tmp_path / "prepared" | |
| prepare(source, destination, None, 16384) | |
| path = destination / "tokens.bin" | |
| data = bytearray(path.read_bytes()) | |
| data[0] ^= 1 | |
| path.write_bytes(data) | |
| with pytest.raises(ValueError, match="checksum"): | |
| PieceDataset(destination) | |
| def test_batching_budget_uniqueness_epoch_and_distributed_window(): | |
| lexical = np.asarray([10, 19, 20, 21, 29, 30, 31, 39, 40, 49, 50] * 8) | |
| routed = lexical + np.asarray([0, 1] * 44) | |
| first = training_batches(routed, lexical, budget=100, seed=42, epoch=0, quantum=4) | |
| again = training_batches(routed, lexical, budget=100, seed=42, epoch=0, quantum=4) | |
| later = training_batches(routed, lexical, budget=100, seed=42, epoch=1, quantum=4) | |
| assert first == again | |
| assert first != later | |
| assert len(first) % 4 == 0 | |
| flattened = [index for batch in first for index in batch] | |
| assert len(flattened) == len(set(flattened)) | |
| assert all(int(lexical[batch].sum()) <= 100 for batch in first) | |
| def test_validation_preserves_reference_order_and_overflow_coverage(): | |
| # Fixed oracle output exercises deferred capacity, cyclic slot placement, | |
| # and a piece larger than the nominal batch budget without dropping it. | |
| lexical = np.asarray([91, 81, 71, 61, 51, 41, 31, 21, 11, 1, 101]) | |
| dataset = SimpleNamespace(lexical_lengths=lexical, routed_lengths=lexical + 1) | |
| batches = validation_batches(dataset, 100, seed=42) | |
| assert batches == [[0, 9], [1], [3, 7], [2], [5, 6], [4, 8], [10]] | |
| assert sorted(index for batch in batches for index in batch) == list(range(len(lexical))) | |
| assert all(int(lexical[batch].sum()) <= 100 or len(batch) == 1 for batch in batches) | |
| def test_preparation_rejects_missing_text(tmp_path): | |
| source = tmp_path / "input.jsonl" | |
| source.write_text('{"wrong": "field"}\n') | |
| with pytest.raises(TypeError, match="text"): | |
| prepare(source, tmp_path / "prepared", None, 16384) | |