-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
247 lines (225 loc) · 10.1 KB
/
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
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
from transformers import (
set_seed,
HfArgumentParser,
TrainingArguments,
)
import argparse
from loguru import logger
import os
from os.path import join
import torch
from transformers import AutoTokenizer
from transformers import AutoModelForCausalLM
from component.collator import SFTDataCollator, PretrainCollator
from component.dataset import (
SFTDataset,
ChatGLM2SFTDataset,
ChatGLM3SFTDataset,
MistralSFTDataset,
ZephyrSFTDataset,
QwenSFTDataset,
PretrainDataset,
LazyPretrainDataset
)
from component.argument import CustomizedArguments
from component.trainer import Trainer
from datasets import load_dataset, concatenate_datasets
import datasets
from itertools import chain
from tqdm import tqdm
import shutil
# from component.loss import TargetLMLoss
def setup_everything():
parser = argparse.ArgumentParser()
parser.add_argument("--train_args_file", type=str, default='train_args/full/qwen-7b-sft-full.json', help="")
parser.add_argument("--local_rank", type=int, help="")
args = parser.parse_args()
train_args_file = args.train_args_file
# train_args_file = 'train_args/finetune.json'
# 读取训练的参数配置
parser = HfArgumentParser((CustomizedArguments, TrainingArguments))
# 解析得到自定义参数,以及自带参数
args, training_args = parser.parse_json_file(json_file=train_args_file)
# 创建输出目录
if not os.path.exists(training_args.output_dir):
os.makedirs(training_args.output_dir)
logger.add(join(training_args.output_dir, 'train.log'))
logger.info("train_args:{}".format(training_args))
# 设置随机种子
set_seed(training_args.seed)
return args, training_args
def load_pretrain_dataset(training_args, args, tokenizer):
def tokenize_function(examples):
output = tokenizer(examples["text"])
output = {'input_ids': output.input_ids}
return output
def group_texts(examples):
# Concatenate all texts.
concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
total_length = len(concatenated_examples[list(examples.keys())[0]])
# We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
# customize this part to your needs.
if total_length >= max_seq_length:
total_length = (total_length // max_seq_length) * max_seq_length
# Split by chunks of max_len.
result = {
k: [t[i : i + max_seq_length] for i in range(0, total_length, max_seq_length)]
for k, t in concatenated_examples.items()
}
return result
data_path = args.train_file
max_seq_length = args.max_seq_length
# 创建缓存路径
cache_dir = join(data_path, 'cache')
os.makedirs(cache_dir, exist_ok=True)
logger.info('Pretraining data path: {}'.format(data_path))
# 扫描所有jsonl文件
logger.info('Scanning all the training file...')
files = []
for root, dir_names, file_names in os.walk(data_path):
for file_name in file_names:
file = join(root, file_name)
if file_name.endswith('.jsonl'):
files.append(file)
logger.info(f'Total num of training file: {len(files)}')
# 预处理所有文本,将其id化,并且进行packing操作
with training_args.main_process_first(desc="dataset map tokenization and grouping"):
pretrain_dataset = [] # 汇总所有dataset
for idx, file in enumerate(tqdm(files)):
logger.info(f'Loading file: {file}')
file_name = os.path.basename(file)
file_name = file_name.replace('.jsonl', '')
cache_path = os.path.join(cache_dir, file_name)
os.makedirs(cache_path, exist_ok=True)
try:
processed_dataset = datasets.load_from_disk(cache_path, keep_in_memory=False)
logger.info(f'Finished loading datasets-{file_name} from cache')
except Exception:
tmp_cache_path = join(cache_path, 'tmp') # 临时缓存目录,会被自动删除
logger.info(f'There is no cache of file {file_name}, start preprocessing...')
raw_dataset = load_dataset("json", data_files=file, cache_dir=tmp_cache_path, keep_in_memory=False)
tokenized_dataset = raw_dataset.map(
tokenize_function,
batched=True,
num_proc=args.tokenize_num_workers,
remove_columns="text",
load_from_cache_file=True,
keep_in_memory=False,
cache_file_names={k: os.path.join(tmp_cache_path, 'tokenized.arrow') for k in raw_dataset},
desc="Running tokenizer on dataset",
)
grouped_datasets = tokenized_dataset.map(
group_texts,
batched=True,
num_proc=args.tokenize_num_workers,
load_from_cache_file=True,
keep_in_memory=False,
cache_file_names={k: os.path.join(tmp_cache_path, 'grouped.arrow') for k in tokenized_dataset},
desc=f"Grouping texts in chunks of {max_seq_length}",
)
processed_dataset = grouped_datasets
processed_dataset.save_to_disk(cache_path)
# 删除临时目录
# shutil.rmtree(tmp_cache_path)
logger.info(f"Training number of {file_name}: {len(processed_dataset['train'])}")
if idx == 0:
pretrain_dataset = processed_dataset['train']
else:
assert pretrain_dataset.features.type == processed_dataset["train"].features.type
pretrain_dataset = concatenate_datasets([pretrain_dataset, processed_dataset["train"]])
logger.info(f"Total training number: {len(pretrain_dataset)}")
return pretrain_dataset
def init_components(args, training_args):
"""
初始化各个组件
"""
logger.info('Initializing components...')
world_size = int(os.environ.get("WORLD_SIZE", 1))
ddp = world_size != 1
if ddp:
device_map = {"": int(os.environ.get("LOCAL_RANK") or 0)}
training_args.ddp_find_unused_parameters = False if ddp else None
# 初始化model
model = AutoModelForCausalLM.from_pretrained(
args.model_name_or_path,
torch_dtype=torch.float16,
trust_remote_code=True
)
# moe模型,需要考虑负载均衡的loss
if 'output_router_logits' in model.config.to_dict():
logger.info('set output_router_logits as True')
model.config.output_router_logits = True
# 加载tokenzier
tokenizer = AutoTokenizer.from_pretrained(
args.model_name_or_path,
trust_remote_code=True,
# llama不支持fast
use_fast=False if model.config.model_type == 'llama' else True
)
# QWenTokenizer比较特殊,pad_token_id、bos_token_id、eos_token_id均为None。eod_id对应的token为<|endoftext|>
if tokenizer.__class__.__name__ == 'QWenTokenizer':
tokenizer.pad_token_id = tokenizer.eod_id
tokenizer.bos_token_id = tokenizer.eod_id
tokenizer.eos_token_id = tokenizer.eod_id
# ChatGLMTokenizer不需要设置,仅设置其他tokenizer
elif tokenizer.__class__.__name__ != 'ChatGLMTokenizer':
assert tokenizer.eos_token_id is not None
assert tokenizer.bos_token_id is not None
tokenizer.pad_token_id = tokenizer.eos_token_id if tokenizer.pad_token_id is None else tokenizer.pad_token_id
# 计算模型参数量
total = sum(p.numel() for p in model.parameters())
logger.info("Total model params: %.2fM" % (total / 1e6))
# 初始化损失函数
# loss_func = TargetLMLoss(ignore_index=-100)
assert args.task_type in ['sft', 'pretrain'], 'task_type should be in [sft, pretrain]'
# 初始化dataset和collator
# 预训练
if args.task_type == 'pretrain':
train_dataset = load_pretrain_dataset(training_args, args, tokenizer)
data_collator = PretrainCollator(tokenizer, args.max_seq_length)
else:
# 指令微调,不同的模型,数据拼接格式不一样
if 'chatglm2' in args.model_name_or_path.lower():
train_dataset = ChatGLM2SFTDataset(args.train_file, tokenizer, args.max_seq_length)
# 加载ChatGLM3的训练集
elif 'chatglm3' in args.model_name_or_path.lower():
train_dataset = ChatGLM3SFTDataset(args.train_file, tokenizer, args.max_seq_length)
elif 'mistral' in args.model_name_or_path.lower() or 'mixtral' in args.model_name_or_path.lower():
train_dataset = MistralSFTDataset(args.train_file, tokenizer, args.max_seq_length)
elif 'zephyr' in args.model_name_or_path.lower():
train_dataset = ZephyrSFTDataset(args.train_file, tokenizer, args.max_seq_length)
elif 'qwen' in args.model_name_or_path.lower():
train_dataset = QwenSFTDataset(args.train_file, tokenizer, args.max_seq_length)
# 按照firefly格式进行拼接
else:
train_dataset = SFTDataset(args.train_file, tokenizer, args.max_seq_length)
# 加载collator
data_collator = SFTDataCollator(tokenizer, args.max_seq_length)
# 初始化Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
# tokenizer=tokenizer,
data_collator=data_collator,
# compute_loss=loss_func
)
return trainer
def main():
# 进行一些配置和检查
args, training_args = setup_everything()
# 加载各种组件
trainer = init_components(args, training_args)
# 开始训练
logger.info("*** starting training ***")
train_result = trainer.train()
# 保存最好的checkpoint
final_save_path = join(training_args.output_dir)
trainer.save_model(final_save_path) # Saves the tokenizer too
# 保存训练指标
metrics = train_result.metrics
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
if __name__ == "__main__":
main()