-
Notifications
You must be signed in to change notification settings - Fork 735
RNN Transducer Loss Autograd Test #1532
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0740ab
select autograd test from carolineechen/audio#2
vincentqb 65c0d59
fix numpy backward: be careful to not modify inplace.
vincentqb fa2956c
gradcheck will fail if input is modified in place.
vincentqb 4f6ec1e
add rnnt_loss autograd test too.
vincentqb 3425bac
leave rtol to default value.
vincentqb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import torch | ||
| from .autograd_impl import Autograd | ||
| from torchaudio_unittest import common_utils | ||
| from .utils import skipIfNoTransducer | ||
|
|
||
|
|
||
| @skipIfNoTransducer | ||
| class TestAutograd(Autograd, common_utils.PytorchTestCase): | ||
| dtype = torch.float32 | ||
| device = torch.device('cpu') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import torch | ||
| from .autograd_impl import Autograd | ||
| from torchaudio_unittest import common_utils | ||
| from .utils import skipIfNoTransducer | ||
|
|
||
|
|
||
| @skipIfNoTransducer | ||
| @common_utils.skipIfNoCuda | ||
| class TestAutograd(Autograd, common_utils.PytorchTestCase): | ||
| dtype = torch.float32 | ||
| device = torch.device('cuda') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from typing import Callable, Tuple | ||
| import torch | ||
| from torch import Tensor | ||
| from torch.autograd import gradcheck | ||
| from torchaudio_unittest.common_utils import ( | ||
| TestBaseMixin, | ||
| ) | ||
| from torchaudio.prototype.rnnt_loss import RNNTLoss, rnnt_loss | ||
| from parameterized import parameterized | ||
| from .utils import ( | ||
| numpy_to_torch, | ||
| get_B1_T10_U3_D4_data, | ||
| get_numpy_data_B2_T4_U3_D3, | ||
| get_numpy_data_B1_T2_U3_D5 | ||
| ) | ||
| from .numpy_transducer import NumpyTransducerLoss | ||
|
|
||
|
|
||
| class Autograd(TestBaseMixin): | ||
| @staticmethod | ||
| def get_data(data_func, device): | ||
| data_np = data_func() | ||
| if type(data_np) == tuple: | ||
| data_np = data_np[0] | ||
| data = numpy_to_torch( | ||
| data=data_np, device=device, requires_grad=True | ||
| ) | ||
| return data | ||
|
|
||
| def assert_grad( | ||
| self, | ||
| loss: Callable[..., Tensor], | ||
| inputs: Tuple[torch.Tensor], | ||
| *, | ||
| enable_all_grad: bool = True, | ||
| ): | ||
| inputs_ = [] | ||
| for i in inputs: | ||
| if torch.is_tensor(i): | ||
| i = i.to(dtype=self.dtype, device=self.device) | ||
| if enable_all_grad: | ||
| i.requires_grad = True | ||
| inputs_.append(i) | ||
| # gradcheck with float32 requires higher atol and epsilon | ||
| assert gradcheck(loss, inputs, eps=1e-3, atol=1e-3, nondet_tol=0.) | ||
|
|
||
| @parameterized.expand([ | ||
| (get_B1_T10_U3_D4_data, ), | ||
| (get_numpy_data_B2_T4_U3_D3, ), | ||
| (get_numpy_data_B1_T2_U3_D5, ), | ||
| ]) | ||
| def test_RNNTLoss_gradcheck(self, data_func): | ||
| data = self.get_data(data_func, self.device) | ||
| inputs = ( | ||
| data["logits"].to(self.dtype), | ||
| data["targets"], | ||
| data["logit_lengths"], | ||
| data["target_lengths"], | ||
| ) | ||
| loss = RNNTLoss(blank=data["blank"], reuse_logits_for_grads=False) | ||
vincentqb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| self.assert_grad(loss, inputs, enable_all_grad=False) | ||
|
|
||
| @parameterized.expand([ | ||
| (get_B1_T10_U3_D4_data, ), | ||
| (get_numpy_data_B2_T4_U3_D3, ), | ||
| (get_numpy_data_B1_T2_U3_D5, ), | ||
| ]) | ||
| def test_rnnt_loss_gradcheck(self, data_func): | ||
| data = self.get_data(data_func, self.device) | ||
| inputs = ( | ||
| data["logits"].to(self.dtype), # logits | ||
| data["targets"], # targets | ||
| data["logit_lengths"], # logit_lengths | ||
| data["target_lengths"], # target_lengths | ||
| data["blank"], # blank | ||
| -1, # clamp | ||
| True, # fused_log_softmax | ||
| False, # reuse_logits_for_grads | ||
| ) | ||
|
|
||
| self.assert_grad(rnnt_loss, inputs, enable_all_grad=False) | ||
|
|
||
| @parameterized.expand([ | ||
| (get_B1_T10_U3_D4_data, ), | ||
| (get_numpy_data_B2_T4_U3_D3, ), | ||
| (get_numpy_data_B1_T2_U3_D5, ), | ||
| ]) | ||
| def test_np_transducer_gradcheck(self, data_func): | ||
| data = self.get_data(data_func, self.device) | ||
| inputs = ( | ||
| data["logits"].to(self.dtype), | ||
| data["logit_lengths"], | ||
| data["target_lengths"], | ||
| data["targets"], | ||
| ) | ||
| loss = NumpyTransducerLoss(blank=data["blank"]) | ||
|
|
||
| self.assert_grad(loss, inputs, enable_all_grad=False) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great find, and thanks for looking into this! can you add autograd tests for the functional version as well?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, added :)