惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
腾讯CDC
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threatpost
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
B
Blog RSS Feed
Forbes - Security
Forbes - Security
P
Privacy & Cybersecurity Law Blog
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
T
Tenable Blog
MyScale Blog
MyScale Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
W
WeLiveSecurity
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
罗磊的独立博客
The Last Watchdog
The Last Watchdog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
美团技术团队
Microsoft Security Blog
Microsoft Security Blog

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
使用 LLaMA-Factory 微调 Qwen3 模型
微信公众号 · 2025-09-21 · via 陈少文的网站

Please enable Javascript to view the contents

1. 原始模型测试

  • 启动环境
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
nerdctl run -it \
        --gpus all \
        --ipc=host \
        -p 8000:8000 \
        --ulimit memlock=-1 \
        --ulimit stack=67108864 \
        --name vllm \
        --volume /data/models:/data/models \
        --entrypoint /bin/bash \
        vllm/vllm-openai:v0.10.1.1

后面 vllm 相关的测试,都基于这个环境。

  • 启动服务
1
2
3
4
5
6
export CUDA_VISIBLE_DEVICES=0
python3 -m vllm.entrypoints.openai.api_server \
  --model /data/models/Qwen3-0.6B \
  --served-model-name /data/models/Qwen3-0.6B \
  --host 0.0.0.0 \
  --port 8000
  • 测试
1
2
3
4
5
6
7
8
curl -X POST "http://127.0.0.1:8000/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "/data/models/Qwen3-0.6B",
    "messages": [
      {"role": "user", "content": "介绍一下你自己/no_think"}
    ]
  }'
 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
{
  "id": "chatcmpl-e831f557a8384a9b833e8033f7ac8ffa",
  "object": "chat.completion",
  "created": 1758524426,
  "model": "/data/models/Qwen3-0.6B",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "<think>\n\n</think>\n\n您好!我是小明,来自中国。很高兴和您交流。如果您有任何问题或需要帮助,请随时告诉我!",
        "refusal": null,
        "annotations": null,
        "audio": null,
        "function_call": null,
        "tool_calls": [],
        "reasoning_content": null
      },
      "logprobs": null,
      "finish_reason": "stop",
      "stop_reason": null
    }
  ],
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "prompt_tokens": 13,
    "total_tokens": 42,
    "completion_tokens": 29,
    "prompt_tokens_details": null
  },
  "prompt_logprobs": null,
  "kv_transfer_params": null
}

2. 数据格式

常见的微调数据格式有 alpaca 和 sharegpt 两种。

2.1 alpaca

适合单轮问答

  • 单轮问答
1
2
3
4
5
{
  "instruction": "介绍一下你自己",
  "input": "",
  "output": "我是 Ops Copilot,一个面向运维领域的智能助手,可以帮助你解决各种运维相关的问题。"
}
  • 多轮对话
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[
  {
    "instruction": "你能帮我监控 Kubernetes 吗?",
    "input": "",
    "output": "当然可以,我可以通过 Prometheus 采集 Kubernetes 的监控指标,并提供告警与可视化。",
    "system": "你是 Ops Copilot,一个面向运维领域的智能助手。",
    "history": [
      [
        "你好",
        "你好,我是 Ops Copilot,一个面向运维领域的智能助手,可以帮助你解决各种运维相关的问题。"
      ]
    ]
  }
]

2.2 sharegpt

适合多轮对话

  • 基本结构
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "conversations": [
    {
      "from": "system",
      "value": "你是一个专业的Python编程助手,请提供清晰、准确的代码示例。"
    },
    {
      "from": "human",
      "value": "如何用Python读取CSV文件?"
    },
    {
      "from": "gpt",
      "value": "可以使用pandas库来读取CSV文件:\n\n```python\nimport pandas as pd\ndf = pd.read_csv('file.csv')\nprint(df.head())\n```\n\n这样就能轻松加载和查看CSV数据了。"
    }
  ]
}

3. 微调模型

  • 启动环境
1
2
3
4
5
6
7
8
9
nerdctl run -it \
        --gpus all \
        --ipc=host \
        --ulimit memlock=-1 \
        --ulimit stack=67108864 \
        --name llamafactory \
        --volume /data/models:/data/models \
        --entrypoint /bin/bash \
        hiyouga/llamafactory:0.9.4

后面 llamafactory 相关的测试,都基于这个环境。

  • 准备数据
1
2
3
4
5
6
7
echo '[
  {
    "instruction": "介绍一下你自己",
    "input": "",
    "output": "我是 Ops Copilot,一个面向运维领域的智能助手,可以帮助你解决各种运维相关的问题。"
  }
]' > /data/models/dataset/alpaca_test.json
  • 注册数据
1
2
3
4
5
echo '{
  "alpaca_test.json": {
    "file_name": "alpaca_test.json"
  }
}' > /data/models/dataset/dataset_info.json
  • 运行微调
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
export CUDA_VISIBLE_DEVICES=0
llamafactory-cli train \
    --do_train \
    --stage sft \
    --model_name_or_path /data/models/Qwen3-0.6B \
    --dataset alpaca_test.json \
    --dataset_dir /data/models/dataset \
    --template qwen3 \
    --finetuning_type lora \
    --output_dir /data/models/Qwen3-0.6B-lora-sft \
    --per_device_train_batch_size 1 \
    --max_steps 20 \
    --learning_rate 1e-4 \
    --logging_steps 1 \
    --save_steps 10 \
    --save_total_limit 1 \
    --overwrite_output_dir \
    --warmup_ratio 0
