Repository navigation
Expand file tree
/
Copy pathsample_grpo.py
More file actions
196 lines (179 loc) · 7.32 KB
/
Copy pathsample_grpo.py
File metadata and controls
196 lines (179 loc) · 7.32 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
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
# -*- coding: utf-8 -*-
# Project : ReasoningNER
# Time : 2025/7/31 10:47
# Author : Hui Huang
import argparse
import json
import os
from typing import Literal
from datasets import Dataset
from rner import (
DATA_FORMATS, TASK_SELECTIONS, InputExample, expand_task_selection,
load_data, validate_data_request,
)
from rner.sampling import example_categories, stable_seed, stratified_sample
import random
def sample_for_grpo(
data_path: str,
dataset_config: str,
num_samples: int,
shuffle: bool = True,
max_count: int = -1,
seed: int = 42,
mode: Literal["train", "dev", "test"] = 'train',
task: str = "ner",
data_format: str = "auto",
dataset_subset: str | None = None,
) -> list[InputExample]:
validate_data_request(data_format, task)
with open(dataset_config, "r", encoding="utf-8") as file:
datasets = json.load(file)
tasks = expand_task_selection(task)
examples = []
for current_task in tasks:
if isinstance(datasets, dict):
dataset_list = datasets.get(current_task, [])
else:
if task == "all":
raise ValueError("--task all requires a task-keyed dataset config object")
dataset_list = datasets
examples.extend(load_data(
data_path, dataset_list, mode=mode, task=current_task,
data_format=data_format, dataset_subset=dataset_subset,
))
dataset_examples = {}
for example in examples:
key = (example.task.value, example.source)
if key not in dataset_examples:
dataset_examples[key] = []
dataset_examples[key].append(example)
eligible_examples = []
for key in sorted(dataset_examples):
samples = dataset_examples[key]
random.Random(stable_seed(seed, *key, "cap")).shuffle(samples)
if max_count > 0:
samples = samples[:max_count]
eligible_examples.extend(samples)
if not eligible_examples:
raise ValueError("No examples were loaded; check --task and --dataset_config")
out_examples, quotas = stratified_sample(
eligible_examples,
target=num_samples,
seed=seed,
dataset_key=lambda example: f"{example.task.value}:{example.source}",
categories_key=example_categories,
)
for source, count in quotas.items():
print(f"Task/Source: {source}. Num samples: {count}.")
print("Num Samples: ", len(out_examples))
if shuffle:
random.Random(stable_seed(seed, "sample-grpo", "final")).shuffle(out_examples)
return out_examples
def convert_examples_to_parquet(
examples: list[InputExample],
filename: str,
add_source: bool = False,
split: str = "train",
soft_f1: bool = False):
data = []
for idx, example in enumerate(examples):
data.append({
"data_source": example.source,
"prompt": [
{
"role": "user",
"content": example.to_prompt(add_source=add_source),
}
],
"ability": example.task.value,
"reward_model": {"style": "rule", "ground_truth": example.to_completion()},
"extra_info": {
"split": split,
"index": idx,
"answer": example.to_completion(),
"task": example.task.value,
"soft_f1": soft_f1,
"schema": json.dumps(example.schema, ensure_ascii=False),
"event_schema": json.dumps(example.event_schema, ensure_ascii=False),
},
})
dataset = Dataset.from_list(data)
dataset.to_parquet(filename)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--base_path", type=str, required=True,
help="Base directory where the dataset files are located.")
parser.add_argument("--output_path", type=str, required=True,
help="Path to the output file.")
parser.add_argument(
"--dataset_config",
type=str,
default="config/dataset/in_domain.json",
help="Path to the JSON file that configures the datasets for GRPO."
)
parser.add_argument("--num_samples", type=int, default=5000,
help="The total number of samples.")
parser.add_argument("--num_dev_samples", type=int, default=300,
help="The total number of samples for dev set.")
parser.add_argument("--max_count", type=int, default=10000,
help="Maximum number of samples to consider from each dataset. -1 for no limit.")
parser.add_argument("--seed", type=int, default=42,
help="Random seed for reproducibility.")
parser.add_argument("--shuffle", action='store_true',
help="Whether to shuffle the final sampled dataset.")
parser.add_argument("--add_source", action="store_true", help="Whether to add a comment in the prompt indicating which dataset the sample comes from.")
parser.add_argument("--save_to_verl", action='store_true', help="Whether to save the data in Verl format")
parser.add_argument("--task", choices=TASK_SELECTIONS, default="ner",
help="Extraction task. 'all' requires a task-keyed dataset config.")
parser.add_argument("--data_format", choices=DATA_FORMATS, default="auto")
parser.add_argument("--dataset_subset", type=str, default=None,
help="B2NERD subset; defaults to NER_en (for example, use NER_zh to override).")
parser.add_argument("--soft_f1", action="store_true",
help="Enable Soft-F1 in generated verl reward metadata.")
args = parser.parse_args()
os.makedirs(args.output_path, exist_ok=True)
sampled_examples = sample_for_grpo(
data_path=args.base_path,
dataset_config=args.dataset_config,
num_samples=args.num_samples,
shuffle=args.shuffle,
max_count=args.max_count,
seed=args.seed,
mode='train',
task=args.task,
data_format=args.data_format,
dataset_subset=args.dataset_subset,
)
if args.save_to_verl:
# verl必须要有dev数据
dev_examples = sample_for_grpo(
data_path=args.base_path,
dataset_config=args.dataset_config,
num_samples=args.num_dev_samples,
shuffle=False,
max_count=args.max_count,
seed=args.seed,
mode='dev',
task=args.task,
data_format=args.data_format,
dataset_subset=args.dataset_subset,
)
convert_examples_to_parquet(
sampled_examples,
filename=os.path.join(args.output_path, 'train.parquet'),
add_source=args.add_source,
split='train',
soft_f1=args.soft_f1,
)
convert_examples_to_parquet(
dev_examples,
filename=os.path.join(args.output_path, 'dev.parquet'),
add_source=args.add_source,
split='test',
soft_f1=args.soft_f1,
)
else:
# 将结果写入文件
with open(os.path.join(args.output_path, "train.json"), 'w', encoding='utf-8') as f:
for example in sampled_examples:
f.write(json.dumps(example.to_dict(), ensure_ascii=False) + "\n")