Skip to content

Commit

Permalink
Add a hook for torchvision.ops (#80)
Browse files Browse the repository at this point in the history
* tests: add a test for torchvision.ops.nms

Reproduces the error from pyinstaller/pyinstaller#5386.

* Add a hook for torchvision.ops

Add a hidden import of torchvision._C extension module that we
fail to pick up automatically. This extension module is required
in functions from torchvision.ops.* modules, where its absence
causes the _assert_has_ops() call to fail.

Fixes the failing test_torchvision_nms.
Fixes pyinstaller/pyinstaller#5386.
  • Loading branch information
rokm authored Dec 15, 2020
1 parent be8d7f0 commit 861fbf6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/80.new.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add hook for ``torchvision.ops`` to ensure that the required extension module (``torchvision._C``) is collected.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ------------------------------------------------------------------
# Copyright (c) 2020 PyInstaller Development Team.
#
# This file is distributed under the terms of the GNU General Public
# License (version 2.0 or later).
#
# The full license is available in LICENSE.GPL.txt, distributed with
# this software.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ------------------------------------------------------------------

# Functions from torchvision.ops.* modules require torchvision._C
# extension module, which PyInstaller fails to pick up automatically...
hiddenimports = ['torchvision._C']
33 changes: 33 additions & 0 deletions src/_pyinstaller_hooks_contrib/tests/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,36 @@ def test_pydantic(pyi_builder):
pyi_builder.test_source("""
import pydantic
""")


def torch_onedir_only(test):
def wrapped(pyi_builder):
if pyi_builder._mode != 'onedir':
pytest.skip('PyTorch tests support only onedir mode '
'due to potential distribution size.')
test(pyi_builder)
return wrapped


@importorskip('torchvision')
@torch_onedir_only
def test_torchvision_nms(pyi_builder):
pyi_builder.test_source("""
import torch
import torchvision
# boxes: Nx4 tensor (x1, y1, x2, y2)
boxes = torch.tensor([
[0.0, 0.0, 1.0, 1.0],
[0.45, 0.0, 1.0, 1.0],
])
# scores: Nx1 tensor
scores = torch.tensor([
1.0,
1.1
])
keep = torchvision.ops.nms(boxes, scores, 0.5)
# The boxes have IoU of 0.55, and the second one has a slightly
# higher score, so we expect it to be kept while the first one
# is discarded.
assert keep == 1
""")

0 comments on commit 861fbf6

Please sign in to comment.