File size: 2,909 Bytes
0d8f2b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from itertools import islice

import pytest

from mathematics_rl.data import categories_for, examples


def write_rows(tmp_path, rows):
    target = tmp_path / "rows.ndjson"
    target.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")
    return str(target)


def row(category="interpolate", **changes):
    return {
        "question": "What is 1+1?",
        "answer": "2",
        "category": category,
        "module": "arithmetic__add_or_sub",
        **changes,
    }


def test_language_and_train_eval_isolation(tmp_path):
    source = write_rows(
        tmp_path,
        [
            row(c, language=lang)
            for lang in ("ru", "en")
            for c in ("train-easy", "train-medium", "train-hard", "interpolate", "extrapolate")
        ],
    )
    for language in ("ru", "en"):
        loaded = list(examples(source=source, language=language, mode="train"))
        assert len(loaded) == 3
        assert {item.language for item in loaded} == {language}
        assert {item.category for item in loaded} == {"train-easy", "train-medium", "train-hard"}
        assert all(item.revision == "local" for item in loaded)
    assert len(list(examples(source=source, mode="extrapolate"))) == 1
    with pytest.raises(ValueError):
        categories_for("train", ["interpolate"])


def test_ids_duplicate_rows_selection_and_reference_privacy(tmp_path):
    source = write_rows(tmp_path, [row(answer="987654321"), row(answer="987654321")])
    first = list(examples(source=source))
    second = list(examples(source=source))
    assert first[0].id != first[1].id
    assert [item.id for item in first] == [item.id for item in second]
    assert first[0].content_hash == first[1].content_hash
    assert "987654321" not in first[0].prompt
    assert first[0].question in first[0].prompt


def test_buffer_shuffle_is_deterministic_and_does_not_change_keys(tmp_path):
    source = write_rows(tmp_path, [row(question=f"What is {i}+1?") for i in range(40)])
    original = list(examples(source=source))
    shuffled = list(examples(source=source, shuffle_buffer=5, seed=11))
    again = list(examples(source=source, shuffle_buffer=5, seed=11))
    assert {item.id for item in shuffled} == {item.id for item in original}
    assert shuffled == again
    assert shuffled != original
    assert len(list(islice(examples(source=source), 2))) == 2


@pytest.mark.parametrize(
    "options",
    [
        {"language": "fr"},
        {"modules": ["unknown"]},
        {"mode": "test"},
        {"shuffle_buffer": -1},
        {"revision": "main"},
    ],
)
def test_bad_configuration_fails_before_network(options):
    with pytest.raises(ValueError):
        next(examples(**options))


def test_bad_source_rows_are_loud(tmp_path):
    source = write_rows(tmp_path, [row(module="unknown")])
    with pytest.raises(ValueError):
        list(examples(source=source))