大语言模型的终极之路
  • 大语言模型的终极之路
  • 更新计划
  • 大语言模型时代的NLP
    • 任务与评测
    • 参考资料
  • 基础知识
    • 负对数似然
    • Transformer
      • Cross Attention
      • 向量流动视角
      • Layer Normalization
      • Attention Block
    • 优化算法
      • 牛顿法
      • 梯度下降
  • 大语言模型
    • 大模型理论
      • Scaling Law
      • The Bitter Lesson
      • 思考,快与慢
    • 模型结构
      • MLP
      • Rotary Embedding
      • RMSNorm
      • Encoder-decoder
      • Decoder-only
      • MOE
      • 常见大模型
        • T5
        • GPT2
        • LLaMA
        • LLaMA 2
        • Mistral
        • GLM
        • Mixture
    • 如何训练一个ChatGPT
    • 微调
      • Instruction Tuning 指令微调
      • Domain Finetune 领域微调
    • 解码
      • 温度采样
      • Beam Search Decoding
  • Prompt 工程
    • Prompt, 一种技术路线
    • Prompt 写作规范
    • In-Context Learning
    • Chain-of-Thought
    • Generate Rather than Read
    • Program-of-Thought
    • Tree-of-Thought
    • 参考资料
  • 知识与幻觉
    • 知识边界
  • 大规模预训练
    • 计算资源消耗
    • Deepspeed
    • Megatron
    • 大规模数据处理
    • CUDA 算子优化
  • 强化学习
    • RLHF
      • RLHF
  • 大模型轻量化
    • 蒸馏
      • 黑盒蒸馏
      • 白盒蒸馏
        • KL 散度
    • 轻量化微调
      • LoRA
    • 量化
    • 剪枝
    • 推理加速
    • 参考资料
  • RAG-大模型检索
    • Page 3
  • 多智能体
    • Page 6
  • 多模态大模型
    • Page 1
  • 大模型安全与鲁棒
由 GitBook 提供支持
在本页
  1. 基础知识

Transformer

上一页负对数似然下一页Cross Attention

最后更新于11个月前

如何计算transformer的参数量

Transformer模型的参数量可以通过计算其各个组成部分的参数来求得。Transformer模型主要由以下几部分组成:输入嵌入层(embedding layer)、自注意力层(self-attention layer)、前馈网络层(feed-forward network layer)、层规范化(layer normalization)和输出全连接层(output fully connected layer)。下面是计算每一部分参数量的方法:

1. 输入嵌入层(Embedding Layer)

假设词汇表的大小为 ( V ),嵌入的维度为 ( d ),那么输入嵌入层的参数量即为词汇表中的每个单词都有一个长度为 ( d ) 的向量。因此,参数量为: Embedding Layer Parameters=V×d\text{Embedding Layer Parameters} = V \times d Embedding Layer Parameters=V×d

2. 自注意力层(Self-Attention Layer)

2.1 多头注意力机制(Multi-Head Attention)

假设有 h h h 个注意力头,每个头的维度为 ( d_k ),输入和输出的维度为 ( d )(通常 ( d ) 可以是 (h×dk h \times d_k h×dk​ ))。注意力机制的参数量主要包括:查询(query)、键(key)、值(value)线性变换的参数和输出线性变换的参数。

每个头的查询、键、值线性变换的参数量为: Parameter per head=3×(d×dk)\text{Parameter per head} = 3 \times (d \times d_k)Parameter per head=3×(d×dk​) 由于有 ( h ) 个头,总的查询、键、值线性变换的参数量为: QKV Parameters=h×3×(d×dk)\text{QKV Parameters} = h \times 3 \times (d \times d_k)QKV Parameters=h×3×(d×dk​) 输出线性变换的参数量为: Output Linear Parameters=d×d\text{Output Linear Parameters} = d \times dOutput Linear Parameters=d×d 因此,多头注意力机制的总参数量为: Multi-Head Attention Parameters=h×3×(d×dk)+(d×d)\text{Multi-Head Attention Parameters} = h \times 3 \times (d \times d_k) + (d \times d) Multi-Head Attention Parameters=h×3×(d×dk​)+(d×d)

3. 前馈网络层(Feed-Forward Network Layer)

前馈网络一般由两个全连接层组成。假设中间层的维度为 ( d_{ff} ),输入和输出的维度为 ( d )。 第一个全连接层的参数量为: First Linear Layer Parameters=d×dff\text{First Linear Layer Parameters} = d \times d_{ff} First Linear Layer Parameters=d×dff​ 第二个全连接层的参数量为: Second Linear Layer Parameters=dff×d\text{Second Linear Layer Parameters} = d_{ff} \times d Second Linear Layer Parameters=dff​×d 因此,前馈网络的总参数量为:Feed-Forward Network Parameters=d×dff+dff×d\text{Feed-Forward Network Parameters} = d \times d_{ff} + d_{ff} \times d Feed-Forward Network Parameters=d×dff​+dff​×d

4. 层规范化(Layer Normalization)

层规范化包含两个可训练的参数:缩放因子 γ \gammaγ 和偏移因子 β\betaβ,每个参数的维度为 ( d ),所以层规范化的总参数量为: Layer Normalization Parameters=2×d\text{Layer Normalization Parameters} = 2 \times dLayer Normalization Parameters=2×d

5. 输出全连接层(Output Fully Connected Layer)

通常在Transformer的最后会有一个输出全连接层,用于预测。假设输出类别数为 ( C ),那么输出全连接层的参数量为: Output Linear Layer Parameters=d×C\text{Output Linear Layer Parameters} = d \times COutput Linear Layer Parameters=d×C

汇总

假设Transformer的层数为 ( L ),那么总的参数量按以下公式汇总:

  1. Embedding Layer Parameters: V×dV \times dV×d

  2. Multi-Head Attention Parameters: L×(h×3×(d×dk)+(d×d))L \times (h \times 3 \times (d \times d_k) + (d \times d)) L×(h×3×(d×dk​)+(d×d))

  3. Feed-Forward Network Parameters: L×(d×dff+dff×d)L \times (d \times d_{ff} + d_{ff} \times d)L×(d×dff​+dff​×d)

  4. Layer Normalization Parameters: L×2×dL \times 2 \times dL×2×d

  5. Output Linear Layer Parameters: d×Cd \times Cd×C

总参数量为所有部分的参数之和: Total Parameters=V×d+L×(h×3×(d×dk)+(d×d))+L×(d×dff+dff×d)+L×2×d+d×C\text{Total Parameters} = V \times d + L \times (h \times 3 \times (d \times d_k) + (d \times d)) + L \times (d \times d_{ff} + d_{ff} \times d) + L \times 2 \times d + d \times C Total Parameters=V×d+L×(h×3×(d×dk​)+(d×d))+L×(d×dff​+dff​×d)+L×2×d+d×C

这个公式可以帮助你较精确地计算一个标准Transformer模型的参数总量。

Attention Block