| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| """ |
| Preprocess the GSM8k dataset to parquet format |
| """ |
|
|
| import re |
| import os |
| import datasets |
|
|
| |
| 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 |
| |
| 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 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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') |
| |
|
|
| tip_prompt = "Put your final answer within \\boxed{{}}." |
| question = question + tip_prompt |
| answer = example.pop('answer') |
|
|
| |
| |
| |
| |
|
|
| 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') |
| 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') |
| save_to_parquet(train_processed_data, output_train_file) |
| |
| |