
RuntimeError: expected mat1 and mat2 to have the same dtype
快速结论:此错误特指在 ROCm 环境(AMD GPU)下运行 vLLM 的 DFlash speculative decoding 时,hidden states 被提升为 float32 而与 float16 的 fc 层权重不匹配。优先排查是否运行在 AMD ROCm 平台并使用了 DFlash 推测解码。
问题场景
用户在 AMD Strix Halo (gfx1151) GPU 上使用 Docker 镜像 vllm/vllm-openai-rocm:latest(vLLM 0.20.2)启动 vLLM 服务,加载 Qwen3.6-27B-AWQ 模型并启用 DFlash speculative decoding(drafter 模型为 z-lab/Qwen3.6-27B-DFlash)时触发崩溃。该问题仅在 ROCm 构建中出现,CUDA 环境正常。
报错原文
RuntimeError: expected mat1 and mat2 to have the same dtype, but got: float != c10::Half
File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/models/qwen3_dflash.py", line 588, in combine_hidden_states
result = self.model.fc(hidden_states)
File "/usr/local/lib/python3.12/dist-packages/vllm/model_executor/layers/utils.py", line 178, in rocm_unquantized_gemm_impl
return torch.nn.functional.linear(x, weight, bias)
vllm.v1.engine.exceptions.EngineDeadError: EngineCore encountered an issue. See stack trace above for root cause.
原因分析
在 ROCm 平台上,vLLM 的 DFlash speculative decoding 过程中,某个 hidden states 张量被隐式提升为 float32,而全连接层(fc)的权重仍保持 float16。当两者进行矩阵乘法时,PyTorch 会抛出 dtype 不匹配错误。此问题是 ROCm 特有的,CUDA 构建不会触发。
环境排查
- 确认是否运行在 AMD GPU 上(运行
rocminfo | grep gfx查看 GFX 架构) - vLLM 版本:0.20.2(Docker 镜像
vllm/vllm-openai-rocm:latest) - ROCm 版本:6.4.2
- 模型:Qwen3.6-27B-AWQ(base) + z-lab/Qwen3.6-27B-DFlash(drafter)
- 启动参数包含
--speculative-config且 method 为dflash
解决步骤
- 临时补丁修复(手动修改源码,可优先尝试):
编辑vllm/model_executor/models/qwen3_dflash.py文件,在第 588 行(combine_hidden_states函数)前插入一行类型转换:hidden_states = hidden_states.to(self.model.fc.weight.dtype) result = self.model.fc(hidden_states)该补丁确保 hidden_states 与 fc 层权重的 dtype 一致。
- 升级到包含修复的最新镜像(推荐):
此问题已通过 Pull Request #42646 在 vLLM 代码库中修复。请拉取最新vllm/vllm-openai-rocm:latest镜像或从源码构建最新版本。
验证方法
应用补丁或升级后,重新启动 vLLM 服务并发送一次推理请求(如 curl ... -d '{"model":"qwen3-27b","messages":[{"role":"user","content":"Hello"}],"max_tokens":50}')。若不再出现 RuntimeError: expected mat1 and mat2 to have the same dtype 且服务正常响应,则问题已解决。



