Decoder-only、BERT、Encoder-Decoder Transformer 的 Mermaid 图。
1. 推理/生成过程时序图(3 个)
1.1 Decoder-only(GPT 类)推理/生成时序图
sequenceDiagram
autonumber
participant U as 用户
participant C as 客户端
participant S as 服务端
participant T as 分词器
participant M as Decoder-only 模型
participant D as 采样/解码器
U->>C: 输入提示词
C->>S: 生成请求
S->>T: 文本转 token IDs
T-->>S: token IDs
S->>M: 输入 token IDs
loop 自回归生成(直到 EOS 或最大长度)
M->>M: Masked Self-Attention + FFN
M-->>S: 输出 logits
S->>D: 对最后位置 logits 采样
D-->>S: 返回新 token ID
S->>S: 追加 token 并更新 KV Cache
end
S->>T: token IDs 转文本
T-->>S: 生成文本
S-->>C: 返回回复
C-->>U: 展示回复1.2 BERT(Encoder-only)推理时序图
BERT 通常用于理解任务,不是生成式模型。下面展示分类/序列标注等下游任务的推理流程。
sequenceDiagram
autonumber
participant U as 用户
participant C as 客户端
participant S as 服务端
participant T as 分词器
participant B as BERT 编码器
participant H as 任务头
U->>C: 输入文本(可含句子对)
C->>S: 推理请求
S->>T: 文本转 token IDs + attention mask
T-->>S: input_ids, token_type_ids, attention_mask
S->>B: 输入编码
B->>B: 多层 Self-Attention + FFN
B-->>S: 输出 hidden states(所有 token)
S->>H: 取 [CLS] 或序列表示
H-->>S: 输出类别/标签/分数
S-->>C: 返回推理结果
C-->>U: 展示结果1.3 Encoder-Decoder Transformer(T5/BART 类)推理/生成时序图
sequenceDiagram
autonumber
participant U as 用户
participant C as 客户端
participant S as 服务端
participant T as 分词器
participant E as 编码器
participant D as 解码器
participant SAMP as 采样器
U->>C: 输入源文本
C->>S: 生成请求
S->>T: 源文本转 token IDs
T-->>S: encoder input IDs
S->>E: 输入编码器
E->>E: 多层 Self-Attention + FFN
E-->>S: 编码表示(encoder hidden states)
S->>D: 初始 decoder input(如 )
loop 自回归生成(直到 )
D->>D: Masked Self-Attn + Cross-Attn(enc) + FFN
D-->>S: 输出 logits
S->>SAMP: 对最后位置 logits 采样
SAMP-->>S: 新 token ID
S->>S: 追加 token 并更新缓存
end
S->>T: token IDs 转文本
T-->>S: 生成文本
S-->>C: 返回回复
C-->>U: 展示回复 2. Decoder-only 与 BERT 预训练流程时序图
2.1 Decoder-only 预训练流程时序图(语言建模任务)
sequenceDiagram
autonumber
participant Data as 训练数据
participant T as 分词器
participant M as Decoder-only 模型
participant L as 损失函数
participant O as 优化器
Data->>T: 原始文本
T->>T: 分词并构建自回归输入/目标
Note over T: input = tokens[:-1], target = tokens[1:]
T-->>M: input_ids, labels
M->>M: 前向传播(Masked Self-Attn)
M-->>L: 输出 logits
L->>L: 计算交叉熵损失(预测下一个 token)
L-->>O: 损失值
O->>M: 反向传播更新参数
Note over L: 重复多轮,遍历整个语料2.2 BERT 预训练流程时序图(MLM + NSP)
sequenceDiagram
autonumber
participant Data as 训练语料(句子对)
participant T as 分词器
participant B as BERT 编码器
participant MLM as MLM 头
participant NSP as NSP 头
participant L as 联合损失
participant O as 优化器
Data->>T: 采样句子对(50% 连续/50% 随机)
T->>T: 添加 [CLS], [SEP],随机 mask 15% tokens
T-->>B: input_ids, token_type_ids, attention_mask
B->>B: 多层 Self-Attention 编码
B-->>MLM: 被 mask 位置的 hidden states
B-->>NSP: [CLS] 表示
MLM->>L: 预测被 mask 的 token(交叉熵)
NSP->>L: 预测是否下一句(二分类)
L->>L: 计算总损失 = MLM_loss + NSP_loss
L-->>O: 损失值
O->>B: 反向传播更新参数
Note over MLM: 重复多轮直至收敛3. 架构数据流图(3 个)
3.1 Decoder-only 架构数据流图
flowchart TD
A[输入 token IDs] --> B[Token Embedding]
A2[位置信息] --> B2[Positional Embedding]
B --> C[加法融合]
B2 --> C
C --> D[Dropout]
D --> E[Transformer Decoder Block 1]
E --> E2[Masked Self-Attention]
E2 --> E3[Add & LayerNorm]
E3 --> E4[Feed-Forward Network]
E4 --> E5[Add & LayerNorm]
E5 --> F[Transformer Decoder Block 2]
F --> G[... 重复 N 层 ...]
G --> H[Final LayerNorm]
H --> I[Linear 投影到词表]
I --> J[Softmax]
J --> K[输出 token 概率分布]3.2 BERT 架构数据流图
flowchart TD
A[输入 token IDs] --> B[Token Embedding]
A2[Segment 信息] --> B2[Segment Embedding]
A3[位置信息] --> B3[Positional Embedding]
B --> C[加法融合]
B2 --> C
B3 --> C
C --> D[LayerNorm + Dropout]
D --> E[Transformer Encoder Block 1]
E --> E2[Self-Attention]
E2 --> E3[Add & LayerNorm]
E3 --> E4[Feed-Forward Network]
E4 --> E5[Add & LayerNorm]
E5 --> F[Transformer Encoder Block 2]
F --> G[... 重复 N 层 ...]
G --> H[Final Hidden States]
H --> I[CLS 表示]
H --> J[Token 表示]
I --> K[分类头 / NSP 头]
J --> L[序列标注头 / QA 头]3.3 Encoder-Decoder Transformer 架构数据流图
flowchart TD
subgraph Encoder
A[源输入 token IDs] --> B[Embedding + Positional Encoding]
B --> C[Encoder Block 1]
C --> C2[Self-Attention]
C2 --> C3[Add & LayerNorm]
C3 --> C4[FFN]
C4 --> C5[Add & LayerNorm]
C5 --> D[Encoder Block 2 ... N]
D --> E[Encoder Hidden States]
end
subgraph Decoder
F[目标输入 token IDs
shifted right] --> G[Embedding + Positional Encoding]
G --> H[Decoder Block 1]
H --> H2[Masked Self-Attention]
H2 --> H3[Add & LayerNorm]
H3 --> H4[Cross-Attention
Key/Value from Encoder]
H4 --> H5[Add & LayerNorm]
H5 --> H6[FFN]
H6 --> H7[Add & LayerNorm]
H7 --> I[Decoder Block 2 ... N]
I --> J[Final LayerNorm]
J --> K[Linear 投影到词表]
K --> L[Softmax]
L --> M[输出 token 概率分布]
end
E --> H4以上共 8 个 Mermaid 图,覆盖了三种主流 Transformer 变体的推理、预训练流程和架构数据流。你可以直接把代码块复制到支持 Mermaid 的 Markdown 编辑器中渲染。如需调整细节或增加其他变体(如 Encoder-Decoder 预训练流程),可以继续告诉我。
评论