forked from CarperAI/trlx
-
Notifications
You must be signed in to change notification settings - Fork 3
/
starling_p3o_lora.py
225 lines (199 loc) · 7.3 KB
/
starling_p3o_lora.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
import json
import math
import os
import sys
from itertools import islice
# import numpy as np
import torch
# import tritonclient.grpc as client_util
from datasets import load_dataset
from huggingface_hub import snapshot_download
from torch import nn
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, TaskType
# from tritonclient.utils import np_to_triton_dtype
from utils import from_openchat_to_llama, from_list_to_openchat
import trlx
from trlx.data.default_configs import (
ModelConfig,
OptimizerConfig,
P3OConfig,
SchedulerConfig,
TokenizerConfig,
TrainConfig,
TRLConfig,
)
default_config = TRLConfig(
train=TrainConfig(
seq_length=2048,
epochs=10000,
total_steps=30000,
batch_size=2,
eval_batch_size=4,
checkpoint_interval=500,
eval_interval=500,
pipeline="PromptPipeline",
save_best=True,
save_optimizer=False,
trainer="AccelerateP3OTrainer",
checkpoint_dir="checkpoints/p3o_hh",
),
model=ModelConfig(
model_path="openchat/openchat_3.5",
# num_layers_unfrozen=7,
peft_config=LoraConfig(
r=8,
task_type=TaskType.CAUSAL_LM,
lora_alpha=32,
lora_dropout=0.1,
),
),
tokenizer=TokenizerConfig(tokenizer_path="openchat/openchat_3.5", truncation_side="left"),
optimizer=OptimizerConfig(name="adamw", kwargs=dict(lr=4e-8, betas=(0.9, 0.95), eps=1.0e-8, weight_decay=1.0e-6)),
scheduler=SchedulerConfig(name="cosine_annealing", kwargs=dict(T_max=10000, eta_min=4e-8)),
method=P3OConfig(
name="P3OConfig",
num_responses_per_query=2,
num_rollouts=32,
chunk_size=4,
p3o_epochs=2,
kl_coef=0.01,
cliprange=0.2,
cliprange_ratio=10.0,
scale_reward="running",
ref_mean=None,
ref_std=None,
cliprange_reward=10,
gen_kwargs=dict(
max_new_tokens=1024,
top_k=0,
top_p=1.0,
do_sample=True,
),
clip_tokenwise=False,
avg_tokenwise=False,
scale_q=False,
),
)
"""
old_version:
clip_tokenwise=False,
avg_tokenwise=False,
new_version:
clip_tokenwise=True,
avg_tokenwise=True/False, doesn't matter
old_version(normalize loss tokenwise):
clip_tokenwise=False,
avg_tokenwise=True,
"""
# accelerate launch --num_processes 1 --config_file ../../configs/accelerate/zero2-bf16.yaml mistral_p3o.py
def create_reward_fn(): # noqa: C901
class GPTRewardModel(nn.Module):
def __init__(self, model_path, eos_token_id, alpha):
super().__init__()
if "mistral" in model_path or "Llama" in model_path:
model = AutoModelForCausalLM.from_pretrained(model_path)
self.transformer = model.model
else:
model = AutoModelForCausalLM.from_pretrained(model_path)
self.transformer = model.gpt_neox
self.config = model.config
# `gpt-neo(x)` models use `hidden_size` attribute names instead of `n_embd``
self.config.n_embd = self.config.hidden_size if hasattr(self.config, "hidden_size") else self.config.n_embd
self.model = model
self.alpha = alpha
self.v_head = nn.Linear(self.config.n_embd, 1, bias=False)
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
self.tokenizer.pad_token = self.tokenizer.unk_token
self.PAD_ID = self.tokenizer(self.tokenizer.pad_token)["input_ids"][0]
self.K = 7
def get_device(self):
return self.model.device
def gradient_checkpointing_enable(self):
self.model.gradient_checkpointing_enable()
return
def forward(
self,
input_ids=None,
past_key_values=None,
attention_mask=None,
position_ids=None,
):
"""
input_ids, attention_mask: torch.Size([bs, seq_len])
return: scores: List[torch.Size([1])
"""
bs = input_ids.shape[0]
transformer_outputs = self.transformer(
input_ids,
past_key_values=past_key_values,
attention_mask=attention_mask,
position_ids=position_ids,
)
hidden_states = transformer_outputs[0]
scores = []
rewards = self.v_head(hidden_states).squeeze(-1)
for i in range(bs):
c_inds = (input_ids[i] == self.PAD_ID).nonzero()
c_ind = c_inds[0].item() if len(c_inds) > 0 else input_ids.shape[1]
scores.append(rewards[i, c_ind - 1])
return scores
reward_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
reward_model = GPTRewardModel("meta-llama/Llama-2-7b-chat-hf", reward_tokenizer.eos_token_id, 0.5)
reward_tokenizer = reward_model.tokenizer
print("Reward tokenizer pad token:", reward_tokenizer.pad_token)
reward_tokenizer.truncation_side = "left"
directory = snapshot_download("banghua/refine_rm")
for fpath in os.listdir(directory):
if fpath.endswith(".pt") or fpath.endswith("model.bin"):
checkpoint = os.path.join(directory, fpath)
break
if os.environ.get("LOCAL_RANK", "0") == "0":
reward_model.load_state_dict(torch.load(checkpoint), strict=False)
reward_model.eval()
reward_model.requires_grad_(False)
reward_device = torch.cuda.device_count() - 1
reward_model = reward_model.to(reward_device)
reward_batch_size = 4
def get_reward(samples):
"""samples: List[str]"""
input_ids = []
attention_masks = []
encodings_dict = reward_tokenizer(
samples,
truncation=True,
max_length=2048,
padding="max_length",
return_tensors="pt",
).to(reward_device)
input_ids = encodings_dict["input_ids"]
attention_masks = encodings_dict["attention_mask"]
mbs = reward_batch_size
out = []
for i in range(math.ceil(len(samples) / mbs)):
rewards = reward_model(input_ids=input_ids[i * mbs : (i + 1) * mbs], attention_mask=attention_masks[i * mbs : (i + 1) * mbs])
out.extend(rewards)
return torch.hstack(out)
def reward_fn(samples, prompts, **kwargs):
samples = [from_openchat_to_llama(sample) for sample in samples]
rewards = get_reward(samples)
return rewards
return reward_fn
def main(hparams={}):
config = TRLConfig.update(default_config, hparams)
dataset = load_dataset("ThWu/cleaned_prompt_r", split="train")
dataset = dataset.train_test_split(test_size=0.001, seed=42)
dataset = dataset.map(from_list_to_openchat)
prompts = [{"prompt": x["prompt"]} for x in dataset["train"]]
eval_prompts = [{"prompt": x["prompt"]} for x in islice(dataset["test"], 100)]
reward_fn = create_reward_fn()
trlx.train(
prompts=prompts,
eval_prompts=eval_prompts,
reward_fn=reward_fn,
config=config,
stop_sequences=["GPT4 Correct User:", "GPT4 Correct Assistant:"],
)
if __name__ == "__main__":
hparams = {} if len(sys.argv) == 1 else json.loads(sys.argv[1])
main(hparams)