-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestQADatasets.py
More file actions
420 lines (396 loc) · 13.4 KB
/
TestQADatasets.py
File metadata and controls
420 lines (396 loc) · 13.4 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from datasets import load_dataset
# # 加载数据集
# dataset = load_dataset("YuvrajSingh9886/Agriculture-Soil-QA-Pairs-Dataset")
# # 查看数据集结构
# print(dataset)
# # 查看数据集中的train length
# print(len(dataset["train"]))
# print(dataset["train"][0])
import os
# from datasets import load_dataset
# from transformers import (
# AutoTokenizer,
# AutoModelForCausalLM,
# TrainingArguments,
# Trainer,
# )
# from peft import LoraConfig, get_peft_model, TaskType
# # Load model directly
# from transformers import AutoTokenizer, AutoModelForCausalLM
#
# tokenizer = AutoTokenizer.from_pretrained("ehristoforu/llama-3-12b-instruct")
# model = AutoModelForCausalLM.from_pretrained("ehristoforu/llama-3-12b-instruct")
# # 1. 加载模型和分词器
# # model_name = "llama3:8b" # 替换为你的 LLaMA3 模型名称
# output_dir = "./llama3_lora_finetuned"
#
# print("Loading tokenizer and model...")
# # tokenizer = AutoTokenizer.from_pretrained(model_name)
# # model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
#
# # 2. 加载数据集
# print("Loading dataset...")
# dataset = load_dataset("YuvrajSingh9886/Agriculture-Soil-QA-Pairs-Dataset")
#
# # 3. 数据预处理
# def preprocess_function(examples):
# inputs = examples["QUESTION.question"]
# targets = examples["ANSWER"]
# return tokenizer(inputs, text_target=targets, truncation=True, max_length=512)
#
# print("Preprocessing dataset...")
# tokenized_datasets = dataset.map(preprocess_function, batched=True)
#
# # 4. 配置 LoRA
# print("Configuring LoRA...")
# lora_config = LoraConfig(
# task_type=TaskType.CAUSAL_LM, # 因果语言模型任务
# r=16,
# lora_alpha=32,
# lora_dropout=0.1
# )
# model = get_peft_model(model, lora_config)
# model.print_trainable_parameters()
#
# # 5. 配置训练参数
# print("Setting up training arguments...")
# training_args = TrainingArguments(
# output_dir=output_dir,
# per_device_train_batch_size=4, # 根据显存调整
# num_train_epochs=3, # 训练轮数
# learning_rate=2e-4, # 学习率
# save_strategy="epoch", # 每个 epoch 保存
# evaluation_strategy="epoch", # 每个 epoch 评估
# logging_dir="./logs",
# fp16=True, # 启用混合精度
# save_total_limit=2 # 最多保存两个模型
# )
#
# # 6. 开始训练
# print("Starting training...")
# trainer = Trainer(
# model=model,
# args=training_args,
# train_dataset=tokenized_datasets["train"],
# eval_dataset=tokenized_datasets["test"]
# )
#
# trainer.train()
#
# # 7. 保存微调后的模型
# print("Saving fine-tuned model...")
# model.save_pretrained(output_dir)
# tokenizer.save_pretrained(output_dir)
#
# # 8. 测试生成
# print("Testing fine-tuned model...")
# input_text = "What is the best soil for growing wheat?"
# inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
# outputs = model.generate(**inputs, max_new_tokens=50)
# print("Generated text:")
# print(tokenizer.decode(outputs[0], skip_special_tokens=True))
# import torch
#
# print("CUDA available:", torch.cuda.is_available())
# print("GPU count:", torch.cuda.device_count())
# if torch.cuda.is_available():
# print("GPU name:", torch.cuda.get_device_name(0))
#
# # 1. 加载模型和分词器
# # model_name = "llama3:8b" # 替换为你的 LLaMA3 模型名称
# output_dir = "./llama3_lora_finetuned"
#
# print("Loading tokenizer and model...")
# # tokenizer = AutoTokenizer.from_pretrained(mode.l_name)
# # model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
#
# # 2. 加载数据集
# print("Loading dataset...")
# dataset = load_dataset("YuvrajSingh9886/Agriculture-Soil-QA-Pairs-Dataset")
# split_datasets = dataset["train"].train_test_split(test_size=0.2, seed=42)
# 3. 数据预处理
# def preprocess_function(examples):
# inputs = examples["QUESTION.question"]
# targets = examples["ANSWER"]
# return tokenizer(inputs, text_target=targets, truncation=True, max_length=512)
#
# print("Preprocessing dataset...")
# tokenized_datasets = split_datasets.map(preprocess_function, batched=True)
#
#
# # 4. 配置 LoRA
# print("Configuring LoRA...")
# lora_config = LoraConfig(
# task_type=TaskType.CAUSAL_LM, # 因果语言模型任务
# r=16,
# lora_alpha=32,
# lora_dropout=0.1
# )
# model = get_peft_model(model, lora_config)
# model.print_trainable_parameters()
#
# # 5. 配置训练参数
# print("Setting up training arguments...")
# training_args = TrainingArguments(
# output_dir=output_dir,
# per_device_train_batch_size=4, # 根据显存调整
# num_train_epochs=3, # 训练轮数
# learning_rate=2e-4, # 学习率
# save_strategy="epoch", # 每个 epoch 保存
# evaluation_strategy="epoch", # 每个 epoch 评估
# logging_dir="./logs",
# fp16=True, # 启用混合精度
# save_total_limit=2 # 最多保存两个模型
# )
#
# # 6. 开始训练
# print("Starting training...")
# trainer = Trainer(
# model=model,
# args=training_args,
# train_dataset=tokenized_datasets["train"],
# eval_dataset=tokenized_datasets["test"]
# )
#
# trainer.train()
#
# # 7. 保存微调后的模型
# print("Saving fine-tuned model...")
# model.save_pretrained(output_dir)
# tokenizer.save_pretrained(output_dir)
#
# # 8. 测试生成
# print("Testing fine-tuned model...")
# input_text = "What is the best soil for growing wheat?"
# inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
# outputs = model.generate(**inputs, max_new_tokens=50)
# print("Generated text:")
# print(tokenizer.decode(outputs[0], skip_special_tokens=True))
# def preprocess_function(examples):
# inputs = examples["QUESTION.question"]
# targets = examples["ANSWER"]
# model_inputs = tokenizer(
# inputs, max_length=512, truncation=True, padding="max_length"
# )
# labels = tokenizer(
# targets, max_length=512, truncation=True, padding="max_length"
# )
# model_inputs["labels"] = labels["input_ids"]
# return model_inputs
#
# tokenized_datasets = split_datasets.map(preprocess_function, batched=True)
#
# # 4. 配置 LoRA
# lora_config = LoraConfig(
# task_type=TaskType.CAUSAL_LM, r=16, lora_alpha=32, lora_dropout=0.1
# )
# model = get_peft_model(model, lora_config)
# model.print_trainable_parameters()
#
# # 5. 创建数据填充器
# data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)
#
# # 6. 配置训练参数
# training_args = TrainingArguments(
# output_dir="./llama3_lora_finetuned",
# per_device_train_batch_size=4,
# num_train_epochs=3,
# learning_rate=2e-4,
# save_strategy="epoch",
# evaluation_strategy="epoch",
# logging_dir="./logs",
# fp16=True,
# save_total_limit=2
# )
#
# # 7. 开始训练
# trainer = Trainer(
# model=model,
# args=training_args,
# train_dataset=tokenized_datasets["train"],
# eval_dataset=tokenized_datasets["test"],
# data_collator=data_collator
# )
#
# trainer.train()
#
# # 8. 保存模型
# model.save_pretrained("./llama3_lora_finetuned")
# tokenizer.save_pretrained("./llama3_lora_finetuned")
from datasets import load_dataset,Dataset,DatasetDict
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
DataCollatorForSeq2Seq,
TrainingArguments,
Trainer,
)
from transformers import BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, TaskType
from accelerate import init_empty_weights, load_checkpoint_and_dispatch
import pandas as pd
import torch
# 1. 加载数据集
# dataset = load_dataset("YuvrajSingh9886/Agriculture-Soil-QA-Pairs-Dataset")
# split_datasets = dataset["train"].train_test_split(test_size=0.2, seed=42)
file_path = "Datasets/qna-dataset-farmgenie-soil-v2.csv" # 替换为你的本地文件路径
data = pd.read_csv(file_path)
# 只保留需要的列
filtered_data = data[['QUESTION.question', 'ANSWER']].dropna()
# 转换为 Hugging Face Dataset 格式
dataset = Dataset.from_pandas(filtered_data)
dataset_dict = DatasetDict({
"train": dataset # 将整个数据集初始化为训练集
})
# 划分训练集和测试集
split_datasets = dataset_dict["train"].train_test_split(test_size=0.2, seed=42)
# 查看分割后的数据集
print("训练集大小:", len(split_datasets["train"]))
print("测试集大小:", len(split_datasets["test"]))
# split_datasets = dataset["train"].train_test_split(test_size=0.2, seed=42)
# 查看处理后的数据集
print(dataset)
print(split_datasets)
for item in split_datasets:
print(item)
# 保存为 Hugging Face Dataset 格式
dataset.save_to_disk("processed_dataset")
# 2. 加载分词器和模型
# tokenizer = AutoTokenizer.from_pretrained("ehristoforu/llama-3-12b-instruct")
# model = AutoModelForCausalLM.from_pretrained("ehristoforu/llama-3-12b-instruct")
# 保存模型和分词器
local_model_path = "./meta_llama_3_8B2"
# model.save_pretrained(local_model_path)
# tokenizer.save_pretrained(local_model_path)
with init_empty_weights():
model = AutoModelForCausalLM.from_pretrained(local_model_path)
# 加载并分发权重
bnb_config = BitsAndBytesConfig(load_in_8bit=True)
# model = AutoModelForCausalLM.from_pretrained(
# "./llama-3-12b-local/",
# # device_map={"": "cpu"},
# device_map="auto",
# offload_folder="offload",
# quantization_config=bnb_config,
# # no_split_module_classes=["LlamaDecoderLayer"]
# )
model = AutoModelForCausalLM.from_pretrained(
local_model_path,
# device_map={"": "cpu"},
device_map="auto",
offload_folder="offload",
quantization_config=bnb_config,
# no_split_module_classes=["LlamaDecoderLayer"]
)
model.gradient_checkpointing_enable()
model.config.use_cache = False
# 加载分词器
tokenizer = AutoTokenizer.from_pretrained(local_model_path)
# 检查分词器的特殊标记
if tokenizer.pad_token is None:
if tokenizer.eos_token:
tokenizer.pad_token = tokenizer.eos_token # 设置 pad_token 为 eos_token
else:
tokenizer.add_special_tokens({'pad_token': '[PAD]'}) # 添加自定义 pad_token
model.resize_token_embeddings(len(tokenizer)) # 更新模型词汇表
# 3. 数据预处理
# def preprocess_function(examples):
# inputs = examples["QUESTION.question"]
# targets = examples["ANSWER"]
# return tokenizer(inputs, text_target=targets, truncation=True, max_length=512)
# tokenized_datasets = split_datasets.map(preprocess_function, batched=True)
def preprocess_function(examples):
inputs = examples["QUESTION.question"]
targets = examples["ANSWER"]
# 对输入进行分词
model_inputs = tokenizer(
inputs,
max_length=512,
truncation=True,
padding="max_length" # 确保所有样本长度一致
)
# 对标签进行分词
labels = tokenizer(
targets,
max_length=512,
truncation=True,
padding="max_length" # 确保标签长度一致
)["input_ids"]
# 将填充值设置为 -100(用于忽略梯度计算)
labels_with_ignore_index = [
[-100 if token == tokenizer.pad_token_id else token for token in label]
for label in labels
]
model_inputs["labels"] = labels_with_ignore_index
return model_inputs
# 应用数据预处理
tokenized_datasets = split_datasets.map(preprocess_function, batched=True)
# 4. 配置 LoRA
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM, r=16, lora_alpha=32, lora_dropout=0.1
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
from torch.nn import functional as F
# 5. 创建数据填充器
data_collator = DataCollatorForSeq2Seq(tokenizer, model=model, padding="longest")
for name, param in model.named_parameters():
print(f"Parameter {name}: dtype={param.dtype}, requires_grad={param.requires_grad}")
if param.dtype.is_floating_point or param.dtype.is_complex:
param.requires_grad = True
# 6. 配置训练参数
def compute_loss(model, inputs):
outputs = model(**inputs)
logits = outputs.logits
labels = inputs["labels"]
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss = F.cross_entropy(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
ignore_index=-100
)
return loss
from transformers import Trainer
class CustomTrainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False, **kwargs):
outputs = model(**inputs)
logits = outputs.logits
labels = inputs["labels"]
# 移位 logits 和 labels 以对齐
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
# 计算交叉熵损失
loss = F.cross_entropy(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
ignore_index=-100
)
return (loss, outputs) if return_outputs else loss
# 配置训练参数
training_args = TrainingArguments(
output_dir="./llama3_lora_finetuned",
per_device_train_batch_size=2,
num_train_epochs=10,
learning_rate=2e-2,
save_strategy="epoch",
evaluation_strategy="epoch",
logging_dir="./logs",
fp16=False
)
optimizer = torch.optim.AdamW(
filter(lambda p: p.requires_grad, model.parameters()), lr=2e-2
)
# 创建 Trainer
trainer = CustomTrainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["test"],
data_collator=data_collator
)
# 开始训练
trainer.train()
# 8. 保存模型
model.save_pretrained("./llama3_lora_finetuned")
tokenizer.save_pretrained("./llama3_lora_finetuned")