PyTorch 导出 ONNX,如何解决不支持 ISTFT

61 阅读1分钟

ONNX 在算子版本 17 引入了 STFT 算子,PyTorch 模型中涉及到 torch.stft() 运算也能正常转换为 ONNX 模型了。

但截至目前(20251207),ONNX 迟迟未能支持 ISTFT 运算,使用 torch.istft() 会导致模型转换出错。相关 issue:

感谢 biendltb 实现了一个替代方案:biendltb/torch-istft-onnx: An onnx-exportable implementation of iSTFT in torch。这个项目提供了一个运算封装 ISTFT,可以作为 torch.istft() 的替代品。

istft = ISTFT(
    n_fft=2048,
    hop_length=512,
    win_length=2048,
    window=torch.hann_window(2048),
    normalized=False,
)

recon_audio = istft(torch.view_as_real(stft_audio))

# 等价于:
# recon_audio = torch.istft(
#     stft_audio,
#     n_fft=2048,
#     hop_length=512,
#     win_length=2048,
#     window=torch.hann_window(2048),
#     normalized=False,
# )