Skip to content

Commit cbff532

Browse files
committed
Deprecate old TORCH_VERSION variables
**Summary:** This commit deprecates the following variables: ``` # Always True TORCH_VERSION_AT_LEAST_2_6 TORCH_VERSION_AT_LEAST_2_5 TORCH_VERSION_AT_LEAST_2_4 TORCH_VERSION_AT_LEAST_2_3 TORCH_VERSION_AT_LEAST_2_2 # TORCH_VERSION_AFTER* was confusing to users TORCH_VERSION_AFTER_2_5 TORCH_VERSION_AFTER_2_4 TORCH_VERSION_AFTER_2_3 TORCH_VERSION_AFTER_2_2 ``` As of this commit, the latest released version of PyTorch is 2.8, which means the oldest pytorch version we support is now 2.6 since we only support 3 of the latest releases. The next commit will remove usages of all of these variables from within torchao. **Test Plan:** ``` python test/test_utils.py -k torch_version_deprecation ``` ghstack-source-id: 2db5800 Pull Request resolved: #2719
1 parent 6cfa477 commit cbff532

File tree

2 files changed

+104
-17
lines changed

2 files changed

+104
-17
lines changed

test/test_utils.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This source code is licensed under the BSD 3-Clause license found in the
55
# LICENSE file in the root directory of this source tree.
66
import unittest
7+
import warnings
78
from unittest.mock import patch
89

910
import torch
@@ -12,7 +13,7 @@
1213
from torchao.utils import TorchAOBaseTensor, torch_version_at_least
1314

1415

15-
class TestTorchVersionAtLeast(unittest.TestCase):
16+
class TestTorchVersion(unittest.TestCase):
1617
def test_torch_version_at_least(self):
1718
test_cases = [
1819
("2.5.0a0+git9f17037", "2.5.0", True),
@@ -35,6 +36,51 @@ def test_torch_version_at_least(self):
3536
f"Failed for torch.__version__={torch_version}, comparing with {compare_version}",
3637
)
3738

39+
def test_torch_version_deprecation(self):
40+
"""
41+
Test that TORCH_VERSION_AT_LEAST_2_6 and before and TORCH_VERSION_AFTER*
42+
trigger a deprecation warning.
43+
"""
44+
# Reset deprecation warning state, otherwise we won't log warnings here
45+
warnings.resetwarnings()
46+
47+
# Importing and referencing should not trigger deprecation warning
48+
with warnings.catch_warnings(record=True) as _warnings:
49+
from torchao.utils import (
50+
TORCH_VERSION_AFTER_2_2,
51+
TORCH_VERSION_AFTER_2_3,
52+
TORCH_VERSION_AFTER_2_4,
53+
TORCH_VERSION_AFTER_2_5,
54+
TORCH_VERSION_AT_LEAST_2_2,
55+
TORCH_VERSION_AT_LEAST_2_3,
56+
TORCH_VERSION_AT_LEAST_2_4,
57+
TORCH_VERSION_AT_LEAST_2_5,
58+
TORCH_VERSION_AT_LEAST_2_6,
59+
)
60+
61+
deprecated_api_to_name = {
62+
TORCH_VERSION_AT_LEAST_2_6: "TORCH_VERSION_AT_LEAST_2_6",
63+
TORCH_VERSION_AT_LEAST_2_5: "TORCH_VERSION_AT_LEAST_2_5",
64+
TORCH_VERSION_AT_LEAST_2_4: "TORCH_VERSION_AT_LEAST_2_4",
65+
TORCH_VERSION_AT_LEAST_2_3: "TORCH_VERSION_AT_LEAST_2_3",
66+
TORCH_VERSION_AT_LEAST_2_2: "TORCH_VERSION_AT_LEAST_2_2",
67+
TORCH_VERSION_AFTER_2_5: "TORCH_VERSION_AFTER_2_5",
68+
TORCH_VERSION_AFTER_2_4: "TORCH_VERSION_AFTER_2_4",
69+
TORCH_VERSION_AFTER_2_3: "TORCH_VERSION_AFTER_2_3",
70+
TORCH_VERSION_AFTER_2_2: "TORCH_VERSION_AFTER_2_2",
71+
}
72+
self.assertEqual(len(_warnings), 0)
73+
74+
# Accessing the boolean value should trigger deprecation warning
75+
with warnings.catch_warnings(record=True) as _warnings:
76+
for api, name in deprecated_api_to_name.items():
77+
num_warnings_before = len(_warnings)
78+
if api:
79+
pass
80+
regex = f"{name} is deprecated and will be removed"
81+
self.assertEqual(len(_warnings), num_warnings_before + 1)
82+
self.assertIn(regex, str(_warnings[-1].message))
83+
3884

3985
class TestTorchAOBaseTensor(unittest.TestCase):
4086
def test_print_arg_types(self):

torchao/utils.py

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import itertools
99
import re
1010
import time
11+
import warnings
1112
from functools import reduce
1213
from importlib.metadata import version
1314
from math import gcd
@@ -377,13 +378,64 @@ def torch_version_at_least(min_version):
377378
return is_fbcode() or compare_versions(torch.__version__, min_version) >= 0
378379

