Skip to content

Added annotation typing to models._utils #2854

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 5 commits into from
Oct 22, 2020

Conversation

frgfm
Copy link
Contributor

@frgfm frgfm commented Oct 21, 2020

Hello folks 👋

As per #2025, annotation typing are welcome in torchvision. So I started with torchvision.models._utils in this PR. A few points of attention:

  • I picked Callable[..., nn.Module] for module constructors (passing BatchNorm2d for instance), I'm unsure whether that is good choice? any recommendation?
  • I only used black styling in class constructors for now, the rest are only annotation typing additions. Happy to change everything to black linting if you prefer.

Any feedback is welcome!

Copy link
Collaborator

@pmeier pmeier left a comment

Choose a reason for hiding this comment

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

Hey @frgfm, thanks a lot for the PR! Could you please split this PR into multiple as it is very hard to review with that many changes. I think it would be best to do a separate PR for every model / file.

I've reviewed the first three files and found a couple of things.

@frgfm frgfm force-pushed the clf-models-typing branch from c06a7dc to 159bc78 Compare October 21, 2020 15:53
@frgfm frgfm changed the title Added annotation typing to classification models Added annotation typing to models._utils Oct 21, 2020
@frgfm
Copy link
Contributor Author

frgfm commented Oct 21, 2020

@pmeier sure, I updated this PR to focus on models._utils. You got a dozen PRs incoming then haha

@frgfm frgfm requested a review from pmeier October 21, 2020 15:56
@pmeier
Copy link
Collaborator

pmeier commented Oct 21, 2020

That is alright. I'll go through them one by one.

Copy link
Collaborator

@pmeier pmeier left a comment

Choose a reason for hiding this comment

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

mypy CI is failing with

torchvision/models/_utils.py:60: error:
Signature of "forward" incompatible with supertype "ModuleDict" [override]
        def forward(self, x: torch.Tensor) ->...

@pmeier
Copy link
Collaborator

pmeier commented Oct 21, 2020

@frgfm Removing the annotation is not the way to go here. Either fix it or if it is unfixable (which might be the case here) use the correct annotations and add a #type: ignore[override] to the line.

@pmeier
Copy link
Collaborator

pmeier commented Oct 22, 2020

The test failures are related. I'll look into it and get back to you.

@pmeier
Copy link
Collaborator

pmeier commented Oct 22, 2020

@frgfm It seems torchscript is the offender here. I suspect it can't handle the type: ignore comment. Could you remove that to see if my suspicion is right?

If it is I'm not sure to move forward. We can't make torchscript and mypy happy.

@frgfm
Copy link
Contributor Author

frgfm commented Oct 22, 2020

@pmeier Yes, I figured out the same thing yesterday unfortunately.
When I remove the ignore, mypy throws the following:

error: Signature of "forward" incompatible
with supertype "ModuleDict"  [override]

Let me know which way you prefer to proceed with!

@pmeier
Copy link
Collaborator

pmeier commented Oct 22, 2020

In general torchscript > mypy holds. Thus, while the type hints are nice to have, passing tests for torchscript is a requirement. Could you simply remove the type: ignore comment and push that change? It is not important for now if mypy fails, but the unit tests need to pass.

@fmassa
Copy link
Member

fmassa commented Oct 22, 2020

Yes.

The situation with torchscript is delicate, and if we need to chose between mypy and torchscript, torchscript wins.

In general, all torchvision models already have type annotation in the forward because they all support torchscript. The difference with mypy is that torchscript assumes that

  • if no input type is passed to an argument, it's assumed to be a tensor
  • return types are inferred

So if you want to play safe, I would say to only add type annotations to the constructor of the models, or the model builder functions. This way it won't affect torchscript, but mypy won't probably be happy either.

Which means that it might be hard to enable full mypy testing to the models.

@pmeier
Copy link
Collaborator

pmeier commented Oct 22, 2020

This way it won't affect torchscript, but mypy won't probably be happy either.

Right now, we allow untyped definitions. Thus, if we simply remove the annotations from forward, mypy should be happy.

So if you want to play safe, I would say to only add type annotations to the constructor of the models, or the model builder functions.

@frgfm In that case you could simply revert the last two commits and this PR should be ready.

@frgfm frgfm force-pushed the clf-models-typing branch from f19fda4 to 2ae0057 Compare October 22, 2020 08:30
@pmeier pmeier requested a review from fmassa October 22, 2020 08:31
Copy link
Collaborator

@pmeier pmeier left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks a lot @frgfm. Note that the further process will be delayed, since we are currently preparing the next release of torchvision.

@frgfm
Copy link
Contributor Author

frgfm commented Oct 22, 2020

LGTM. Thanks a lot @frgfm. Note that the further process will be delayed, since we are currently preparing the next release of torchvision.

Should I update

from torch.jit.annotations import Dict

to

from typing import Dict

though?

@pmeier
Copy link
Collaborator

pmeier commented Oct 22, 2020

Yes that would be nice!

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

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

Thanks!

@fmassa fmassa merged commit 3756b60 into pytorch:master Oct 22, 2020
@frgfm frgfm deleted the clf-models-typing branch October 22, 2020 12:52
bryant1410 pushed a commit to bryant1410/vision-1 that referenced this pull request Nov 22, 2020
* style: Added typing in models._utils

* fix: Removed non-necessary import

* fix: Removed type annotation of forward method

* refactor: Removed un-necessary import
vfdev-5 pushed a commit to Quansight/vision that referenced this pull request Dec 4, 2020
* style: Added typing in models._utils

* fix: Removed non-necessary import

* fix: Removed type annotation of forward method

* refactor: Removed un-necessary import
@oke-aditya oke-aditya mentioned this pull request Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants