知识卡片
模型组件按职责协作
结构图:类图
classDiagram
class Qwen3Model {
forward(in_idx, cache) logits
reset_kv_cache()
}
class TransformerBlock {
forward(x, mask, cos, sin, start_pos, cache) x_next_cache
}
class GroupedQueryAttention {
forward(x, mask, cos, sin, start_pos, cache) context_next_cache
}
class FeedForward {
forward(x) projected_x
}
class KVCache {
get(layer_idx) layer_cache
update(layer_idx, value)
reset()
}
Qwen3Model *-- TransformerBlock : 多层组合
TransformerBlock *-- GroupedQueryAttention : 组合
TransformerBlock *-- FeedForward : 组合
Qwen3Model ..> KVCache : 读取并更新外部状态
Qwen3Model组织嵌入、各层块和词表输出;块负责残差内的注意力及前馈协作;KVCache独立承载每层历史键值。实线菱形表示模块组合,虚线表示使用依赖,不是继承。forward返回logits,生成与选词由模型外层负责;reset_kv_cache仅重置模型位置,外部缓存有独立reset。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- · 查看原书依据
class Qwen3Model(nn.Module): def __init__(self, cfg): super().__init__() # Main model parameters self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"], dtype=cfg["dtype"]) self.trf_blocks = nn.ModuleList( [TransformerBlock(cfg) for _ in range(cfg["n_layers"])] ) self.final_norm = RMSNorm(cfg["emb_dim"]) self.out_head = nn.Linear( cfg["emb_dim"], cfg["vocab_size"], bias=False, dtype=cfg["dtype"] ) # Reusable utilities if cfg["head_dim"] is None:
参考来源
- 位置:《Build a Reasoning Model (From Scratch)》第09章《附录C Qwen3模型源码》(来源项目:a19c7933-4eda-4176-aebd-30fe7e1ad292;源文件:extracted/Build a Reasoning Model (From Scratch)/_epub-src/OEBPS/Text/appendix-c.html)
- 结论依据:摘录中关于模型组件按职责协作的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。
- 原始内容:class Qwen3Model(nn.Module): def __init__(self, cfg): super().__init__() # Main model parameters self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"], dtype=cfg["dtype"]) self.trf_blocks = nn.ModuleList( [TransformerBlock(cfg) for _ in range(cfg["n_layers"])] ) self.final_norm = RMSNorm(cfg["emb_dim"]) self.out_head = nn.Linear( cfg["emb_dim"], cfg["vocab_size"], bias=False, dtype=cfg["dtype"] ) # Reusable utilities if cfg["head_dim"] is None: