-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_ppo.py
161 lines (139 loc) · 5.1 KB
/
train_ppo.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
import json
import math
import os
import sys
import io
import torch
from torch import nn
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModelForSequenceClassification
import trlx
from trlx.data.default_configs import (
ModelConfig,
OptimizerConfig,
PPOConfig,
SchedulerConfig,
TokenizerConfig,
TrainConfig,
TRLConfig,
)
default_config = TRLConfig(
train=TrainConfig(
seq_length=1024,
epochs=10000,
total_steps=2000,
batch_size=3,
checkpoint_interval=1000,
eval_interval=500,
pipeline="PromptPipeline",
trainer="AcceleratePPOTrainer",
checkpoint_dir="outputs/bloom-1b7-ppo",
save_optimizer=False
),
model=ModelConfig(model_path="outputs/bloom-1b7-sft", num_layers_unfrozen=4),
tokenizer=TokenizerConfig(tokenizer_path="outputs/bloom-1b7-sft", truncation_side="left"),
optimizer=OptimizerConfig(name="adamw", kwargs=dict(lr=5e-6, 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=5e-6)),
method=PPOConfig(
name="PPOConfig",
num_rollouts=64,
chunk_size=4,
ppo_epochs=4,
init_kl_coef=0.05,
target=6,
horizon=10000,
gamma=1,
lam=0.95,
cliprange=0.2,
cliprange_value=0.2,
vf_coef=1,
scale_reward="running",
ref_mean=None,
ref_std=None,
cliprange_reward=10,
gen_kwargs=dict(
max_new_tokens=512,
top_k=0,
top_p=1.0,
do_sample=True,
),
),
)
def _make_r_io_base(f, mode: str):
if not isinstance(f, io.IOBase):
f = open(f, mode=mode)
return f
def jload(f, mode="r"):
"""Load a .json file into a dictionary."""
f = _make_r_io_base(f, mode)
jdict = json.load(f)
f.close()
return jdict
rm_model_path = 'outputs/bloom-1b7-rm'
def create_reward_fn(): # noqa: C901
reward_tokenizer = AutoTokenizer.from_pretrained(rm_model_path)
# reward_tokenizer.pad_token = reward_tokenizer.eos_token
reward_tokenizer.truncation_side = "left"
reward_tokenizer.padding_side = "right"
if os.environ.get("RANK", "0") == "0":
class RewardModel(nn.Module):
def __init__(self, checkpoint_path):
super().__init__()
self.model = AutoModelForSequenceClassification.from_pretrained(checkpoint_path)
def forward(self, input_ids: torch.LongTensor, attention_mask = None) -> torch.Tensor:
outputs = self.model(input_ids, attention_mask=attention_mask)
value = outputs['logits'].squeeze(-1)
return value
reward_model = RewardModel(rm_model_path)
reward_model.eval()
reward_model.requires_grad_(False)
reward_device = torch.cuda.device_count() - 1
reward_model = reward_model.half().to(reward_device)
reward_batch_size = 64
delta_reward = True
def get_reward(samples):
input = reward_tokenizer(
samples,
padding=True,
truncation=True,
max_length=1024,
return_tensors="pt",
).to(reward_device)
mbs = reward_batch_size
out = []
for i in range(math.ceil(len(samples) / mbs)):
batch_ixs = slice(i * mbs, (i + 1) * mbs)
input_ids = input.input_ids[batch_ixs]
attention_mask = input.attention_mask[batch_ixs]
rewards = reward_model(input_ids, attention_mask)
out.extend(rewards)
return torch.hstack(out)
def reward_fn(samples, prompts, original_output, **kwargs):
# samples = [s + reward_tokenizer.eos_token for s in samples]
# Fix: eos_token is appended in trainer
samples = [s if s.endswith(reward_tokenizer.eos_token) else s + reward_tokenizer.eos_token for s in samples]
rewards = get_reward(samples)
if not delta_reward:
return rewards
original_samples = [p + o + reward_tokenizer.eos_token for p, o in zip(prompts, original_output)]
# original_samples = [p + o for p, o in zip(prompts, original_output)]
original_rewards = get_reward(original_samples)
return rewards - original_rewards
else:
reward_fn = True
return reward_fn
def main(hparams={}):
config = TRLConfig.update(default_config, hparams)
dataset = jload('data/prompt_data.json')
prompts = [{"prompt": x["query"], "original_output": x["response"]} for x in dataset[500:]]
eval_prompts = [{"prompt": x["query"], "original_output": x["response"]} for x in dataset[:500]]
reward_fn = create_reward_fn()
trlx.train(
prompts=prompts,
eval_prompts=eval_prompts,
reward_fn=reward_fn,
config=config,
stop_sequences=["<Human>", "<Assistant>"],
)
if __name__ == "__main__":
hparams = {} if len(sys.argv) == 1 else json.loads(sys.argv[1])
main(hparams)