File size: 5,294 Bytes
e6f9ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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 the GSM8k dataset to parquet format
"""

import re
import os
import datasets

# from verl.utils.hdfs_io import copy, makedirs
import argparse

import json
import pandas as pd
from transformers import AutoTokenizer
from tqdm import tqdm
from sklearn.model_selection import train_test_split

tokenizer_path = "./model/Qwen3-0.6B"
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
MAX_PROMPT_LENGTH = 16384
# SYSTEM_PROMPT = "A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively, i.e., <think> reasoning process here </think> <answer> answer here </answer>."
data_source = 'deepmath_new'


def read_jsonl(file_path):
    data_list = []
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        for line in lines:
            temp_json = json.loads(line)
            data_list.append(temp_json)
    
    return data_list

def preprocess(question_raw, use_chat=True, add_think_token=True, use_system=False):
    post_turn_text = question_raw
    # turn_list = []
    # if use_chat:
    #     if use_system:
    #         turn_list.append({'role':'system','content':SYSTEM_PROMPT},{'role':'user','content':question_raw})
    #     else:
    #         turn_list.append({'role':'user','content':question_raw})
    #     post_turn_text = tokenizer.apply_chat_template(
    #         turn_list, 
    #         add_generation_prompt=True, 
    #         tokenize=False,
    #         enable_thinking=add_think_token
    #     )
    # if add_think_token:
    #     post_turn_text += "<think>\n"
    return post_turn_text

def make_map_fn(split, use_chat=True, add_think_token=True, use_system=False):
    def process_fn(example, idx, split, use_chat=use_chat, add_think_token=add_think_token, use_system=use_system):
        question = example.pop('input')
        # question = preprocess(question_raw, use_chat=use_chat, add_think_token=add_think_token)

        tip_prompt = "Put your final answer within \\boxed{{}}."
        question = question + tip_prompt
        answer = example.pop('answer')

        # print('\n\n')
        # # print(question)
        # print(answer)
        # print('\n\n')

        data = {
            "data_source": data_source,  # 需要替换成实际的数据源
            "prompt": [{
                "role": "user",
                "content": question,
            }],
            "ability": "xxx",
            "reward_model": {
                "style": "rule",
                "ground_truth": answer
            },
            "extra_info": {
                'split': split,
                'index': idx,
            }
        }
        return data

    return process_fn

def process_json_file(data, split):
    process_fn = make_map_fn(split, use_chat=True, add_think_token=True, use_system=False)
    processed_data = [process_fn(example, idx, split) for idx, example in tqdm(enumerate(data))]
    print(f"原始数据数量: {len(processed_data)}")
    filter_processed_data = [item for item in processed_data if len(item['prompt'][0]['content'])<=MAX_PROMPT_LENGTH]
    print(f"过滤后数据数量: {len(filter_processed_data)}")
    
    return filter_processed_data

def save_to_parquet(data, output_file):
    print(f"数据已保存为 {output_file}")
    print(f"data_size: {len(data)}")
    df = pd.DataFrame(data)
    df.to_parquet(output_file)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--local_path', default='./verl-distillation-ori/data')
    parser.add_argument('--hdfs_dir', default=None)

    args = parser.parse_args()
    local_path = args.local_path

    split_ratio = -1
    input_path = './dpo_training/data/sample_deepmath_train.json'
    input_data = read_jsonl(input_path)

    if split_ratio == -1:
        train_data = input_data
    else:
        train_data, test_data = train_test_split(input_data, test_size=split_ratio,random_state=42)
        test_processed_data = process_json_file(test_data, split='test')
        output_test_file = os.path.join(args.local_path,f'{data_source}/test.parquet') # 保存为Parquet文件
        save_to_parquet(test_processed_data, output_test_file)

    train_processed_data = process_json_file(train_data, split='train')
    output_train_file = os.path.join(args.local_path,f'{data_source}/{data_source}_train.parquet') # 保存为Parquet文件
    save_to_parquet(train_processed_data, output_train_file)