Skip to content

Commit 7214e67

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: 2bb4119 Pull Request resolved: #2719
1 parent 6cfa477 commit 7214e67

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-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:
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: 45 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,52 @@ def torch_version_at_least(min_version):
377378
return is_fbcode() or compare_versions(torch.__version__, min_version) >= 0
378379

379380

381+
def _deprecated_torch_version_at_least(version_str: str) -> str:
382+
version_str_var_name = "_".join(version_str.split(".")[:2])
383+
deprecation_msg = f"TORCH_VERSION_AT_LEAST_{version_str_var_name} is deprecated and will be removed in torchao 0.14.0"
384+
return _BoolDeprecationWrapper(
385+
torch_version_at_least(version_str),
386+
deprecation_msg,
387+
)
388+
389+
390+
def _deprecated_torch_version_after(version_str: str) -> str:
391+
bool_value = is_fbcode() or version("torch") >= version_str
392+
version_str_var_name = "_".join(version_str.split(".")[:2])
393+
deprecation_msg = f"TORCH_VERSION_AFTER_{version_str_var_name} is deprecated and will be removed in torchao 0.14.0"
394+
return _BoolDeprecationWrapper(bool_value, deprecation_msg)
395+
396+
397+
class _BoolDeprecationWrapper:
398+
"""
399+
A deprecation wrapper that logs a warning when the given bool value is accessed.
400+
"""
401+
402+
def __init__(self, bool_value: bool, msg: str):
403+
self.bool_value = bool_value
404+
self.msg = msg
405+
406+
def __bool__(self):
407+
warnings.warn(self.msg)
408+
return self.bool_value
409+
410+
def __eq__(self, other):
411+
return bool(self) == bool(other)
412+
413+
380414
TORCH_VERSION_AT_LEAST_2_8 = torch_version_at_least("2.8.0")
381415
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")
416+
417+
# Deprecated
418+
TORCH_VERSION_AT_LEAST_2_6 = _deprecated_torch_version_at_least("2.6.0")
419+
TORCH_VERSION_AT_LEAST_2_5 = _deprecated_torch_version_at_least("2.5.0")
420+
TORCH_VERSION_AT_LEAST_2_4 = _deprecated_torch_version_at_least("2.4.0")
421+
TORCH_VERSION_AT_LEAST_2_3 = _deprecated_torch_version_at_least("2.3.0")
422+
TORCH_VERSION_AT_LEAST_2_2 = _deprecated_torch_version_at_least("2.2.0")
423+
TORCH_VERSION_AFTER_2_5 = _deprecated_torch_version_after("2.5.0.dev")
424+
TORCH_VERSION_AFTER_2_4 = _deprecated_torch_version_after("2.4.0.dev")
425+
TORCH_VERSION_AFTER_2_3 = _deprecated_torch_version_after("2.3.0.dev")
426+
TORCH_VERSION_AFTER_2_2 = _deprecated_torch_version_after("2.2.0.dev")
387427

388428

389429
"""
@@ -766,11 +806,6 @@ def fill_defaults(args, n, defaults_tail):
766806
return r
767807

768808

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-
774809
# Supported AMD GPU Models and their LLVM gfx Codes:
775810
#
776811
# | AMD GPU Model | LLVM gfx Code |
@@ -857,12 +892,6 @@ def ceil_div(a, b):
857892
return (a + b - 1) // b
858893

859894

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-
866895
def is_package_at_least(package_name: str, min_version: str):
867896
package_exists = importlib.util.find_spec(package_name) is not None
868897
if not package_exists:

0 commit comments

Comments
 (0)