You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复Multi-Tacotron-Voice-Cloning的张量类型不匹配RuntimeError

解决Multi-Tacotron-Voice-Cloning在GPU运行时的张量类型不匹配错误

在Google Colab的GPU运行环境中执行命令!python demo_cli.py -p "audio/input/ex.wav" -t "Hello my friends" --no_sound时,抛出以下错误:

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

该错误的核心原因是模型权重加载在了CPU上,但输入数据被自动传到了GPU,导致张量设备不匹配。需要修改demo_cli.py中的模型加载逻辑,确保所有模型都部署到GPU上,具体修改点如下:


1. 添加全局设备检测逻辑

在导入torch模块后,添加代码检测可用设备,统一管理模型部署目标:

# 放在import torch之后
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

2. 修改编码器(encoder)加载逻辑

原代码默认将编码器加载到CPU,需修改为指定GPU设备:
找到这行代码:

encoder.load_model(args.enc_model_fpath)

替换为:

encoder.load_model(args.enc_model_fpath, device=device)

如果encoder/inference.py中的load_model函数没有device参数,需要同步修改该函数,添加设备参数并将模型移至对应设备。

3. 修改合成器(synthesizer)初始化逻辑

合成器初始化时需指定GPU设备:
找到这行代码:

synthesizer = Synthesizer(args.syn_model_dir.joinpath("taco_pretrained"), low_mem=args.low_mem)

替换为:

synthesizer = Synthesizer(args.syn_model_dir.joinpath("taco_pretrained"), low_mem=args.low_mem, device=device)

如果Synthesizer类没有device参数,需要修改synthesizer/inference.py的__init__方法,添加设备参数并将内部模型移至对应设备。

4. 修改声码器(vocoder)加载逻辑

声码器同样需要加载到GPU:
找到这行代码:

vocoder.load_model(args.voc_model_fpath)

替换为:

vocoder.load_model(args.voc_model_fpath, device=device)

如果vocoder/inference.py中的load_model函数没有device参数,需要同步修改该函数,添加设备参数并将模型移至对应设备。


额外检查

如果修改后仍报错,需确认模型推理函数(如encoder.embed_utterance、synthesizer.synthesize_spectrograms、vocoder.infer_waveform)内部是否将输入张量转移到了指定设备,确保输入数据与模型权重的设备一致。

内容的提问来源于stack exchange,提问作者solidsnake777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 23:45:34