"""Split documents, train one tokenizer, encode data and inspect token IDs.""" import argparse from contextlib import contextmanager import hashlib import json from pathlib import Path import shutil import sqlite3 import tempfile import numpy as np from helpers.tokenizer import EOT, Tokenizer DEFAULT_MAX_DOCUMENT_BYTES = 16 * 1024 * 1024 def sha256(path): digest = hashlib.sha256() with Path(path).open('rb') as stream: for chunk in iter(lambda: stream.read(1024 * 1024), b''): digest.update(chunk) return digest.hexdigest() def resolve_format(path, requested='auto'): if requested not in ('auto', 'text', 'jsonl'): raise ValueError('format must be auto, text or jsonl') return ('jsonl' if Path(path).suffix.lower() == '.jsonl' else 'text') if requested == 'auto' else requested def iter_documents(path, separator=EOT, input_format='auto', text_field='text', max_document_bytes=DEFAULT_MAX_DOCUMENT_BYTES): """Yield verbatim nonempty documents; memory is bounded by one document.""" if max_document_bytes < 1 or not separator: raise ValueError('Need a positive document limit and a nonempty separator') kind = resolve_format(path, input_format) with Path(path).open('rb') as stream: if kind == 'jsonl': line_number = 0 while raw := stream.readline(max_document_bytes * 8 + 1): line_number += 1 if len(raw) > max_document_bytes * 8: raise ValueError(f'JSONL line {line_number} exceeds the input limit') if not raw.strip(): continue try: record = json.loads(raw) text = record[text_field] except (ValueError, KeyError, TypeError) as error: raise ValueError(f'Invalid JSONL text record at line {line_number}') from error if not isinstance(text, str): raise ValueError(f'JSONL field {text_field!r} must be text at line {line_number}') if len(text.encode('utf-8')) > max_document_bytes: raise ValueError(f'Document at line {line_number} exceeds max_document_bytes') if text.strip(): yield text else: delimiter, pending = separator.encode('utf-8'), b'' for chunk in iter(lambda: stream.read(65536), b''): pieces = (pending + chunk).split(delimiter) pending = pieces.pop() for piece in pieces: if len(piece) > max_document_bytes: raise ValueError('Document exceeds max_document_bytes') text = piece.decode('utf-8') if text.strip(): yield text # The suffix may contain the first few bytes of a delimiter. if len(pending) > max_document_bytes + len(delimiter) - 1: raise ValueError('Document exceeds max_document_bytes; check the separator') if len(pending) > max_document_bytes: raise ValueError('Document exceeds max_document_bytes') text = pending.decode('utf-8') if text.strip(): yield text def validation_member(text, seed, fraction): if not 0 < fraction < 1: raise ValueError('val_fraction must be between zero and one') digest = hashlib.sha256(str(seed).encode('ascii') + b'\0' + text.encode('utf-8')).digest() return int.from_bytes(digest[:8], 'big') < int(fraction * (1 << 64)) class DocumentStore: """Temporary indexed spool; duplicate hashes are checked against exact text. The SQLite index stores hashes and file offsets, not corpus text. Text is written once to a JSON-string spool. Both are deleted by staged_output(). """ def __init__(self, directory): directory = Path(directory) self.spool_path = directory / 'documents.jsonl' self.spool = self.spool_path.open('w+b', buffering=0) self.database = sqlite3.connect(directory / 'documents.sqlite') self.database.execute('PRAGMA journal_mode=OFF') self.database.execute('PRAGMA synchronous=OFF') self.database.execute('PRAGMA cache_size=-8192') self.database.execute('PRAGMA temp_store=FILE') self.database.execute('CREATE TABLE documents (digest BLOB PRIMARY KEY, split TEXT, offset INTEGER, size INTEGER) WITHOUT ROWID') self.database.execute('CREATE INDEX split_order ON documents(split, offset)') def add(self, text, split): digest = hashlib.sha256(text.encode('utf-8')).digest() previous = self.database.execute('SELECT split, offset, size FROM documents WHERE digest=?', (digest,)).fetchone() if previous is not None: existing_split, offset, size = previous self.spool.seek(offset) if json.loads(self.spool.read(size)) != text: raise RuntimeError('SHA-256 collision between different documents') return False, existing_split record = (json.dumps(text, ensure_ascii=False) + '\n').encode('utf-8') offset = self.spool.seek(0, 2) self.spool.write(record) self.database.execute('INSERT INTO documents VALUES (?, ?, ?, ?)', (digest, split, offset, len(record))) return True, None def documents(self, split, hash_order=False): # Hash order gives a reproducible mix independent of input-file order. order = 'digest' if hash_order else 'offset' with self.spool_path.open('rb') as stream: for offset, size in self.database.execute(f'SELECT offset, size FROM documents WHERE split=? ORDER BY {order}', (split,)): stream.seek(offset) yield json.loads(stream.read(size)) def close(self): self.spool.close() self.database.close() def __enter__(self): return self def __exit__(self, *exception): self.close() @contextmanager def staged_output(out): out = Path(out) if out.exists(): raise FileExistsError(f'Refusing to replace {out}; choose a new output directory') out.parent.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(prefix='.a1-prepare-', dir=out.parent) as temporary: workspace = Path(temporary) stage = workspace / 'output' stage.mkdir() yield stage, workspace if out.exists(): raise FileExistsError(f'Output appeared while preparing: {out}') stage.rename(out) def write_documents(path, documents, output_format='text', cap=None, separator=EOT): """Write a deterministic in-order whole-document subset within a byte cap.""" if cap is not None and cap < 1: raise ValueError('Document byte caps must be positive') count, total, omitted = 0, 0, 0 with Path(path).open('wb') as stream: for text in documents: raw = text.encode('utf-8') if cap is not None and total + len(raw) > cap: omitted += 1 continue if output_format == 'jsonl': stream.write((json.dumps({'text': text}, ensure_ascii=False) + '\n').encode('utf-8')) else: if separator in text: raise ValueError('A document contains the output separator; use JSONL output') stream.write(raw + separator.encode('utf-8')) total += len(raw) count += 1 if not count: raise ValueError(f'No whole document fits in {path.name}; increase its byte cap or supply more data') return {'documents': count, 'bytes': total, 'omitted_for_cap': omitted, 'file': Path(path).name, 'sha256': sha256(path)} def split_data(source, out, seed=0, val_fraction=0.01, val_bytes=None, laptop_train_bytes=2097152, laptop_val_bytes=65536, train_bytes=None, input_format='auto', output_format='auto', text_field='text', separator=EOT, max_document_bytes=DEFAULT_MAX_DOCUMENT_BYTES): if not 0 < val_fraction < 1: raise ValueError('val_fraction must be between zero and one') if any(value is not None and value < 1 for value in (val_bytes, laptop_train_bytes, laptop_val_bytes, train_bytes)): raise ValueError('Byte caps must be positive') source = Path(source).resolve() kind = resolve_format(source, input_format) output_kind = kind if output_format == 'auto' else output_format if output_kind not in ('text', 'jsonl'): raise ValueError('Output format must be auto, text or jsonl') extension = 'jsonl' if output_kind == 'jsonl' else 'txt' with staged_output(out) as (stage, temporary), DocumentStore(temporary) as store: total, duplicates, counts = 0, 0, {'train': 0, 'val': 0} for text in iter_documents(source, separator, kind, text_field, max_document_bytes): total += 1 split = 'val' if validation_member(text, seed, val_fraction) else 'train' fresh, _ = store.add(text, split) if not fresh: duplicates += 1 else: counts[split] += 1 manifest = {'format': 'course-document-split-v1', 'source': {'path': str(source), 'sha256': sha256(source), 'format': kind}, 'seed': seed, 'val_fraction': val_fraction, 'assignment': 'sha256(ascii(seed) + NUL + UTF8(text)), first 64 bits, big endian', 'subset_selection': 'in-order whole documents; skip documents exceeding the remaining cap', 'caps': {'train': train_bytes, 'val': val_bytes, 'laptop_train': laptop_train_bytes, 'laptop_val': laptop_val_bytes}, 'input_documents': total, 'unique_documents': total - duplicates, 'duplicates_removed': duplicates, 'assigned_documents': counts, 'disjoint': True, 'laptop_subsets_nested': True, 'splits': {}} for split, cap in [('train', train_bytes), ('val', val_bytes)]: path = stage / f'{split}.{extension}' manifest['splits'][split] = write_documents(path, store.documents(split), output_kind, cap, separator) laptop_cap = laptop_train_bytes if split == 'train' else laptop_val_bytes laptop_path = stage / f'laptop-{split}.{extension}' # Read only the selected main split: omitted holdout docs never leak # into training, and the laptop data is an exact document subset. manifest['splits'][f'laptop_{split}'] = write_documents( laptop_path, iter_documents(path, separator, output_kind, 'text', max_document_bytes), output_kind, laptop_cap, separator) (stage / 'split.json').write_text(json.dumps(manifest, indent=2) + '\n') return manifest def fitting_prefix(documents, cap): if cap is not None and cap < 1: raise ValueError('fit_bytes must be positive') selected, total, seen = [], 0, set() digest = hashlib.sha256() for text in documents: if text in seen: continue raw = text.encode('utf-8') if cap is not None and total + len(raw) > cap: break selected.append(text) seen.add(text) total += len(raw) digest.update(len(raw).to_bytes(8, 'big')) digest.update(raw) if not selected: raise ValueError('No whole training document fits --fit-bytes; increase the cap') return selected, {'selection': 'training_prefix_whole_documents', 'max_bytes': cap, 'documents': len(selected), 'bytes': total, 'framed_text_sha256': digest.hexdigest()} def write_tokens(path, documents, tokenizer): eot = tokenizer.token_to_id(EOT) count, byte_count, token_count = 0, 0, 1 with Path(path).open('wb') as stream: stream.write(np.asarray([eot], dtype='