Skip to content

torch.compile introduces runtime dependency on setuptools or packagingΒ #113940

@pmeier

Description

@pmeier

πŸ› 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.html

As 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:

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

from .torch_version import __version__ as __version__

__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

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

for cmp_method in ["__gt__", "__lt__", "__eq__", "__ge__", "__le__"]:
setattr(TorchVersion, cmp_method, lambda x, y, method=cmp_method: x._cmp_wrapper(y, method))

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:

  1. Add a runtime dependency on packaging, i.e. have another go at Replaced deprecated pkg_resources.packaging with packaging moduleΒ #113023.
  2. Vendor the relevant parts of packaging.version as 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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions