-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatglm4ie.py
397 lines (307 loc) · 13.2 KB
/
chatglm4ie.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'''
基于chatGLM3完成IE任务(实体关系信息抽取) chatglm3-6B-base
Qlora -4bit + 半精度float16
'''
from torch.utils.data import Dataset, DataLoader, random_split
from transformers.data.data_collator import DataCollatorForSeq2Seq
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from transformers import get_scheduler, TrainingArguments, Trainer, TrainerCallback
from torch.nn.utils import clip_grad_norm_
import json, os
import torch
from tqdm import tqdm
import argparse
import time
from time import strftime, gmtime
import warnings
import logging
from peft import get_peft_model, LoraConfig, TaskType, PeftModel, prepare_model_for_kbit_training
from bitsandbytes.optim import AdamW8bit
import random
warnings.filterwarnings("ignore")
device = "cuda"
class MyDataset(Dataset):
'''
从文件读取医疗文本数据
'''
def __init__(self, data_path, tokenizer):
prompt_prefix = "你现在是一个信息抽取模型,请你帮我找出下文中的实体关系三元组,形式为\"主体-主体类别_关系_客体-客体类别\",三元组之间用\\n分割。\n文本:"
prompt_suffix = "\n实体关系三元组有:\n"
self.ietridata = []
with open(data_path, "r", encoding="utf-8") as fr:
for data in tqdm(fr):
sample = json.loads(data.strip())
text = sample["text"]
ask = prompt_prefix + text + prompt_suffix
# print(ask)
ask = tokenizer.build_chat_input(ask, history=[], role='user')
# print(ask)
# print(tokenizer.decode(ask["input_ids"][0].numpy().tolist()))
answer = ''
for spo in sample['spo_list']:
answer += spo["subject"] + "-" + spo["subject_type"] \
+ "_" + spo["predicate"] \
+ "_" + spo["object"]["@value"] + "-" + spo["object_type"]["@value"] + "\n"
# print(answer)
answer = tokenizer(answer, add_special_tokens=False)
# print(answer)
# print(tokenizer.decode(answer["input_ids"]))
input_ids = ask["input_ids"][0].numpy().tolist() + answer["input_ids"] + [tokenizer.eos_token_id]
attention_mask = ask['attention_mask'][0].numpy().tolist() + answer['attention_mask'] + [1]
labels = [-100] * len(ask['input_ids'][0]) + answer["input_ids"] + [tokenizer.eos_token_id]
self.ietridata.append({'input_ids': input_ids[:pargs.max_length],
'attention_mask': attention_mask[:pargs.max_length],
"labels": labels[:pargs.max_length],
})
random.shuffle(self.ietridata)
def __len__(self):
return len(self.ietridata)
def __getitem__(self, idx):
data = self.ietridata[idx]
return data
def compute_metrics(data):
preds, labels = data
preds = torch.tensor(preds).to(device)
labels = torch.tensor(labels).to(device)
preds_shift = preds[:, :-1]
preds = torch.argmax(preds_shift, dim=-1)
labels_shift = labels[:, 1:]
keeplabels = torch.logical_not(torch.eq(labels_shift, -100))
rightlabel = torch.sum(torch.eq(preds, labels_shift)).item()
total_label = torch.sum(keeplabels).item()
eval_acc = rightlabel / total_label
return {'acc': eval_acc}
def format_time(time):
if time >= 3600:
return strftime("%H:%M:%S", gmtime(time))
else:
return strftime("%M:%S", gmtime(time))
def create_logger(name, filename):
logger = logging.getLogger(name=name)
logger.setLevel(logging.INFO)
# consoleHandler = logging.StreamHandler()
fileHandler = logging.FileHandler(filename=filename, mode="a", encoding="utf-8")
simple_formatter = logging.Formatter(fmt="%(asctime)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# consoleHandler.setFormatter(simple_formatter)
fileHandler.setFormatter(simple_formatter)
# logger.addHandler(consoleHandler)
logger.addHandler(fileHandler)
return logger
class MyTrainCallback(TrainerCallback):
def __init__(self, mylogger):
self.mylogger = mylogger
def on_epoch_begin(self, args, state, control, **kwargs):
history = state.log_history
if len(history) > 0:
loghis = history[-1]
self.mylogger.info(
'epoch: ' + str(loghis['epoch']) +
' loss: ' + str(loghis['loss']) +
' learning_rate: ' + str(loghis['learning_rate']))
def on_train_end(self, args, state, control, **kwargs):
history = state.log_history
loghis = history[-2]
self.mylogger.info(
'epoch: ' + str(loghis['epoch']) +
' loss: ' + str(loghis['loss']) +
' learning_rate: ' + str(loghis['learning_rate'])
)
loghis = history[-1]
self.mylogger.info("耗时:" + format_time(loghis['train_runtime']))
def train():
mycheckpoint = "models/chatglm4ie"
if not os.path.exists(mycheckpoint):
os.makedirs(mycheckpoint)
logger = create_logger(name="train_log",
filename=mycheckpoint + "/chatglm4ie.log")
logger.info(
"------------------------------------------------------------------------------------------------------------------------------------------")
logger.info("TRAIN LOGGING......")
logger.info(
"基于QLora 4-bit微调chatglm3实现IE任务,超参数有--num_samples %d --max_length %d --num_epochs %d --lr %e --batch_size %d --accum_steps %d" % (
pargs.num_samples, pargs.max_length, pargs.num_epochs, pargs.lr, pargs.batch_size, pargs.accum_steps))
logger.info("开始创建分词器...")
pretrained_checkpoint = "uer/ZhipuAI/chatglm3-6b-base"
tokenizer = AutoTokenizer.from_pretrained(pretrained_checkpoint,
trust_remote_code=True,
)
logger.info(tokenizer)
logger.info("开始读取数据...")
dataset = MyDataset(pargs.train_path, tokenizer)
data_collator = DataCollatorForSeq2Seq(
tokenizer=tokenizer,
)
logger.info("开始创建模型...")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
)
model = AutoModelForCausalLM.from_pretrained(
pretrained_checkpoint,
trust_remote_code=True,
quantization_config=bnb_config,
torch_dtype=torch.float16,
)
model.gradient_checkpointing_enable()
model = prepare_model_for_kbit_training(model)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
inference_mode=False,
r=8,
lora_alpha=32,
lora_dropout=0.1,
)
logger.info(lora_config)
model = get_peft_model(model, lora_config)
logger.info(lora_config)
model.print_trainable_parameters()
# 计算参数量和 trainable 参数量
trainable_param_count, param_count = model.get_nb_trainable_parameters()
logger.info("trainable params: %d || all params: %d || trainable%%: %f" % (
trainable_param_count, param_count, (100.0 * trainable_param_count) / param_count))
model.to(device)
logger.info("开始设置训练参数TrainingArguments...")
# 半精度eps重新设置,否则会导致loss上溢出或下溢出
training_args = TrainingArguments(
output_dir=mycheckpoint,
overwrite_output_dir=True,
logging_strategy="epoch",
per_device_train_batch_size=pargs.batch_size,
gradient_accumulation_steps=pargs.accum_steps,
num_train_epochs=pargs.num_epochs,
lr_scheduler_type="linear",
warmup_ratio=0.1,
dataloader_drop_last=False,
learning_rate=pargs.lr,
weight_decay=1e-2,
adam_epsilon=1e-4,
max_grad_norm=1.0,
save_strategy="no",
optim="paged_adamw_8bit",
fp16=True,
)
mytraincallback = MyTrainCallback(logger)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset[:pargs.num_samples],
data_collator=data_collator,
callbacks=[mytraincallback],
)
logger.info("开始训练...")
model.config.use_cache = False
trainer.train()
logger.info("保存模型")
trainer.model.save_pretrained(mycheckpoint)
def generator(checkpoint):
logger = create_logger(name="train_log",
filename=checkpoint + "/chatglm4ie.log")
logger.info(
"------------------------------------------------------------------------------------------------------------------------------------------")
logger.info("INFER LOGGING......")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
)
lora_config = LoraConfig.from_pretrained(checkpoint)
tokenizer = AutoTokenizer.from_pretrained(lora_config.base_model_name_or_path,
trust_remote_code=True,
)
model = AutoModelForCausalLM.from_pretrained(
lora_config.base_model_name_or_path,
trust_remote_code=True,
quantization_config=bnb_config,
load_in_8bit=True,
torch_dtype=torch.float16,
)
model = PeftModel.from_pretrained(model, checkpoint)
model.half()
model.eval()
prompt_prefix = "你现在是一个信息抽取模型,请你帮我找出下文中的实体关系三元组,形式为\"主体-主体类别_关系_客体-客体类别\",三元组之间用\\n分割。\n文本:"
prompt_suffix = "\n实体关系三元组有:\n"
intent = True
while intent:
query = input("\n文本:")
if query == '':
intent = False
continue
prompt = prompt_prefix + query + prompt_suffix
logger.info(prompt)
btime = time.time()
# 方法一:直接使用模型的chat函数
output = model.chat(tokenizer,
prompt,
history=[],
min_length=3,
max_new_tokens=pargs.max_new_tokens,
pad_token_id=tokenizer.pad_token_id,
repetition_penalty=3.5,
length_penalty=0.5,
early_stopping=True,
num_beams=3,
do_sample=True,
top_k=50,
top_p=0.95,
)
etime = time.time()
tries = output[0]
print(tries)
logger.info(tries)
logger.info("耗时:" + format_time(etime - btime))
# print("\n实体有:\n")
# for entity in entities.split("\n"):
# print(" " + entity)
# 方法二:使用generate函数
res = tokenizer.build_chat_input(prompt, history=[], role="user")
inputs = res["input_ids"].cuda()
attention_mask = res["attention_mask"].cuda()
btime = time.time()
generated_ids = model.generate(
inputs=inputs,
attention_mask=attention_mask,
min_length=3,
max_new_tokens=pargs.max_new_tokens,
pad_token_id=tokenizer.pad_token_id,
repetition_penalty=3.5,
length_penalty=0.5,
early_stopping=True,
num_beams=3,
do_sample=True,
top_k=50,
top_p=0.95,
)
etime = time.time()
decoded_pres = tokenizer.batch_decode(generated_ids,
# skip_special_tokens=True,
)[0]
print(decoded_pres)
logger.info(decoded_pres)
logger.info("耗时:" + format_time(etime - btime))
# prefix = "[gMask]sop<|user|> \n " + prompt_prefix + prompt + "<|assistant|>\n "
# entities = decoded_pres[len(prefix):].strip()
#
# print("\n实体有:\n")
# for entity in entities.split("\n"):
# print(" " + entity)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--mode", default="train", type=str, required=True)
parser.add_argument("--train_path", default="data/CMeIE/CMeIE_train.jsonl", type=str)
parser.add_argument("--num_samples", default=5000, type=int)
parser.add_argument("--max_length", default=600, type=int)
parser.add_argument("--max_new_tokens", default=8192, type=int)
parser.add_argument("--num_epochs", default=10, type=int)
parser.add_argument("--batch_size", default=2, type=int)
parser.add_argument("--accum_steps", default=4, type=int)
parser.add_argument("--lr", default=5e-4, type=float)
pargs = parser.parse_args()
if pargs.mode == "train":
train()
elif pargs.mode == "infer":
generator("models/chatglm4ie")