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
| import json import pandas as pd import torch from datasets import Dataset from modelscope import snapshot_download, AutoTokenizer from swanlab.integration.transformers import SwanLabCallback from peft import LoraConfig, TaskType, get_peft_model from transformers import ( AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForSeq2Seq, ) import swanlab def process_func(example): """ 将数据集进行预处理 """ MAX_LENGTH = 384 input_ids, attention_mask, labels = [], [], [] instruction = tokenizer( f"<|im_start|>system\n{example['instruction']}<|im_end|>\n<|im_start|>user\n{example['input']}<|im_end|>\n<|im_start|>assistant\n", add_special_tokens=False, ) response = tokenizer(f"{example['output']}", add_special_tokens=False) input_ids = ( instruction["input_ids"] + response["input_ids"] + [tokenizer.pad_token_id] ) attention_mask = instruction["attention_mask"] + response["attention_mask"] + [1] labels = ( [-100] * len(instruction["input_ids"]) + response["input_ids"] + [tokenizer.pad_token_id] ) if len(input_ids) > MAX_LENGTH: input_ids = input_ids[:MAX_LENGTH] attention_mask = attention_mask[:MAX_LENGTH] labels = labels[:MAX_LENGTH] return {"input_ids": input_ids, "attention_mask": attention_mask, "labels": labels}
def predict(messages, model, tokenizer): device = "cuda" text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) model_inputs = tokenizer([text], return_tensors="pt").to(device)
generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512) generated_ids = [ output_ids[len(input_ids) :] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) ]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
return response
model_dir = snapshot_download("qwen/Qwen2.5-7B-Instruct", cache_dir="./", revision="master")
tokenizer = AutoTokenizer.from_pretrained("./qwen/Qwen2___5-7B-Instruct/", use_fast=False, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("./qwen/Qwen2___5-7B-Instruct/", device_map="auto", torch_dtype=torch.bfloat16) model.enable_input_require_grads()
train_jsonl_path = "DISC-Law-SFT-Pair-QA-released-new.jsonl" train_df = pd.read_json(train_jsonl_path, lines=True)[5:5000] train_ds = Dataset.from_pandas(train_df) train_dataset = train_ds.map(process_func, remove_columns=train_ds.column_names) test_df = pd.read_json(train_jsonl_path, lines=True)[:5]
config = LoraConfig( task_type=TaskType.CAUSAL_LM, target_modules=[ "q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj", ], inference_mode=False, r=64, lora_alpha=16, lora_dropout=0.1, )
peft_model = get_peft_model(model, config)
args = TrainingArguments( output_dir="./output/Qwen2.5-7b", per_device_train_batch_size=4, gradient_accumulation_steps=4, logging_steps=10, num_train_epochs=2, save_steps=100, learning_rate=1e-4, save_on_each_node=True, gradient_checkpointing=True, report_to="none", )
class HuanhuanSwanLabCallback(SwanLabCallback): def on_train_begin(self, args, state, control, model=None, **kwargs): if not self._initialized: self.setup(args, state, model, **kwargs) print("训练开始") print("未开始微调,先取3条主观评测:") test_text_list = [] for index, row in test_df[:3].iterrows(): instruction = row["instruction"] input_value = row["input"]
messages = [ {"role": "system", "content": f"{instruction}"}, {"role": "user", "content": f"{input_value}"}, ]
response = predict(messages, peft_model, tokenizer) messages.append({"role": "assistant", "content": f"{response}"}) result_text = f"【Q】{messages[1]['content']}\n【LLM】{messages[2]['content']}\n" print(result_text) test_text_list.append(swanlab.Text(result_text, caption=response))
swanlab.log({"Prediction": test_text_list}, step=0) def on_epoch_end(self, args, state, control, **kwargs): test_text_list = [] for index, row in test_df.iterrows(): instruction = row["instruction"] input_value = row["input"] ground_truth = row["output"]
messages = [ {"role": "system", "content": f"{instruction}"}, {"role": "user", "content": f"{input_value}"}, ]
response = predict(messages, peft_model, tokenizer) messages.append({"role": "assistant", "content": f"{response}"}) if index == 0: print("epoch", round(state.epoch), "主观评测:") result_text = f"【Q】{messages[1]['content']}\n【LLM】{messages[2]['content']}\n【GT】 {ground_truth}" print(result_text) test_text_list.append(swanlab.Text(result_text, caption=response))
swanlab.log({"Prediction": test_text_list}, step=round(state.epoch)) swanlab_callback = HuanhuanSwanLabCallback( project="Qwen2.5-LoRA-Law", experiment_name="7b", config={ "model": "https://modelscope.cn/models/Qwen/Qwen2.5-7B-Instruct", "dataset": "https://huggingface.co/datasets/ShengbinYue/DISC-Law-SFT", "github": "https://github.com/datawhalechina/self-llm", "system_prompt": "你是一个法律专家,请根据用户的问题给出专业的回答", "lora_rank": 64, "lora_alpha": 16, "lora_dropout": 0.1, }, )
trainer = Trainer( model=peft_model, args=args, train_dataset=train_dataset, data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, padding=True), callbacks=[swanlab_callback], )
trainer.train()
swanlab.finish()
|