Instructions to use qingpingwan/Qwen2.5-7B-Lora-Law with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Adapters
How to use qingpingwan/Qwen2.5-7B-Lora-Law with Adapters:
from adapters import AutoAdapterModel model = AutoAdapterModel.from_pretrained("undefined") model.load_adapter("qingpingwan/Qwen2.5-7B-Lora-Law", set_active=True) - Notebooks
- Google Colab
- Kaggle
file_2
Browse files- README.md +100 -3
- adapter_config.json +34 -0
- rng_state.pth +3 -0
- scheduler.pt +3 -0
- training_args.bin +3 -0
README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Qwen2.5-Coder-7B-LoRA-Law
|
| 2 |
+
|
| 3 |
+
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.
|
| 4 |
+
|
| 5 |
+
## Model Description
|
| 6 |
+
|
| 7 |
+
This model is intended to provide informative and comprehensive answers to legal questions posed in Chinese.
|
| 8 |
+
|
| 9 |
+
## Training Details
|
| 10 |
+
|
| 11 |
+
* **Base Model:** Qwen/Qwen2.5-Coder-7B-Instruct
|
| 12 |
+
* **Dataset:** [DISC-Law-SFT](https://huggingface.co/datasets/ShengbinYue/DISC-Law-SFT), specifically the `DISC-Law-SFT-Pair-QA-released.jsonl` subset.
|
| 13 |
+
* **Fine-tuning Method:** LoRA
|
| 14 |
+
* **LoRA Parameters:**
|
| 15 |
+
* `r`: 64
|
| 16 |
+
* `lora_alpha`: 16
|
| 17 |
+
* `lora_dropout`: 0.1
|
| 18 |
+
* **Training Hyperparameters:**
|
| 19 |
+
* `per_device_train_batch_size`: 4
|
| 20 |
+
* `gradient_accumulation_steps`: 8
|
| 21 |
+
* `learning_rate`: 2e-5
|
| 22 |
+
* `num_train_epochs`: 2
|
| 23 |
+
* **System Prompt:** 你是一个法律专家,请根据用户的问题给出专业的回答
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
## How to Merge Lora weights
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
```python
|
| 30 |
+
from peft import PeftModel
|
| 31 |
+
from transformers import AutoModelForCausalLM
|
| 32 |
+
import torch
|
| 33 |
+
import os
|
| 34 |
+
from modelscope import AutoTokenizer
|
| 35 |
+
import shutil
|
| 36 |
+
|
| 37 |
+
# 保证原始模型的各个文件不遗漏保存到merge_path中
|
| 38 |
+
def copy_files_not_in_B(A_path, B_path):
|
| 39 |
+
"""
|
| 40 |
+
Copies files from directory A to directory B if they exist in A but not in B.
|
| 41 |
+
|
| 42 |
+
:param A_path: Path to the source directory (A).
|
| 43 |
+
:param B_path: Path to the destination directory (B).
|
| 44 |
+
"""
|
| 45 |
+
# 保证路径存在
|
| 46 |
+
if not os.path.exists(A_path):
|
| 47 |
+
raise FileNotFoundError(f"The directory {A_path} does not exist.")
|
| 48 |
+
if not os.path.exists(B_path):
|
| 49 |
+
os.makedirs(B_path)
|
| 50 |
+
|
| 51 |
+
# 获取路径A中所有非权重文件
|
| 52 |
+
files_in_A = os.listdir(A_path)
|
| 53 |
+
files_in_A = set([file for file in files_in_A if not (".bin" in file or "safetensors" in file)])
|
| 54 |
+
# List all files in directory B
|
| 55 |
+
files_in_B = set(os.listdir(B_path))
|
| 56 |
+
|
| 57 |
+
# 找到所有A中存在但B中不存在的文件
|
| 58 |
+
files_to_copy = files_in_A - files_in_B
|
| 59 |
+
|
| 60 |
+
# 将文件或文件夹复制到B路径下
|
| 61 |
+
for file in files_to_copy:
|
| 62 |
+
src_path = os.path.join(A_path, file)
|
| 63 |
+
dst_path = os.path.join(B_path, file)
|
| 64 |
+
|
| 65 |
+
if os.path.isdir(src_path):
|
| 66 |
+
# 复制目录及其内容
|
| 67 |
+
shutil.copytree(src_path, dst_path)
|
| 68 |
+
else:
|
| 69 |
+
# 复制文件
|
| 70 |
+
shutil.copy2(src_path, dst_path)
|
| 71 |
+
|
| 72 |
+
def merge_lora_to_base_model():
|
| 73 |
+
model_name_or_path = '...' # 原模型地址
|
| 74 |
+
adapter_name_or_path = '...' # 微调后模型的保存地址
|
| 75 |
+
save_path = '...'
|
| 76 |
+
|
| 77 |
+
# 如果文件夹不存在,就创建
|
| 78 |
+
if not os.path.exists(save_path):
|
| 79 |
+
os.makedirs(save_path)
|
| 80 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path,trust_remote_code=True,)
|
| 81 |
+
|
| 82 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 83 |
+
model_name_or_path,
|
| 84 |
+
trust_remote_code=True,
|
| 85 |
+
low_cpu_mem_usage=True,
|
| 86 |
+
torch_dtype=torch.float16,
|
| 87 |
+
device_map="auto"
|
| 88 |
+
)
|
| 89 |
+
# 加载保存的 Adapter
|
| 90 |
+
model = PeftModel.from_pretrained(model, adapter_name_or_path, device_map="auto",trust_remote_code=True)
|
| 91 |
+
# 将 Adapter 合并到基础模型中
|
| 92 |
+
merged_model = model.merge_and_unload() # PEFT 的方法将 Adapter 权重合并到基础模型
|
| 93 |
+
# 保存合并后的模型
|
| 94 |
+
tokenizer.save_pretrained(save_path)
|
| 95 |
+
merged_model.save_pretrained(save_path, safe_serialization=False)
|
| 96 |
+
copy_files_not_in_B(model_name_or_path, save_path)
|
| 97 |
+
print(f"合并后的模型已保存至: {save_path}")
|
| 98 |
+
|
| 99 |
+
if __name__ == '__main__':
|
| 100 |
+
merge_lora_to_base_model()
|
adapter_config.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"alpha_pattern": {},
|
| 3 |
+
"auto_mapping": null,
|
| 4 |
+
"base_model_name_or_path": "/data4/zxf_2/large_model/Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 5 |
+
"bias": "none",
|
| 6 |
+
"fan_in_fan_out": false,
|
| 7 |
+
"inference_mode": true,
|
| 8 |
+
"init_lora_weights": true,
|
| 9 |
+
"layer_replication": null,
|
| 10 |
+
"layers_pattern": null,
|
| 11 |
+
"layers_to_transform": null,
|
| 12 |
+
"loftq_config": {},
|
| 13 |
+
"lora_alpha": 16,
|
| 14 |
+
"lora_dropout": 0.1,
|
| 15 |
+
"megatron_config": null,
|
| 16 |
+
"megatron_core": "megatron.core",
|
| 17 |
+
"modules_to_save": null,
|
| 18 |
+
"peft_type": "LORA",
|
| 19 |
+
"r": 64,
|
| 20 |
+
"rank_pattern": {},
|
| 21 |
+
"revision": null,
|
| 22 |
+
"target_modules": [
|
| 23 |
+
"k_proj",
|
| 24 |
+
"o_proj",
|
| 25 |
+
"q_proj",
|
| 26 |
+
"up_proj",
|
| 27 |
+
"v_proj",
|
| 28 |
+
"gate_proj",
|
| 29 |
+
"down_proj"
|
| 30 |
+
],
|
| 31 |
+
"task_type": "CAUSAL_LM",
|
| 32 |
+
"use_dora": false,
|
| 33 |
+
"use_rslora": false
|
| 34 |
+
}
|
rng_state.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:15f7ae7387a6a20adcc4e41674e4b55262300d5db43e9d4607298751c4898447
|
| 3 |
+
size 14244
|
scheduler.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f39804fe11ca958234bcad7fd5559b97aa33843cccfb2c28d5eafcc996c5d612
|
| 3 |
+
size 1064
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0e670f89bd0dc656a39495b7c1bff9e99f54efc3785a5d4557104b2c834cb896
|
| 3 |
+
size 5112
|