Skip to content

Commit

Permalink
Add userbase library dir to windows dll search path (#125684)
Browse files Browse the repository at this point in the history
Fixes #125109 which is a regression introduced by pytorch/builder#1467 that adds dynamic dependency to mkl, which if installed in the user-dir is placed into `sysconfig.sysconfig.get_config_var("userbase") / "Library" / "bin"`

Fix this one, but adding `userbase` folder to the DLL search path

Testing before this fix:
```
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr  9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Administrator\AppData\Roaming\Python\Python312\site-packages\torch\__init__.py", line 141, in <module>
    raise err
OSError: [WinError 126] The specified module could not be found. Error loading "C:\Users\Administrator\AppData\Roaming\Python\Python312\site-packages\torch\lib\shm.dll" or one of its dependencies.
>>> exit()
```

After:
```
c:\Program Files\Python312>python
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr  9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> exit()
```
Co-authored-by: Nikita Shulga <2453524+malfet@users.noreply.github.com>
Pull Request resolved: #125684
Approved by: https://github.com/malfet
  • Loading branch information
atalman authored and pytorchmergebot committed May 7, 2024
1 parent 7864d28 commit fdfef75
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion torch/__init__.py
Expand Up @@ -66,9 +66,11 @@ def _running_with_deploy():
################################################################################

if sys.platform == 'win32':
import sysconfig
pfiles_path = os.getenv('ProgramFiles', 'C:\\Program Files')
py_dll_path = os.path.join(sys.exec_prefix, 'Library', 'bin')
th_dll_path = os.path.join(os.path.dirname(__file__), 'lib')
usebase_path = os.path.join(sysconfig.get_config_var("userbase"), 'Library', 'bin')

# When users create a virtualenv that inherits the base environment,
# we will need to add the corresponding library directory into
Expand All @@ -79,7 +81,7 @@ def _running_with_deploy():
else:
base_py_dll_path = ''

dll_paths = list(filter(os.path.exists, [th_dll_path, py_dll_path, base_py_dll_path]))
dll_paths = list(filter(os.path.exists, [th_dll_path, py_dll_path, base_py_dll_path, usebase_path]))

if all(not os.path.exists(os.path.join(p, 'nvToolsExt64_1.dll')) for p in dll_paths):
nvtoolsext_dll_path = os.path.join(
Expand Down

3 comments on commit fdfef75

@hasspark
Copy link

Choose a reason for hiding this comment

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

Thanks a lot

@haohaoOwO
Copy link

Choose a reason for hiding this comment

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

Thanks a lot

@MagaDaur
Copy link

@MagaDaur MagaDaur commented on fdfef75 May 18, 2024

Choose a reason for hiding this comment

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

top g, thanks

Please sign in to comment.