Skip to content

Commit 71ca427

Browse files
vfdev-5pytorchmergebot
authored andcommitted
Replaced deprecated pkg_resources.packaging with packaging module (#113023)
Usage of `from pkg_resources import packaging` leads to a deprecation warning: ``` DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html ``` and in strict tests where warnings are errors, this leads to CI breaks, e.g.: pytorch/vision#8092 Replacing `pkg_resources.package` with `package` as it is now a pytorch dependency: https://github.com/pytorch/pytorch/blob/fa9045a8725214c05ae4dcec5a855820b861155e/requirements.txt#L19 Pull Request resolved: #113023 Approved by: https://github.com/Skylion007, https://github.com/malfet
1 parent f49b8e9 commit 71ca427

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

tools/dynamo/verify_dynamo.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import traceback
66
import warnings
77

8-
from pkg_resources import packaging
9-
10-
MIN_CUDA_VERSION = packaging.version.parse("11.6")
11-
MIN_ROCM_VERSION = packaging.version.parse("5.4")
8+
MIN_CUDA_VERSION = "11.6"
9+
MIN_ROCM_VERSION = "5.4"
1210
MIN_PYTHON_VERSION = (3, 8)
1311

1412

@@ -28,11 +26,12 @@ def check_python():
2826
def check_torch():
2927
import torch
3028

31-
return packaging.version.parse(torch.__version__)
29+
return torch.__version__
3230

3331

3432
# based on torch/utils/cpp_extension.py
3533
def get_cuda_version():
34+
from torch.torch_version import TorchVersion
3635
from torch.utils import cpp_extension
3736

3837
CUDA_HOME = cpp_extension._find_cuda_home()
@@ -50,10 +49,11 @@ def get_cuda_version():
5049
raise VerifyDynamoError("CUDA version not found in `nvcc --version` output")
5150

5251
cuda_str_version = cuda_version.group(1)
53-
return packaging.version.parse(cuda_str_version)
52+
return TorchVersion(cuda_str_version)
5453

5554

5655
def get_rocm_version():
56+
from torch.torch_version import TorchVersion
5757
from torch.utils import cpp_extension
5858

5959
ROCM_HOME = cpp_extension._find_rocm_home()
@@ -75,16 +75,17 @@ def get_rocm_version():
7575

7676
hip_str_version = hip_version.group(1)
7777

78-
return packaging.version.parse(hip_str_version)
78+
return TorchVersion(hip_str_version)
7979

8080

8181
def check_cuda():
8282
import torch
83+
from torch.torch_version import TorchVersion
8384

8485
if not torch.cuda.is_available() or torch.version.hip is not None:
8586
return None
8687

87-
torch_cuda_ver = packaging.version.parse(torch.version.cuda)
88+
torch_cuda_ver = TorchVersion(torch.version.cuda)
8889

8990
# check if torch cuda version matches system cuda version
9091
cuda_ver = get_cuda_version()
@@ -112,14 +113,13 @@ def check_cuda():
112113

113114
def check_rocm():
114115
import torch
116+
from torch.torch_version import TorchVersion
115117

116118
if not torch.cuda.is_available() or torch.version.hip is None:
117119
return None
118120

119121
# Extracts main ROCm version from full string
120-
torch_rocm_ver = packaging.version.parse(
121-
".".join(list(torch.version.hip.split(".")[0:2]))
122-
)
122+
torch_rocm_ver = TorchVersion(".".join(list(torch.version.hip.split(".")[0:2])))
123123

124124
# check if torch rocm version matches system rocm version
125125
rocm_ver = get_rocm_version()

torch/utils/cpp_extension.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from torch.torch_version import TorchVersion
2626

2727
from setuptools.command.build_ext import build_ext
28-
from pkg_resources import packaging # type: ignore[attr-defined]
2928

3029
IS_WINDOWS = sys.platform == 'win32'
3130
IS_MACOS = sys.platform.startswith('darwin')
@@ -402,9 +401,11 @@ def _check_cuda_version(compiler_name: str, compiler_version: TorchVersion) -> N
402401
if cuda_version is None:
403402
return
404403

405-
cuda_str_version = cuda_version.group(1)
406-
cuda_ver = packaging.version.parse(cuda_str_version)
407-
torch_cuda_version = packaging.version.parse(torch.version.cuda)
404+
cuda_ver = cuda_str_version = cuda_version.group(1)
405+
if torch.version.cuda is None:
406+
return
407+
408+
torch_cuda_version = TorchVersion(torch.version.cuda)
408409
if cuda_ver != torch_cuda_version:
409410
# major/minor attributes are only available in setuptools>=49.4.0
410411
if getattr(cuda_ver, "major", None) is None:
@@ -1133,7 +1134,7 @@ def CUDAExtension(name, sources, *args, **kwargs):
11331134
extra_compile_args_dlink += [f'-L{x}' for x in library_dirs]
11341135
extra_compile_args_dlink += [f'-l{x}' for x in dlink_libraries]
11351136

1136-
if (torch.version.cuda is not None) and packaging.version.parse(torch.version.cuda) >= packaging.version.parse('11.2'):
1137+
if (torch.version.cuda is not None) and TorchVersion(torch.version.cuda) >= '11.2':
11371138
extra_compile_args_dlink += ['-dlto'] # Device Link Time Optimization started from cuda 11.2
11381139

11391140
extra_compile_args['nvcc_dlink'] = extra_compile_args_dlink
@@ -1449,7 +1450,9 @@ def build_precompile_header(pch_cmd):
14491450
raise RuntimeError(f"Compile PreCompile Header fail, command: {pch_cmd}") from e
14501451

14511452
extra_cflags_str = listToString(extra_cflags)
1452-
extra_include_paths_str = " ".join([f'-I{include}' for include in extra_include_paths])
1453+
extra_include_paths_str = (
1454+
"" if extra_include_paths is None else " ".join([f"-I{include}" for include in extra_include_paths])
1455+
)
14531456

14541457
lib_include = os.path.join(_TORCH_PATH, 'include')
14551458
torch_include_dirs = [
@@ -2344,9 +2347,8 @@ def sanitize_flags(flags):
23442347
# --generate-dependencies-with-compile was added in CUDA 10.2.
23452348
# Compilation will work on earlier CUDA versions but header file
23462349
# dependencies are not correctly computed.
2347-
required_cuda_version = packaging.version.parse('11.0')
2348-
has_cuda_version = torch.version.cuda is not None
2349-
if has_cuda_version and packaging.version.parse(torch.version.cuda) >= required_cuda_version:
2350+
required_cuda_version = '11.0'
2351+
if torch.version.cuda is not None and TorchVersion(torch.version.cuda) >= required_cuda_version:
23502352
cuda_compile_rule.append(' depfile = $out.d')
23512353
cuda_compile_rule.append(' deps = gcc')
23522354
# Note: non-system deps with nvcc are only supported

0 commit comments

Comments
 (0)