forked from AkaliKong/MiniOneRec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrl_gpr.py
More file actions
359 lines (306 loc) · 13.8 KB
/
rl_gpr.py
File metadata and controls
359 lines (306 loc) · 13.8 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
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
from datasets import Dataset
from trl import GRPOConfig, GRPOTrainer
import random
import numpy as np
import torch
from data import D3Dataset, SidDataset, RLTitle2SidDataset, RLSeqTitle2SidDataset, RLSid2TitleDataset, RLSidhis2TitleDataset
from torch.utils.data import ConcatDataset
from transformers import AutoModelForCausalLM, AutoTokenizer
import os
from minionerec_trainer import ReReTrainer
from sasrec import SASRec
from fire import Fire
import pickle
import math
import json
from sklearn.metrics import ndcg_score
import re
os.environ['WANDB_MODE'] = 'disabled'
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def train(
# model/data params
model_path: str = "",
seed: int = 42,
train_file: str = "",
eval_file: str = "",
info_file: str = "",
category: str = "",
# wandb params
wandb_project: str = "",
wandb_run_name: str = "",
# training hyperparams
output_dir: str = "",
train_batch_size: int = 32,
eval_batch_size: int = 32,
gradient_accumulation_steps: int = 1,
temperature: float = 1.0,
add_gt: bool = False,
eval_step: float = 0.199,
num_generations: int = 16,
num_train_epochs: int = 1,
learning_rate: float = 1e-6,
beta: float = 0.04,
beam_search: bool = False,
test_during_training: bool = True,
dynamic_sampling: bool = False,
mask_all_zero: bool = False,
sync_ref_model: bool = False,
test_beam: int = 20,
reward_type: str = "rule",
sample_train: bool = False,
ada_path: str = "",
cf_path: str = "",
sid_index_path: str = "",
item_meta_path: str = "",
dapo: bool = False,
gspo: bool = False,
):
torch.backends.cuda.enable_flash_sdp(False)
torch.backends.cuda.enable_mem_efficient_sdp(False)
set_seed(seed)
category_dict = {"Industrial_and_Scientific": "industrial and scientific items", "Office_Products": "office products", "Toys_and_Games": "toys and games", "Sports": "sports and outdoors", "Books": "books"}
print(category)
with open(info_file, 'r') as f:
info = f.readlines()
# Extract semantic_id (first column) from the format: semantic_id \t item_title \t item_id
item_name = [_.split('\t')[0].strip() for _ in info]
item2id = {name: i for i, name in enumerate(item_name)}
# Parse semantic IDs for HEPO
def parse_sid(sid):
return re.findall(r'\[.*?\]', sid)
item2id_parts = {}
for name in item_name:
parts = parse_sid(name)
item2id_parts[name] = tuple(parts)
def hepo_reward(prompts, completions):
history = [prompt2history.get(prompt, "") for prompt in prompts]
targets_full_sid = [history2target.get(elm, "") for elm in history]
rewards = []
for i, comp_full_sid in enumerate(completions):
comp_full_sid = comp_full_sid.strip(" \n\"")
target_full_sid = targets_full_sid[i].strip(" \n\"")
if target_full_sid not in item2id_parts:
rewards.append(0.0)
continue
target_parts = item2id_parts[target_full_sid]
comp_parts = parse_sid(comp_full_sid)
reward = 0.0
# Hierarchical Reward
if len(comp_parts) > 0 and len(target_parts) > 0 and comp_parts[0] == target_parts[0]:
reward = 0.2
if len(comp_parts) > 1 and len(target_parts) > 1 and comp_parts[1] == target_parts[1]:
reward = 0.5
if len(comp_parts) > 2 and len(target_parts) > 2 and comp_parts[2] == target_parts[2]:
reward = 1.0
rewards.append(reward)
return rewards
sample = -1
train_datasets = []
# train_data = D3Dataset(train_file, category=category_dict[category], sample=sample)
# train_datasets.append(train_data)
train_data1 = SidDataset(train_file, category=category_dict[category], sample=sample)
train_datasets.append(train_data1)
train_data2 = RLTitle2SidDataset(item_file=item_meta_path, index_file=sid_index_path, category=category_dict[category], sample=sample)
train_datasets.append(train_data2)
train_data3 = RLSeqTitle2SidDataset(train_file, category=category_dict[category], sample=10000)
train_datasets.append(train_data3)
# train_data4 = RLSid2TitleDataset(item_file=item_meta_path, index_file=sid_index_path, category=category_dict[category], sample=sample)
# train_datasets.append(train_data4)
# train_data5 = RLSidhis2TitleDataset(train_file, item_file=item_meta_path, index_file=sid_index_path, category=category_dict[category], sample=sample)
# train_datasets.append(train_data5)
# train_data6 = RLTitle2Sid_1LayerDataset(item_file=item_meta_path, index_file=sid_index_path, category=category_dict[category], sample=sample)
# train_datasets.append(train_data6)
# train_data7 = RLTitle2Sid_2LayerDataset(item_file=item_meta_path, index_file=sid_index_path, category=category_dict[category], sample=sample)
# train_datasets.append(train_data7)
train_data = ConcatDataset(train_datasets)
# eval_data = D3Dataset(eval_file, category=category_dict[category], sample=sample)
eval_data = SidDataset(eval_file, category=category_dict[category], sample=sample)
train_dataset = Dataset.from_dict({k : [elm[k] for elm in train_data] for k in train_data[0].keys()})
train_dataset = train_dataset.shuffle(seed=seed)
if sample_train and "sft" in model_path:
train_dataset = train_dataset.select(range(int(0.2 * len(train_dataset)), len(train_dataset)))
eval_dataset = Dataset.from_dict({k : [elm[k] for elm in eval_data] for k in eval_data[0].keys()})
eval_dataset = eval_dataset.shuffle(seed=seed)
# prompt2history = {**train_data.prompt2history, **eval_data.prompt2history}
# history2target = {**train_data.history2target, **eval_data.history2target}
prompt2history = {}
history2target = {}
# Collect prompt2history and history2target from all train datasets
for dataset in train_datasets:
if hasattr(dataset, 'prompt2history'):
prompt2history.update(dataset.prompt2history)
if hasattr(dataset, 'history2target'):
history2target.update(dataset.history2target)
# Add eval_data mappings
if hasattr(eval_data, 'prompt2history'):
prompt2history.update(eval_data.prompt2history)
if hasattr(eval_data, 'history2target'):
history2target.update(eval_data.history2target)
print("train_dataset: ", train_dataset)
print("eval_dataset: ", eval_dataset)
llm_model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map="auto")
device = llm_model.device
tokenizer = AutoTokenizer.from_pretrained(model_path)
len_seq = 10
item_num = len(item_name)
print(f"item_num: {item_num}")
if reward_type == "sasrec":
model = SASRec(32, item_num, len_seq, 0.3, device)
model.to(device)
model.load_state_dict(torch.load(cf_path))
model.eval()
if reward_type == "semantic":
with open(ada_path, "rb") as f:
item_ada_embd = pickle.load(f)
item_ada_embd = torch.tensor(item_ada_embd).to(llm_model.device)
print("Load item_ada_embd successfully.")
ndcg_rewards = [-1.0/math.log2(i+2) for i in range(num_generations)]
ndcg_rewards = [-elm/sum(ndcg_rewards) for elm in ndcg_rewards]
def ndcg_rule_reward(prompts, completions):
history = [prompt2history[prompt] for prompt in prompts]
targets = [history2target[elm] for elm in history]
repeat = num_generations
rewards = []
flag = False
lis = []
for i, completion in enumerate(completions):
if completion.strip("\n\"") == targets[i].strip("\n\""):
flag = True
lis.append(0.0)
else:
lis.append(ndcg_rewards[i%num_generations])
if (i+1)%num_generations == 0:
if flag:
rewards.extend(lis)
else:
rewards.extend([0.0] * repeat)
flag = False
lis = []
return rewards
def rule_reward(prompts, completions):
history = [prompt2history[prompt] for prompt in prompts]
targets = [history2target[elm] for elm in history]
rewards = []
for i, completion in enumerate(completions):
if completion.strip("\n\" ") == targets[i].strip("\n\" "):
rewards.append(1.0)
else:
rewards.append(0.0)
return rewards
def semantic_reward(prompts, completions):
history = [prompt2history[prompt] for prompt in prompts]
targets = [history2target[elm] for elm in history]
target_ids = [item2id[elm.strip("\"\n")] for elm in targets]
completions = [elm.strip("\"\n") for elm in completions]
for i, completion in enumerate(completions):
if completion not in item2id:
print("==============================")
print(prompts[i])
print(f"Invalid item: {completion}")
print("==============================")
completion_ids = [item2id[elm] for elm in completions]
rewards = torch.cosine_similarity(item_ada_embd[target_ids], item_ada_embd[completion_ids], dim=-1)
print(rewards)
return rewards
def cf_reward(prompts, completions):
history = [prompt2history[prompt] for prompt in prompts]
history_list = [elm.split("::") for elm in history]
pred_ids = []
for i, elm in enumerate(completions):
elm = elm.strip("\n\"")
if elm not in item_name:
# print("========Invalid Item========")
# print(f"Invalid item: {elm}")
# print(f"Prompt: {prompts[i]}")
# print("============================")
pred_ids.append(random.randint(0, item_num-1))
else:
pred_ids.append(item2id[elm])
len_lis = []
history_ids = []
for his in history_list:
# Filter out items not in item2id mapping
his = [item2id[elm] for elm in his if elm in item2id]
# If all items filtered out, use a default item
if len(his) == 0:
his = [item_num] # padding item
len_lis.append(len(his))
if len(his) < len_seq:
his = his + [item_num] * (len_seq - len(his))
history_ids.append(his[:len_seq]) # truncate if too long
seq = torch.LongTensor(history_ids).to(device)
pred = torch.LongTensor(pred_ids).to(device)
with torch.no_grad():
predictions = model.forward_eval(seq, torch.tensor(np.array(len_lis)).to(device))
scores = torch.gather(predictions, 1, pred.view(-1, 1)).view(-1)
return scores
if reward_type == "rule":
reward_fun = rule_reward
elif reward_type == "ranking":
reward_fun = [rule_reward, ndcg_rule_reward]
elif reward_type == "ranking_only":
reward_fun = ndcg_rule_reward
elif reward_type == "semantic":
reward_fun = semantic_reward
elif reward_type == "sasrec":
reward_fun = [cf_reward, hepo_reward] # Combine rewards
os.environ['WANDB_PROJECT'] = wandb_project
os.environ["WANDB_MODE"] = "offline"
training_args = GRPOConfig(output_dir=output_dir,
save_steps=0.1,
save_total_limit=20,
eval_strategy="steps",
max_completion_length=128,
num_generations=num_generations,
temperature=temperature,
sync_ref_model=sync_ref_model,
per_device_eval_batch_size=eval_batch_size,
per_device_train_batch_size=train_batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
eval_steps=eval_step,
logging_steps=1,
learning_rate=learning_rate,
beta=beta,
warmup_ratio=0.03,
max_grad_norm= 0.3,
num_train_epochs=num_train_epochs,
bf16=True,
optim="paged_adamw_32bit",
lr_scheduler_type="cosine",
save_strategy="steps",
report_to="wandb",
run_name=wandb_run_name,
)
trainer = ReReTrainer(
model=model_path,
base_model=model_path,
dapo=dapo,
gspo=gspo,
add_gt=add_gt,
dynamic_sampling=dynamic_sampling,
beam_search=beam_search,
test_during_training=test_during_training,
test_beam=test_beam,
info_file=info_file,
prompt2history=prompt2history,
history2target=history2target,
reward_funcs=reward_fun,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
args=training_args,
)
trainer.train()
trainer.save_model(output_dir)
output_dir = os.path.join(output_dir, "final_checkpoint")
trainer.model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
if __name__ == "__main__":
Fire(train)