Qwen2.5-Coder-7B-LoRA-Law
This repository contains trained weights for a fine-tuned version of the Qwen2.5-Coder-7B-Instruct model, specialized for answering legal questions in Chinese. The model is fine-tuned using LoRA on the DISC-Law-SFT dataset.
Model Description
This model is intended to provide informative and comprehensive answers to legal questions posed in Chinese.
Training Details
- Base Model: Qwen/Qwen2.5-Coder-7B-Instruct
- Dataset: DISC-Law-SFT, specifically the
DISC-Law-SFT-Pair-QA-released.jsonl
subset. - Fine-tuning Method: LoRA
- LoRA Parameters:
r
: 64lora_alpha
: 16lora_dropout
: 0.1
- Training Hyperparameters:
per_device_train_batch_size
: 4gradient_accumulation_steps
: 8learning_rate
: 2e-5num_train_epochs
: 2
- System Prompt: 你是一个法律专家,请根据用户的问题给出专业的回答
How to Merge Lora weights
from peft import PeftModel
from transformers import AutoModelForCausalLM
import torch
import os
from modelscope import AutoTokenizer
import shutil
# 保证原始模型的各个文件不遗漏保存到merge_path中
def copy_files_not_in_B(A_path, B_path):
"""
Copies files from directory A to directory B if they exist in A but not in B.
:param A_path: Path to the source directory (A).
:param B_path: Path to the destination directory (B).
"""
# 保证路径存在
if not os.path.exists(A_path):
raise FileNotFoundError(f"The directory {A_path} does not exist.")
if not os.path.exists(B_path):
os.makedirs(B_path)
# 获取路径A中所有非权重文件
files_in_A = os.listdir(A_path)
files_in_A = set([file for file in files_in_A if not (".bin" in file or "safetensors" in file)])
# List all files in directory B
files_in_B = set(os.listdir(B_path))
# 找到所有A中存在但B中不存在的文件
files_to_copy = files_in_A - files_in_B
# 将文件或文件夹复制到B路径下
for file in files_to_copy:
src_path = os.path.join(A_path, file)
dst_path = os.path.join(B_path, file)
if os.path.isdir(src_path):
# 复制目录及其内容
shutil.copytree(src_path, dst_path)
else:
# 复制文件
shutil.copy2(src_path, dst_path)
def merge_lora_to_base_model():
model_name_or_path = '...' # 原模型地址
adapter_name_or_path = '...' # 微调后模型的保存地址
save_path = '...'
# 如果文件夹不存在,就创建
if not os.path.exists(save_path):
os.makedirs(save_path)
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path,trust_remote_code=True,)
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
trust_remote_code=True,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
device_map="auto"
)
# 加载保存的 Adapter
model = PeftModel.from_pretrained(model, adapter_name_or_path, device_map="auto",trust_remote_code=True)
# 将 Adapter 合并到基础模型中
merged_model = model.merge_and_unload() # PEFT 的方法将 Adapter 权重合并到基础模型
# 保存合并后的模型
tokenizer.save_pretrained(save_path)
merged_model.save_pretrained(save_path, safe_serialization=False)
copy_files_not_in_B(model_name_or_path, save_path)
print(f"合并后的模型已保存至: {save_path}")
if __name__ == '__main__':
merge_lora_to_base_model()
- Downloads last month
- 4
Inference Providers
NEW
This model is not currently available via any of the supported third-party Inference Providers, and
HF Inference API was unable to determine this model’s pipeline type.