File size: 4,189 Bytes
64b0d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from __future__ import annotations

import argparse
import json
import random
from pathlib import Path


SYSTEM_PROMPT = (
    "Solve exact-answer reasoning problems. "
    "Inside the think block, reason in pseudocode only. "
    "Do not use English prose inside the think block. "
    "After thinking, end with exactly one line formatted as ANSWER: <answer>."
)


USER_INSTRUCTION = (
    "Inside the think block, reason in pseudocode only. "
    "Do not use English prose inside the think block. "
    "Prefer lines like `x = 51`, `x = x + 34`, `state -> x = 85`."
)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("--output-dir", required=True)
    parser.add_argument("--train", type=int, default=2000)
    parser.add_argument("--valid", type=int, default=200)
    parser.add_argument("--test", type=int, default=200)
    parser.add_argument("--seed", type=int, default=20260406)
    return parser.parse_args()


def make_arithmetic_task(rng: random.Random, task_id: str) -> dict[str, str]:
    a = rng.randint(14, 49)
    b = rng.randint(5, 17)
    c = rng.randint(3, 11)
    d = rng.randint(2, 9)
    e = rng.randint(6, 15)
    start = rng.randint(20, 80)
    s1 = start + a
    s2 = s1 + b
    s3 = s2 * c
    sub = d * e
    answer = s3 - sub
    prompt = (
        f"Start with {start}. Add {a}. Add {b}. Multiply the result by {c}. "
        f"Subtract {d} times {e}. What number do you get?"
    )
    think = "\n".join(
        [
            f"x = {start}",
            f"x = x + {a} -> {s1}",
            f"x = x + {b} -> {s2}",
            f"x = x * {c} -> {s3}",
            f"y = {d} * {e} -> {sub}",
            f"x = x - y -> {answer}",
        ]
    )
    return {"id": task_id, "kind": "arithmetic", "prompt": prompt, "answer": str(answer), "think": think}


def make_state_task(rng: random.Random, task_id: str) -> dict[str, str]:
    base_x = rng.randint(2, 8)
    base_y = rng.randint(3, 9)
    base_z = rng.randint(4, 10)
    x = base_x
    y = base_y
    z = base_z
    steps: list[str] = [
        f"x = {base_x}",
        f"y = {base_y}",
        f"z = {base_z}",
    ]

    x = x + y
    steps.append(f"x = x + y -> {x}")
    y = y * 2
    steps.append(f"y = y * 2 -> {y}")
    z = z + x - 1
    steps.append(f"z = z + x - 1 -> {z}")
    x = x * z
    steps.append(f"x = x * z -> {x}")
    y = y + z
    steps.append(f"y = y + z -> {y}")
    answer = x - y
    steps.append(f"result = x - y -> {answer}")

    prompt = (
        "Run this exact program and report the final value of x - y.\n\n"
        f"x = {base_x}\n"
        f"y = {base_y}\n"
        f"z = {base_z}\n"
        "x = x + y\n"
        "y = y * 2\n"
        "z = z + x - 1\n"
        "x = x * z\n"
        "y = y + z"
    )
    think = "\n".join(steps)
    return {"id": task_id, "kind": "state", "prompt": prompt, "answer": str(answer), "think": think}


def make_task(rng: random.Random, task_id: str) -> dict[str, str]:
    if rng.random() < 0.5:
        return make_arithmetic_task(rng, task_id)
    return make_state_task(rng, task_id)


def make_record(task: dict[str, str]) -> dict:
    assistant = f"<think>\n{task['think']}\n</think>\n\nANSWER: {task['answer']}"
    user = f"{USER_INSTRUCTION}\n\nProblem:\n{task['prompt']}"
    return {
        "messages": [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user},
            {"role": "assistant", "content": assistant},
        ]
    }


def write_split(path: Path, count: int, seed: int) -> None:
    rng = random.Random(seed)
    with path.open("w", encoding="utf-8") as handle:
        for index in range(count):
            task = make_task(rng, f"{path.stem}-{index:05d}")
            handle.write(json.dumps(make_record(task)) + "\n")


def main() -> None:
    args = parse_args()
    output_dir = Path(args.output_dir)
    output_dir.mkdir(parents=True, exist_ok=False)
    write_split(output_dir / "train.jsonl", args.train, args.seed)
    write_split(output_dir / "valid.jsonl", args.valid, args.seed + 1)
    write_split(output_dir / "test.jsonl", args.test, args.seed + 2)


if __name__ == "__main__":
    main()