Skip to content

Commit

Permalink
Fix access to _msvccompiler from newer distutils (pytorch#141363)
Browse files Browse the repository at this point in the history
Newer versions of distutils no longer import `_msvccompiler` upon init(on Windows platform, that was not the case on other platforms even before 74), but it's still accessible if one chooses to import it directly.
Test plan:
```
% python -c 'from setuptools import distutils; print(distutils.__version__, hasattr(distutils, "_msvccompiler")); from distutils import _msvccompiler; import setuptools; print(setuptools.__version__, _msvccompiler.__file__)'
3.10.9 False
65.5.0 /usr/local/fbcode/platform010/Python3.10.framework/Versions/3.10/lib/python3.10/site-packages/setuptools/_distutils/_msvccompiler.py
```
and
```
% python -c 'from setuptools import distutils; print(distutils.__version__, hasattr(distutils, "_msvccompiler")); from distutils import _msvccompiler; import setuptools; print(setuptools.__version__, _msvccompiler.__file__)'
3.13.0 False
75.6.0 /Users/malfet/py312-venv/lib/python3.13/site-packages/setuptools/_distutils/_msvccompiler.py
```

Gave up trying to appease the linker, so rewrote it as following function:
```python
def _get_vc_env(vc_arch: str) -> dict[str, str]:
    try:
        from setuptools import distutils  # type: ignore[import]

        return distutils._msvccompiler._get_vc_env(vc_arch)  # type: ignore[no-any-return]
    except AttributeError:
        from setuptools._distutils import _msvccompiler  #type: ignore[import]

        return _msvccompiler._get_vc_env(vc_arch)  # type: ignore[no-any-return]
```

This PR also undoes setuptools version restriction introduced by  pytorch#136489 as premise for restriction is incorrect

Fixes pytorch#141319

Pull Request resolved: pytorch#141363
Approved by: https://github.com/huydhn, https://github.com/atalman
  • Loading branch information
malfet authored and pytorchmergebot committed Nov 25, 2024
1 parent 6ad0423 commit 2398e75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 1 addition & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ numpy
psutil
pyyaml
requests
# Setuptools>=74.0.0 stopped support for directly using private funcs(_msvccompiler)
# and consolidated all compiler logic in distutils used in Pytorch build, so older
# is required until pytorch build not refactored to work for latest setuptools.
setuptools<=72.1.0
setuptools
types-dataclasses
typing-extensions>=4.10.0
sympy==1.13.1 ; python_version >= "3.9"
Expand Down
15 changes: 12 additions & 3 deletions tools/build_pytorch_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
import platform
from glob import glob

from setuptools import distutils # type: ignore[import]

from .setup_helpers.cmake import CMake, USE_NINJA
from .setup_helpers.env import check_negative_env_flag, IS_64BIT, IS_WINDOWS


def _get_vc_env(vc_arch: str) -> dict[str, str]:
try:
from setuptools import distutils # type: ignore[import]

return distutils._msvccompiler._get_vc_env(vc_arch) # type: ignore[no-any-return]
except AttributeError:
from setuptools._distutils import _msvccompiler # type: ignore[import]

return _msvccompiler._get_vc_env(vc_arch) # type: ignore[no-any-return]


def _overlay_windows_vcvars(env: dict[str, str]) -> dict[str, str]:
vc_arch = "x64" if IS_64BIT else "x86"

Expand All @@ -33,7 +42,7 @@ def _overlay_windows_vcvars(env: dict[str, str]) -> dict[str, str]:
"emulation is enabled!"
)

vc_env: dict[str, str] = distutils._msvccompiler._get_vc_env(vc_arch)
vc_env = _get_vc_env(vc_arch)
# Keys in `_get_vc_env` are always lowercase.
# We turn them into uppercase before overlaying vcvars
# because OS environ keys are always uppercase on Windows.
Expand Down
13 changes: 10 additions & 3 deletions torch/utils/cpp_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,15 @@ def _get_num_workers(verbose: bool) -> Optional[int]:
return None


def _get_vc_env(vc_arch: str) -> dict[str, str]:
try:
from setuptools import distutils
return distutils._msvccompiler._get_vc_env(vc_arch)
except AttributeError:
from setuptools._distutils import _msvccompiler
return _msvccompiler._get_vc_env(vc_arch)


def _run_ninja_build(build_directory: str, verbose: bool, error_prefix: str) -> None:
command = ['ninja', '-v']
num_workers = _get_num_workers(verbose)
Expand All @@ -2121,9 +2130,7 @@ def _run_ninja_build(build_directory: str, verbose: bool, error_prefix: str) ->

plat_name = distutils.util.get_platform()
plat_spec = PLAT_TO_VCVARS[plat_name]

vc_env = distutils._msvccompiler._get_vc_env(plat_spec)
vc_env = {k.upper(): v for k, v in vc_env.items()}
vc_env = {k.upper(): v for k, v in _get_vc_env(plat_spec).items()}
for k, v in env.items():
uk = k.upper()
if uk not in vc_env:
Expand Down

0 comments on commit 2398e75

Please sign in to comment.