File size: 3,502 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | """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)
|