
how to use –force-fp16 to sovle the problem.KSampler: Expected query, key, and value to have the same dtype, but got query.dtype: struct c1
快速结论:该报错通常在使用 IPAdapter Plus 与 KSampler 组合时出现,原因是模型、Conditioning 或 latent 的 dtype(Half vs float)不匹配。优先检查启动参数 --force-fp16 的写法是否正确,以及是否为工作流中混合使用了 fp16 和 fp32 模型。
问题场景
用户在 ComfyUI 中使用 IPAdapter Plus 节点(配合 AnimateDiff-Evolved 或 Advanced-ControlNet 等自定义节点),运行包含 KSampler 的工作流时触发。该问题可能出现在用户从 Text2Image 切换到 Image2Image / IPAdapter 工作流时。用户已尝试添加 --force-fp16 启动参数,但报错仍然出现。
报错原文
Error occurred when executing KSampler:
Expected query, key, and value to have the same dtype, but got query.dtype: struct c10::Half key.dtype: float and value.dtype: float instead.
File "D:\comfyui\ComfyUI-master\execution.py", line 151, in recursive_execute
...
File "D:\comfyui\ComfyUI-master\comfy\samplers.py", line 662, in inner_sample
samples = sampler.sample(self, sigmas, extra_args, callback, noise, latent_image, denoise_mask, disable_pbar)
File "D:\comfyui\ComfyUI-master\comfy\samplers.py", line 567, in sample
samples = self.sampler_function(model_k, noise, sigmas, extra_args=extra_args, callback=k_callback, disable=disable_pbar, **self.extra_options)
...
原因分析
可能原因:
– 用户在启动 ComfyUI 时错误地使用了 --force -fp16(带空格),而不是正确的 --force-fp16(连字符)。
– 即使正确使用了 --force-fp16,IPAdapter Plus 或其他自定义节点(如 AnimateDiff、Advanced-ControlNet)可能在内部强制将某些张量转为 float32,导致与 KSampler 中的 fp16 query 不匹配。
– 模型加载时 dtype 不一致:例如 IPAdapter 模型加载为 fp32,但主模型(checkpoint)加载为 fp16。
– 用户工作流中混合了来自不同来源的 Conditioning/输入,它们的数据类型可能不一致。
环境排查
- ComfyUI 版本:确认是否为最新版本(或至少是 2024年8月前后的版本)。
- Python 版本:通常为 3.10.x / 3.11.x。
- PyTorch / CUDA 版本:确认 PyTorch 是否与 CUDA 版本兼容(建议 PyTorch 2.x + CUDA 11.8 或 12.x)。
- 显卡显存:如果显存不足,fp16 可能被自动回退到 fp32 造成不一致。
- 自定义节点列表:列出所有已安装的节点,尤其是 IPAdapter Plus、AnimateDiff-Evolved、Advanced-ControlNet、ComfyUI-Manager 等。
- 启动参数:检查 ComfyUI 启动命令或 .bat 文件中
--force-fp16的拼写(无空格,中间是连字符)。
解决步骤
- 检查启动参数:确保启动命令中写的是
--force-fp16(而不是--force -fp16或--force_fp16)。示例:python main.py --force-fp16或.\python_embeded\python.exe -s ComfyUI\main.py --force-fp16。 - 确认模型和 Conditioning 的 dtype:在 ComfyUI 中使用“Show dtype”节点(或类似调试节点)检查模型、正向/负向 Conditioning 以及 latent 的 dtype。确保它们都是 float16(Half)或都是 float32。
- 卸载/更新自定义节点(可优先尝试):此问题在多个节点组合时更易出现。为避免冲突,可尝试:
- 暂时禁用或卸载 AnimateDiff-Evolved、Advanced-ControlNet 等自定义节点,仅使用 IPAdapter Plus 和基础节点测试。
- 更新所有节点到最新版本(通过 ComfyUI-Manager 或手动 git pull)。
- 回退到原始 ComfyUI 设置:如果以上均无效,尝试使用 ComfyUI 的原生批次(batch)节点,而不是从其他工作流复制的复杂逻辑。
- 检查模型来源:部分 IPAdapter 模型(如 ip-adapter-faceid-plusv2)需要使用 fp16 版本或特定精度。重新下载模型并检查其格式(确保为 safetensors 而非 bin)。
- 如果问题仍然存在,可参考 Issue #108(原 Issue 中提及)以获取进一步讨论。
验证方法
重启 ComfyUI 后,加载一个简单的工作流(仅包含 Checkpoint Loader、IPAdapter Plus、KSampler、VAE Decode、Save Image),运行一次。如果不再出现 Expected query, key, and value to have the same dtype 错误,且正常生成图片,则问题解决。如果仍然报错,请检查步骤 2 中的 dtype 一致性。



