Skip to content
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

[WIP] Add TorchScript compatibility for LightningModules #1952

Closed
wants to merge 2 commits into from
Closed

[WIP] Add TorchScript compatibility for LightningModules #1952

wants to merge 2 commits into from

Conversation

neighthan
Copy link
Contributor

@neighthan neighthan commented May 26, 2020

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
    • The issue is here (but there's been no discussion / approval)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
    • Didn't seem necessary
  • Did you write any new necessary tests?
    • This should probably be done if we want TorchScript to work going forward; if that's not a priority, then no tests are needed (this fix could be merged without necessarily committing to long-term TorchScript support)
  • If you made a notable change (that affects users), did you update the CHANGELOG?
    • Didn't seem necessary

What does this PR do?

Fixes #1951.

@mergify mergify bot requested a review from a team May 26, 2020 03:16
@neighthan neighthan changed the title Improve _device type annotations. [WIP] Improve _device type annotations. May 26, 2020
@neighthan
Copy link
Contributor Author

I didn't test this enough; I think this isn't complete yet.

@neighthan neighthan changed the title [WIP] Improve _device type annotations. [WIP] Add TorchScript compatibility for LightningModules. May 26, 2020
@neighthan neighthan marked this pull request as draft May 26, 2020 03:44
@codecov
Copy link

codecov bot commented May 26, 2020

Codecov Report

Merging #1952 into master will decrease coverage by 0%.
The diff coverage is 83%.

@@          Coverage Diff           @@
##           master   #1952   +/-   ##
======================================
- Coverage      88%     88%   -0%     
======================================
  Files          74      74           
  Lines        4650    4652    +2     
======================================
+ Hits         4070    4071    +1     
- Misses        580     581    +1     

_device: ...
_dtype: Union[str, torch.dtype]
_device: torch.device
_dtype: torch.dtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why shouldn't I be omit strings here?


@property
def dtype(self) -> Union[str, torch.dtype]:
def dtype(self) -> torch.dtype:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for strings

@@ -17,7 +17,7 @@ def dtype(self, new_dtype: Union[str, torch.dtype]):
raise RuntimeError('Cannot set the dtype explicitly. Please use module.to(new_dtype).')

@property
def device(self) -> Union[str, torch.device]:
def device(self) -> torch.device:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again strings :D

@@ -125,6 +125,8 @@ def type(self, dst_type: Union[str, torch.dtype]) -> torch.nn.Module:
Returns:
Module: self
"""
if isinstance(dst_type, str):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this is working for all the types. Even if it is. Can't we maybe find a better way to do this? For devices, you can simply do device = torch.device(device_str). I know, this does not work for dtypes, but maybe something similar? But in general: why doesn't strings work with torchscript?

@mergify mergify bot requested a review from a team May 26, 2020 05:36
@awaelchli
Copy link
Contributor

It would be good to have a test that torch script can compile the template we use in tests, because the device properties were added only recently and it seems that it broke the compatibility.

@Borda
Copy link
Member

Borda commented Jun 9, 2020

@neighthan how is it going here? 🐰

@Borda Borda changed the title [WIP] Add TorchScript compatibility for LightningModules. [WIP] Add TorchScript compatibility for LightningModules Jun 9, 2020
@Borda Borda added the feature Is an improvement or enhancement label Jun 9, 2020
@Borda Borda added this to the 0.9.0 milestone Jun 9, 2020
@neighthan
Copy link
Contributor Author

Could you add the help-wanted label here? The project where I was hoping to use TorchScript didn't benefit from it, so writing the tests that should be in this PR / checking the issues with str dtypes is low-priority for me right now.

@awaelchli awaelchli added the help wanted Open to be worked on label Jun 27, 2020
@lezwon
Copy link
Contributor

lezwon commented Jul 20, 2020

@awaelchli @Borda I'd like to work on this issue. Would require a little help on what would be a good approach to fix this though :]

@justusschock
Copy link
Member

@lezwon It's basically adding the missing tests (like you did with onnx) and then checking the dtype-str issue. The rest should be fine from what I saw :)

@lezwon
Copy link
Contributor

lezwon commented Jul 20, 2020

@justusschock cool :) I'll get started with it then. 👍

@awaelchli awaelchli modified the milestones: 0.9.0, 0.9.x Aug 8, 2020
@neighthan
Copy link
Contributor Author

@lezwon this is resolved in your PR here, right? I'll close this now

@neighthan neighthan closed this Aug 18, 2020
@neighthan neighthan deleted the feature/1951_torchscript branch August 18, 2020 21:09
@Borda Borda modified the milestones: 0.9.x, 0.9.0 Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement help wanted Open to be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support torch.jit.script on LightningModules
5 participants