微调LLM前你需要了解的一些概念-- 基于 Qwen3 配置文件的实践
·
本文基于如下 Qwen3 配置文件进行讲解:
{
"architectures": [
"Qwen3ForSequenceClassification"
],
"attention_bias": false,
"attention_dropout": 0.0,
"bos_token_id": 151643,
"dtype": "bfloat16",
"eos_token_id": 151645,
"head_dim": 128,
"hidden_act": "silu",
"hidden_size": 1024,
"id2label": {
"2": 68,
"3": 99,
"5": 158,
"8": 239,
xxx
},
"initializer_range": 0.02,
"intermediate_size": 3072,
"label2id": {
"0": "10197",
"1": "102",
"10": "1168",
xxx
},
"layer_types": [
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention",
"full_attention"
],
"max_position_embeddings": 40960,
"max_window_layers": 28,
"model_type": "qwen3",
"num_attention_heads": 16,
"num_hidden_layers": 28,
"num_key_value_heads": 8,
"pad_token_id": 151643,
"rms_norm_eps": 1e-06,
"rope_parameters": {
"rope_theta": 1000000,
"rope_type": "default"
},
"sliding_window": null,
"tie_word_embeddings": true,
"transformers_version": "5.3.0",
"use_cache": true,
"use_sliding_window": false,
"vocab_size": 151669
}
目标是把前面学习过的 Transformer、QKV、多头注意力、MLP、输出层、分类头等概念,和一个真实模型配置对应起来,做到“知道概念,也看得懂工程配置”。
1. 这个配置文件描述的是什么模型
配置里最关键的一项是:
"architectures": [
"Qwen3ForSequenceClassification"
]
这说明它不是普通的文本生成模型,而是一个序列分类模型:
Qwen3 Transformer 主干
+ Sequence Classification 分类头
结合目录名:
oncall_tenant_predict
可以推断它的任务大概率是:
输入一段 oncall / 告警 / 问题描述文本,预测它应该归属到哪个 tenant、团队或业务标签。
所以它和普通生成式 LLM 的区别在最后一层。
普通生成模型:
输入文本
→ Transformer
→ 词表输出层 W_out
→ vocab logits
→ softmax
→ 下一个 token
这个序列分类模型:
输入文本
→ Transformer
→ 分类头
→ class logits
→ softmax
→ tenant 类别
也就是说:
Transformer 主干认知是一样的,但最后任务头不同。
2. 配置文件里的关键参数
从配置中抽出和模型结构最相关的字段:
{
"model_type": "qwen3",
"hidden_size": 1024,
"num_hidden_layers": 28,
"num_attention_heads": 16,
"num_key_value_heads": 8,
"head_dim": 128,
"intermediate_size": 3072,
"hidden_act": "silu",
"vocab_size": 151669,
"max_position_embeddings": 40960,
"rms_norm_eps": 1e-6,
"attention_dropout": 0.0,
"dtype": "bfloat16",
"tie_word_embeddings": true,
"use_cache": true
}
对应成人话:
| 配置字段 | 含义 |
|---|---|
model_type = qwen3 |
使用 Qwen3 架构 |
hidden_size = 1024 |
每个 token 的主干 hidden state 是 1024 维 |
num_hidden_layers = 28 |
有 28 层 Transformer Block |
num_attention_heads = 16 |
每层有 16 个 Query attention heads |
num_key_value_heads = 8 |
每层有 8 个 Key/Value heads,说明使用 GQA |
head_dim = 128 |
每个 attention head 的维度是 128 |
intermediate_size = 3072 |
MLP 中间层升维到 3072 |
hidden_act = silu |
MLP 使用 SiLU 激活函数 |
vocab_size = 151669 |
tokenizer 词表大小 |
max_position_embeddings = 40960 |
最大上下文长度约 40960 token |
rms_norm_eps = 1e-6 |
RMSNorm 的数值稳定参数 |
dtype = bfloat16 |
使用 bf16 数值格式 |
use_cache = true |
推理时可使用 KV Cache |
整体结构可以概括为:
Token IDs
→ Embedding
→ 28 层 Qwen3 Transformer Blocks
→ 序列级 hidden state
→ 分类头
→ tenant logits
→ softmax
→ tenant 预测结果
图示如下:
oncall 文本
Tokenizer
Token IDs
Embedding: vocab_size x hidden_size
28 层 Transformer Blocks
序列级 hidden state
Classification Head
268 个类别 logits
softmax
tenant / 业务标签
3. 28 层 Transformer Block 对应什么
配置:
"num_hidden_layers": 28更多推荐




所有评论(0)