ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

llama2.c sample.py 生成不停止:为什么无法在特殊 token 处自动终止及如何处理

llama2.c sample.py 生成不停止:为什么无法在特殊 token 处自动终止及如何处理 llama2.c sample.py 生成不停止为什么无法在特殊 token 处自动终止及如何处理【免费下载链接】llama2.cInference Llama 2 in one file of pure C项目地址: https://gitcode.com/GitHub_Trending/ll/llama2.c用 llama2.c 仓库里的 PyTorch 推理脚本 sample.py 采样时一个常见现象是模型明明已经生成了段落分隔用的特殊 token脚本却不会像 C 版./run那样停下来而是一直输出到max_new_tokens上限才结束。本文基于仓库文档与源码说明这个差异的根因并给出可核对的处理办法要么改用会在特殊 token 处自动终止的 C 版推理要么手动把生成步数控制在终止点以内。现象sample.py 一直生成到 max_new_tokens 上限仓库的 doc/stories260K.md 用 stories260K 测试模型CPU 即可运行对比了两条推理路径。C 版的运行结果如下文档示例输出$ ./run stories260K/stories260K.bin -z stories260K/tok512.bin -t 0.0 Once upon a time, there was a little girl named Lily. She loved to play outside in the park. One day, she saw a big, red ball. She wanted to play with it, but it was too high. Lilys mom said, Lily, lets go to the park. Lily was sad and didnt know what to do. She said, I want to play with your ball, but it cant find it. Lily was sad and didnt know what to do. She said, Im sorry, Lily. I didnt know what to do. Lily didnt want to help her mom, so she said, Im sorry, mom. I didnt know what to do. Her mom said, Dont worry, Lily. We can help you.而对应的 Python 参考运行则必须手动把步数写死$ python sample.py --checkpointstories260K/stories260K.pt --tokenizerstories260K/tok512.model --temperature0.0 --max_new_tokens257文档原文给出了原因说明作者把 257 步硬编码是因为sample.py目前不会像run.c那样在特殊 BOS token 处终止thesample.pyscript doesnt currently terminate on the special BOS token like the run.c script does。注意max_new_tokens在这里只是计数上限没有任何提前停止的语义。为什么 run.c 能停、sample.py 不能run.c 的终止逻辑。run.c 的采样主循环里有一个数据相关的终止条件遇到 BOStoken id 1就跳出循环//>def generate(self, idx, max_new_tokens, temperature1.0, top_kNone): for _ in range(max_new_tokens): # if the sequence context is growing too long we must crop it at block_size idx_cond idx if idx.size(1) self.params.max_seq_len else idx[:, -self.params.max_seq_len:] ... # append sampled index to the running sequence and continue idx torch.cat((idx, idx_next), dim1) return idx见 model.py#L313-L343。这是一个纯计数的for循环每步取最后一个位置的 logits按 temperature 缩放必要时按 top_k 截断、softmax 后采样一个 token 拼回序列没有任何对 BOS / EOS 的检查跑满max_new_tokens步才返回。Python 端其实认识这些特殊 tokentokenizer.py 的Tokenizer从 sentencepiece 模型读出bos_id/eos_id/pad_idtokenizer.py#L21-L25仓库根目录的 tokenizer.model 里bos_id为 1、eos_id为 2doc/train_llama_tokenizer.md 也列出了 Llama 2 tokenizer 训练配置中的bos_id: 1、eos_id: 2。但这些 id 只用于 prompt 编码enc.encode(start, bosTrue, eosFalse)见 sample.py#L70没有进入采样循环。所以这是两条推理路径的实现差异C 版把 BOS 当作序列分隔符来截断Python 参考实现则是定长的计数循环。处理办法用 C 版 run 获得特殊 token 处自动终止推荐sample.py的采样器没有暴露任何停止 token参数无法让它提前停止。最稳的替代是改用 C 版二进制make run ./run stories260K/stories260K.bin -z stories260K/tok512.bin -t 0.0参数说明以 run.c 的error_usage为准见 run.c#L891-L904-z指定 tokenizer 的.bin文件-t 0.0是贪心确定性采样-n是运行步数默认 2560 表示最大序列长度-s是随机种子。C 版在遇到 BOS 时提前停止或达到步数上限停止generate结束时会在 stderr 打印achieved tok/s: ...见 run.c#L777-L780。保留 Python 路径时手动限定步数doc/stories260K.md 给出的做法就是手动硬编码max_new_tokens。如果你已经用 C 版确认了某次采样停在第 N 步例如上面的 257在sample.py里把max_new_tokens设为同一个值sample.py#L17输出就不会越过终止点。用确定性参数对比两条路径验证问题根源复现用 stories260K 模型跑 doc/stories260K.md 中上面列出的两条命令模型与 tokenizer 文件位于stories260K/目录stories260K.bin/stories260K.pt为 C 版与 PyTorch 两种权重的导出格式。C 版输出在 BOS 处截断长度明显小于max_new_tokens。加同一-sC 版/--seedPython 版种子做确定性复现再跑一次sample.py ... --temperature0.0 --max_new_tokens257对比两份文本按仓库文档两条路径在同一模型上的采样结果相同Which gives the same results所以 Python 版应当以 C 版输出为前缀、并继续生成到 257 步。若 Python 版输出前缀与 C 版一致但长度达到上限即可确认不停止是采样循环缺少终止检查导致的而非权重或 tokenizer 不匹配。限制该 BOS 终止逻辑只存在于run.c的generate路径-m chat模式是另一套逻辑用 EOSid 2结束 Assistant 回合run.c#L866-L880不要把两者的终止语义混用。sample.py中max_new_tokens、temperature、top_k、seed等均可按--keyvalue从命令行覆盖见 sample.py#L14-L26 与 configurator.py但没有任何参数可以配置遇到某个 token 就停止。stories260K 是约 26 万参数的小测试模型doc/stories260K.md示例输出质量粗糙不代表终止机制有异常判断停止是否生效只看输出长度是否小于max_new_tokens。仓库测试 test_all.py 对两条路径各做 200 步的确定性前向并与已知正确输出比对可用于确认改动后的 C / Python 行为仍与基线一致该测试会从 huggingface.co 下载 stories260K 的模型与 tokenizer 到test/目录见 test_all.py#L28-L38。【免费下载链接】llama2.cInference Llama 2 in one file of pure C项目地址: https://gitcode.com/GitHub_Trending/ll/llama2.c创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表