-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtrain_zero_math.py
388 lines (334 loc) · 14.3 KB
/
train_zero_math.py
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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# Copyright 2025 Garena Online Private Limited
#
# 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.
import functools
import itertools
import logging
import time
from dataclasses import dataclass, field
from multiprocessing import Pool, TimeoutError
from typing import Any, List, Literal, Tuple
import numpy as np
import torch
import tree
from oat.actors.base import ActorBase
from oat.algorithms.ppo import PPOActor, PPOArgs, PPOLearner
from oat.args import default_args_validation, get_default_args
from oat.interface import get_program, lp
from oat.oracles.base import PreferenceOracleBase, RewardOracleBase
from oat.types import Metric, TrajectoryData
from oat.utils.data import PromptDataset, load_data_from_disk_or_hf
from torch.utils.data import DataLoader
from datasets import load_from_disk
from understand_r1_zero.math_grader import (answer_tag_reward_fn,
boxed_reward_fn)
"""
1. To do RL from base models, we use proper prompt template to make the base model answer questions.
"""
def apply_qwen_math_template(question: str):
return (
"<|im_start|>system\nPlease reason step by step, and put your final answer within \\boxed{}.<|im_end|>\n<|im_start|>user\n"
+ question
+ "<|im_end|>\n<|im_start|>assistant\n"
)
def apply_r1_template(question: str):
return (
"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 is enclosed within <think> </think> and answer is enclosed within <answer> </answer> tags, respectively, i.e., <think> reasoning process here </think> <answer> answer here </answer>.\nUser: "
+ question
+ "\nAssistant: <think>"
)
def apply_no_template(question: str):
return question
TEMPLATE_FACTORY = {
"qwen_math": apply_qwen_math_template,
"r1": apply_r1_template,
"no": apply_no_template,
}
"""
2. To train reasoning models that solve math questions, we need to define an oracle (environment) that provides rule-based verification rewards.
We instantiate the oracle based on Oat's OracleBase and implement the grading logic.
"""
class MATHOracle(RewardOracleBase, PreferenceOracleBase):
"""Defines the verification rules for the math answer grading."""
def __init__(self, template, verifier_version) -> None:
super().__init__()
if template == "r1":
math_reward_fn = answer_tag_reward_fn
else:
math_reward_fn = boxed_reward_fn
self.math_reward_fn = functools.partial(
math_reward_fn, fast=verifier_version == "fast"
)
# Process pool is used to enable the timeout mechanism for answer grading in our distributed training setup.
self.mp_pool = Pool(2)
def get_reward(
self,
inputs: List[str],
responses: List[str],
references: List[str],
batch_size: int = 4,
) -> Tuple[torch.Tensor, Metric]:
# Parameters used by Oat when using model-based reward, here we don't need.
del inputs, batch_size
rewards = []
infos = []
for resp, ref in zip(responses, references):
res = self.mp_pool.apply_async(self.math_reward_fn, (resp, ref))
try:
info, r = res.get(timeout=1)
rewards.append(r)
infos.append(info)
except TimeoutError:
rewards.append(0.0)
infos.append({"formatted": False})
return torch.tensor(rewards), infos
def compare(
self,
inputs: List[str],
candidates_A: List[str],
candidates_B: List[str],
batch_size: int = 4,
return_probs: bool = False,
disable_tqdm: bool = False,
) -> Tuple[List[Any], Metric]:
"""Facilitates easier evaluation, returning accuracy as winning probability."""
del batch_size, return_probs, disable_tqdm
rewards, info = self.get_reward(inputs, candidates_A, candidates_B)
return rewards.numpy(), info
"""
2. Define extra arguments needed besides Oat's PPOArgs, mainly about choosing the prompt template.
"""
@dataclass
class ZeroMathArgs(PPOArgs):
# Template.
prompt_template: Literal["qwen_math", "no", "r1"] = field(default="qwen_math")
# Evaluation benchmarks used.
test_split: str = "all" # Use "aime,math" to only evaluate on selected benchmarks.
# Verifier.
verifier_version: Literal["fast", "math_verify"] = field(default="fast")
"""
3. Instantiate the actor based on Oat's PPOActor, which controls the reasoning trace generation (`self.sampling_params`) and the rewarding (`self.oracle`).
"""
class ZeroMathActor(PPOActor):
def __init__(self, ipc_server, vllm_args, args: ZeroMathArgs) -> None:
super().__init__(ipc_server, vllm_args, args)
self.oracle = MATHOracle(
template=args.prompt_template, verifier_version=args.verifier_version
)
if args.prompt_template in ["qwen_math", "no"]:
# These two templates are better used for Qwen models, which can themselves stop generation. Hence we unset all external stopping conditions.
self.sampling_params.stop = None
self.sampling_params.stop_token_ids = None
self.eval_sampling_params.stop = None
self.eval_sampling_params.stop_token_ids = None
elif args.prompt_template == "r1":
# Let's stop when the model completes its answer.
self.sampling_params.stop = ["</answer>"]
self.sampling_params.include_stop_str_in_output = True
self.eval_sampling_params.stop = ["</answer>"]
self.eval_sampling_params.include_stop_str_in_output = True
def step(
self,
prompts: List[str],
formatted_prompts: List[str],
references: List[str] = None,
) -> List[TrajectoryData]:
"""Main logic for the actor to generate trajectories (reasoning traces)."""
assert not self.eval_mode
info = {}
logging.info(f"actor start")
# step 1. generate
st = time.time()
outputs = self.generate(formatted_prompts, self.sampling_params)
candidates = []
prompt_token_ids = []
no_eos = []
response_ids = []
response_logprobs = []
resp_lens = []
for i in range(len(outputs)):
# for each prompt
prompt_token_ids.append(outputs[i].prompt_token_ids)
candidates.append([])
response_logprobs.append([])
response_ids.append([])
for k in range(self.sampling_params.n):
# for each response
candidates[i].append(outputs[i].outputs[k].text)
no_eos.append(outputs[i].outputs[k].finish_reason == "length")
token_ids = outputs[i].outputs[k].token_ids
logps = outputs[i].outputs[k].logprobs
logps = [item[token_ids[i]].logprob for i, item in enumerate(logps)]
response_logprobs[i].append(logps)
response_ids[i].append(token_ids)
resp_lens.append(len(token_ids))
info["actor/generate_time"] = time.time() - st
# step 2. verify
st = time.time()
rewards, oracle_infos = self.oracle.get_reward(
list(
itertools.chain.from_iterable(
itertools.repeat(x, self.sampling_params.n) for x in prompts
)
),
tree.flatten(candidates),
list(
itertools.chain.from_iterable(
itertools.repeat(x, self.sampling_params.n) for x in references
)
),
)
info["actor/verify_time"] = time.time() - st
logging.info(f"actor reward {rewards.mean()}")
info["actor/rewards"] = rewards.mean().item()
info["actor/num_data"] = rewards.numel()
info["actor/formatted"] = np.mean([i["formatted"] for i in oracle_infos])
info["actor/response_tok_len"] = np.mean(resp_lens)
info["actor/sampling_max_tokens"] = self.sampling_params.max_tokens
info["actor/sampling_temperature"] = self.sampling_params.temperature
rewards = rewards.reshape(len(prompts), -1)
no_eos = np.array(no_eos).reshape(len(prompts), -1)
info["actor/no_eos_count"] = no_eos.sum()
trajectory_data = []
for i in range(len(candidates)):
prompt = prompts[i]
candidates_per_prompt = candidates[i]
for j in range(len(candidates_per_prompt)):
reward = rewards[i][j].item()
if no_eos[i][j]:
# Set zero reward for truncated outputs.
reward = 0
dense_rewards = [0] * len(response_ids[i][j])
dense_rewards[-1] = reward
trajectory_data.append(
TrajectoryData(
prompt=prompt,
prompt_ids=prompt_token_ids[i],
response=candidates_per_prompt[j],
response_ids=response_ids[i][j],
response_logprobs=response_logprobs[i][j],
rewards=dense_rewards,
loss_mask=not no_eos[i][j] if self.args.ignore_no_eos else True,
info=info,
)
)
logging.info(f"actor finished data_len={len(trajectory_data)}")
handle = self.ipc_client.serialize_ipc(trajectory_data)
return handle
"""
4. Instantiate the learner based on PPOLearner. Here we adapt the `evaluate` logic to run multiple math benchmarks.
"""
class ZeroMathLearner(PPOLearner):
def _init(self, args: ZeroMathArgs, actors: List[ActorBase]) -> None:
super()._init(args, actors)
self.eval_dataset_dict = load_from_disk(args.eval_data) # TODO: get fro HF.
if args.test_split != "all":
self.eval_dataset_dict = {
k: v for k, v in self.eval_dataset_dict.items() if k in args.test_split
}
self.args = args
def _apply_template(self, example):
problem = example[self.args.input_key]
example[self.args.input_key] = TEMPLATE_FACTORY[args.prompt_template](problem)
return example
def prepare_data(self, strategy, tokenizer):
prompt_dataset = load_data_from_disk_or_hf(self.args.prompt_data)
prompts_data = prompt_dataset[args.train_split].select(
range(min(args.max_train, len(prompt_dataset[args.train_split])))
)
# Prepare the data: templated questions & gt final answers.
prompts_data = prompts_data.map(lambda x: self._apply_template(x))
self.prompts_dataset = PromptDataset(
prompts_data,
tokenizer,
strategy,
input_key=args.input_key,
output_key=args.output_key,
apply_chat_template=False, # Because we have applied already.
get_reference=True,
)
self.prompts_dataloader = strategy.setup_dataloader(
self.prompts_dataset,
strategy.args.rollout_batch_size_per_device,
pin_memory=True,
shuffle=True,
)
self.eval_prompts_dataset = self.eval_prompts_dataloader = (
None # We use our own `self.eval_dataset_dict`.
)
def eval_dataloader_collate_fn(self, item_list):
problems = []
formatted_problems = []
answers = []
for item in item_list:
problems.append(item["problem"])
formatted_problems.append(
TEMPLATE_FACTORY[args.prompt_template](item["problem"])
)
answers.append(item["answer"])
return formatted_problems, problems, answers
def evaluate(self, dataloader, steps):
# Discard the default eval dataloader, and run eval on multiple benchmarks.
del dataloader
all_metrics = {}
accuracies = []
scores = []
lens = []
for benchmark_name, dataset in self.eval_dataset_dict.items():
eval_prompts_dataloader = DataLoader(
dataset,
batch_size=self.args.eval_batch_size,
shuffle=False,
drop_last=False,
collate_fn=self.eval_dataloader_collate_fn,
)
metrics = super().evaluate(
eval_prompts_dataloader, f"{steps}_{benchmark_name}"
)
all_metrics.update(
{
k.replace("eval/", f"eval/{benchmark_name}/"): v
for k, v in metrics.items()
}
)
accuracies.append(metrics["eval/accuracy"])
scores.append(metrics["eval/score"])
lens.append(metrics["eval/response_tok_len"])
all_metrics.update(
{
"eval/average/accuracy": np.mean(accuracies),
"eval/average/score": np.mean(scores),
"eval/average/response_tok_len": np.mean(lens),
}
)
return all_metrics
def run_zero_math_rl(args: ZeroMathArgs):
# Define a distributed program that composes Actors and Learners.
program, local_resources = get_program(
args, learner_cls=ZeroMathLearner, actor_cls=ZeroMathActor
)
# Launch the program in a local, multi-processing way!
lp.launch(
program,
launch_type=args.launch_type,
local_resources=local_resources,
terminal="current_terminal",
)
if __name__ == "__main__":
args: ZeroMathArgs = get_default_args(ZeroMathArgs)
# Customization:
args.algo = "PPO"
args.online_evaluation = True # Use GT answer for online verification.
args = default_args_validation(args)
run_zero_math_rl(args)