File size: 2,960 Bytes
1a06781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import random

from functional_rl import v2

from . import REVISION

ENC = v2.ENC


def make_episode(row: dict, level: int):
    if type(level) is not int or not 1 <= level <= 16:
        raise ValueError('Missing-token length must be an integer in [1, 16]')
    demos = row.get('demonstrations', ())
    context = ''.join(d['reference']+'\n\n' for d in demos)
    document = context+row['reference']
    ids = ENC.encode_ordinary(document)
    prompt_ids, completion_ids = ids[:-level], ids[-level:]
    prompt, completion = ENC.decode(prompt_ids), ENC.decode(completion_ids)
    protected = context+row['protected_prefix']
    if len(ids)>1024 or not prompt.startswith(protected) or prompt+completion != document:
        raise ValueError('Suffix must remove only the function body, preserving its complete signature and docstring')
    return v2.Episode('basic_python', row['source_id'], row['group'], level,
        prompt, completion, tuple(prompt_ids), tuple(completion_ids),
        prompt[len(context):], row['reference'], row['answer'], tuple(row['tests']), '',
        version=REVISION, question=row['question'], provenance=row['provenance'],
        demonstrations=tuple(dict(task='basic_python',source_id=d['source_id'],group=d['group'],
            text=d['reference'],split='sft') for d in demos))


def available_hints(row: dict) -> list[int]:
    result = []
    for level in range(1,17):
        try:
            make_episode(row,level)
            result.append(level)
        except ValueError:
            pass
    return result


class Pool:
    def __init__(self, rows: list[dict]):
        self.by_hint = {level: {} for level in range(1,17)}
        for row in rows:
            for level in row['hint_levels']:
                self.by_hint[level].setdefault(row['provenance']['family'],[]).append(row)
        if any(not groups for groups in self.by_hint.values()):
            raise ValueError('At least one function must support each missing-token length')

    def sample(self, rng: random.Random):
        level = rng.randint(1,16)
        families = self.by_hint[level]
        family = rng.choice(sorted(families))
        return make_episode(rng.choice(families[family]),level)


def fixed_panel(rows: list[dict], queries: int, seed: int):
    if queries < 16 or queries % 16:
        raise ValueError('Use a positive multiple of 16 evaluation questions')
    pool = Pool(rows)
    panel, counts = [], {}
    for index in range(queries):
        level = index%16+1
        families = sorted(pool.by_hint[level])
        family = families[(index//16)%len(families)]
        candidates = sorted(pool.by_hint[level][family],key=lambda row: hashlib.sha256(
            f'{seed}/{level}/{row["group"]}'.encode()).hexdigest())
        key=(level,family)
        position=counts.get(key,0)
        row=candidates[position%len(candidates)]
        counts[key]=position+1
        panel.append((row,level))
    return panel