[JIT] Disallow plain List type annotation without arg#38130
[JIT] Disallow plain List type annotation without arg#38130SplitInfinity wants to merge 1 commit intopytorch:masterfrom
Conversation
|
Note that an annotation of |
suo
left a comment
There was a problem hiding this comment.
Nice! Can we add a test that asserts that a failure is properly thrown when we hit this condition? In particular, I want to see if the string frontend is affected by this failure as well.
Summary: This commit detects and prohibits the case in which `typing.List` is used as an annotation without a type argument (i.e. `typing.List[T]`). At present, `typing.List` is always assumed to have one argument, and when it is used without one, `typing.List.__args__[0]` is nonempty and set to some `typing.TypeVar` instance, which has no JIT type equivalent. Consequently, trying to convert `typing.List` to a JIT type results in a `c10::ListType` with `nullptr` for its element type, which can cause a segmentation fault. This is fixed by returning a `ListType` from `jit.annotations.try_ann_to_type` only if the element type is converted successfully to a JIT type and returning `None` otherwise. Test Plan: I ran the code from the issue that reported this segmentation fault, and also added some unit tests.
|
I think both are affected because the problem is in the |
| a: List = x.tolist() | ||
| return a | ||
|
|
||
| with self.assertRaisesRegex(RuntimeError, r"Unknown type name"): |
There was a problem hiding this comment.
self.checkScriptRaisesRegex is a useful utility for doing this; it will check eager/source FE/ast FE and compare results.
There was a problem hiding this comment.
No exception is raised in eager though, and there is no way to opt out of asserting that eager raises the exception too.
|
Also, should we wait until someone tries to use an |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@SplitInfinity has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
facebook-github-bot
left a comment
There was a problem hiding this comment.
@SplitInfinity has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@SplitInfinity merged this pull request in 1456515. |
**Summary** This commit extends the work done in #38130 and disallows plain Python3-style `List` type annotations. **Test Plan** This commit extends `TestList.test_no_element_type_annotation` to the Python3-style type annotation. [ghstack-poisoned]
Summary: Pull Request resolved: #44584 **Summary** This commit extends the work done in #38130 and disallows plain Python3-style `List` type annotations. **Test Plan** This commit extends `TestList.test_no_element_type_annotation` to the Python3-style type annotation. Test Plan: Imported from OSS Reviewed By: gmagogsfm Differential Revision: D23721514 Pulled By: SplitInfinity fbshipit-source-id: 48957868286f44ab6d5bf5e1bf97f0a4ebf955df
Summary: Pull Request resolved: #44584 **Summary** This commit extends the work done in #38130 and disallows plain Python3-style `List` type annotations. **Test Plan** This commit extends `TestList.test_no_element_type_annotation` to the Python3-style type annotation. Test Plan: Imported from OSS Reviewed By: gmagogsfm Differential Revision: D23721514 Pulled By: SplitInfinity fbshipit-source-id: 48957868286f44ab6d5bf5e1bf97f0a4ebf955df
Summary: **Summary** This commit detects and prohibits the case in which `typing.List` is used as an annotation without a type argument (i.e. `typing.List[T]`). At present, `typing.List` is always assumed to have one argument, and when it is used without one, `typing.List.__args__[0]` is nonempty and set to some `typing.TypeVar` instance, which has no JIT type equivalent. Consequently, trying to convert `typing.List` to a JIT type results in a `c10::ListType` with `nullptr` for its element type, which can cause a segmentation fault. This is fixed by returning a `ListType` from `jit.annotations.try_ann_to_type` only if the element type is converted successfully to a JIT type and returning `None` otherwise. **Test Plan** I ran the code from the issue (pytorch#37530) that reported this problem and also ran some unit tests. *Before* ``` $ python3 segfault.py Segmentation fault (core dumped) ``` *After* ``` $ python3 segfault.py Traceback (most recent call last): ... RuntimeError: Unknown type name 'List': File "segfault.py", line 9 classmethod def cat(cls, box_lists: List): ~~~~ <--- HERE return cls(torch.cat([x for x in box_lists])) 'Boxes.cat' is being compiled since it was called from 'Boxes' File "segfault.py", line 13 def f(t: torch.Tensor): b = Boxes(t) ~~~~~ <--- HERE c = Boxes(torch.tensor([3, 4])) return Boxes.cat([b, c]) 'Boxes' is being compiled since it was called from 'f' File "segfault.py", line 13 def f(t: torch.Tensor): b = Boxes(t) ~~~~~~~~~~~ <--- HERE c = Boxes(torch.tensor([3, 4])) return Boxes.cat([b, c]) ``` **Fixes** This pull request fixes pytorch#37530. Pull Request resolved: pytorch#38130 Differential Revision: D21485284 Pulled By: SplitInfinity fbshipit-source-id: 9b51ef6340485a24c8b7cfb85832d4668b8ac51a
Summary: Pull Request resolved: pytorch#44584 **Summary** This commit extends the work done in pytorch#38130 and disallows plain Python3-style `List` type annotations. **Test Plan** This commit extends `TestList.test_no_element_type_annotation` to the Python3-style type annotation. Test Plan: Imported from OSS Reviewed By: gmagogsfm Differential Revision: D23721514 Pulled By: SplitInfinity fbshipit-source-id: 48957868286f44ab6d5bf5e1bf97f0a4ebf955df
Summary
This commit detects and prohibits the case in which
typing.Listisused as an annotation without a type argument (i.e.
typing.List[T]).At present,
typing.Listis always assumed to have one argument, andwhen it is used without one,
typing.List.__args__[0]is nonempty andset to some
typing.TypeVarinstance, which has no JIT type equivalent.Consequently, trying to convert
typing.Listto a JIT type results ina
c10::ListTypewithnullptrfor its element type, which can causea segmentation fault.
This is fixed by returning a
ListTypefromjit.annotations.try_ann_to_typeonly if the element type is convertedsuccessfully to a JIT type and returning
Noneotherwise.Test Plan
I ran the code from the issue (#37530) that reported this problem and also ran some unit tests.
Before
After
Fixes
This pull request fixes #37530.