-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Description
π Describe the bug
# main.py
import torch
@torch.compile()
def fn():
passβ― python -VV
Python 3.9.18 | packaged by conda-forge | (main, Aug 30 2023, 03:49:32)
[GCC 12.3.0]
β― python -Werror main.py
[...]
ModuleNotFoundError: No module named 'packaging.version'
[...]
DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.htmlAs seen above, I don't have packaging installed in my env and pkg_resources is deprecated. Not only that, but starting with Python 3.12, setuptools (which supplies pkg_resources) will no longer be installed as part of a venv. Meaning, users of that will see errors when trying to use torch.compile.
The underlying issue, i.e. the non-declared dependency, was already reported in #71280 and highlighted again in #71902 (comment) but never addressed.
torch.compile hits this because it crawls torch modules at startup:
pytorch/torch/_dynamo/allowed_functions.py
Lines 155 to 158 in 7731c97
| def _allowed_function_ids() -> Dict[int, str]: | |
| """ | |
| Walk torch.* and get the ids of all the stuff in it | |
| """ |
And with that it for example hits
Line 35 in 7731c97
| from .torch_version import __version__ as __version__ |
pytorch/torch/torch_version.py
Line 89 in 7731c97
| __version__ = TorchVersion(internal_version) |
That by itself would not cause the dependency issue to show up, because we only lazy load either of the packages (#71345). However, during the crawling we also check each obj we find whether it is part of a special case list
pytorch/torch/_dynamo/allowed_functions.py
Lines 225 to 233 in 7731c97
| if obj in ( | |
| torch.func.grad, | |
| deprecated_func.grad, | |
| torch.func.vmap, | |
| deprecated_func.vmap, | |
| torch.nn.functional.triplet_margin_with_distance_loss, | |
| torch.cond, | |
| ): | |
| continue |
and that in turn materializes the version
pytorch/torch/torch_version.py
Lines 86 to 87 in 7731c97
| for cmp_method in ["__gt__", "__lt__", "__eq__", "__ge__", "__le__"]: | |
| setattr(TorchVersion, cmp_method, lambda x, y, method=cmp_method: x._cmp_wrapper(y, method)) |
pytorch/torch/torch_version.py
Lines 75 to 77 in 7731c97
| def _cmp_wrapper(self, cmp: Any, method: str) -> bool: | |
| try: | |
| return getattr(Version(self), method)(self._convert_to_version(cmp)) |
ultimately triggering the traceback above.
I think we should do one of two things:
- Add a runtime dependency on
packaging, i.e. have another go at Replaced deprecated pkg_resources.packaging with packaging moduleΒ #113023. - Vendor the relevant parts of
packaging.versionas was proposed in Import packaging.version in torch_version, if availableΒ #71902 (comment)
cc @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @zou3519 @malfet @rgommers @vfdev-5 @hauntsaninja
Versions
2.2.0.dev20231116+cpu