|
3 | 3 | # |
4 | 4 | # This source code is licensed under the BSD 3-Clause license found in the |
5 | 5 | # LICENSE file in the root directory of this source tree. |
| 6 | +import sys |
| 7 | +import warnings |
| 8 | + |
6 | 9 | import pytest |
7 | 10 | import torch |
8 | 11 |
|
@@ -165,3 +168,39 @@ def test_uintx_model_size(dtype): |
165 | 168 | quantize_(linear[0], UIntXWeightOnlyConfig(dtype)) |
166 | 169 | quantized_size = get_model_size_in_bytes(linear) |
167 | 170 | assert bf16_size * _dtype_to_ratio[dtype] == quantized_size |
| 171 | + |
| 172 | + |
| 173 | +def test_uintx_api_deprecation(): |
| 174 | + """ |
| 175 | + Test that deprecated uintx APIs trigger deprecation warnings on import. |
| 176 | + TODO: Remove this test once the deprecated APIs have been removed. |
| 177 | + """ |
| 178 | + deprecated_apis = [ |
| 179 | + ( |
| 180 | + "Int8DynamicActInt4WeightCPULayout", |
| 181 | + "torchao.dtypes.uintx.dyn_int8_act_int4_wei_cpu_layout", |
| 182 | + ), |
| 183 | + ("CutlassInt4PackedLayout", "torchao.dtypes.uintx.cutlass_int4_packed_layout"), |
| 184 | + ("BlockSparseLayout", "torchao.dtypes.uintx.block_sparse_layout"), |
| 185 | + ] |
| 186 | + |
| 187 | + for api_name, module_path in deprecated_apis: |
| 188 | + # Clear the cache to force re-importing and trigger the warning again |
| 189 | + modules_to_clear = [module_path, "torchao.dtypes"] |
| 190 | + for mod in modules_to_clear: |
| 191 | + if mod in sys.modules: |
| 192 | + del sys.modules[mod] |
| 193 | + |
| 194 | + with warnings.catch_warnings(record=True) as w: |
| 195 | + warnings.simplefilter("always") # Ensure all warnings are captured |
| 196 | + |
| 197 | + # Dynamically import the deprecated API |
| 198 | + exec(f"from torchao.dtypes import {api_name}") |
| 199 | + |
| 200 | + assert any( |
| 201 | + issubclass(warning.category, DeprecationWarning) |
| 202 | + and api_name in str(warning.message) |
| 203 | + for warning in w |
| 204 | + ), ( |
| 205 | + f"Expected deprecation warning for {api_name}, got: {[str(warning.message) for warning in w]}" |
| 206 | + ) |
0 commit comments