Skip to content

Commit

Permalink
hooks: torch: automatically increase recursion limit for torch >= 2.0…
Browse files Browse the repository at this point in the history
….0 (#570)

With torch 2.0.0, we seem to hit the recursion limit just by
importing the module. Therefore, if we detect torch >= 2.0.0,
ensure that recursion limit is at least 5000 by raising it if
necessary.
  • Loading branch information
rokm committed Apr 7, 2023
1 parent 988208a commit af32727
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions news/570.update.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add automatic increase of recursion limit in the ``torch`` hook to ensure that
recursion limit is at least 5000 if ``torch`` 2.0.0 or later is detected.
11 changes: 10 additions & 1 deletion src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# ------------------------------------------------------------------

from PyInstaller.utils.hooks import get_package_paths
from PyInstaller.utils.hooks import logger, get_package_paths, is_module_satisfies

datas = [(get_package_paths('torch')[1],"torch"),]

# With torch 2.0.0, PyInstaller's modulegraph analysis hits the recursion limit.
# So, unless the user has already done so, increase it automatically.
if is_module_satisfies('torch >= 2.0.0'):
import sys
new_limit = 5000
if sys.getrecursionlimit() < new_limit:
logger.info("hook-torch: raising recursion limit to %d", new_limit)
sys.setrecursionlimit(new_limit)

0 comments on commit af32727

Please sign in to comment.