Force JIT to do type inference even when mypy annotated #39670
Labels
module: typing
Related to mypy type annotations
oncall: jit
Add this issue/PR to JIT oncall triage queue
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
In #38211 I discovered a very interesting aspect of TorchScript's type system, which is that it is staged. Suppose you have a constructor like this:
In a normal type system, you would have to declare weight/bias as
Optional[Tensor]
, as it is not known at compile time what type they are (you must know the type of affine). However, in TorchScript, the type of the module is not ascribed until after the module has been constructed, at which point we do know what the type ofaffine
is. So if you don't annotate weight/bias, TorchScript will properly inferNone
orTensor
as their types.Here's the problem: if you annotate weight/bias for mypy, TorchScript will directly use this annotation, and no longer carry out its own type inference. And this in turn may cause code that previously typechecked in TorchScript to stop typechecking. This means that adding most general mypy signatures for modules can cause TorchScript to stop working.
There should be some way to force TorchScript to do type inference, even if there is an annotation. The relevant code is pretty easy to change, so the big question is what the syntax for triggering this type inference should be.
cc @gmagogsfm @ezyang @malfet @rgommers @xuzhao9 @gramster @suo
The text was updated successfully, but these errors were encountered: