RuntimeError: Input type (float) and bias type (c10::Half) should be the same

这个报错通常出现在手动调用 Whisper 的 WhisperFeatureExtractor 后,直接把 float32 的 input_features 送入以 float16(半精度)加载的模型,例如 openai/whisper-large-v3 、 openai/whisper-large

快速结论:这个报错通常出现在手动调用 Whisper 的 WhisperFeatureExtractor 后,直接把 float32 的 input_features 送入以 float16(半精度)加载的模型,例如 openai/whisper-large-v3、openai/whisper-large-v3-turbo 或 distil-whisper/distil-large-v3,特征与模型权重精度不一致时触发。优先排查模型参数 dtype 与特征 dtype 是否一致,并把输入特征转换到 model.dtype。

适用环境:Issue 中确认的环境为 transformers 5.7.0、torch 2.11.0、Python 3.12.12、macOS 15.7.1(arm64),并可在 CPU 上复现;后续评论提到在 5.15.0、5.16.1 以及 main(c93057d)上仍然存在。

最快修复方案:Issue 中已确认并提供维护者认可的做法是:先运行 fe(...) 得到特征,再执行 inputs.to(model.device, dtype=model.dtype),然后调用 model.generate(**inputs)。

注意事项:这是调用侧的精度对齐,不是模型或特征提取器本身的自动修复;from_pretrained 仍可能按 config.dtype 加载 float16 权重,而 WhisperFeatureExtractor 仍输出 float32。Issue 中另有 dtype=torch.float32 和 feats.to(model.dtype) 两种已验证的绕过方式,但对 encoder 内部强制转换等方案,Issue 表示需等待对应 PR 合入,尚未成为已确认修复。

问题场景

使用 Transformers 加载 Whisper 模型并手动调用其特征提取器,是 Issue 中复现的路径:使用 WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v3")(未传 dtype),再用同一 checkpoint 的 WhisperFeatureExtractor 生成 input_features,随后调用 model.model.encoder(feats) 或 model.generate(input_features)。Issue 标题指出 large-v3 与 large-v3-turbo 的配置声明 float16,而特征提取器输出 float32;评论进一步确认 distil-whisper/distil-large-v3 同样会触发。高层 pipeline() 会在内部做类型转换,因此不受影响;受影响的是手动推理、微调脚本和文档示例这类路径。

报错原文

torch.float16 torch.float32
RuntimeError: Input type (float) and bias type (c10::Half) should be the same

评论中 distil-whisper/distil-large-v3 的复现输出为:

distil-whisper/distil-large-v3
  weights torch.float16, features torch.float32
  RuntimeError: Input type (float) and bias type (c10::Half) should be the same

原因分析

最可能的原因是模型权重精度与输入特征精度不一致。from_pretrained 会遵循 config.torch_dtype;Issue 中列出的 whisper-large-v3、whisper-large-v3-turbo,以及评论补充的 distil-whisper/distil-large-v3,其配置声明为 float16,因此模型参数为 torch.float16。而 WhisperFeatureExtractor 始终返回 float32,于是 WhisperEncoder.forward 直接把 float32 输入送入 float16 的卷积层时,就出现上述报错。较早的 whisper-tiny/base/small/medium/large-v2 配置为 float32,因此没有该不匹配;wavlm-base-plus 等非 Whisper 模型使用不同的特征提取器,评论也特别说明这些只是查看配置、未实际执行,不能直接与 Whisper 结论等同。报错只显示类型名,没有提示 checkpoint 声明的 dtype,因此容易被误认为用户输入错误。

环境排查

  • 确认所用 checkpoint 是否为 openai/whisper-large-v3、openai/whisper-large-v3-turbo 或 distil-whisper/distil-large-v3,这些在 Issue 中确认存在该不匹配。
  • 检查模型参数 dtype,例如 next(model.parameters()).dtype 是否为 torch.float16。
  • 检查特征张量 dtype,例如 feats.dtype 是否为 torch.float32。
  • 确认是否走手动路径(自定义推理、微调脚本、文档示例)而非 pipeline();后者内部会做转换。
  • 确认调用 model.generate(input_features) 前是否只把特征移到了设备(to(model.device)),却没有把 dtype 对齐到 model.dtype。
  • 版本方面,Issue 在 transformers 5.7.0 复现,评论指出在 5.15.0、5.16.1 和 main 上仍存在;该行为由 checkpoint 配置决定,不是某台机器或某个本地设置导致。

解决步骤

  1. 按 Issue 中维护者认可的方式,对特征同时指定设备和 dtype:
    inputs = fe(wav, sampling_rate=16000, return_tensors="pt").to(model.device, dtype=model.dtype)
    model.generate(**inputs)

    评论确认该写法在 openai/whisper-large-v3-turbo 上可避开报错。

  2. 如果当前代码路径是直接调用 encoder 或 model.generate(input_features),可先尝试把 feats 转为 model.dtype:
    feats = feats.to(model.dtype)

    Issue 中该方式在 large-v3 上已验证。

  3. 另一种已验证绕过方式是在加载模型时强制 float32:
    model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v3", dtype=torch.float32)

    该方式同样在 large-v3 上验证过。

  4. 如果使用的是 docs/source/en/model_doc/whisper.md 中的 AutoModel 示例,Issue 指出示例只写了 input_features.to(model.device),在 5.15.0 上仍会报错;按讨论,维护者同意把该行改为 input_features.to(model.device, dtype=model.dtype),模型侧保持不变。
  5. 如果希望在库内部彻底消除该问题,Issue 中提到的 WhisperEncoder.forward 内部转换方案由 PR #47816 实现,但 Issue 讨论时尚未合入,也不在本次记录的已确认修复内;需要等待该 PR 合入并发布对应版本。

验证方法

修复后,先打印 next(model.parameters()).dtype 与处理后的 inputs["input_features"].dtype,确认二者一致;再执行 model.generate(**inputs),确认不再出现 RuntimeError: Input type (float) and bias type (c10::Half) should be the same,并能正常输出转写结果。Issue 评论提到在文档示例中补上 dtype=model.dtype 后,示例转写正常,可作为验证参考。

参考来源

huggingface/transformers #47805

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 29691

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注