Skip to content

Port autograd code for rnnt #3970

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/scripts/unittest-linux/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ fi

(
cd test
pytest torchaudio_unittest -k "not backend and not /io/ and not prototype and not sox and not ffmpeg and not fairseq and not hdemucs"
pytest torchaudio_unittest -k "not backend and not /io/ and not prototype and not sox and not ffmpeg and not fairseq and not hdemucs and not (torchscript and rnnt)"
)
1 change: 0 additions & 1 deletion src/libtorchaudio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ if(BUILD_RNNT)
rnnt/compute_alphas.cpp
rnnt/compute_betas.cpp
rnnt/compute.cpp
rnnt/autograd.cpp
)
if (USE_CUDA)
list(
Expand Down
69 changes: 0 additions & 69 deletions src/libtorchaudio/rnnt/autograd.cpp

This file was deleted.

1 change: 1 addition & 0 deletions src/libtorchaudio/rnnt/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
"int blank,"
"float clamp,"
"bool fused_log_softmax) -> (Tensor, Tensor?)");
m.def("torchaudio::rnnt_loss_forward", &rnnt_loss);
}
29 changes: 21 additions & 8 deletions src/torchaudio/functional/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,19 @@ def _fix_waveform_shape(
waveform_shift = waveform_shift.view(shape[:-1] + waveform_shift.shape[-1:])
return waveform_shift

class RnntLoss(torch.autograd.Function):
@staticmethod
def forward(ctx, *args):
output, saved = torch.ops.torchaudio.rnnt_loss_forward(*args)
ctx.save_for_backward(saved)
return output

@staticmethod
def backward(ctx, dy):
grad = ctx.saved_tensors[0]
grad_out = dy.view((-1, 1, 1, 1))
result = grad * grad_out;
return (result, None, None, None, None, None, None, None)

def _rnnt_loss(
logits: Tensor,
Expand Down Expand Up @@ -1803,14 +1816,14 @@ def _rnnt_loss(
if blank < 0: # reinterpret blank index if blank < 0.
blank = logits.shape[-1] + blank

costs, _ = torch.ops.torchaudio.rnnt_loss(
logits=logits,
targets=targets,
logit_lengths=logit_lengths,
target_lengths=target_lengths,
blank=blank,
clamp=clamp,
fused_log_softmax=fused_log_softmax,
costs = RnntLoss.apply(
logits,
targets,
logit_lengths,
target_lengths,
blank,
clamp,
fused_log_softmax
)

if reduction == "mean":
Expand Down
Loading