File size: 5,270 Bytes
de32d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2024 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Preprocess TIGER-Lab/AceCode-87K into verl parquet for the binary code-RL recipe.

Writes the same schema as scripts/preprocess_knights_and_knaves_data.py:
    data_source | prompt (chat, single user turn) | ability | reward_model | extra_info

  * data_source : "acecode" for train, "acecode_eval" for the held-out split
                  (both route to verl/utils/reward_score/acecode.py, binary all-tests-pass)
  * prompt      : the AceCode question + a fixed suffix asking for ONE ```python block
  * ground_truth: JSON {"tests": [...]} straight from the dataset's test list
  * split       : the LAST --eval-size rows of a seeded shuffle are eval, the rest train

Rows are dropped when the question or the tests are unusable (empty / non-assert / too
few tests) or when the prompt would exceed --max-prompt-tokens under the model's chat
template (data.filter_overlong_prompts would drop them at train time anyway; doing it
here keeps the parquet row count honest).

Usage (login node has internet; needs the `maxrl` conda env):
    python scripts/preprocess_acecode_data.py \
        --out-dir /n/netscratch/qianqian_lab/Lab/gzeng/RL-ASSETS/acecode_data/acecode87k \
        --model-path $HOME/.cache/maxrl/models/Qwen3-1.7B-Base
"""

from __future__ import annotations

import argparse
import json
import os

PROMPT_SUFFIX = (
    "\nPlease reason step by step, then write a self-contained Python solution. "
    "Give your final program in a single ```python code block."
)

MIN_TESTS = 3  # AceCoder filtering keeps prompts with enough signal for binary reward


def build_row(question: str, tests: list[str], data_source: str, split: str, index: int, src_id) -> dict:
    return {
        "data_source": data_source,
        "prompt": [{"role": "user", "content": question + PROMPT_SUFFIX}],
        "ability": "code",
        "reward_model": {"style": "rule", "ground_truth": json.dumps({"tests": tests})},
        "extra_info": {"split": split, "index": index, "acecode_id": str(src_id), "num_tests": len(tests)},
    }


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--dataset", default="TIGER-Lab/AceCode-87K")
    ap.add_argument("--out-dir", required=True)
    ap.add_argument("--eval-size", type=int, default=1000)
    ap.add_argument("--seed", type=int, default=42)
    ap.add_argument("--model-path", required=True, help="tokenizer for the prompt-length filter")
    ap.add_argument("--max-prompt-tokens", type=int, default=3072)
    ap.add_argument("--max-tests", type=int, default=16, help="cap tests per prompt (reward caps at 32)")
    args = ap.parse_args()

    import pandas as pd
    from datasets import load_dataset
    from transformers import AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained(args.model_path)
    ds = load_dataset(args.dataset, split="train")
    ds = ds.shuffle(seed=args.seed)

    kept, dropped_tests, dropped_long = [], 0, 0
    seen_questions: set[int] = set()
    for row in ds:
        question = (row.get("question") or "").strip()
        tests = [t.strip() for t in (row.get("test_cases") or []) if isinstance(t, str) and t.strip()]
        tests = [t for t in tests if t.startswith("assert")]
        if not question or len(tests) < MIN_TESTS:
            dropped_tests += 1
            continue
        qh = hash(question)
        if qh in seen_questions:
            dropped_tests += 1
            continue
        seen_questions.add(qh)

        n_tokens = len(
            tokenizer.apply_chat_template(
                [{"role": "user", "content": question + PROMPT_SUFFIX}],
                add_generation_prompt=True,
                tokenize=True,
            )
        )
        if n_tokens > args.max_prompt_tokens:
            dropped_long += 1
            continue
        kept.append((question, tests[: args.max_tests], row.get("id")))

    assert len(kept) > args.eval_size, f"only {len(kept)} usable rows"
    eval_rows = kept[-args.eval_size :]
    train_rows = kept[: -args.eval_size]

    os.makedirs(args.out_dir, exist_ok=True)
    train_df = pd.DataFrame(
        [build_row(q, t, "acecode", "train", i, sid) for i, (q, t, sid) in enumerate(train_rows)]
    )
    eval_df = pd.DataFrame(
        [build_row(q, t, "acecode_eval", "eval", i, sid) for i, (q, t, sid) in enumerate(eval_rows)]
    )
    train_df.to_parquet(os.path.join(args.out_dir, "train.parquet"))
    eval_df.to_parquet(os.path.join(args.out_dir, "eval.parquet"))

    print(
        f"wrote {len(train_df)} train / {len(eval_df)} eval to {args.out_dir} "
        f"(dropped: {dropped_tests} bad-tests/dup, {dropped_long} overlong)"
    )


if __name__ == "__main__":
    main()