
Misc. bug: llama-quantize fails on DeepSeek-V4 — i32 ffn_gate_tid2eid routing table not excluded from quantization
快速结论:在使用 llama-quantize 对 DeepSeek-V4 模型进行量化时,因 ffn_gate_tid2eid.weight 张量为 i32 类型且未被排除量化,导致报错 cannot dequantize/convert tensor type i32。优先排查该张量是否在量化白名单中被遗漏。
问题场景
用户在 macOS/Metal 环境下,使用 llama-quantize 工具对 DeepSeek-V4(如 deepseek-ai/DeepSeek-V4-Flash-DSpark)的 GGUF 文件进行量化时触发。典型命令为:llama-quantize --allow-requantize v4-q8.gguf v4-q4.gguf Q4_K_M。
报错原文
[ 21/1328] blk.0.ffn_gate_tid2eid.weight - [ 6, 129280, 1, 1], type = i32,
llama_model_quantize: failed to quantize: cannot dequantize/convert tensor type i32
(抛出位置:src/llama-quant.cpp:228)
原因分析
blk.N.ffn_gate_tid2eid.weight(张量名 LLM_TENSOR_FFN_GATE_TID2EID)是一个 token-id 到 expert-id 的索引表,形状为 {n_expert_used, n_vocab},类型为 i32。它不是权重张量,不应参与量化。代码中已存在对另一个 MoE 路由张量 ffn_gate_inp.weight 的排除逻辑:
quantize &= name.find("ffn_gate_inp.weight") == std::string::npos;
但 tid2eid 在 DeepSeek-V4 支持(#24162)时被遗漏。该张量无法被 llama_tensor_quantize_impl 处理,导致量化中断。
环境排查
- 模型来源:DeepSeek-V4 官方或社区 GGUF 文件,已包含
ffn_gate_tid2eid.weight张量。 - 工具版本:
llama-quantize(位于llama.cpp仓库,版本在505b1ed1及更新后修复)。 - 后端:macOS/Metal 或任何不支持
i32张量量化的后端。 - 前置步骤:使用
convert_hf_to_gguf.py转换并指定--fp8-as-q8参数。
解决步骤
- 确认修复版本:该问题已在
llama.cpp主分支中修复(#25787)。升级至包含该提交的版本(如505b1ed1之后的任意 commit)。 - 手动修复(适用于旧版本):打开
src/llama-quant.cpp,找到以下代码段:
quantize &= name.find("ffn_gate_inp.weight") == std::string::npos;
- 在其后添加一行:
quantize &= name.find("tid2eid") == std::string::npos; // DeepSeek-V4: i32 token-id -> expert-id table
- 重新编译:运行
make或对应编译命令,构建新的llama-quantize可执行文件。 - 再次尝试量化:使用原始命令重新量化。此时
tid2eid张量会被原样复制(约 2.959 MiB),不再触发错误。
验证方法
重新运行 llama-quantize,观察输出日志:
- 不再出现
cannot dequantize/convert tensor type i32错误。 - 量化过程成功完成至目标类型(如 Q4_K_M)。
- 可进一步用
llama-cli等工具加载量化模型,验证推理正常。
重要提示
可优先尝试:如果目的是优化磁盘占用或推理速度,应注意 DeepSeek-V4 的专家张量已为 MXFP4 格式,直接使用 --allow-requantize 可能导致文件体积增加(因上转换)。社区推荐按 Unsloth 的方案(仅量化专家张量)以避免影响模型质量。当前修复仅为排除 tid2eid 的量化错误,不涉及量化策略优化。
参考来源
ggml-org/llama.cpp #25754 — 原始 Issue 报告及解决方案



