File size: 7,186 Bytes
4318522 ec86d11 d96f432 ec86d11 d96f432 ec86d11 4318522 ec86d11 4318522 ec86d11 d96f432 ec86d11 4318522 ec86d11 d96f432 4318522 d96f432 4318522 d96f432 ec86d11 4318522 d96f432 ec86d11 d96f432 4318522 ec86d11 4318522 d96f432 ec86d11 4318522 ec86d11 d96f432 ec86d11 4318522 d96f432 4318522 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 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
@dataclass
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())))
|