Skip to content

[Cherry-pick 2.0.1] Fix virtual function issue with CTC decoder (#3230) #3238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/torchaudio_unittest/models/decoder/ctc_decoder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,19 @@ def test_index_to_tokens(self, tokens):

expected_tokens = ["|", "f", "|", "o", "a"]
self.assertEqual(tokens, expected_tokens)

def test_lm_lifecycle(self):
"""Passing lm without assiging it to a vaiable won't cause runtime error

https://github.com/pytorch/audio/issues/3218
"""
from torchaudio.models.decoder import ctc_decoder

from .ctc_decoder_utils import CustomZeroLM

decoder = ctc_decoder(
lexicon=get_asset_path("decoder/lexicon.txt"),
tokens=get_asset_path("decoder/tokens.txt"),
lm=CustomZeroLM(),
)
decoder(torch.zeros((1, 3, NUM_TOKENS), dtype=torch.float32))
6 changes: 6 additions & 0 deletions torchaudio/models/decoder/_ctc_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ def __init__(
)
else:
self.decoder = _LexiconFreeDecoder(decoder_options, lm, silence, self.blank, transitions)
# https://github.com/pytorch/audio/issues/3218
# If lm is passed like rvalue reference, the lm object gets garbage collected,
# and later call to the lm fails.
# This ensures that lm object is not deleted as long as the decoder is alive.
# https://github.com/pybind/pybind11/discussions/4013
self.lm = lm

def _get_tokens(self, idxs: torch.IntTensor) -> torch.LongTensor:
idxs = (g[0] for g in it.groupby(idxs))
Expand Down