Qwen3.5 Dense Model Architecture
本文以 transformers==5.14.1 中的 Qwen3_5TextModel 和 Qwen3_5ForCausalLM 实现为依据,讨论文本主干,不展开视觉编码器和多模态输入。源码参考:modular_qwen3_5.py、cache_utils.py。 总览 Qwen3.5 是由线性注意力层和标准全注意力层交替组成的 hybrid decoder-only Transformer。 文本主干的计算顺序是:Embedding → 多个 Qwen3_5DecoderLayer → 最终 Qwen3_5RMSNorm → lm_head。每个 decoder layer 都包含一个 token mixer 和一个 MLP;token mixer 的具体类型由 config.layer_types[layer_idx] 决定。 在默认配置中,full_attention_interval=4,因此每第 4 个 layer 使用 full_attention,其余 layer 使用 linear_attention。 配置定义 Qwen3_5TextConfig 同时定义模型宽度、层数、两类 attention 的维度和层类型。 参数 含义 影响的模块 hidden_size token 表示维度 H embedding、attention 输出、MLP 输入输出 intermediate_size MLP 中间维度 I gate_proj、up_proj、down_proj num_hidden_layers decoder layer 数量 layers num_attention_heads 全注意力的 query head 数 Nq Qwen3_5Attention num_key_value_heads 全注意力的 key/value head 数 Nkv GQA 和 KV cache head_dim 全注意力每个 head 的维度 Q/K/V 和 RoPE linear_key_head_dim 线性 attention 的 key/query head 维度 Dk Gated DeltaNet linear_value_head_dim 线性 attention 的 value head 维度 Dv Gated DeltaNet linear_num_key_heads 线性 attention 的初始 key/query head 数 Gated DeltaNet linear_num_value_heads 线性 attention 的 value head 数 recurrent state linear_conv_kernel_dim 线性 attention 的 causal convolution kernel 大小 conv1d 和 convolution cache layer_types 每层的 linear_attention 或 full_attention decoder layer 分支 layer_types 未提供时,配置代码按 full_attention_interval 生成: ...