阿里qwen微调


附件见后面
            process_data_law.py,
            qwen_lora_merge.py
一、本地部署

1 代码下载

项目地址: https://github.com/QwenLM/Qwen
下载到本地: git clone  GitHub - QwenLM/Qwen: The official repo of Qwen (通义千问) chat & pretrained large language model proposed by Alibaba Cloud.

git clone https://521github.com/extdomains/github.com/QwenLM/Qwen.git
2 环境安装

conda create -n qwen2 python==3.10.1
conda activate qwen2
pip install -r requirements.txt

pytorch离线下载 (https://download.pytorch.org/whl/torch_stable.html)

3 模型下载

模型占用资源情况
3.1 7B模型下载

git clone https://www.modelscope.cn/qwen/Qwen-7B-Chat.git
/media/waj/新加卷/github/NLP/llm_model/Qwen-7B-Chat
3.1 1.8B模型下载

git clone https://www.modelscope.cn/qwen/Qwen-1_8B-Chat.git

/media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat
int4 量化版本
git clone https://www.modelscope.cn/qwen/Qwen-1_8B-Chat-Int4.git
(使用量化版本,条件1 :torch 2.0及以上,transformers版本为4.32.0及以上 ;
                                条件2:  pip install auto-gptq optimum)
4 本地模型推理

1 安装依赖包:pip intall -r requirements_web_demo.txt
2 vim web_demo.py 修改里面的模型路径 (可选)
3 推理 :
GPU 推理
python web_demo.py --server-name 0.0.0.0 -c /media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat

python web_demo.py --server-name 0.0.0.0 -c E:\\360Downloads\\qwen\\Qwen-1_8B-Chat

python web_demo.py --server-name 0.0.0.0 -c E:\\360Downloads\\qwen\\Qwen-1_8B-Chat

cpu 

推理:(5~6G内存)
python web_demo.py  --server-name 0.0.0.0   -c /media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat  --cpu-only 
5 数据集

5.1 数据下载

https://hf-mirror.com/datasets/ShengbinYue/DISC-Law-SFT
下载 :https://modelscope.cn/datasets/Robin021/DISC-Law-SFT/files

数据格式:
[
{
;id;: ;identity_0;,
;conversations;: [
{
;from;: ;user;,
;value;: ;你好;
     },
     {
       ;from;: ;assistant;,
       ;value;: ;我是一个语言模型,我叫通义千问。;
     }
   ]
 }
]
5.2 数据处理

python  process_data_law.py
处理好的数据存放路径: /media/waj/新加卷/github/NLP/Qwen/train_data_law.json
6 模型训练

6.1 依赖安装

pip install ;peft<0.8.0; deepspeed

6.2 训练

修改v 中模型路径和数据路径
MODEL=;/media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat;
DATA=;/media/waj/新加卷/github/NLP/Qwen/train_data_law.json;
bash finetune/finetune_lora_single_gpu.sh
7 模型推理


7.1 lora模型合并

微调好的模型目录:
path_to_adapter=;/media/waj/新加卷/github/NLP/test/Qwen-main/output_qwen/checkpoint-1200/;
模型保存目录
new_model_directory=;/media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat_law2;
python qwen_lora_merge.py

7.2 模型web界面推理

python web_demo.py --server-name 0.0.0.0 -c /media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat_law2

附件

附件1 :process_data_law.py
import json

# 读取以.jsonl结尾的文件
json_data = []
with open('/media/waj/新加卷/github/NLP/data/DISC-Law-SFT/DISC-Law-SFT-Triplet-released.jsonl', 'r', encoding='utf-8') as file:
for line in file:
data = json.loads(line)
       json_data.append(data)
# 待填入的模板
template = []

# 遍历json数据集
for idx, data in enumerate(json_data[:]):
   conversation = [
       {
           ;from;: ;user;,
           ;value;: data[;input;]
       },
       {
           ;from;: ;assistant;,
           ;value;: data[;output;]
       }
   ]
   
   template.append({
       ;id;: f;identity_{idx};,
       ;conversations;: conversation
   })
print(len(template))
# 输出填充数据后的模板
print(json.dumps(template[2], ensure_ascii=False, indent=2))
# 将template写入到本地文件
output_file_path = ;train_data_law.json;
with open(output_file_path, 'w', encoding='utf-8') as f:
   json.dump(template, f, ensure_ascii=False, indent=2)

print(f;处理好的数据已写入到本地文件: {output_file_path};)


附件2 qwen_lora_merge.py
#1 模型合并
from peft import AutoPeftModelForCausalLM
path_to_adapter=;/media/waj/新加卷/github/NLP/test/Qwen-main/output_qwen/checkpoint-1200/;
new_model_directory=;/media/waj/新加卷/github/NLP/llm_model/Qwen-1_8B-Chat_law2;
model = AutoPeftModelForCausalLM.from_pretrained(
   path_to_adapter, # path to the output directory
   device_map=;auto;,
   trust_remote_code=True
).eval()

merged_model = model.merge_and_unload()
# max_shard_size and safe serialization are not necessary.
# They respectively work for sharding checkpoint and save the model to safetensors
merged_model.save_pretrained(new_model_directory, max_shard_size=;2048MB;, safe_serialization=True)

#2  分词器保存
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(
   path_to_adapter, # path to the output directory
   trust_remote_code=True
)

tokenizer.save_pretrained(new_model_directory)

一、脚本获取

官方链接: Github-Qwen

夸克网盘链接: Qwen,提取码:SYXe

二、使用步骤

1.数据格式

示例:

[

    {

        "id": "identity_0",

        "conversations": [

            {

                "from": "user",

                "value": "你好"

            },

            {

                "from": "assistant",

                "value": "我是一个语言模型,我叫通义千问。"

            }

        ]

    },

    {

        "id": "identity_1",

        "conversations": [

            {

                "from": "user",

                "value": "你是谁?"

            },

            {

                "from": "assistant",

                "value": "我是一个语言模型,我叫通义千问。"

            }

        ]

    }

]

2.SFT微调:

单卡微调:

脚本路径:

Qwen-main/finetune/finetune_lora_single_gpu.sh

1

部分参数说明:

MODEL:模型路径

DATA:自定义数据集路径

output_dir:输出模型路径

num_train_epochs: 设置训练的轮数

model_max_length:模型处理序列长度,根据自身数据定义

per_device_train_batch_size: 训练批处理大小设置

save_steps: 模型每n步保存一次

执行命令:

bash finetune/finetune_lora_single_gpu.sh

1

多卡微调:

脚本路径:

Qwen-main/finetune/finetune_lora_ds.sh

1

部分参数说明:

MODEL:模型路径

DATA:自定义数据集路径

GPUS_PER_NODE:置每个节点上GPU的数量。在分布式训练环境中,这用于定义每个节点上可用的GPU数目。

output_dir:输出模型路径

num_train_epochs: 设置训练的轮数

model_max_length:模型处理序列长度,根据自身数据定义

per_device_train_batch_size: 训练批处理大小设置

save_steps: 模型每n步保存一次

8

多卡限定设置(如果你GPU不支持NCCL请加上以下设置,例如:RTX3090):

export CUDA_DEVICE_MAX_CONNECTIONS=1

设置CUDA设备最大连接数为1。这可能用于限制一个CUDA设备上的并行处理任务数量。

export NCCL_IB_DISABLE=1

禁用NCCL(NVIDIA Collective Communications Library)的InfiniBand支持。

export NCCL_P2P_DISABLE=1

禁用NCCL的点对点(Peer-to-Peer)通信。

执行命令:

bash finetune/finetune_lora_ds.sh

1

3.模型合并:

脚本路径(官方未直接提供脚本文件,需通过链接下载获取):

Qwen-main/merger_model.py

1

参数说明:

path_to_adapter:模型输出路径或其中某个checkpoint路径

new_model_directory:合并结果路径

1

2

文件迁移:

合并后会有一些文件未在合并后的目录中,去源模型文件中将文件复制到合并后的目录

以Qwen-7B为例:

cache_autogptq_cuda_256.cpp

cache_autogptq_cuda_kernel_256.cu

qwen.tiktoken

tokenization_qwen.py

tokenizer_config.json

4.模型推理:

脚本路径:

Qwen-main/web_demo.py

python finetune.py \

  --model_name_or_path E:\02\qwen\Qwen-1_8B-Chat \

  --data_path E:\02\qwen\Qwen-main\train_data_law.json \

  --bf16 True \

  --output_dir output \

  --num_train_epochs 5 \

  --per_device_train_batch_size 2 \

  --per_device_eval_batch_size 1 \

  --gradient_accumulation_steps 8 \

  --evaluation_strategy "no" \

  --save_strategy "steps" \

  --save_steps 1000 \

  --save_total_limit 10 \

  --learning_rate 3e-4 \

  --weight_decay 0.1 \

  --adam_beta2 0.95 \

  --warmup_ratio 0.01 \

  --lr_scheduler_type "cosine" \

  --logging_steps 1 \

  --report_to "none" \

  --model_max_length 512 \

  --lazy_preprocess True \

  --gradient_checkpointing \

  --use_lora

你可以使用Python来处理操作手册并将其转换为所需的格式。以下是一个简单的示例代码:

def process_manual(manual):

    conversations = []

    steps = manual.split("\n\n")  # 假设操作手册中的步骤是通过空行分隔的

    for step in steps:

        user_input = step.strip()  # 假设每个步骤中只包含用户输入,没有助手的回应

        if user_input:

            conversations.append({"from": "user", "value": user_input})

    return conversations

# 示例操作手册

manual = """

Step 1: Connect the power cable to the device.

Step 2: Press the power button to turn on the device.

Step 3: Wait for the device to boot up.

"""

formatted_conversations = process_manual(manual)

for conversation in formatted_conversations:

    print(conversation)

这段代码将操作手册分割成步骤,并将每个步骤转换为一个包含用户输入的对话对象。你可以根据实际情况修改和扩展这个代码,以满足你的需求。

  • 24
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShengFangZaiXiaMo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值