知识卡片
可重建缓冲区不必写入权重
RoPE的cos/sin可注册为非持久buffer:随模型设备操作移动,但不随state_dict保存。重新加载需有匹配配置以重建;权重文件本身并未完整表达旋转基数、上下文长度等计算约定。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- · 查看原书依据
self.register_buffer("cos", cos, persistent=False) self.register_buffer("sin", sin, persistent=False) self.cfg = cfg self.current_pos = 0 # Track current position in KV cache def forward(self, in_idx, cache=None): tok_embeds = self.tok_emb(in_idx) x = tok_embeds num_tokens = x.shape[1] if cache is not None: pos_start = self.current_pos pos_end = pos_start + num_tokens self.current_pos = pos_end mask = torch.triu( torch.ones( pos_end, pos_end, device=x.device, dtype=torch.bool ),
参考来源
- 位置:《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)
- 结论依据:摘录中关于可重建缓冲区不必写入权重的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。
- 原始内容:self.register_buffer("cos", cos, persistent=False) self.register_buffer("sin", sin, persistent=False) self.cfg = cfg self.current_pos = 0 # Track current position in KV cache def forward(self, in_idx, cache=None): tok_embeds = self.tok_emb(in_idx) x = tok_embeds num_tokens = x.shape[1] if cache is not None: pos_start = self.current_pos pos_end = pos_start + num_tokens self.current_pos = pos_end mask = torch.triu( torch.ones( pos_end, pos_end, device=x.device, dtype=torch.bool ),