Improve language tag
#2
by
lbourdois
- opened
README.md
CHANGED
@@ -1,210 +1,222 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
language:
|
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 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
)
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- zho
|
5 |
+
- eng
|
6 |
+
- fra
|
7 |
+
- spa
|
8 |
+
- por
|
9 |
+
- deu
|
10 |
+
- ita
|
11 |
+
- rus
|
12 |
+
- jpn
|
13 |
+
- kor
|
14 |
+
- vie
|
15 |
+
- tha
|
16 |
+
- ara
|
17 |
+
library_name: transformers
|
18 |
+
base_model:
|
19 |
+
- Qwen/Qwen2.5-0.5B
|
20 |
+
---
|
21 |
+
|
22 |
+
簡単な算数問題を解けるように GRPO で学習してみた。学習コードは下の方にあります。
|
23 |
+
|
24 |
+
学習データは簡単な問題なのでその場で合成したものを使いました。(コード参照)
|
25 |
+
|
26 |
+
prompt format:
|
27 |
+
```
|
28 |
+
あなたはアシスタントとして回答します。
|
29 |
+
ユーザーの質問に対して、<think></think>ブロック内で思考してから<answer></answer>でファイナルアンサーしてください。
|
30 |
+
具体的には、「<think>ここに思考過程</think><answer>ここに解答</answer>」という形です。
|
31 |
+
「ユーザー」の質問の後に、「アシスタント」が回答します。
|
32 |
+
ユーザー:
|
33 |
+
次の ? に入る数値を計算して回答してください。
|
34 |
+
{formula}
|
35 |
+
|
36 |
+
アシスタント:
|
37 |
+
```
|
38 |
+
|
39 |
+
example `formula`:
|
40 |
+
```
|
41 |
+
4 + 3 * 2 = ?
|
42 |
+
```
|
43 |
+
|
44 |
+
expected output:
|
45 |
+
```xml
|
46 |
+
<think>思考内容</think><answer>解答</answer>
|
47 |
+
```
|
48 |
+
|
49 |
+
## Example
|
50 |
+
|
51 |
+
```py
|
52 |
+
from transformers import pipeline
|
53 |
+
|
54 |
+
formula = "9 + 3 * 5 = ?" # A + B * C か A * B + C の形式のみ対応
|
55 |
+
|
56 |
+
prompt = f"""\
|
57 |
+
あなたはアシスタントとして回答します。
|
58 |
+
ユーザーの質問に対して、<think></think>ブロック内で思考してから<answer></answer>でファイナルアンサーしてください。
|
59 |
+
具体的には、「<think>ここに思考過程</think><answer>ここに解答</answer>」という形です。
|
60 |
+
「ユーザー」の質問の後に、「アシスタント」が回答します。
|
61 |
+
ユーザー:
|
62 |
+
次の ? に入る数値を計算して回答してください。
|
63 |
+
{formula}
|
64 |
+
|
65 |
+
アシスタント:
|
66 |
+
"""
|
67 |
+
|
68 |
+
print(pipe(prompt)[0]["generated_text"][len(prompt):])
|
69 |
+
# <think>9 + 3 * 5 = 9 + 15 = 24</think><answer>24</answer>
|
70 |
+
```
|
71 |
+
|
72 |
+
## Training information
|
73 |
+
|
74 |
+
- Base model: [Qwen/Qwen2.5-0.5B](https://huggingface.co/Qwen/Qwen2.5-0.5B)
|
75 |
+
- Device: 1x A100 80G
|
76 |
+
- GPU Hour: about 1 hour
|
77 |
+
- Total training steps: 140 steps ([the last checkpoint](https://huggingface.co/p1atdev/qwen2.5-0.5b-grpo-math-01/blob/9ede090f5ed41d88c16ffbc56a81b0772f19679e/model.safetensors))
|
78 |
+
|
79 |
+
Wandb log: https://wandb.ai/p1atdev/grpo-math-01/runs/ytv8wxll
|
80 |
+
|
81 |
+
## Training code
|
82 |
+
|
83 |
+
```py
|
84 |
+
import random
|
85 |
+
import re
|
86 |
+
|
87 |
+
import torch
|
88 |
+
from datasets import Dataset
|
89 |
+
from trl import GRPOConfig, GRPOTrainer
|
90 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
91 |
+
import wandb
|
92 |
+
|
93 |
+
SYSTEM_PROMPT = """命令:
|
94 |
+
あなたはアシスタントとして回答します。
|
95 |
+
ユーザーの質問に対して、<think></think>ブロック内で思考してから<answer></answer>でファイナルアンサーしてください。
|
96 |
+
具体的には、「<think>ここに思考過程</think><answer>ここに解答</answer>」という形です。
|
97 |
+
「ユーザー」の質問の後に、「アシスタント」が回答します。
|
98 |
+
ユーザー:
|
99 |
+
"""
|
100 |
+
MODEL_NAME = "Qwen/Qwen2.5-0.5B"
|
101 |
+
|
102 |
+
def generate_problem():
|
103 |
+
# written by ChatGPT
|
104 |
+
# 1~10 の間のランダムな整数を3つ生成
|
105 |
+
a = random.randint(1, 10)
|
106 |
+
b = random.randint(1, 10)
|
107 |
+
c = random.randint(1, 10)
|
108 |
+
|
109 |
+
# 足し算と掛け算の両方を含むように、2通りのパターンからランダムに選択
|
110 |
+
if random.randint(0, 1) == 0:
|
111 |
+
# パターン1: 足し算+掛け算 => 例: a + b * c
|
112 |
+
expression = f"{a} + {b} * {c}"
|
113 |
+
else:
|
114 |
+
# パターン2: 掛け算+足し算 => 例: a * b + c
|
115 |
+
expression = f"{a} * {b} + {c}"
|
116 |
+
|
117 |
+
# Python の eval() を用いて答えを計算(演算子の優先順位に従う)
|
118 |
+
answer = eval(expression)
|
119 |
+
|
120 |
+
return f"{expression} = ?", answer
|
121 |
+
|
122 |
+
|
123 |
+
def generate_random_pair(max_count: int):
|
124 |
+
for i in range(max_count):
|
125 |
+
formula, answer = generate_problem()
|
126 |
+
question = f"""{SYSTEM_PROMPT}
|
127 |
+
次の ? に入る数値を計算して回答してください。
|
128 |
+
{formula}
|
129 |
+
|
130 |
+
アシスタント:
|
131 |
+
"""
|
132 |
+
yield {"id": i, "prompt": question, "ground_truth": answer}
|
133 |
+
|
134 |
+
|
135 |
+
# format reward
|
136 |
+
FORMAT_PATTERN = re.compile(r"^<think>.*?</think><answer>.*?</answer>$")
|
137 |
+
|
138 |
+
def format_reward_func(completions: list[str], **kwargs):
|
139 |
+
"""Reward function that checks if the completion has a specific format."""
|
140 |
+
matches = [FORMAT_PATTERN.match(content) for content in completions]
|
141 |
+
return [1.0 if match else 0.0 for match in matches]
|
142 |
+
|
143 |
+
|
144 |
+
# answer reward
|
145 |
+
ANSWER_PATTERN = re.compile(r"<answer>(\d+)</answer>")
|
146 |
+
|
147 |
+
def answer_reward_func(completions: list[str], ground_truth: list[str], **kwargs):
|
148 |
+
# Regular expression to capture content inside \boxed{}
|
149 |
+
matches = [ANSWER_PATTERN.search(completion) for completion in completions]
|
150 |
+
contents = [match.group(1) if match else "" for match in matches]
|
151 |
+
# Reward 1 if the content is the same as the ground truth, 0 otherwise
|
152 |
+
return [1.0 if c == str(gt) else 0.0 for c, gt in zip(contents, ground_truth)]
|
153 |
+
|
154 |
+
|
155 |
+
def main():
|
156 |
+
ds = Dataset.from_generator(generate_random_pair, gen_kwargs={"max_count": 100000}) # 100000 is too many, we don't need so much for this task
|
157 |
+
model = AutoModelForCausalLM.from_pretrained(
|
158 |
+
MODEL_NAME,
|
159 |
+
attn_implementation="flash_attention_2",
|
160 |
+
torch_dtype=torch.bfloat16,
|
161 |
+
device_map="auto",
|
162 |
+
)
|
163 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
164 |
+
tokenizer.pad_token = tokenizer.eos_token
|
165 |
+
|
166 |
+
project_name = YOUR_WANDB_PROJECT_NAME
|
167 |
+
push_hub_name = YOUR_PUSH_HUB_NAME
|
168 |
+
|
169 |
+
wandb.init(project=project_name)
|
170 |
+
train_args = GRPOConfig(
|
171 |
+
output_dir="./grpo-01", #! output path
|
172 |
+
use_vllm=False, # True to use vLLM
|
173 |
+
overwrite_output_dir=True,
|
174 |
+
num_train_epochs=10,
|
175 |
+
num_generations=4,
|
176 |
+
per_device_train_batch_size=16,
|
177 |
+
# per_device_eval_batch_size=4,
|
178 |
+
gradient_accumulation_steps=1,
|
179 |
+
gradient_checkpointing=True,
|
180 |
+
learning_rate=1e-4, # maybe a bit high
|
181 |
+
warmup_ratio=0.01,
|
182 |
+
weight_decay=0.01,
|
183 |
+
optim="adamw_8bit",
|
184 |
+
adam_epsilon=1e-8,
|
185 |
+
lr_scheduler_type="cosine_with_min_lr",
|
186 |
+
lr_scheduler_kwargs={
|
187 |
+
"min_lr": 5e-5,
|
188 |
+
"num_cycles": 0.5,
|
189 |
+
},
|
190 |
+
# eval_strategy="steps", # eval did not work well
|
191 |
+
# eval_steps=10,
|
192 |
+
save_steps=10,
|
193 |
+
save_total_limit=2,
|
194 |
+
logging_steps=1,
|
195 |
+
logging_first_step=True,
|
196 |
+
# load_best_model_at_end=True,
|
197 |
+
# metric_for_best_model="eval_loss",
|
198 |
+
torch_compile=False, # compile does not work
|
199 |
+
fp16=False,
|
200 |
+
bf16=True,
|
201 |
+
report_to=["wandb"],
|
202 |
+
hub_model_id=push_hub_name,
|
203 |
+
hub_private_repo=True,
|
204 |
+
push_to_hub=True,
|
205 |
+
save_safetensors=True,
|
206 |
+
)
|
207 |
+
|
208 |
+
trainer = GRPOTrainer(
|
209 |
+
model=model,
|
210 |
+
processing_class=tokenizer,
|
211 |
+
train_dataset=ds,
|
212 |
+
# eval_dataset=ds["test"],
|
213 |
+
reward_funcs=[format_reward_func, answer_reward_func],
|
214 |
+
args=train_args,
|
215 |
+
)
|
216 |
+
|
217 |
+
trainer.train()
|
218 |
+
|
219 |
+
|
220 |
+
if __name__ == "__main__":
|
221 |
+
main()
|
222 |
+
```
|