|
|
import re
|
|
|
from datasets import Dataset
|
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
|
|
|
from peft import LoraConfig, get_peft_model, PeftModel
|
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
model_name = "Qwen/Qwen2.5-0.5B-Instruct"
|
|
|
|
|
|
base_model = AutoModelForCausalLM.from_pretrained(
|
|
|
model_name,
|
|
|
torch_dtype="auto",
|
|
|
device_map="auto"
|
|
|
)
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
raw_data_path = r"data/CoTdata.txt"
|
|
|
with open(raw_data_path, "r", encoding="utf-8") as f:
|
|
|
raw_lines = f.readlines()
|
|
|
|
|
|
|
|
|
def process_line(line):
|
|
|
|
|
|
pattern = r"^(.*?)<think>(.*?)</think>[::](.*)$"
|
|
|
match = re.match(pattern, line.strip())
|
|
|
if match:
|
|
|
keywords = match.group(1).strip()
|
|
|
cot = match.group(2).strip()
|
|
|
poem = match.group(3).strip()
|
|
|
|
|
|
training_text = (
|
|
|
f"【输入】:根据以下关键词生成一首歌词,歌词中包含多个句子,确保句子通顺、诗意、格式正确。"
|
|
|
f"让我们一步一步的思考(思考过程包含在<think>和</think>之间):{keywords}\n\n"
|
|
|
f"【输出】:<think>{cot}</think>\n{poem}"
|
|
|
)
|
|
|
return training_text
|
|
|
else:
|
|
|
|
|
|
print("跳过格式错误的行:", line.strip())
|
|
|
return None
|
|
|
|
|
|
|
|
|
processed_samples = []
|
|
|
for line in raw_lines:
|
|
|
result = process_line(line)
|
|
|
if result:
|
|
|
processed_samples.append(result)
|
|
|
|
|
|
|
|
|
dataset = Dataset.from_dict({"text": processed_samples})
|
|
|
|
|
|
|
|
|
model = PeftModel.from_pretrained(base_model, r"D:\GoodMusicV3.0\3_24_LoRA").to("cuda")
|
|
|
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
|
|
lora_config = LoraConfig(
|
|
|
r=8,
|
|
|
lora_alpha=32,
|
|
|
target_modules=["q_proj", "k_proj", "v_proj"],
|
|
|
lora_dropout=0.1,
|
|
|
bias="none",
|
|
|
task_type="CAUSAL_LM"
|
|
|
)
|
|
|
model = get_peft_model(model, lora_config)
|
|
|
model.cuda()
|
|
|
|
|
|
|
|
|
def tokenize_function(examples):
|
|
|
|
|
|
tokenized = tokenizer(examples["text"], truncation=True, padding="max_length", max_length=256)
|
|
|
tokenized["labels"] = tokenized["input_ids"].copy()
|
|
|
return tokenized
|
|
|
|
|
|
|
|
|
tokenized_dataset = dataset.map(tokenize_function, batched=True)
|
|
|
|
|
|
|
|
|
training_args = TrainingArguments(
|
|
|
output_dir="./lora",
|
|
|
num_train_epochs=1000,
|
|
|
per_device_train_batch_size=16,
|
|
|
learning_rate=2e-5,
|
|
|
weight_decay=0.01,
|
|
|
logging_steps=10000,
|
|
|
save_steps=15000,
|
|
|
fp16=True,
|
|
|
)
|
|
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
model=model,
|
|
|
args=training_args,
|
|
|
train_dataset=tokenized_dataset,
|
|
|
tokenizer=tokenizer,
|
|
|
)
|
|
|
|
|
|
|
|
|
trainer.train()
|
|
|
|
|
|
|
|
|
generation_config = {
|
|
|
"max_new_tokens": 1024,
|
|
|
"temperature": 1.0,
|
|
|
"top_p": 0.9,
|
|
|
"top_k": 40,
|
|
|
"repetition_penalty": 1.2,
|
|
|
"do_sample": True,
|
|
|
"encoder_no_repeat_ngram_size": 4,
|
|
|
}
|
|
|
if True:
|
|
|
prompt = "根据以下关键词生成一首歌词,歌词中包含多个句子,句子与句子之间使用/隔开,让我们一步一步的思考(思考过程包含在<think>和</think>之间):温柔,轮廓,洒脱:"
|
|
|
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
|
|
outputs = model.generate(input_ids, **generation_config)
|
|
|
decoded = tokenizer.decode(outputs[0], skip_special_tokens=False)
|
|
|
|
|
|
print(decoded)
|
|
|
|
|
|
|
|
|
model.save_pretrained("4_2_LoRA_3")
|
|
|
|