Hello everyone,
I’m trying to export torchaudio.models.decoder.ctc_decoder to TorchScript so that I can use it in a C++ application. However, I have encountered several issues along the way.
What I Tried
-
Using
torch.jit.script()- Running
torch.jit.script(decoder)gives the following error:torch.jit.frontend.UnsupportedNodeError: Lambda aren't supported - The issue seems to be related to a lambda function inside the
_get_tokensmethod ofCTCDecoder, which TorchScript does not support.
- Running
-
Using
torch.jit.trace()- I tried tracing the decoder using:
scripted_decoder = torch.jit.trace(decoder, dummy_input) - This resulted in the following error:
RuntimeError: Tracer cannot infer type of [[CTCHypothesis(tokens=tensor([...]))]] - The problem here is that the decoder returns a list of
CTCHypothesisobjects, which TorchScript cannot process.
- I tried tracing the decoder using:
-
Wrapping the Decoder in
nn.Module- I attempted to wrap the decoder inside an
nn.Modulesubclass to make it compatible with scripting, but the sameCTCHypothesistype inference error occurred.
- I attempted to wrap the decoder inside an
Question
- Is there any recommended way to export
torchaudio.models.decoder.ctc_decoderto TorchScript? - If not, what would be the best approach to deploy it in a C++ application?
- Would modifying the source code of
CTCDecoder(e.g., replacing lambda functions and using standard PyTorch types) be a feasible solution?
Any guidance or workarounds would be greatly appreciated!
Thanks in advance!