CosyVoice icon indicating copy to clipboard operation
CosyVoice copied to clipboard

导出ONNX格式时报错

Open Sunnycl opened this issue 1 year ago • 15 comments

运行命令: python cosyvoice/bin/export_onnx.py --model_dir pretrained_models/CosyVoice2-0.5B

报错内容: Traceback (most recent call last): File "/Projects/TTS_metric/CosyVoice/cosyvoice/bin/export_onnx.py", line 116, in main() File "/Projects/TTS_metric/CosyVoice/cosyvoice/bin/export_onnx.py", line 112, in main torch.testing.assert_allclose(output_pytorch, torch.from_numpy(output_onnx).to(device), rtol=1e-2, atol=1e-4) File "/home/miniconda3/envs/tts2/lib/python3.10/site-packages/torch/testing/_comparison.py", line 1565, in assert_allclose torch.testing.assert_close( File "/home/miniconda3/envs/tts2/lib/python3.10/site-packages/torch/testing/_comparison.py", line 1523, in assert_close raise error_metas[0].to_error(msg) AssertionError: Tensor-likes are not close!

Mismatched elements: 3220 / 71200 (4.5%) Greatest absolute difference: 0.028653383255004883 at index (0, 79, 2) (up to 0.0001 allowed) Greatest relative difference: 49.96367645263672 at index (1, 17, 89) (up to 0.01 allowed)

Sunnycl avatar Mar 03 '25 09:03 Sunnycl

我也遇到了同样的问题 ,请问目前解决了么

yuehaotian-gpt avatar Mar 04 '25 07:03 yuehaotian-gpt

同上

JohnHerry avatar Mar 26 '25 12:03 JohnHerry

我是通过把误差上限调高解决的,目前使用量化后的参数可以正常生成

Sunnycl avatar Apr 01 '25 02:04 Sunnycl

CosyVoice2 flow2 的模型,可以转onnx, 验证报错后也不影响转trt合成。 但是CosyVoice1 flow1模型,转onnx后验证不报错,但是转成trt fp16后,合成的东西都是 Nan

JohnHerry avatar Apr 01 '25 02:04 JohnHerry

我目前用开发分支微调了cosyvoice2的flow模型后,onnx格式转换出现了同样的报错,并且转trt后生成的内容都是NAN,推理时间增加,并打印很多报错 [04/03/2025-15:50:41] [TRT] [E] 3: [executionContext.cpp::setInputShape::2068] Error Code 3: API Usage Error (Parameter check failed at: runtime/api/executionContext.cpp::setInputShape::2068, condition: satisfyProfile Runtime dimension does not satisfy any optimization profile.)

你有没有这个报错

Sunnycl avatar Apr 03 '25 08:04 Sunnycl

运行命令: python cosyvoice/bin/export_onnx.py --model_dir pretrained_models/CosyVoice2-0.5B

报错内容: Traceback (most recent call last): File "/Projects/TTS_metric/CosyVoice/cosyvoice/bin/export_onnx.py", line 116, in main() File "/Projects/TTS_metric/CosyVoice/cosyvoice/bin/export_onnx.py", line 112, in main torch.testing.assert_allclose(output_pytorch, torch.from_numpy(output_onnx).to(device), rtol=1e-2, atol=1e-4) File "/home/miniconda3/envs/tts2/lib/python3.10/site-packages/torch/testing/_comparison.py", line 1565, in assert_allclose torch.testing.assert_close( File "/home/miniconda3/envs/tts2/lib/python3.10/site-packages/torch/testing/_comparison.py", line 1523, in assert_close raise error_metas[0].to_error(msg) AssertionError: Tensor-likes are not close!

Mismatched elements: 3220 / 71200 (4.5%) Greatest absolute difference: 0.028653383255004883 at index (0, 79, 2) (up to 0.0001 allowed) Greatest relative difference: 49.96367645263672 at index (1, 17, 89) (up to 0.01 allowed)

我也遇到了这个问题。 我看是onnx计算出来的结果和pytorch计算的结果有差异。 绝对误差和相对误差都超了检查阈值, 超的挺多的。0.0286, 49.963

WendongGan avatar Apr 15 '25 06:04 WendongGan

我目前用开发分支微调了cosyvoice2的flow模型后,onnx格式转换出现了同样的报错,并且转trt后生成的内容都是NAN,推理时间增加,并打印很多报错 [04/03/2025-15:50:41] [TRT] [E] 3: [executionContext.cpp::setInputShape::2068] Error Code 3: API Usage Error (Parameter check failed at: runtime/api/executionContext.cpp::setInputShape::2068, condition: satisfyProfile Runtime dimension does not satisfy any optimization profile.)

你有没有这个报错

请注意,Trt是硬件环境依赖的。你在开发机器转Trt,然后到运行环境去跑,两个环境的显卡,驱动,cuda版本不一样,就各种问题。你需要严格在运行环境编译和执行。

JohnHerry avatar Apr 15 '25 09:04 JohnHerry

你们解决了么? 注释掉检查的torch.testing.assert_allclose(i, torch.from_numpy(j).to(device), rtol=1e-2, atol=1e-4)两行可以导出,测试了下,效果好像还行。 不知道实际影响有多大,或者要导出无误差和无损的话,可以怎么优化?

WendongGan avatar Apr 16 '25 01:04 WendongGan

我目前用开发分支微调了cosyvoice2的flow模型后,onnx格式转换出现了同样的报错,并且转trt后生成的内容都是NAN,推理时间增加,并打印很多报错 [04/03/2025-15:50:41] [TRT] [E] 3: [executionContext.cpp::setInputShape::2068] Error Code 3: API Usage Error (Parameter check failed at: runtime/api/executionContext.cpp::setInputShape::2068, condition: satisfyProfile Runtime dimension does not satisfy any optimization profile.) 你有没有这个报错

请注意,Trt是硬件环境依赖的。你在开发机器转Trt,然后到运行环境去跑,两个环境的显卡,驱动,cuda版本不一样,就各种问题。你需要严格在运行环境编译和执行。

你是编译环境对齐后就没有超出误差了吗,我的这个报错是trt的profile和模型输入动态维度没对齐导致的,在构建ONNX格式时仍然会超出误差,但生成正常语音

Sunnycl avatar Apr 16 '25 02:04 Sunnycl

我目前用开发分支微调了cosyvoice2的flow模型后,onnx格式转换出现了同样的报错,并且转trt后生成的内容都是NAN,推理时间增加,并打印很多报错 [04/03/2025-15:50:41] [TRT] [E] 3: [executionContext.cpp::setInputShape::2068] Error Code 3: API Usage Error (Parameter check failed at: runtime/api/executionContext.cpp::setInputShape::2068, condition: satisfyProfile Runtime dimension does not satisfy any optimization profile.) 你有没有这个报错

请注意,Trt是硬件环境依赖的。你在开发机器转Trt,然后到运行环境去跑,两个环境的显卡,驱动,cuda版本不一样,就各种问题。你需要严格在运行环境编译和执行。

你是编译环境对齐后就没有超出误差了吗,我的这个报错是trt的profile和模型输入动态维度没对齐导致的,在构建ONNX格式时仍然会超出误差,但生成正常语音

没有,我也是,转换后Trt fp16可以正常推理。 请问你的模型训练上下文是 fp32全精度的?还是 amp 混合精度的?是不是混合精度训出来模型会好一点?我也在摸索。

JohnHerry avatar Apr 17 '25 09:04 JohnHerry

另外,好像转onnx的验证报错,貌似跟转换成模型的可用性没有直接关系。 我之前训练过 cosyvoice1 flow1版本的模型,转onnx的时候验证通过没有问题。 但是转成TensorRT fp16 之后,推理得到梅尔谱都是Nan。 最后合成一堆静音。

JohnHerry avatar Apr 17 '25 09:04 JohnHerry

我用的混合精度训练dev分支那个代码,官方有提供v1的flow训练代码吗,你可以看看是不是execute_v2执行失败导致的。我看folw的代码,v1和v2的forward只有加mask的方式不一样,正常v2部署没问题,v1应该也没问题,旧版共用了一个推理,更新这个加了cache我目前用是execute_v2正常execute_async_v3无法执行

Sunnycl avatar Apr 17 '25 10:04 Sunnycl

我训练flow1的代码版本是比较老的,2024年9月份以前的吧。

JohnHerry avatar Apr 18 '25 01:04 JohnHerry

@JohnHerry 遇到同样问题,fp16有NaN问题,只能fp32,fp32的结果也不太好

HaiFengZeng avatar Apr 30 '25 10:04 HaiFengZeng

我有个问题,为什么只导出一个flow解码器的onnx,其他的要怎么导出

BrainBlairfrist avatar Dec 24 '25 10:12 BrainBlairfrist