Skip to content

Commit 8dcd004

Browse files
committed
Deprecate old TORCH_VERSION variables
**Summary:** This commit deprecates the following variables: ``` 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_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 we can drop support for 2.5 and before 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: 376113e Pull Request resolved: #2719
1 parent 6cfa477 commit 8dcd004

File tree

2 files changed

+99
-16
lines changed

2 files changed

+99
-16
lines changed

test/test_utils.py

Lines changed: 45 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,49 @@ 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_5 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+
)
59+
60+
deprecated_api_to_name = {
61+
TORCH_VERSION_AT_LEAST_2_5: "TORCH_VERSION_AT_LEAST_2_5",
62+
TORCH_VERSION_AT_LEAST_2_4: "TORCH_VERSION_AT_LEAST_2_4",
63+
TORCH_VERSION_AT_LEAST_2_3: "TORCH_VERSION_AT_LEAST_2_3",
64+
TORCH_VERSION_AT_LEAST_2_2: "TORCH_VERSION_AT_LEAST_2_2",
65+
TORCH_VERSION_AFTER_2_5: "TORCH_VERSION_AFTER_2_5",
66+
TORCH_VERSION_AFTER_2_4: "TORCH_VERSION_AFTER_2_4",
67+
TORCH_VERSION_AFTER_2_3: "TORCH_VERSION_AFTER_2_3",
68+
TORCH_VERSION_AFTER_2_2: "TORCH_VERSION_AFTER_2_2",
69+
}
70+
self.assertEqual(len(_warnings), 0)
71+
72+
# Accessing the boolean value should trigger deprecation warning
73+
with warnings.catch_warnings(record=True) as _warnings:
74+
for api, name in deprecated_api_to_name.items():
75+
num_warnings_before = len(_warnings)
76+
if api:
77+
pass
78+
regex = f"{name} is deprecated and will be removed"
79+
self.assertEqual(len(_warnings), num_warnings_before + 1)
80+
self.assertIn(regex, str(_warnings[-1].message))
81+
3882

3983
class TestTorchAOBaseTensor(unittest.TestCase):
4084
def test_print_arg_types(self):

torchao/utils.py

Lines changed: 54 additions & 15 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,62 @@ 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")
382410
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")
411+
412+
# Deprecated
413+
TORCH_VERSION_AT_LEAST_2_5 = _BoolDeprecationWrapper(
414+
torch_version_at_least("2.5.0"), _get_old_torch_version_deprecation_msg("2_5")
415+
)
416+
TORCH_VERSION_AT_LEAST_2_4 = _BoolDeprecationWrapper(
417+
torch_version_at_least("2.4.0"), _get_old_torch_version_deprecation_msg("2_4")
418+
)
419+
TORCH_VERSION_AT_LEAST_2_3 = _BoolDeprecationWrapper(
420+
torch_version_at_least("2.3.0"), _get_old_torch_version_deprecation_msg("2_3")
421+
)
422+
TORCH_VERSION_AT_LEAST_2_2 = _BoolDeprecationWrapper(
423+
torch_version_at_least("2.2.0"), _get_old_torch_version_deprecation_msg("2_2")
424+
)
425+
TORCH_VERSION_AFTER_2_5 = _BoolDeprecationWrapper(
426+
_torch_version_after("2.5.0.dev"), _get_torch_version_after_deprecation_msg("2_5")
427+
)
428+
TORCH_VERSION_AFTER_2_4 = _BoolDeprecationWrapper(
429+
_torch_version_after("2.4.0.dev"), _get_torch_version_after_deprecation_msg("2_4")
430+
)
431+
TORCH_VERSION_AFTER_2_3 = _BoolDeprecationWrapper(
432+
_torch_version_after("2.3.0.dev"), _get_torch_version_after_deprecation_msg("2_3")
433+
)
434+
TORCH_VERSION_AFTER_2_2 = _BoolDeprecationWrapper(
435+
_torch_version_after("2.2.0.dev"), _get_torch_version_after_deprecation_msg("2_2")
436+
)
387437

388438

389439
"""
@@ -766,11 +816,6 @@ def fill_defaults(args, n, defaults_tail):
766816
return r
767817

768818

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-
774819
# Supported AMD GPU Models and their LLVM gfx Codes:
775820
#
776821
# | AMD GPU Model | LLVM gfx Code |
@@ -857,12 +902,6 @@ def ceil_div(a, b):
857902
return (a + b - 1) // b
858903

859904

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

0 commit comments

Comments
 (0)