forked from ContextualAI/HALOs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.py
227 lines (193 loc) · 8.95 KB
/
label.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
"""
A script for creating feedback datasets from data that was sampled with train.sample.
Sample usage with Accelerate would be
accelerate launch --config_file accelerate_config/fsdp_4gpu.yaml --main_process_port 29500 \
label.py models/llama3-8B-bt/FINAL outputs.json reward_data.json --feedback_type pairwise
where models/llama3-8B-bt/FINAL is the reward model that was trained using a BradleyTerryTrainer,
outputs.json consists of samples produced by train.sample, and the type of feedback generated is
pairwise.
"""
import argparse
import json
import os
import torch
import random
from accelerate import Accelerator
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from tqdm import tqdm
from typing import List, Dict
import re
from train.utils import StreamingJSONWriter
from train.dataloader import SFTDataLoader
from collections import defaultdict
def process_batch(batch: Dict,
reward_model: AutoModelForSequenceClassification,
accelerator: Accelerator) -> List[Dict]:
"""
Process a batch through the reward model using the already tokenized sequences.
Add the reward as a field to each item.
"""
reward_model.eval()
with torch.no_grad():
outputs = reward_model(
input_ids=batch['target_combined_input_ids'],
attention_mask=batch['target_combined_attention_mask']
)
# Use the positive class logit as the reward score
reward_scores = outputs.logits[:, 1]
# Create list of dicts with all necessary information
processed_samples = []
for i in range(len(batch['prompt'])):
sample = {
'prompt_key': ' '.join([ x['content'] for x in batch['prompt'][i] ]), # flattened prompt
'prompt': batch['prompt'][i],
'instruction': batch['original_prompt'][i] if 'original_prompt' in batch else batch['prompt'][i][0]['content'],
'output': batch['target'][i],
'reward': reward_scores[i].item()
}
processed_samples.append(sample)
# Gather using all_gather_object
gathered_samples = [None] * accelerator.num_processes
torch.distributed.all_gather_object(gathered_samples, processed_samples)
# Only return flattened list on main process
if accelerator.is_main_process:
return [item for sublist in gathered_samples for item in sublist]
return []
def convert_batch_to_binary_feedback(samples: List[Dict], threshold: float=0.5) -> List[Dict]:
"""
Convert samples to binary feedback format. A sample is considered desirable if its
reward crosses the threshold (label = 1) and undesirable otherwise (label = 0).
"""
feedback = []
for sample in samples:
feedback_item = {
'prompt_key': sample['prompt_key'],
'prompt': sample['prompt'],
'output': sample['output'],
'label': 1 if sample['reward'] > threshold else 0,
'reward': sample['reward'],
'type': 'binary_feedback',
}
feedback.append(feedback_item)
return feedback
def convert_to_pairwise_feedback(samples: List[Dict], seed: int, threshold: float=0) -> List[Dict]:
"""
Convert samples to pairwise feedback format.
"""
random.seed(seed)
# Group samples by prompt_id
grouped = defaultdict(list)
for sample in samples:
grouped[sample['prompt_key']].append(sample)
feedback = []
for prompt_key, group in grouped.items():
if len(group) < 2:
continue
# Sort by reward
group.sort(key=lambda x: x['reward'], reverse=True)
# Create pairs
for i in range(len(group) - 1):
higher_reward, lower_reward = group[i], group[i + 1]
if higher_reward['reward'] == lower_reward['reward']:
continue
if threshold and abs(higher_reward['reward'] - lower_reward['reward']) < threshold:
continue
# Randomly decide order
if random.random() < 0.5:
sample_A, sample_B = higher_reward, lower_reward
label = 1
else:
sample_A, sample_B = lower_reward, higher_reward
label = 0
feedback_item = {
'prompt_key': prompt_key,
'prompt': sample_A['prompt'],
'output_A': sample_A['output'],
'output_B': sample_B['output'],
'label': label,
'reward_A': sample_A['reward'],
'reward_B': sample_B['reward'],
'reward_difference': abs(sample_A['reward'] - sample_B['reward']),
'type': 'pairwise_feedback',
}
feedback.append(feedback_item)
return feedback
def main(args):
accelerator = Accelerator()
if accelerator.is_main_process:
print(f"Loading reward model from {args.reward_model_path}")
reward_model = AutoModelForSequenceClassification.from_pretrained(
args.reward_model_path,
local_files_only=True,
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
args.reward_model_path,
local_files_only=True,
trust_remote_code=True
)
reward_model = accelerator.prepare(reward_model)
# Initialize the dataloader with samples file
dataloader = SFTDataLoader(
dataset_names=[args.samples_path],
tokenizer=tokenizer,
process_index=accelerator.process_index,
num_processes=accelerator.num_processes,
split='train', # We'll handle train/test split later
microbatch_size=(args.batch_size // accelerator.num_processes),
max_length=args.max_length,
max_prompt_length=args.max_prompt_length,
n_epochs=1,
seed=args.seed
)
# Process all samples first to get rewards
all_processed_samples = []
for batch in tqdm(dataloader, disable=not accelerator.is_main_process):
processed_batch = process_batch(batch, reward_model, accelerator)
if accelerator.is_main_process:
all_processed_samples.extend(processed_batch)
# Set up output writer
if accelerator.is_main_process:
# Split into train and test based on prompt_ids
prompt_keys = list(set(sample['prompt_key'] for sample in all_processed_samples))
random.Random(args.seed).shuffle(prompt_keys)
test_prompt_keys = set(prompt_keys[:int(args.fraction_test * len(prompt_keys))])
print(f"Writing feedback to {args.output_path}")
output_file = open(args.output_path, 'w')
writer = StreamingJSONWriter(output_file)
# Process and write feedback
if args.feedback_type == 'binary':
feedback = convert_batch_to_binary_feedback(all_processed_samples, threshold=args.threshold)
elif args.feedback_type == 'pairwise':
feedback = convert_to_pairwise_feedback(all_processed_samples, args.seed, threshold=args.threshold)
else:
feedback = all_processed_samples
# Add split information and write
for item in feedback:
item['split'] = 'test' if item['prompt_key'] in test_prompt_keys else 'train'
item.pop('prompt_key')
writer.write_item(item)
writer.close()
output_file.close()
accelerator.end_training()
if torch.distributed.is_initialized():
torch.distributed.destroy_process_group()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Assign rewards to samples using a reward model")
parser.add_argument("reward_model_path", type=str, help="Path to the reward model")
parser.add_argument("samples_path", type=str, help="Path to the JSON file containing samples")
parser.add_argument("output_path", type=str, help="Path to save the feedback file")
parser.add_argument("--batch_size", type=int, default=16, help="Batch size for processing")
parser.add_argument("--max_length", type=int, default=1024,
help="Maximum sequence length for reward model input")
parser.add_argument("--max_prompt_length", type=int, default=512,
help="Maximum prompt length for reward model input")
parser.add_argument("--feedback_type", type=str, choices=['binary', 'pairwise', None], default=None,
help="Type of feedback to generate (either binary, pairwise, or just annotate with rewards)")
parser.add_argument("--threshold", type=float, default=0,
help="Reward threshold for feedback (absolute threshold for binary feedback and minimum reward difference for pairwise)")
parser.add_argument("--seed", type=int, default=0, help="Random seed for reproducibility")
parser.add_argument("--fraction_test", type=float, default=0.1,
help="Fraction of prompts to use for test set (default: 0.1)")
args = parser.parse_args()
main(args)