File size: 4,305 Bytes
930d0cb
 
 
 
f1b4114
930d0cb
 
f1b4114
930d0cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reproduce v2 selection from pinned, unchanged v1 payloads and measured pass counts."""
import argparse
import hashlib
import json
from pathlib import Path
import pyarrow as pa
import pyarrow.parquet as pq
from huggingface_hub import hf_hub_download

def main():
    ap=argparse.ArgumentParser(description=__doc__)
    ap.add_argument('--out',type=Path,required=True)
    ap.add_argument('--source',type=Path)
    args=ap.parse_args()
    args.out.mkdir(parents=True,exist_ok=True)
    assert not any((args.out/name).exists() for name in ['tasks.parquet','archive.parquet','pass_counts.parquet'])
    selection=json.loads((Path(__file__).parent/'metadata/selection.json').read_text())
    source=args.source or Path(hf_hub_download(selection['repo_id'],selection['source_folder']+'/tasks.parquet',
        revision=selection['source_revision'],repo_type='dataset',cache_dir=args.out/'.source-cache'))
    assert hashlib.sha256(source.read_bytes()).hexdigest()==selection['source_sha256']
    pf=pq.ParquetFile(source)
    fields=['instruct_successes','instruct_graded_attempts','instruct_missing_attempts',
        'coder_successes','coder_graded_attempts','coder_missing_attempts',
        'target_attempts_per_model','instruct_target_attempts','coder_target_attempts']
    schema=pf.schema_arrow.remove_metadata()
    for field in fields:schema=schema.append(pa.field(field,pa.int64()))
    kept=[];excluded=[];counts=[];seen=set()
    for batch in pf.iter_batches(batch_size=32):
        for row in batch.to_pylist():
            task=row['path'];v=selection['tasks'][task]
            assert task not in seen and row['content_sha256']==v['content_sha256']
            seen.add(task)
            retain=v['instruct_successes']>0 or v['coder_successes']>0
            assert retain==v['kept']
            row.update({field:v[field] for field in fields})
            if retain:kept.append(row)
            else:
                row.update(archive_stage=6,archive_reason=v['exclusion_reason'],archive_labels=[v['exclusion_reason']])
                excluded.append(row)
            for model,budget in selection['target_attempts'].items():
                if v[model+'_graded_attempts']==budget:
                    counts.append({'path':task,'content_sha256':v['content_sha256'],'model':selection['models'][model],
                        'attempts':budget,'graded':budget,'solved':v[model+'_successes'],'retained_v2':retain})
    assert seen==set(selection['tasks'])
    assert len(kept)==selection['summary']['solved'] and len(excluded)==selection['summary']['excluded']
    def metadata(rows):
        image=json.loads(pf.schema_arrow.metadata[b'ot.images.v1']);names={r['path'] for r in rows}
        image['tasks']={k:v for k,v in image['tasks'].items() if k in names}
        bundles={e['bundle'] for task in image['tasks'].values() for e in task['environments']}
        image['bundles']={k:v for k,v in image['bundles'].items() if k in bundles}
        if 'uncached_task_ids' in image:image['uncached_task_ids']=sorted(names-set(image['tasks']))
        image['coverage']={'cached_tasks':len(image['tasks']),'total_tasks':len(names)}
        assert len(image['tasks'])==len(names)
        return {b'ot.images.v1':json.dumps(image).encode(),
            b'ot.release.v2':json.dumps({k:v for k,v in selection.items() if k!='tasks'}).encode()}
    pq.write_table(pa.Table.from_pylist(kept,schema=schema).replace_schema_metadata(metadata(kept)),args.out/'tasks.parquet',compression='zstd')
    archive_schema=schema
    for name,kind in [('archive_stage',pa.int64()),('archive_reason',pa.string()),('archive_labels',pa.list_(pa.string()))]:
        archive_schema=archive_schema.set(archive_schema.get_field_index(name),pa.field(name,kind))
    pq.write_table(pa.Table.from_pylist(excluded,schema=archive_schema).replace_schema_metadata(metadata(excluded)),args.out/'archive.parquet',compression='zstd')
    # Preserve model-first order used in the release pass-count table.
    counts.sort(key=lambda r:(list(selection['models'].values()).index(r['model']),r['path']))
    pq.write_table(pa.Table.from_pylist(counts),args.out/'pass_counts.parquet',compression='zstd')
    print(json.dumps({'kept':len(kept),'excluded':len(excluded),'pass_count_rows':len(counts)}))

if __name__=='__main__':main()