Download code/refined_math/aligned.py from davidheineman/tiny-rl-refined-math-v1: direct link, hf CLI and curl.
- Browser
- Download file 7.19 kB
-
https://huggingface.co/datasets/davidheineman/tiny-rl-refined-math-v1/resolve/main/code/refined_math/aligned.py
- Command line
-
hf download hf://datasets/davidheineman/tiny-rl-refined-math-v1/code/refined_math/aligned.py
-
curl -L -o aligned.py https://huggingface.co/datasets/davidheineman/tiny-rl-refined-math-v1/resolve/main/code/refined_math/aligned.py
7.19 kB
| from dataclasses import dataclass | |
| import gzip | |
| import json | |
| from pathlib import Path | |
| from omegaconf import OmegaConf | |
| from rich import print | |
| from refined_v4.study import read, write | |
| from . import data | |
| class Config: | |
| root: str = str(data.ROOT / 'iterations/aligned-v1') | |
| tokens: int = 2097152 | |
| canonical_fraction: float = .5 | |
| bridge_fraction: float = 0. | |
| small_final_fraction: float = 0. | |
| auxiliary_fraction: float = 0. | |
| original_make_row = data.make_row | |
| def render_row(task: str, source: dict, style: dict, style_id: int, include_hints: bool = True): | |
| row = original_make_row(task, source, style, style_id, include_hints=False) | |
| if task in ('gsm8k', 'partial_gsm8k') and source['source'] == 'math-v1/small-arithmetic': | |
| row['provenance']['source_variant'] = 'bounded_arithmetic_auxiliary' | |
| if style.get('arithmetic_bridge') and task in ('gsm8k', 'partial_gsm8k'): | |
| expression = source['steps'][-1]['expression'] | |
| row['question'] = 'Evaluate ' + expression + '.' | |
| row['reference'] = ('Reasoning: ' + expression + '\nAnswer: ' + source['answer'] | |
| if task == 'gsm8k' else 'Answer: ' + expression + '\nSo, the answer is ' + expression + ' = ' + source['answer']) | |
| row['provenance']['source_variant'] = 'arithmetic_bridge' | |
| row['provenance']['rendered_question_sha256'] = data.digest(row['question']) | |
| row['provenance']['teacher_controls'] = 'Computation subtask from the verified final training-source equation. Original word problem remains in provenance; source group and answer are unchanged.' | |
| if include_hints: | |
| row['hint_levels'] = list(data.envs.available_hints(row)) | |
| return row | |
| def build(config: Config): | |
| root = Path(config.root) | |
| root.mkdir(parents=True, exist_ok=True) | |
| original = read(data.ROOT / 'styles.json') | |
| if not 0 <= config.canonical_fraction <= 1 or not 0 <= config.bridge_fraction < 1: | |
| raise ValueError('Invalid curriculum mixture') | |
| styles = ([data.DEFAULT_STYLE] if config.canonical_fraction == 1 else | |
| [data.DEFAULT_STYLE] * round(len(original['styles']) * config.canonical_fraction / (1 - config.canonical_fraction)) + original['styles']) | |
| bridges = round(len(styles) * config.bridge_fraction / (1 - config.bridge_fraction)) | |
| styles += [dict(data.DEFAULT_STYLE, arithmetic_bridge=True)] * bridges | |
| style_path = root / 'styles.json' | |
| if not style_path.exists(): | |
| write(style_path, dict(styles=styles, source_styles_sha256=data.signature(data.ROOT / 'styles.json'), | |
| small_final_fraction=config.small_final_fraction, | |
| auxiliary_fraction=config.auxiliary_fraction, | |
| policy=f'{config.canonical_fraction:.0%} canonical wording among full examples; {config.bridge_fraction:.0%} computation-subtask examples for GSM; immutable source folds.')) | |
| if read(style_path)['styles'] != styles: | |
| raise ValueError('Frozen aligned styles differ') | |
| if read(style_path).get('small_final_fraction', 0.) != config.small_final_fraction: | |
| raise ValueError('Frozen curriculum sampling differs') | |
| if read(style_path).get('auxiliary_fraction', 0.) != config.auxiliary_fraction: | |
| raise ValueError('Frozen auxiliary sampling differs') | |
| if not (root / 'data').exists(): | |
| (root / 'data').symlink_to(data.ROOT / 'pilot-2m/data', target_is_directory=True) | |
| settings = data.Config(root=str(root), tokens=config.tokens, pilot_tokens=min(2097152, config.tokens), | |
| small_final_fraction=config.small_final_fraction, auxiliary_fraction=config.auxiliary_fraction) | |
| pools, audit = data.base_sources(settings) | |
| auxiliary_groups = {source['group'] for source in pools['arithmetic']['sft']} | |
| heldout_groups = {source['group'] for task in data.TASKS for split in data.SPLITS if split != 'sft' | |
| for source in pools[task][split]} | |
| if auxiliary_groups & heldout_groups: | |
| raise ValueError('Auxiliary source group appears in a held-out fold') | |
| data.make_row = render_row | |
| data.build_sft(settings, pools, styles) | |
| result = {} | |
| for task in data.TASKS: | |
| folder = root / 'sft' / task | |
| manifest = read(folder / 'manifest.json') | |
| total, canonical, examples, bridge_count, small_count, auxiliary_count = 0, 0, 0, 0, 0, 0 | |
| by_id = {source['source_id']: source for source in pools[task]['sft']} | |
| if task in ('gsm8k', 'partial_gsm8k') and config.auxiliary_fraction: | |
| by_id.update({source['source_id']: source for source in pools['arithmetic']['sft']}) | |
| with gzip.open(folder / 'train.jsonl.gz', 'rt') as stream: | |
| for line in stream: | |
| document = json.loads(line) | |
| total += document['budget_tokens'] | |
| reconstructed = [] | |
| for example in document['examples']: | |
| examples += 1 | |
| auxiliary_count += (task in ('gsm8k', 'partial_gsm8k') | |
| and by_id[example['source_id']]['source'] == 'math-v1/small-arithmetic') | |
| small_count += data.small_final_calculation(by_id[example['source_id']]) | |
| style = data.DEFAULT_STYLE if example['style_id'] == -1 else styles[example['style_id']] | |
| bridge = bool(style.get('arithmetic_bridge') and task in ('gsm8k', 'partial_gsm8k')) | |
| bridge_count += bridge | |
| canonical += not bridge and all(style[k] == data.DEFAULT_STYLE[k] for k in data.DEFAULT_STYLE) | |
| row = render_row(task, by_id[example['source_id']], style, example['style_id'], include_hints=False) | |
| reconstructed.append(data.envs.render(row)) | |
| if data.digest(row['reference']) != example['reference_sha256']: | |
| raise ValueError('Curriculum reference differs from its declared source') | |
| if document['text'] != '\n\n'.join(reconstructed): | |
| raise ValueError('Curriculum document does not reconstruct exactly') | |
| if total != config.tokens or manifest['sha256'] != data.signature(folder / 'train.jsonl.gz'): | |
| raise ValueError('Aligned corpus budget or checksum differs') | |
| result[task] = dict(prediction_tokens=total, examples=examples, | |
| frozen_style_fraction=canonical / examples, sha256=manifest['sha256']) | |
| result[task]['bridge_example_fraction'] = bridge_count / examples | |
| result[task]['small_final_example_fraction'] = small_count / examples | |
| result[task]['auxiliary_example_fraction'] = auxiliary_count / examples | |
| result[task]['small_final_training_sources'] = sum(data.small_final_calculation(source) | |
| for source in by_id.values()) | |
| write(root / 'alignment-audit.json', dict(tasks=result, source_environment=str(data.ROOT / 'pilot-2m/data'), | |
| source_splits_unchanged=True, auxiliary_groups_disjoint_from_all_heldout_folds=True, | |
| task_revision='math-style-alignment-v1')) | |
| print('[green]Aligned 2M corpora ready[/green]', result) | |
| if __name__ == '__main__': | |
| build(OmegaConf.to_object(OmegaConf.merge(OmegaConf.structured(Config), OmegaConf.from_cli()))) | |