8000 Add path used by pip's build isolation procedure to DLL search by Luthaf · Pull Request #131340 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Add path used by pip's build isolation procedure to DLL search #131340

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

Closed
wants to merge 1 commit into from

Conversation

Luthaf
Copy link
Contributor
@Luthaf Luthaf commented Jul 22, 2024

Without this, trying to import torch in a downstream setup.py file would result in

The specified module could not be found. Error loading "C:\...\pip-build-env-himl3xh3\normal\Lib\site-packages\torch\lib\shm.dll" or one of its dependencies."

This seems to be because pip does not use a full virtualenv for build isolation, instead creating directories and manually adding them to sys.path. The same issue does not seem to apply when using python -m build.


To reproduce, you can create a directory with two files:

# pyproject.toml
[project]
name = "windows-torch-mkl-pip"
version = "0.0.0"

[build-system]
requires = [
    "setuptools",
    "torch"
]
# setup.py
from setuptools import setup

import torch


setup()

Then, trying to build a wheel with pip install . will give some output similar to:

Installing collected packages: tbb, mpmath, intel-openmp, typing-extensions, sympy, numpy, networkx, mkl, MarkupSafe, fsspec, filelock, jinja2, torch
      Creating C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\normal\Scripts
    Successfully installed MarkupSafe-2.1.5 filelock-3.14.0 fsspec-2024.6.0 inte
8000
l-openmp-2021.4.0 jinja2-3.1.4 mkl-2021.4.0 mpmath-1.3.0 networkx-3.3 numpy-1.26.4 sympy-1.12.1 tbb-2021.12.0 torch-2.3.1+cpu typing-extensions-4.12.2
    Created temporary directory: C:\Users\runneradmin\AppData\Local\Temp\pip-modern-metadata-ascqww5w
    Preparing metadata (pyproject.toml): started
    Running command Preparing metadata (pyproject.toml)
    Traceback (most recent call last):
      File "C:\Users\runneradmin\AppData\Local\Temp\cibw-run-7yztij8w\cp312-win_amd64\build\venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
        main()
      File "C:\Users\runneradmin\AppData\Local\Temp\cibw-run-7yztij8w\cp312-win_amd64\build\venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\runneradmin\AppData\Local\Temp\cibw-run-7yztij8w\cp312-win_amd64\build\venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 149, in prepare_metadata_for_build_wheel
        return hook(metadata_directory, config_settings)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\overlay\Lib\site-packages\setuptools\build_meta.py", line 366, in prepare_metadata_for_build_wheel
        self.run_setup()
      File "C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setup
        exec(code, locals())
      File "<string>", line 295, in <module>
      File "C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\normal\Lib\site-packages\torch\__init__.py", line 143, in <module>
        raise err
    OSError: [WinError 126] The specified module could not be found. Error loading "C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\normal\Lib\site-packages\torch\lib\shm.dll" or one of its dependencies.
    error: subprocess-exited-with-error
    
    Preparing metadata (pyproject.toml) did not run successfully.
    exit code: 1
    
    See above for output.

Torch is properly installed in C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\normal\Lib\site-packages\torch\ and all the mkl libraries are in C:\Users\runneradmin\AppData\Local\Temp\pip-build-env-himl3xh3\normal\Library\bin, but this directory is not covered by existing DLL paths.


This is similar to #125109, and the fix is similar to #125684. Ping @atalman and @malfet since you fixed & reviewed the previous similar fix.

cc @malfet @seemethere @peterjc123 @mszhanyi @skyline75489 @nbcsm @vladimir-aubrecht @iremyux @Blackhex @cristianPanaite

Without this, trying to `import torch` in a downstream `setup.py` file
would result in

```
The specified module could not be found. Error loading "C:\...\pip-build-env-himl3xh3\normal\Lib\site-packages\torch\lib\shm.dll" or one of its dependencies."
```

This seems to be because pip does not use a full virtualenv for build
isolation, instead creating directories and manually adding them to
`sys.path`
Copy link
pytorch-bot bot commented Jul 22, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/131340

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 1 Cancelled Job

As of commit f2b49a9 with merge base 0246b28 (image):

NEW FAILURE - The following job has failed:

CANCELLED JOB - The following job was cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@Luthaf Luthaf mentioned this pull request Jul 22, 2024
< 8000 span style="transform:rotate(-90deg); width:12px; height:12px; display: none"> 4 tasks
@Luthaf
Copy link
Contributor Author
Luthaf commented Jul 22, 2024

For completeness, we are using import torch in setup.py to access torch.utils.cmake_prefix and torch.__version__.

@malfet malfet added module: build Build system issues module: windows Windows support for PyTorch triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jul 22, 2024
# torch & mkl in a custom path that's included in sys.path but does not match
# any of the other DLLs path above, so we need to manually add it here.
mkl_dll_path = os.path.join(
os.path.dirname(__file__), "..", "..", "..", "Library", "bin"
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels wrong, I would prefer it to be expressed as sysconfig.get_config_var("XYZ") + something else, because during the normal installation this will add a search path to some random folder, that could be used to mess with some existing installs

8000 Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had a look through what's in sysconfig.get_config_vars(), but this was not here. I think this is really a specific of how pip does build isolation. Another solution might be to explicitly search for mkl distribution and use this path, but I could not find a way to find it other than walking through sys.path and looking for mkl.dist-info (import mkl fails since the package does not install any python files)

@malfet malfet self-assigned this Jul 22, 2024
@Luthaf
Copy link
Contributor Author
Luthaf commented Aug 14, 2024

@malfet anything I can do to help merge this?

Copy link
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Oct 13, 2024
@Luthaf
Copy link
Contributor Author
Luthaf commented Oct 14, 2024

@malfet could you please remove the stale label & let me know how I can help to get this through?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: build Build system issues module: windows Windows support for PyTorch open source Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

36DD
3 participants
0