1
2
3
4
5
6
7
***** train metrics *****
  epoch                    =       20.0
  total_flos               =     1991GF
  train_loss               =     1.0216
  train_runtime            = 0:00:05.97
  train_samples_per_second =      3.345
  train_steps_per_second   =      3.345
  • 查看 lora 权重
1
2
3
4
5
6
ls /data/models/Qwen3-0.6B-lora-sft

README.md                  all_results.json     special_tokens_map.json  trainer_log.jsonl
adapter_config.json        chat_template.jinja  tokenizer.json           trainer_state.json
adapter_model.safetensors  checkpoint-1         tokenizer_config.json    training_args.bin
added_tokens.json          merges.txt           train_results.json       vocab.json
  • 合并权重
1
2
3
4
5
6
7
8
export CUDA_VISIBLE_DEVICES=0
llamafactory-cli export \
    --model_name_or_path /data/models/Qwen3-0.6B \
    --adapter_name_or_path /data/models/Qwen3-0.6B-lora-sft \
    --export_dir /data/models/Qwen3-0.6B-lora-merged \
    --template qwen \
    --export_size 2 \
    --finetuning_type lora
  • 查看合并后的模型
1
2
3
4
5
ls /data/models/Qwen3-0.6B-lora-merged/

Modelfile            config.json             model.safetensors        tokenizer_config.json
added_tokens.json    generation_config.json  special_tokens_map.json  vocab.json
chat_template.jinja  merges.txt              tokenizer.json

微调前后,模型大小没有变化。

4. 测试微调后的模型

4.1 合并 lora 模型

  • 启动服务
1
2
3
4
5
6
export CUDA_VISIBLE_DEVICES=0
python3 -m vllm.entrypoints.openai.api_server \
  --model /data/models/Qwen3-0.6B-lora-merged \
  --served-model-name /data/models/Qwen3-0.6B \
  --host 0.0.0.0 \
  --port 8000

合并之后的模型,具有更好的推理性能,也更容易管理和部署。

  • 测试
1
2
3
4
5
6
7
8
curl -X POST "http://127.0.0.1:8000/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "/data/models/Qwen3-0.6B",
    "messages": [
      {"role": "user", "content": "介绍一下你自己/no_think"}
    ]
  }'
 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
{
  "id": "chatcmpl-9da628e2e9ed466e82b2c5ff2ea295ad",
  "object": "chat.completion",
  "created": 1758596914,
  "model": "/data/models/Qwen3-0.6B",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "<think>\n\n</think>\n\n我是 Ops Copilot,一个面向运维领域的智能助手,可以帮助你解决各种运维相关的问题。",
        "refusal": null,
        "annotations": null,
        "audio": null,
        "function_call": null,
        "tool_calls": [],
        "reasoning_content": null
      },
      "logprobs": null,
      "finish_reason": "stop",
      "stop_reason": null
    }
  ],
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "prompt_tokens": 13,
    "total_tokens": 38,
    "completion_tokens": 25,
    "prompt_tokens_details": null
  },
  "prompt_logprobs": null,
  "kv_transfer_params": null
}

看到模型输出的自我介绍是 Ops Copilot,说明我们构造的数据已经训练到模型中了。

4.2 单独加载 lora 模型

  • 启动服务
1
2
3
4
5
6
7
8
export CUDA_VISIBLE_DEVICES=0
python3 -m vllm.entrypoints.openai.api_server \
  --model /data/models/Qwen3-0.6B \
  --served-model-name /data/models/Qwen3-0.6B \
  --enable-lora \
  --lora-modules ops-lora=/data/models/Qwen3-0.6B-lora-sft \
  --host 0.0.0.0 \
  --port 8000

单独加载 lora 模型更灵活,也更于分享模型,还可以同时加载多个 lora 模型。

  • 测试

需要注意的是,这里的 model 名称是 lora 模块的名称 ops-lora

1
2
3
4
5
6
7
8
curl -X POST "http://127.0.0.1:8000/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "ops-lora",
    "messages": [
      {"role": "user", "content": "介绍一下你自己/no_think"}
    ]
  }'
1
{"id":"chatcmpl-8ad998bdcbc3447892ba0228d7b5f55d","object":"chat.completion","created":1758597178,"model":"ops-lora","choices":[{"index":0,"message":{"role":"assistant","content":"<think>\n\n</think>\n\n我是 Ops Copilot,一个面向运维领域的智能助手,可以帮助你解决各种运维相关的问题。","refusal":null,"annotations":null,"audio":null,"function_call":null,"tool_calls":[],"reasoning_content":null},"logprobs":null,"finish_reason":"stop","stop_reason":null}],"service_tier":null,"system_fingerprint":null,"usage":{"prompt_tokens":13,"total_tokens":38,"completion_tokens":25,"prompt_tokens_details":null},"prompt_logprobs":null,"kv_transfer_params":null}

合并 lora 模型与单独加载 lora 模型,输出结果一致。

5. 总结

本篇主要是记录 LLaMAFactory 微调 Qwen3 模型的过程,并测试了微调后的模型。其中很多参数并没有深入研究,只是尝试走一遍微调流程,为 Ops Copilot 模型的微调做准备。

目前的大模型相关的工具链越来越成熟,相关的操作变得越来越简单。LLaMAFactory 还提供了 Web 界面操作,可以很方便地进行微调。


微信公众号