LLM(5) | Encoder 和 Decoder 架构

LLM(5) | Encoder 和 Decoder 架构

0. 目的

LLM 模型都是 transformer 结构的, 先前已经粗略翻阅了提出 transformer 模型的论文 “Attention Is All You Need”, 了解到了 transformer 结构是第一个完全基于 attention 的模型。

而在一些资料中, 看到有人对 LLM 进行分类, 分成 encoder-only, decoder-only, encode-decode 三类, 感觉很晕, 有必要了解下什么是 encoder 和 decoder。

本文主要是对 Understanding Encoder And Decoder LLMs 的翻译。

老规矩, 中文翻译后的括号里, 是个人粗浅的笔记和想法。

1. 概要

Several people asked me to dive a bit deeper into large language model (LLM) jargon and explain some of the more technical terms we nowadays take for granted. This includes references to “encoder-style” and “decoder-style” LLMs. What do these terms mean?

我被人问了好几次, 让我更深入的说说 LLM 术语, 并解释我们现在认为理所当然的一些更技术性的术语。 这包括对 encode-style 和 decoder-style 的 LLM。 这些术语是什么意思?
( LLM 火起来后, 经常发现一些缩写,术语, 让不了解它的人很晕。 有些老铁让作者讲讲。 作者 sebastianraschka 以前是 University of Wisconsin-Madison 的 Assistant Professor, 后来全职加入 lighting ai。)

To explain the difference between encoder- and decoder-style LLMs, I wanted to share an excerpt from my new book, Machine Learning Q and AI, that I completed last week.

为了解释 encoder-style 和 decoder-style LLM 的区别, 我想分享一段我上周完成的新书 “Machine Learning Q and AI” 的摘录。
(作者写了一本书, 看来在讲授 AI 方面有经验.)

This book is aimed at people who are already familiar with machine learning and deep learning (“AI”) and are interested in diving into more advanced topics. There are 30 chapters in total, covering various topics, including

这本书是针对那些已经熟悉机器学习和深度学习(AI), 并对深入更高级话题感兴趣的人, 总共有30章, 涵盖了各种主题,包括:

  • 多GPU训练范式的解释 (Explanations of multi-GPU training paradigms)
  • 微调 transformer (Finetuning transformers)
  • encoder 和 decoder 风格的 LLM 之前的区别 (Differences between encoder- and decoder-style LLMs)
  • 更多其他主题 (And many more!)

(看了下电子书需要购买,20+美元)

2. encoder 和 decoder 风格的 transformer (Encoder- And Decoder-Style Transformers)

Fundamentally, both encoder- and decoder-style architectures use the same self-attention layers to encode word tokens. However, the main difference is that encoders are designed to learn embeddings that can be used for various predictive modeling tasks such as classification. In contrast, decoders are designed to generate new texts, for example, answering user queries.

基本上, endoder- 和 decoder- 风格的架构, 都使用相同的 self-attention 层来编码单词标记 (word tokens). 然而, 主要区别在于 encoder 的设计,是为了学习可以用于各种预测建模任务的嵌入。 相反, decoder 的设计初衷是生成新的文本, 比如回答用户的查询。
(encoder 是为了学习一个 embedding, 这个 embedding 能用于预测性的任务比如分类; decoder 是为了生成新的文本, 比如回答问题.)

原始的 transformer (The original transformer)

The original transformer architecture (Attention Is All You Need, 2017), which was developed for English-to-French and English-to-German language translation, utilized both an encoder and a decoder, as illustrated in the figure below.

原始的 transformer 架构是在2017年的论文 “Attention Is All You Need” 里提出的, 左图是 encoder, 右图是 decoder:
在这里插入图片描述

In the figure above, the input text (that is, the sentences of the text that is to be translated) is first tokenized into individual word tokens, which are then encoded via an embedding layer before it enters the encoder part.

在上图中, 输入文本(即要翻译的文本的句子)首先被分词成单个词元(token), 然后通过 embedding 层进行编码, 然后进入编码器部分。

Then, after adding a positional encoding vector to each embe

### BLIP-2 结合不同结构大模型的方式 BLIP-2 是一种多模态预训练框架,它通过引入 Q-Former 将视觉特征与语言模型连接起来[^3]。具体来说: #### 1. **结合 Decoder-only 大模型** 当使用 decoder-only 的大语言模型 (LLM) 时,BLIP-2 主要依靠 Q-Former 提取并传递图像特征给该模型。由于 decoder-only 架构本身是一个单向的语言生成器,因此它的设计更接近传统语言模型的工作模式[^1]。 在这种情况下,Q-Formers 负责将从冻结的图像编码器(Image Encoder)中提取出来的视觉特征转换为适合输入到 LLM 的形式。这些经过处理后的查询向量作为上下文嵌入被附加至提示词序列前部,从而引导后续文本生成过程。 ```python class BLIP2_DecoderOnly: def __init__(self, q_former, llm_decoder): self.q_former = q_former self.llm_decoder = llm_decoder def forward(self, img_features, prompt_tokens): queries = self.q_former(img_features) # Extract visual features via Q-former combined_input = torch.cat([queries, prompt_tokens], dim=1) output = self.llm_decoder(combined_input) # Generate text based on the combined input return output ``` --- #### 2. **结合 Encoder-Decoder模型** 对于 encoder-decoder 类型的大模型,则需要分别利用其内部两部分功能:encoder 部分用于理解来自 Q-Former 的信息;而 decoder 则继续完成基于此条件下的自然语言生成任务[^2]。 此时,image transformer text transformer 同样会参与到整个流程当中,其中前者负责同固定的图像编码模块互动以获取必要的表征数据,后者则兼具了常规意义上的文本编码解码职责。 ```python class BLIP2_EncoderDecoder: def __init__(self, q_former, enc_dec_model): self.q_former = q_former self.enc_dec_model = enc_dec_model def encode(self, img_features): queries = self.q_former(img_features) # Convert image features into query vectors encoded_output = self.enc_dec_model.encoder(queries) return encoded_output def decode(self, encoded_state, target_seq): decoded_output = self.enc_dec_model.decoder(target_seq, memory=encoded_state) return decoded_output ``` --- ### 训练方法概述 为了使上述架构能够有效运作,在实际操作过程中还需要注意以下几个方面的调整优化策略: - 数据增强技术的应用可以提升系统的泛化能力; - 参数微调阶段应特别关注跨模态间对齐质量改进措施的选择; - 并行计算资源分配合理规划有助于加速整体收敛速度。 此外值得注意的是,尽管当前主流趋势倾向于采用大规模无监督预训练加下游领域特定应用场景下进一步精调相结合的方式来构建此类复杂系统,但对于某些特殊需求场景而言也可能存在其他替代方案值得探索尝试。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值