davidheineman's picture
Publish coherent math-v1 datasets
8f79f59 verified
Raw History Blame Contribute Delete
6.2 kB
from collections import Counter
from dataclasses import dataclass
from functools import lru_cache
import gzip
import json
from pathlib import Path
import random
from omegaconf import OmegaConf
from rich import print
from tqdm import tqdm
from refined_v4.data import atomic_json, signature
from . import data, envs
@dataclass
class Config:
root: str = str(data.ROOT)
tokenizer_samples: int = 256
sampler_draws: int = 16000
def rows(path: Path):
with gzip.open(path, 'rt') as stream:
for line in stream:
yield json.loads(line)
def audit(config: Config):
root = Path(config.root)
sources, source_audit = data.base_sources(data.Config(root=str(root)))
styles = json.loads((root / 'styles.json').read_text())['styles']
results = dict(revision=data.REVISION, source_exclusions=len(source_audit['exclusions']), stages={})
for stage, budget in (('', 16777216), ('pilot-2m', 2097152)):
folder = root / stage
task_results = {}
for task in data.TASKS:
manifest = json.loads((folder / 'data' / task / 'manifest.json').read_text())
groups = {}
folds = {}
for split in data.SPLITS:
path = folder / manifest['splits'][split]['path']
assert signature(path) == manifest['splits'][split]['sha256'], (task, split, 'checksum')
folds[split] = list(rows(path))
groups[split] = {row['group'] for row in folds[split]}
assert len(groups[split]) == len(folds[split]), (task, split, 'duplicate source')
for index, left in enumerate(data.SPLITS):
for right in data.SPLITS[index + 1:]:
assert groups[left].isdisjoint(groups[right]), (task, left, right, 'leakage')
by_id = {source['source_id']: source for source in sources[task]['sft']}
@lru_cache(maxsize=200000)
def expected(identity: str, style_id: int):
source = by_id[identity]
assert source['group'] in groups['sft'], (task, identity, 'non-SFT source')
style = data.DEFAULT_STYLE if style_id == -1 else styles[style_id]
row = data.make_row(task, source, style, style_id, include_hints=False)
return envs.render(row), dict(source_id=identity, group=row['group'],
reference_sha256=data.digest(row['reference']), difficulty=source['difficulty'], style_id=style_id)
sft = json.loads((folder / 'sft' / task / 'manifest.json').read_text())
path = folder / sft['path']
assert signature(path) == sft['sha256'], (task, 'SFT checksum')
rng = random.Random(f'audit/{stage}/{task}')
samples = set(rng.sample(range(sft['documents']), min(config.tokenizer_samples, sft['documents'])))
budget_tokens = 0
documents = 0
examples = 0
bands = Counter()
for index, document in enumerate(tqdm(rows(path), total=sft['documents'],
desc=f'Audit {stage or "full"}/{task}', colour='cyan', mininterval=10)):
declared = document['examples']
assert 2 <= len(declared) <= 4
assert len({row['group'] for row in declared}) == len(declared)
rendered = [expected(row['source_id'], row['style_id']) for row in declared]
assert declared == [value[1] for value in rendered], (task, index, 'metadata')
assert document['text'] == '\n\n'.join(value[0] for value in rendered), (task, index, 'source mismatch')
assert 0 < document['budget_tokens'] <= document['tokens'] <= 1024
if index in samples or index == sft['documents'] - 1:
assert len(envs.ENC.encode_ordinary(document['text'])) == document['tokens']
if index == 0:
demos = folds['rl'][0]['demonstrations']
assert [row['group'] for row in declared[:2]] == [row['group'] for row in demos]
if index < sft['documents'] - 1:
assert document['budget_tokens'] == document['tokens']
budget_tokens += document['budget_tokens']
documents += 1
examples += len(declared)
bands.update(row['difficulty'] for row in declared)
assert budget_tokens == sft['training_token_budget'] == budget
assert documents == sft['documents'] and examples == sft['examples']
assert dict(bands) == sft['difficulty_counts']
pool = envs.Pool(folds['rl'])
hints = Counter(pool.sample(rng).hint_level for _ in range(config.sampler_draws))
expected_count = config.sampler_draws / 16
assert all(abs(hints[level] - expected_count) < max(10, expected_count * .2) for level in range(1, 17))
panel = envs.fixed_panel(folds['test'], 64, 2718)
assert Counter(level for row, level in panel) == {level: 4 for level in range(1, 17)}
for row, level in panel:
item = envs.make_episode(row, level)
assert envs.reward(item, item.completion) == 1.
task_results[task] = dict(tokens=budget_tokens, documents=documents, examples=examples,
source_reconstruction='Every SFT document exactly matches its immutable source and declared style',
tokenized_documents=len(samples | {sft['documents'] - 1}),
split_counts={split: len(values) for split, values in folds.items()},
source_splits_disjoint=True, difficulty_counts=dict(bands),
sampled_hint_counts=dict(hints), fixed_panel_gold_passes=64,
sft_sha256=sft['sha256'])
print(f'[green]{stage or "full"}/{task}: {budget_tokens:,} tokens; every document reconstructed[/green]')
results['stages'][stage or 'full'] = task_results
atomic_json(root / 'report-cache/data-audit.json', results)
return results
if __name__ == '__main__':
audit(OmegaConf.to_object(OmegaConf.merge(OmegaConf.structured(Config), OmegaConf.from_cli())))