-
Notifications
You must be signed in to change notification settings - Fork 667
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
Save/load TorchScript object in test #1446
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -6,17 +6,21 @@ | |
from parameterized import parameterized | ||
|
||
from torchaudio_unittest import common_utils | ||
from torchaudio_unittest.common_utils import TempDirMixin, TestBaseMixin | ||
from torchaudio_unittest.common_utils import ( | ||
skipIfRocm, | ||
) | ||
|
||
|
||
class Functional(common_utils.TestBaseMixin): | ||
class Functional(TempDirMixin, TestBaseMixin): | ||
"""Implements test for `functinoal` modul that are performed for different devices""" | ||
def _assert_consistency(self, func, tensor, shape_only=False): | ||
tensor = tensor.to(device=self.device, dtype=self.dtype) | ||
|
||
ts_func = torch.jit.script(func) | ||
path = self.get_temp_path('func.zip') | ||
torch.jit.script(func).save(path) | ||
ts_func = torch.jit.load(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns a scripted module whose forward is the function you saved. |
||
|
||
output = func(tensor) | ||
ts_output = ts_func(tensor) | ||
if shape_only: | ||
|
@@ -565,15 +569,18 @@ def func(tensor): | |
self._assert_consistency(func, tensor) | ||
|
||
|
||
class FunctionalComplex: | ||
class FunctionalComplex(TempDirMixin, TestBaseMixin): | ||
complex_dtype = None | ||
real_dtype = None | ||
device = None | ||
|
||
def _assert_consistency(self, func, tensor, test_pseudo_complex=False): | ||
assert tensor.is_complex() | ||
tensor = tensor.to(device=self.device, dtype=self.complex_dtype) | ||
ts_func = torch.jit.script(func) | ||
|
||
path = self.get_temp_path('func.zip') | ||
torch.jit.script(func).save(path) | ||
ts_func = torch.jit.load(path) | ||
|
||
if test_pseudo_complex: | ||
tensor = torch.view_as_real(tensor) | ||
|
This file contains 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.
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.
we shouldn't remove this and continue checkingscripted_fn(input) == fn(input)
wherescripted_fn = torch.jit.script(fn)
Synced with Meghan offline:
As mentioned in the comment below,
torch.jit.load
returns a scripted module, so the current test is ok, but I think it would be nice to move this code to a helper function that takes in a function/module and returns a scripted module, with a note (see the comment below) indicating what is exactly happening.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.
I agree with the above. To clarify,
torch.jit.load
returns a scripted module with aforward
method equivalent to the function that was serialized. The caller does not notice the difference because__call__
for the module callsforward
.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.
Hmm, I have a mixed feeling about introducing another helper function.
First, this method IS the helper function used by the actual tests.
Secondly, my view is that if it's something that needs explanation with comment, keeping it along side of tests make it easier to follow the logic of tests.
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.
At the moment, you duplicate the same code logic at different places, which I think strongly suggests that we should just have one helper function that can be used in all the tests.