forked from PeterGriffinJin/Search-R1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_validate.py
More file actions
173 lines (137 loc) · 6.34 KB
/
benchmark_validate.py
File metadata and controls
173 lines (137 loc) · 6.34 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
"""
Batch benchmark evaluator for Search-R1 checkpoints.
Usage example (same level as `train_grpo.sh`):
python3 benchmark_validate.py \
actor_rollout_ref.model.path=verl_checkpoints/your_exp/actor/global_step_1000 \
data.val_files=data/nq_search/test.parquet \
data.val_batch_size=256 \
trainer.n_gpus_per_node=8 \
trainer.nnodes=1 \
do_search=true \
max_turns=2 \
retriever.url=http://127.0.0.1:8000/retrieve \
retriever.topk=3
"""
import json
from pprint import pprint
import hydra
import ray
from omegaconf import OmegaConf, open_dict
from verl.utils.reward_score import qa_em
from verl.trainer.ppo.ray_trainer import RayPPOTrainer, ResourcePoolManager, Role
def _select_rm_score_fn(data_source):
if data_source in ['nq', 'triviaqa', 'popqa', 'hotpotqa', '2wikimultihopqa', 'musique', 'bamboogle']:
return qa_em.compute_score_em
raise NotImplementedError
class RewardManager:
"""Function RM aligned with main_ppo.py."""
def __init__(self, tokenizer, num_examine=1, format_score=0.0):
self.tokenizer = tokenizer
self.num_examine = num_examine
self.format_score = format_score
def __call__(self, data):
import torch
if 'rm_scores' in data.batch.keys():
return data.batch['rm_scores']
reward_tensor = torch.zeros_like(data.batch['responses'], dtype=torch.float32)
already_print_data_sources = {}
for i in range(len(data)):
data_item = data[i]
prompt_ids = data_item.batch['prompts']
prompt_length = prompt_ids.shape[-1]
valid_prompt_length = data_item.batch['attention_mask'][:prompt_length].sum()
valid_prompt_ids = prompt_ids[-valid_prompt_length:]
response_ids = data_item.batch['responses']
valid_response_length = data_item.batch['attention_mask'][prompt_length:].sum()
valid_response_ids = response_ids[:valid_response_length]
sequences = torch.cat((valid_prompt_ids, valid_response_ids))
sequences_str = self.tokenizer.decode(sequences)
ground_truth = data_item.non_tensor_batch['reward_model']['ground_truth']
data_source = data_item.non_tensor_batch['data_source']
compute_score_fn = _select_rm_score_fn(data_source)
score = compute_score_fn(solution_str=sequences_str,
ground_truth=ground_truth,
format_score=self.format_score)
reward_tensor[i, valid_response_length - 1] = score
if data_source not in already_print_data_sources:
already_print_data_sources[data_source] = 0
if already_print_data_sources[data_source] < self.num_examine:
already_print_data_sources[data_source] += 1
print(sequences_str)
return reward_tensor
_RAY_WORKER_ENV_VARS = {
'TOKENIZERS_PARALLELISM': 'true',
'NCCL_DEBUG': 'WARN',
'OMP_NUM_THREADS': '1',
'MKL_NUM_THREADS': '1',
'OPENBLAS_NUM_THREADS': '1',
'NUMEXPR_NUM_THREADS': '1',
}
@hydra.main(config_path='verl/trainer/config', config_name='ppo_trainer', version_base=None)
def main(config):
if not ray.is_initialized():
ray.init(runtime_env={'env_vars': _RAY_WORKER_ENV_VARS})
from verl.utils.fs import copy_local_path_from_hdfs
from verl.utils import hf_tokenizer
pprint(OmegaConf.to_container(config, resolve=True))
OmegaConf.resolve(config)
# Benchmark only uses validation; still keep trainer initialization robust.
with open_dict(config):
if not config.data.get('train_files'):
config.data.train_files = config.data.val_files
if not config.data.get('train_data_num'):
config.data.train_data_num = config.data.val_data_num
config.trainer.val_before_train = False
config.trainer.val_only = True
local_path = copy_local_path_from_hdfs(config.actor_rollout_ref.model.path)
tokenizer = hf_tokenizer(local_path)
if config.actor_rollout_ref.actor.strategy == 'fsdp':
from verl.workers.fsdp_workers import ActorRolloutRefWorker, CriticWorker
from verl.single_controller.ray import RayWorkerGroup
ray_worker_group_cls = RayWorkerGroup
elif config.actor_rollout_ref.actor.strategy == 'megatron':
from verl.workers.megatron_workers import ActorRolloutRefWorker, CriticWorker
from verl.single_controller.ray.megatron import NVMegatronRayWorkerGroup
ray_worker_group_cls = NVMegatronRayWorkerGroup
else:
raise NotImplementedError
role_worker_mapping = {
Role.ActorRollout: ray.remote(ActorRolloutRefWorker),
Role.Critic: ray.remote(CriticWorker),
Role.RefPolicy: ray.remote(ActorRolloutRefWorker),
}
global_pool_id = 'global_pool'
resource_pool_spec = {global_pool_id: [config.trainer.n_gpus_per_node] * config.trainer.nnodes}
mapping = {
Role.ActorRollout: global_pool_id,
Role.Critic: global_pool_id,
Role.RefPolicy: global_pool_id,
}
if config.reward_model.enable:
if config.reward_model.strategy == 'fsdp':
from verl.workers.fsdp_workers import RewardModelWorker
elif config.reward_model.strategy == 'megatron':
from verl.workers.megatron_workers import RewardModelWorker
else:
raise NotImplementedError
role_worker_mapping[Role.RewardModel] = ray.remote(RewardModelWorker)
mapping[Role.RewardModel] = global_pool_id
reward_fn = RewardManager(tokenizer=tokenizer, num_examine=0)
val_reward_fn = RewardManager(tokenizer=tokenizer, num_examine=1)
resource_pool_manager = ResourcePoolManager(resource_pool_spec=resource_pool_spec, mapping=mapping)
trainer = RayPPOTrainer(config=config,
tokenizer=tokenizer,
role_worker_mapping=role_worker_mapping,
resource_pool_manager=resource_pool_manager,
ray_worker_group_cls=ray_worker_group_cls,
reward_fn=reward_fn,
val_reward_fn=val_reward_fn)
trainer.init_workers()
try:
metrics = trainer._validate()
print('Benchmark metrics:')
print(json.dumps(metrics, ensure_ascii=False, indent=2))
finally:
trainer.shutdown()
if __name__ == '__main__':
main()