379380

381+
# Deprecated, will be deleted in the future
382+
def _torch_version_after(min_version):
383+
return is_fbcode() or version("torch") >= min_version
384+
385+
386+
def _get_old_torch_version_deprecation_msg(version_str: str) -> str:
387+
return f"TORCH_VERSION_AT_LEAST_{version_str} is deprecated and will be removed in torchao 0.14.0"
388+
389+
390+
def _get_torch_version_after_deprecation_msg(version_str: str) -> str:
391+
return f"TORCH_VERSION_AFTER_{version_str} is deprecated and will be removed in torchao 0.14.0"
392+
393+
394+
class _BoolDeprecationWrapper:
395+
"""
396+
A deprecation wrapper that logs a warning when the given bool value is accessed.
397+
"""
398+
399+
def __init__(self, bool_value: bool, msg: str):
400+
self.bool_value = bool_value
401+
self.msg = msg
402+
403+
def __bool__(self):
404+
warnings.warn(self.msg)
405+
return self.bool_value
406+
407+
380408
TORCH_VERSION_AT_LEAST_2_8 = torch_version_at_least("2.8.0")
381409
TORCH_VERSION_AT_LEAST_2_7 = torch_version_at_least("2.7.0")
382-
TORCH_VERSION_AT_LEAST_2_6 = torch_version_at_least("2.6.0")
383-
TORCH_VERSION_AT_LEAST_2_5 = torch_version_at_least("2.5.0")
384-
TORCH_VERSION_AT_LEAST_2_4 = torch_version_at_least("2.4.0")
385-
TORCH_VERSION_AT_LEAST_2_3 = torch_version_at_least("2.3.0")
386-
TORCH_VERSION_AT_LEAST_2_2 = torch_version_at_least("2.2.0")
410+
411+
# Deprecated
412+
TORCH_VERSION_AT_LEAST_2_6 = _BoolDeprecationWrapper(
413+
torch_version_at_least("2.6.0"), _get_old_torch_version_deprecation_msg("2_6")
414+
)
415+
TORCH_VERSION_AT_LEAST_2_5 = _BoolDeprecationWrapper(
416+
torch_version_at_least("2.5.0"), _get_old_torch_version_deprecation_msg("2_5")
417+
)
418+
TORCH_VERSION_AT_LEAST_2_4 = _BoolDeprecationWrapper(
419+
torch_version_at_least("2.4.0"), _get_old_torch_version_deprecation_msg("2_4")
420+
)
421+
TORCH_VERSION_AT_LEAST_2_3 = _BoolDeprecationWrapper(
422+
torch_version_at_least("2.3.0"), _get_old_torch_version_deprecation_msg("2_3")
423+
)
424+
TORCH_VERSION_AT_LEAST_2_2 = _BoolDeprecationWrapper(
425+
torch_version_at_least("2.2.0"), _get_old_torch_version_deprecation_msg("2_2")
426+
)
427+
TORCH_VERSION_AFTER_2_5 = _BoolDeprecationWrapper(
428+
_torch_version_after("2.5.0.dev"), _get_torch_version_after_deprecation_msg("2_5")
429+
)
430+
TORCH_VERSION_AFTER_2_4 = _BoolDeprecationWrapper(
431+
_torch_version_after("2.4.0.dev"), _get_torch_version_after_deprecation_msg("2_4")
432+
)
433+
TORCH_VERSION_AFTER_2_3 = _BoolDeprecationWrapper(
434+
_torch_version_after("2.3.0.dev"), _get_torch_version_after_deprecation_msg("2_3")
435+
)
436+
TORCH_VERSION_AFTER_2_2 = _BoolDeprecationWrapper(
437+
_torch_version_after("2.2.0.dev"), _get_torch_version_after_deprecation_msg("2_2")
438+
)
387439

388440

389441
"""
@@ -766,11 +818,6 @@ def fill_defaults(args, n, defaults_tail):
766818
return r
767819

768820

769-
## Deprecated, will be deleted in the future
770-
def _torch_version_at_least(min_version):
771-
return is_fbcode() or version("torch") >= min_version
772-
773-
774821
# Supported AMD GPU Models and their LLVM gfx Codes:
775822
#
776823
# | AMD GPU Model | LLVM gfx Code |
@@ -857,12 +904,6 @@ def ceil_div(a, b):
857904
return (a + b - 1) // b
858905

859906

860-
TORCH_VERSION_AFTER_2_5 = _torch_version_at_least("2.5.0.dev")
861-
TORCH_VERSION_AFTER_2_4 = _torch_version_at_least("2.4.0.dev")
862-
TORCH_VERSION_AFTER_2_3 = _torch_version_at_least("2.3.0.dev")
863-
TORCH_VERSION_AFTER_2_2 = _torch_version_at_least("2.2.0.dev")
864-
865-
866907
def is_package_at_least(package_name: str, min_version: str):
867908
package_exists = importlib.util.find_spec(package_name) is not None
868909
if not package_exists:

0 commit comments

Comments
 (0)