Skip to content

Conversation

@SplitInfinity
Copy link

@SplitInfinity SplitInfinity commented Oct 7, 2020

Stack from ghstack:

Summary
In try_ann_to_type, if an annotation has an attribute named
__torch_script_class__, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a __torch_script_class__
attribute on the class object.

Test Plan
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

Fixes
This commit fixes #45860.

Differential Revision: D24310027

**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

[ghstack-poisoned]
@SplitInfinity SplitInfinity requested a review from apaszke as a code owner October 7, 2020 01:35
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Oct 7, 2020
SplitInfinity pushed a commit that referenced this pull request Oct 7, 2020
**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

ghstack-source-id: d90df83
Pull Request resolved: #45940
return EnumType(_qualified_name(ann), get_enum_value_type(ann, loc), list(ann))
if inspect.isclass(ann):
if hasattr(ann, "__torch_script_class__"):
if hasattr(ann, "__torch_script_class__") and len(ann.mro()) <= 2:
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this break torch.no_grad ? I think it would be more intuitive to switch to storing map of {python class pointer, jit class} instead of annotating the class

Copy link
Author

@SplitInfinity SplitInfinity Oct 7, 2020

Choose a reason for hiding this comment

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

We have a map from qualified_name -> cls (Python class, not JIT type) in _state.py for all classes that have truly been scripted; what if we changed this to

Suggested change
if hasattr(ann, "__torch_script_class__") and len(ann.mro()) <= 2:
if torch.jit._state._get_script_class(_qualified_name(ann)):

In the example that motivated this fix, A would be in _state._script_classes, but B would not.

Copy link
Contributor

Choose a reason for hiding this comment

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

yea sgtm

Copy link
Author

@SplitInfinity SplitInfinity Oct 7, 2020

Choose a reason for hiding this comment

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

Although I realize now that inheritance is prohibited if the class is scripted directly, but allowed if it is scripted implicitly (e.g. through try_ann_to_type) and doesn't use any features of inheritance (like super()). This seems inconsistent and awkward, but as you pointed out, no_grad relies on this. 😞

**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a `__torch_script_class__`
attribute on the class object.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this pull request Oct 7, 2020
**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a `__torch_script_class__`
attribute on the class object.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

ghstack-source-id: 90cfb48
Pull Request resolved: #45940
@codecov
Copy link

codecov bot commented Oct 7, 2020

Codecov Report

Merging #45940 into gh/splitinfinity/58/base will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@                     Coverage Diff                      @@
##           gh/splitinfinity/58/base   #45940      +/-   ##
============================================================
+ Coverage                     68.25%   68.27%   +0.02%     
============================================================
  Files                           410      410              
  Lines                         53611    53248     -363     
============================================================
- Hits                          36593    36357     -236     
+ Misses                        17018    16891     -127     
Impacted Files Coverage Δ
torch/jit/annotations.py 92.23% <100.00%> (+0.45%) ⬆️
torch/testing/_internal/jit_utils.py 89.71% <100.00%> (+<0.01%) ⬆️
torch/testing/_internal/te_utils.py 0.00% <0.00%> (-82.15%) ⬇️
torch/nn/modules/channelshuffle.py 0.00% <0.00%> (-63.64%) ⬇️
torch/multiprocessing/spawn.py 79.26% <0.00%> (-6.60%) ⬇️
torch/fx/experimental/GraphManipulation.py 94.28% <0.00%> (-5.72%) ⬇️
torch/utils/data/_utils/worker.py 21.49% <0.00%> (-2.83%) ⬇️
torch/nn/quantized/modules/activation.py 88.88% <0.00%> (-1.66%) ⬇️
torch/autograd/__init__.py 84.28% <0.00%> (-1.43%) ⬇️
torch/distributions/binomial.py 94.93% <0.00%> (-1.01%) ⬇️
... and 61 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc846db...f401180. Read the comment docs.

Copy link
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

Looks good, two other invocations we need to update i think

**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a `__torch_script_class__`
attribute on the class object.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this pull request Oct 14, 2020
**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a `__torch_script_class__`
attribute on the class object.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

ghstack-source-id: 6fe19a4
Pull Request resolved: #45940
@dr-ci
Copy link

dr-ci bot commented Oct 14, 2020

💊 CI failures summary and remediations

As of commit f401180 (more details on the Dr. CI page):


  • 1/1 failures introduced in this PR---

1 failure not recognized by patterns:

Job Step Action
CircleCI pytorch_linux_xenial_py3_6_gcc5_4_test Run tests 🔁 rerun
This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 1 times.

@SplitInfinity
Copy link
Author

The test failure is a mypy type check failure in FX.

Copy link
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

LGTM

@facebook-github-bot
Copy link
Contributor

@SplitInfinity merged this pull request in 75bf5f2.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@SplitInfinity merged this pull request in 75bf5f2.

SplitInfinity pushed a commit that referenced this pull request Oct 15, 2020
**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a `__torch_script_class__`
attribute on the class object.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

ghstack-source-id: 6fe19a4
Pull Request resolved: #45940
malfet pushed a commit that referenced this pull request Oct 15, 2020
**Summary**
In `try_ann_to_type`, if an annotation has an attribute named
`__torch_script_class__`, it is assumed to be a TorchScript class that
has already been scripted. However, if it is a class that extends
another class, this code path causes a crash because it looks up the
JIT type for the class by name in the compilation unit. This JIT type
obviously cannot exist because inheritance is not supported.

This commit fixes this by looking up the qualified name of a class
in torch.jit._state._script_class in order to ascertain whether it has
already been scripted (instead of looking for a `__torch_script_class__`
attribute on the class object.

**Test Plan**
This commit adds a unit test consisting of the code sample from the
issue that reported this problem.

**Fixes**
This commit fixes #45860.

ghstack-source-id: 6fe19a4
Pull Request resolved: #45940
@facebook-github-bot facebook-github-bot deleted the gh/splitinfinity/58/head branch October 18, 2020 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants