比较Causal decoder、Prefix decoder和encoder-decoder

note:不同的语言模型通过attention mask设计,mask取全1就对应双向注意力,mask取下三角矩阵就对应单向注意力,我们可以只使用transformer encoder的情况下,自定义attention mask来兼容不同模型架构(如auto agressive单向注意力、auto encoding 双向注意力、encoder-decoder 解码器用单向注意力 且用交叉注意力连接两者):
在这里插入图片描述

如下图:
蓝色:the attention between prefix tokens
绿色:the attention between prefix and target tokens
黄色:the attention betweetn target tokens and masked attention
请添加图片描述

  • 因果解码器(causal decoder,当前主流):因果解码器架构采用单向注意力掩码,以确保每个输入标记只能关注过去的标记和它本身。输入和输出标记通过解码器以相同的方式进行处理。
    • chatGPT等
    • 因果解码器:GPT,BLOOM,Gopher等。
  • 前缀解码器(prefix decoder):前缀解码器结构修正了因果编码器的掩码机制,以使其能可对前缀标记执行双向注意力,并仅对生成的标记执行单向注意力。这样,与encoder-decoder类似,可以双向编码前缀序列并自回归低逐个预测输出标记,其中在编码和解码阶段共享相同的参数。现在前缀编码器的大模型包括U-PaLM、GLM-130B等。
  • 编码器-解码器(encoder-decoder):传统 Transformer 模型是建立在编码器-解码器架构上的 ,由两个 Transformer 块分别作为编码器和解码器。编码器采用堆叠的多头自注意层对输入序列进行编码以生成其潜在表示,而解码器对这些表示进行交叉注意并自回归地生成目标序列。目前,只有少数大语言模型是基于编码器-解码器架构构建的例如 Flan-T5。

Reference

[1] 为什么现在的LLM都是Decoder only的架构.某乎

### Transformer 中 Encoder-Only Decoder-Only 架构的工作机制比较 #### 1. **Encoder-Only 架构** Transformer 的原始设计中包含了 Encoder Decoder 部分,其中 Encoder 负责编码输入序列的信息。在某些应用场景下(如 BERT),仅使用 Encoder 部分来构建模型。 - **工作机制** Encoder 是一种多层堆叠的结构,每一层由 Self-Attention 子层前馈神经网络 (Feed Forward Network) 组成[^2]。通过多次叠加这些子层,模型能够捕捉到输入序列中的复杂模式。 - 输入经过嵌入层 (Embedding Layer),将 token 映射为向量表示。 - 使用位置编码 (Positional Encoding) 来引入顺序信息,因为纯注意力机制无法感知序列的位置关系。 - 多头自注意力机制允许模型关注不同位置上的上下文信息。 - 前馈神经网络进一步处理特征提取后的数据。 - **特点** - 主要用于理解任务,例如分类、命名实体识别等。 - 并行化程度高,在训练过程中可以一次性处理整个输入序列[^3]。 ```python class EncoderLayer(nn.Module): def __init__(self, d_model, num_heads, d_ff, dropout=0.1): super(EncoderLayer, self).__init__() self.self_attention = MultiHeadAttention(d_model, num_heads) self.feed_forward = PositionWiseFeedForward(d_model, d_ff) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.dropout = nn.Dropout(dropout) def forward(self, x, mask=None): attn_output = self.self_attention(x, x, x, mask) out1 = self.norm1(x + self.dropout(attn_output)) ff_output = self.feed_forward(out1) output = self.norm2(out1 + self.dropout(ff_output)) return output ``` --- #### 2. **Decoder-Only 架构** Decoder-Only 结构去除了传统的 Encoder 部分,专注于生成任务。这种架构特别适合于自回归生成场景,例如 GPT 系列模型。 - **工作机制** 解码器同样是一个多层堆叠的结构,每层包含三个主要组件:Self-Attention 层、交叉注意力层以及前馈神经网络[^4]。然而,在 Decoder-Only 模型中,交叉注意力被移除,只剩下 Self-Attention FFN。 - 输入通常是从左至右逐步生成的目标序列的一部分。 - Masked Self-Attention 确保当前时刻的预测不会看到未来的时间步信息。 - 输出的概率分布基于 Softmax 计算得出。 - **特点** - 更加注重生成能力,适用于文本生成、对话系统等领域。 - 利用了因果掩码 (Causal Masking),使得模型能够在不泄露未来信息的情况下完成逐词生成[^1]。 ```python class DecoderLayer(nn.Module): def __init__(self, d_model, num_heads, d_ff, dropout=0.1): super(DecoderLayer, self).__init__() self.masked_self_attention = MultiHeadAttention(d_model, num_heads) self.ffn = PositionWiseFeedForward(d_model, d_ff) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.dropout = nn.Dropout(dropout) def forward(self, x, mask=None): masked_attn_output = self.masked_self_attention(x, x, x, mask) out1 = self.norm1(x + self.dropout(masked_attn_output)) ffn_output = self.ffn(out1) output = self.norm2(out1 + self.dropout(ffn_output)) return output ``` --- #### 3. **图解对比** | 特性 | Encoder-Only | Decoder-Only | |---------------------|---------------------------------------|--------------------------------------| | **核心功能** | 序列理解 | 文本生成 | | **组成模块** | Self-Attention + FFN | Masked Self-Attention + FFN | | **并行化支持** | 完全支持 | 受限于自回归特性 | | **典型应用** | 分类、NER | 对话系统、文章摘要 | ![Encoder-Only vs Decoder-Only](https://example.com/transformer_architecture_comparison.png) > 注:上述链接仅为示意,请替换为实际可用资源或自行绘制图表。 --- #### 4. **总结** Encoder-Only Decoder-Only 各有侧重,前者擅长理解分析静态输入,后者则更适配动态生成任务。两者的设计理念均源于经典的 Encoder-Decoder 框架,但在具体实现上进行了针对性优化以满足特定需求。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山顶夕景

小哥哥给我买个零食可好

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

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

打赏作者

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

抵扣说明:

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

余额充值