forked from mst272/LLM-Dojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlhf_train.py
217 lines (190 loc) · 8.28 KB
/
rlhf_train.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
import importlib
import multiprocessing
from peft import LoraConfig, TaskType, get_peft_model, prepare_model_for_kbit_training
from datasets import Dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
AutoModelForSequenceClassification,
HfArgumentParser,
BitsAndBytesConfig,
)
import pandas as pd
import torch
import torch.nn as nn
from trl.trainer.utils import SIMPLE_QUERY_CHAT_TEMPLATE
from common_args import CommonArgs
def load_config(args):
# 根据config_option加载相应的配置
module_path = args.train_args_path.replace("/", ".").rstrip(".py")
# 动态导入模块
module = importlib.import_module(module_path)
# 每个模块导入的类名均为TrainArgument
class_name = args.rlhf_type + "Config"
# 使用getattr获取模块中的类
argument = getattr(module, class_name)
train_argument = argument()
return train_argument
def find_all_linear_names(model):
"""
找出所有全连接层,为所有全连接添加adapter
"""
cls = nn.Linear
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, cls):
names = name.split('.')
lora_module_names.add(names[-1])
if 'lm_head' in lora_module_names: # needed for 16-bit
lora_module_names.remove('lm_head')
lora_module_names = list(lora_module_names)
return lora_module_names
def split_data(raw_datasets, eval_samples):
train_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples))
eval_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples, len(raw_datasets)))
return train_dataset, eval_dataset
def load_data_prompt(tokenizer, train_data_path, eval_samples):
raw_datasets = pd.read_json(train_data_path, lines=True)
for i in range(len(raw_datasets)):
pro = raw_datasets['prompt'][i]
res = tokenizer.apply_chat_template(pro, tokenize=False)
raw_datasets.loc[i, 'prompt'] = res
raw_datasets = Dataset.from_pandas(raw_datasets, preserve_index=False)
def tokenize(element):
outputs = tokenizer(
element['prompt'],
padding=False,
)
return {"input_ids": outputs["input_ids"]}
raw_datasets = raw_datasets.map(
tokenize,
remove_columns=raw_datasets.column_names,
batched=True,
num_proc=multiprocessing.cpu_count(), # multiprocessing.cpu_count(),
load_from_cache_file=False,
)
train_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples))
eval_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples, len(raw_datasets)))
return train_dataset, eval_dataset
def load_data_all(tokenizer, train_data_path, eval_samples):
raw_datasets = pd.read_json(train_data_path, lines=True)
for i in range(len(raw_datasets)):
raw_datasets.loc[i, 'prompt'] = tokenizer.apply_chat_template(raw_datasets['prompt'][i], tokenize=False)
raw_datasets.loc[i, 'chosen'] = tokenizer.apply_chat_template(raw_datasets['chosen'][i], tokenize=False)
raw_datasets.loc[i, 'rejected'] = tokenizer.apply_chat_template(raw_datasets['rejected'][i], tokenize=False)
raw_datasets = Dataset.from_pandas(raw_datasets, preserve_index=False)
train_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples))
eval_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples, len(raw_datasets)))
return train_dataset, eval_dataset
def main():
parser = HfArgumentParser((CommonArgs,))
args = parser.parse_args_into_dataclasses()[0]
# 根据CommonArgs中的config_option动态加载配置
config = load_config(args)
################
# Model & Tokenizer
################
tokenizer = AutoTokenizer.from_pretrained(
config.sft_model_path,
padding_side="left",
trust_remote_code=True,
)
if tokenizer.chat_template is None:
tokenizer.chat_template = SIMPLE_QUERY_CHAT_TEMPLATE
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({"pad_token": "[PAD]"})
model_kwargs = dict(
trust_remote_code=True
)
if args.train_mode == 'qlora':
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16 if config.fp16 else torch.bfloat16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
llm_int8_threshold=6.0,
llm_int8_has_fp16_weight=False,
)
model_kwargs.update(quantization_config=quantization_config)
# 如果模型不支持AutoModelForSequenceClassification需要在对应config文件中添加映射
try:
reward_model = AutoModelForSequenceClassification.from_pretrained(config.reward_model_path, num_labels=1,
**model_kwargs)
except Exception as e:
assert False, "模型不支持AutoModelForSequenceClassification需要在对应config文件中添加映射"
ref_policy = AutoModelForCausalLM.from_pretrained(config.sft_model_path, **model_kwargs)
policy = AutoModelForCausalLM.from_pretrained(config.sft_model_path, **model_kwargs)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
target_modules=find_all_linear_names(policy),
r=args.lora_rank, # Lora 秩
lora_alpha=args.lora_alpha, # Lora alpha,具体作用参见 Lora 原理
lora_dropout=args.lora_dropout, # Dropout 比例
use_dora=args.use_dora
)
if args.train_mode == 'lora':
policy.enable_input_require_grads()
policy = get_peft_model(policy, lora_config)
elif args.train_mode == 'qlora':
policy = prepare_model_for_kbit_training(policy, use_gradient_checkpointing=config.gradient_checkpointing)
policy = get_peft_model(policy, lora_config)
################
# Dataset
################
# raw_datasets = pd.read_json(config.train_data_path, lines=True)
# for i in range(len(raw_datasets)):
# pro = raw_datasets['prompt'][i]
# res = tokenizer.apply_chat_template(pro, tokenize=False)
# raw_datasets.loc[i, 'prompt'] = res
# raw_datasets = Dataset.from_pandas(raw_datasets, preserve_index=False)
# eval_samples = config.eval_samples
# train_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples))
# eval_dataset = raw_datasets.select(range(len(raw_datasets) - eval_samples, len(raw_datasets)))
################
# Training
################
if args.rlhf_type == 'RLOO':
from trl.trainer.rloo_trainer import RLOOTrainer
train_dataset, eval_dataset = load_data_prompt(tokenizer, config.train_data_path, config.eval_samples)
trainer = RLOOTrainer(
config=config,
tokenizer=tokenizer,
policy=policy,
ref_policy=ref_policy,
reward_model=reward_model,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
)
elif args.rlhf_type == 'PPO':
from trl.trainer.ppov2_trainer import PPOv2Trainer
train_dataset, eval_dataset = load_data_prompt(tokenizer, config.train_data_path, config.eval_samples)
value_model = AutoModelForSequenceClassification.from_pretrained(config.reward_model_path, num_labels=1,
trust_remote_code=True)
trainer = PPOv2Trainer(
config=config,
tokenizer=tokenizer,
policy=policy,
ref_policy=ref_policy,
reward_model=reward_model,
value_model=value_model,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
)
# todo: 更优雅的方式实现?
elif args.rlhf_type in ['CPO', 'SimPO', 'CPOSimPO']:
from trl import CPOTrainer
train_dataset, eval_dataset = load_data_all(tokenizer, config.train_data_path, config.eval_samples)
trainer = CPOTrainer(
policy,
args=config,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
tokenizer=tokenizer,
)
else:
raise Exception
trainer.train()
trainer.save_model(config.output_dir)
# trainer.generate_completions()
if __name__ == "__main__":
main()