|
| 1 | +# |
| 2 | +# Copyright (c) 2023 Apple Inc. All rights reserved. |
| 3 | +# Provided subject to the LICENSE file in the top level directory. |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | +import unittest |
| 8 | +import warnings |
| 9 | + |
| 10 | + |
| 11 | +class TestMPSDeprecation(unittest.TestCase): |
| 12 | + """Tests that MPS backend deprecation warnings are properly emitted.""" |
| 13 | + |
| 14 | + def setUp(self) -> None: |
| 15 | + # Clear cached MPS modules so module-level warnings fire again on each test |
| 16 | + for key in [ |
| 17 | + k for k in sys.modules if k.startswith("executorch.backends.apple.mps") |
| 18 | + ]: |
| 19 | + del sys.modules[key] |
| 20 | + |
| 21 | + def test_mps_package_import_warns(self) -> None: |
| 22 | + with warnings.catch_warnings(record=True) as w: |
| 23 | + warnings.simplefilter("always") |
| 24 | + import executorch.backends.apple.mps # noqa: F401 |
| 25 | + |
| 26 | + future_warnings = [ |
| 27 | + warning |
| 28 | + for warning in w |
| 29 | + if issubclass(warning.category, FutureWarning) |
| 30 | + and "deprecated" in str(warning.message).lower() |
| 31 | + and "mps" in str(warning.message).lower() |
| 32 | + ] |
| 33 | + self.assertTrue( |
| 34 | + len(future_warnings) > 0, |
| 35 | + "Importing executorch.backends.apple.mps should emit a FutureWarning", |
| 36 | + ) |
| 37 | + |
| 38 | + def test_mps_partition_package_import_warns(self) -> None: |
| 39 | + with warnings.catch_warnings(record=True) as w: |
| 40 | + warnings.simplefilter("always") |
| 41 | + import executorch.backends.apple.mps.partition # noqa: F401 |
| 42 | + |
| 43 | + future_warnings = [ |
| 44 | + warning |
| 45 | + for warning in w |
| 46 | + if issubclass(warning.category, FutureWarning) |
| 47 | + and "deprecated" in str(warning.message).lower() |
| 48 | + and "mps" in str(warning.message).lower() |
| 49 | + ] |
| 50 | + self.assertTrue( |
| 51 | + len(future_warnings) > 0, |
| 52 | + "Importing executorch.backends.apple.mps.partition should emit a FutureWarning", |
| 53 | + ) |
| 54 | + |
| 55 | + def test_mps_backend_class_warns(self) -> None: |
| 56 | + with warnings.catch_warnings(record=True) as w: |
| 57 | + warnings.simplefilter("always") |
| 58 | + from executorch.backends.apple.mps.mps_preprocess import ( # noqa: F401 |
| 59 | + MPSBackend, |
| 60 | + ) |
| 61 | + |
| 62 | + future_warnings = [ |
| 63 | + warning |
| 64 | + for warning in w |
| 65 | + if issubclass(warning.category, FutureWarning) |
| 66 | + and "MPS backend is deprecated" in str(warning.message) |
| 67 | + ] |
| 68 | + self.assertTrue( |
| 69 | + len(future_warnings) > 0, |
| 70 | + "Importing MPSBackend should emit a FutureWarning about deprecation", |
| 71 | + ) |
| 72 | + |
| 73 | + def test_mps_partitioner_class_warns(self) -> None: |
| 74 | + with warnings.catch_warnings(record=True) as w: |
| 75 | + warnings.simplefilter("always") |
| 76 | + from executorch.backends.apple.mps.partition.mps_partitioner import ( # noqa: F401 |
| 77 | + MPSPartitioner, |
| 78 | + ) |
| 79 | + |
| 80 | + future_warnings = [ |
| 81 | + warning |
| 82 | + for warning in w |
| 83 | + if issubclass(warning.category, FutureWarning) |
| 84 | + and "MPS partitioner is deprecated" in str(warning.message) |
| 85 | + ] |
| 86 | + self.assertTrue( |
| 87 | + len(future_warnings) > 0, |
| 88 | + "Importing MPSPartitioner should emit a FutureWarning about deprecation", |
| 89 | + ) |
| 90 | + |
| 91 | + def test_deprecation_message_mentions_alternative(self) -> None: |
| 92 | + with warnings.catch_warnings(record=True) as w: |
| 93 | + warnings.simplefilter("always") |
| 94 | + from executorch.backends.apple.mps.mps_preprocess import ( # noqa: F401 |
| 95 | + MPSBackend, |
| 96 | + ) |
| 97 | + |
| 98 | + future_warnings = [ |
| 99 | + warning for warning in w if issubclass(warning.category, FutureWarning) |
| 100 | + ] |
| 101 | + self.assertTrue(len(future_warnings) > 0) |
| 102 | + message = str(future_warnings[-1].message) |
| 103 | + self.assertIn( |
| 104 | + "CoreML", |
| 105 | + message, |
| 106 | + "Deprecation warning should mention CoreML as an alternative", |
| 107 | + ) |
| 108 | + |
| 109 | + def test_deprecation_message_mentions_removal_version(self) -> None: |
| 110 | + with warnings.catch_warnings(record=True) as w: |
| 111 | + warnings.simplefilter("always") |
| 112 | + from executorch.backends.apple.mps.mps_preprocess import ( # noqa: F401 |
| 113 | + MPSBackend, |
| 114 | + ) |
| 115 | + |
| 116 | + future_warnings = [ |
| 117 | + warning for warning in w if issubclass(warning.category, FutureWarning) |
| 118 | + ] |
| 119 | + self.assertTrue(len(future_warnings) > 0) |
| 120 | + message = str(future_warnings[-1].message) |
| 121 | + self.assertIn( |
| 122 | + "1.4", |
| 123 | + message, |
| 124 | + "Deprecation warning should mention the removal version (1.4)", |
| 125 | + ) |
0 commit comments