data.py 4.01 KB
Newer Older
chenych's avatar
chenych committed
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
import re
from datasets import load_dataset, Dataset

SYSTEM_PROMPT = """
Respond in the following format:
<reasoning>
...
</reasoning>
<answer>
...
</answer>
"""

def extract_hash_answer(text: str) -> str | None:
    if "####" not in text:
        return None
    return text.split("####")[1].strip().replace(",", "").replace("$", "")

def extract_deepseek_r1_answer(text) -> str | None:
    words_to_check = ["applied_math", "Advanced-Math", "GSM8K_zh", 'EduChat-Math']
    pattern = r'\b(' + '|'.join(map(re.escape, words_to_check)) + r')\b'
    has_match = bool(re.search(pattern, text['repo_name'], flags=re.IGNORECASE))
    if has_match:
        pattern = r"\\boxed\{(.*)\}"
        match = re.search(pattern, text['output'])
        if match:
            return match.group(1)
        else:
            return None
    else:
        return None

# uncomment middle messages for 1-shot prompting
def get_gsm8k_questions(dataset='openai/gsm8k', split="train") -> Dataset:
    data = load_dataset(dataset, 'main')[split] # type: ignore
    data = data.map(lambda x: { # type: ignore
        'prompt': [
            {'role': 'system', 'content': SYSTEM_PROMPT},
            {'role': 'user', 'content': x['question']}
        ],
        'answer': extract_hash_answer(x['answer'])
    },
    num_proc=16,
    remove_columns=["question"]) # type: ignore
    data = data.filter(lambda x: x['answer'] is not None, num_proc=16)
    # print("---", data[0])
    return data # type: ignore

def get_deepseek_r1_questions(dataset='Congliu/Chinese-DeepSeek-R1-Distill-data-110k-SFT', split="train") -> Dataset:
    data = load_dataset(dataset)[split] # type: ignore
    data = data.map(lambda x: {
        'prompt': [
            {'role': 'system', 'content': SYSTEM_PROMPT},
            {'role': 'user', 'content': x['instruction']}
        ],
        'answer': extract_deepseek_r1_answer(x)
    },
    num_proc=16, # type: ignore
    remove_columns=["instruction", "output", "repo_name", "prompt_tokens_len", "input", "reasoning_content_tokens_len", "score", "content_tokens_len"],
    )

    data = data.filter(lambda x: x['answer'] is not None, num_proc=32) # type: ignore
    print("GET {} data in Chinese-DeepSeek-R1-Distill-data-110k-SFT".format(len(data)))
    return data # type: ignore

def get_hiyoga(dataset='hiyouga/math12k', split='train')-> Dataset:
    data = load_dataset(dataset)[split] # type: ignore
    data = data.map(lambda x: {
        'prompt': [
            {'role': 'system', 'content': SYSTEM_PROMPT},
            {'role': 'user', 'content': x['problem']}
        ],
        'answer': x['answer']
    },
    remove_columns=["problem"],
    num_proc=16,
    )
    data = data.filter(lambda x: x['answer'] is not None, num_proc=16)
    # print(len(data))
    return data # type: ignore

def get_unsloth_openmath(dataset="unsloth/OpenMathReasoning-mini", split='cot') -> Dataset:
    data = load_dataset(dataset)[split]
    data = data.map(lambda x: {
        'prompt': [
            {'role': 'system', 'content': SYSTEM_PROMPT},
            {'role': 'user', 'content': x['problem']}
        ],
        'answer': x['expected_answer']
    },
    remove_columns=["expected_answer", "problem_type", "problem_source", "generation_model", "pass_rate_72b_tir", "generated_solution", "inference_mode", "problem",],
    num_proc=16,
    )
    data = data.filter(lambda x: x['answer'] is not None, num_proc=16)

    # print("len of unsloth", len(data))
    # print("=====", data)
    return data # type: ignore


def get_openr1_dapo_math(dataset="open-r1/DAPO-Math-17k-Processed", split="train") -> Dataset:
    data = load_dataset(dataset, "all")[split]
    data = data.map(lambda x: {
        'prompt': [
            {'role': 'system', 'content': SYSTEM_PROMPT},
            {'role': 'user', 'content': x['prompt']}
        ],
        'answer': x['solution']
    },
    remove_columns=["solution", "data_source", "source_prompt", "ability", "reward_model", "extra_info"],
    num_proc=16,
    )
    data = data.filter(lambda x: x['answer'] is not None, num_proc=16)

    return data # type: ignore