-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathretool.py
More file actions
106 lines (87 loc) · 4.14 KB
/
Copy pathretool.py
File metadata and controls
106 lines (87 loc) · 4.14 KB
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
# 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.
import asyncio
import logging
import re
from typing import Any
import datasets
from recipe.retool.retool_dataset_utils import map_fn, map_fn2
from verl.tools.base_tool import OpenAIFunctionToolSchema
from verl.tools.sandbox_fusion_tools import SandboxFusionTool
from verl.tools.schemas import ToolResponse
from verl.utils.dataset import RLHFDataset
from verl.utils.reward_score import math_dapo
from verl.utils.rollout_trace import rollout_trace_op
logger = logging.getLogger(__name__)
class CustomSandboxFusionTool(SandboxFusionTool):
def __init__(self, config: dict, tool_schema: OpenAIFunctionToolSchema):
super().__init__(config, tool_schema)
self.code_pattern = re.compile(r"```python(.*?)```", re.DOTALL)
@rollout_trace_op
async def execute(self, instance_id: str, parameters: dict[str, Any], **kwargs) -> tuple[ToolResponse, float, dict]:
code = parameters["code"]
matches = self.code_pattern.findall(code)
if matches:
code = matches[0].strip()
# NOTE: some script may not explicitly print result, we need to add a print statement to the end of the script
lines = code.split("\n")
for i, line in reversed(list(enumerate(lines))):
if line == "":
continue
if not lines[i].startswith("print"):
lines[i] = f"print({line})"
break
code = "\n".join(lines)
timeout = parameters.get("timeout", self.default_timeout)
language = parameters.get("language", self.default_language)
if not isinstance(code, str):
code = str(code)
if self.use_ray_execution_pool:
result = await self.execution_pool.execute.remote(self.execute_code, instance_id, code, timeout, language)
else:
async with self._async_semaphore:
result = await asyncio.get_running_loop().run_in_executor(
None, self.execute_code, instance_id, code, timeout, language
)
if isinstance(result, ToolResponse):
return result, None, None
return ToolResponse(text=None if result is None else str(result)), None, None
class CustomRLHFDataset(RLHFDataset):
"""Custom dataset class to process Maxwell-Jia/AIME_2024, yentinglin/aime_2025 datasets."""
def _read_files_and_tokenize(self):
dataframes = []
for parquet_file in self.data_files:
# read parquet files and cache
dataframe = datasets.load_dataset(parquet_file)["train"]
data_source = "/".join(parquet_file.split("/")[-2:])
if data_source in ["Maxwell-Jia/AIME_2024", "yentinglin/aime_2025"]:
dataframe = dataframe.map(
map_fn, fn_kwargs={"data_source": data_source}, remove_columns=dataframe.column_names
)
else:
dataframe = dataframe.map(map_fn2, num_proc=16)
dataframes.append(dataframe)
self.dataframe: datasets.Dataset = datasets.concatenate_datasets(dataframes)
print(f"dataset len: {len(self.dataframe)}")
def compute_score(data_source, solution_str, ground_truth, extra_info, **kwargs):
# use \\boxed{...} answer
result = math_dapo.compute_score(solution_str, ground_truth, strict_box_verify=True)
# encourage model to call tools
num_turns = extra_info["num_turns"]
if result["score"] < 0:
tool_call_reward = (num_turns - 2) / 2 * 0.1
result["score"] = min(-0.6, result["score"] + tool_call_reward)
if result["pred"] is None:
result["pred"] = ""
return result