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

Add compatibility checks for C++ extensions #2467

Merged
merged 3 commits into from
Oct 9, 2020

Conversation

fmassa
Copy link
Member

@fmassa fmassa commented Jul 13, 2020

Fixes #2148

For now, I don't print the PyTorch and torchvision in the error messages as it would require a bit more work (the torchvision version is a global variable if just imported from .version), but I believe it's already an improvement compared to what we had before.

Another thing we might want to consider doing is to add the original stack trace in the error message, but again if we want to make it work with torchscript we would probably need to wrap this on a function that returns the string.

@eellison do you see any issue with the current approach of having the _has_ops implementation be switched at runtime?

@fmassa fmassa requested review from ppwwyyxx and eellison July 13, 2020 12:41
@@ -1,6 +1,10 @@
_HAS_OPS = False
Copy link
Member Author

Choose a reason for hiding this comment

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

This can probably be removed now

except (ImportError, OSError):
pass


def _assert_has_ops():
Copy link
Member Author

Choose a reason for hiding this comment

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

I should do something similar for the video and image ops, but I'll do that in a follow-up PR once we define more precisely if this implementation is enough

@@ -23,10 +27,25 @@ def _register_extensions():
try:
_register_extensions()
_HAS_OPS = True
def _has_ops():
return True
except (ImportError, OSError):
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for addressing this!
Perhaps it's possible to define _has_ops function here that returns (bool has_ops, str err_msg). Then the exact error message from the exception can be used in _assert_has_ops

Copy link
Member Author

@fmassa fmassa Oct 6, 2020

Choose a reason for hiding this comment

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

Thanks for the review, I forgot to answer this.

I think this should be possible to be done, let me try this.

Given that torchscript doesn't support globals, I think the only way we could have this is to have pre-canned error messages that are used to construct the _has_ops function.

Something like

try:
    _register_extensions()
    ...
except (ImportError, OSError) as err:
    err_str = repr(err)
    if err_type_1 in err_str:
        def _has_ops():
            ...
    elif err_type_2 in err_str:
        ...

which makes it a bit less readable, and kinds of defeats the purpose of having the error message being returned because we would only be able to nicely print a few types of errors (the ones we hard-coded).

But maybe I'm missing something here?

@cpuhrsch
Copy link
Contributor

Since torchvision is using TORCH_LIBRARY we should probably also bring this to attention of @jamesr66a for torchbind.

@codecov
Copy link

codecov bot commented Oct 6, 2020

Codecov Report

Merging #2467 into master will increase coverage by 0.02%.
The diff coverage is 84.21%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2467      +/-   ##
==========================================
+ Coverage   73.12%   73.14%   +0.02%     
==========================================
  Files          96       96              
  Lines        8316     8335      +19     
  Branches     1293     1294       +1     
==========================================
+ Hits         6081     6097      +16     
- Misses       1838     1840       +2     
- Partials      397      398       +1     
Impacted Files Coverage Δ
torchvision/extension.py 68.18% <57.14%> (-2.09%) ⬇️
torchvision/ops/boxes.py 93.40% <100.00%> (+0.14%) ⬆️
torchvision/ops/deform_conv.py 70.96% <100.00%> (+0.96%) ⬆️
torchvision/ops/ps_roi_align.py 73.33% <100.00%> (+1.90%) ⬆️
torchvision/ops/ps_roi_pool.py 75.00% <100.00%> (+1.92%) ⬆️
torchvision/ops/roi_align.py 70.96% <100.00%> (+2.00%) ⬆️
torchvision/ops/roi_pool.py 75.00% <100.00%> (+1.92%) ⬆️

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 a9e4cea...135ba12. Read the comment docs.

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

LGTM !

@fmassa fmassa merged commit b217165 into pytorch:master Oct 9, 2020
@fmassa fmassa deleted the check-compatible-cpp branch October 9, 2020 10:50
bryant1410 pushed a commit to bryant1410/vision-1 that referenced this pull request Nov 22, 2020
* Add compatibility checks for C++ extensions

* Fix lint
vfdev-5 pushed a commit to Quansight/vision that referenced this pull request Dec 4, 2020
* Add compatibility checks for C++ extensions

* Fix lint
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.

Improve error message when incompatible torchvision / PyTorch are used
4 participants