QWEN 2.5模型结构解析与代码解读

阿里开源了一系列很好的大模型,其中QWEN 2.5系列的大模型性能很好,DeepSeek也因为QWEN的优良性能,而选择了该系列模型进行知识蒸馏。

我对Qwen模型的源码也进行了研究,以进一步了解大模型发展的最新技术。从架构上,Qwen2.5也是采用了基于Transformer的Decoder Only的架构,引入了GQA分组查询,SwiGLU激活,RoPE旋转位置编码,QKV偏置以及RMSNorm正则化等技术。

以Qwen 2.5 0.5B参数的模型为例,以下代码可以打印该模型的架构:

from transformers import AutoModel, AutoConfig


model_path = '../models/Qwen2.5-0.5B'
# 从本地加载(需确保存在 config.json 和 model.safetensors)
config = AutoConfig.from_pretrained(model_path)
model = AutoModel.from_pretrained(
    model_path, 
    config=config,
    use_safetensors=True  # 强制使用 safetensors 格式
)

print(model)

模型结构如下:

Qwen2Model(
  (embed_tokens): Embedding(151936, 896)
  (layers): ModuleList(
    (0-23): 24 x Qwen2DecoderLayer(
      (self_attn): Qwen2SdpaAttention(
        (q_proj): Linear(in_features=896, out_features=896, bias=True)
        (k_proj): Linear(in_features=896, out_features=128, bias=True)
        (v_proj): Linear(in_features=896, out_features=128, bias=True)
        (o_proj): Linear(in_features=896, out_features=896, bias=False)
        (rotary_emb): Qwen2RotaryEmbedding()
      )
      (mlp): Qwen2MLP(
        (gate_proj): Linear(in_features=896, out_features=4864, bias=False)
        (up_proj): Linear(in_features=896, out_features=4864, bias=False)
        (down_proj): Linear(in_features=4864, out_features=896, bias=False)
        (act_fn): SiLU()
      )
      (input_layernorm): Qwen2RMSNorm((896,), eps=1e-06)
      (post_attention_layernorm): Qwen2RMSNorm((896,), eps=1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gzroy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值