|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import io |
| 8 | +import logging |
| 9 | +import unittest |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner |
| 13 | +from executorch.exir import to_edge, to_edge_transform_and_lower |
| 14 | +from torch.export import export |
| 15 | + |
| 16 | + |
| 17 | +class TestXnnpackPartitioner(unittest.TestCase): |
| 18 | + """Test cases for XnnpackPartitioner functionality and deprecation warnings.""" |
| 19 | + |
| 20 | + class SimpleModel(torch.nn.Module): |
| 21 | + def __init__(self): |
| 22 | + super().__init__() |
| 23 | + self.linear = torch.nn.Linear(10, 5) |
| 24 | + |
| 25 | + def forward(self, x): |
| 26 | + return self.linear(x) |
| 27 | + |
| 28 | + def test_deprecation_warning_for_to_backend_workflow(self): |
| 29 | + """ |
| 30 | + Test that the deprecated to_edge + to_backend workflow shows a deprecation warning. |
| 31 | + """ |
| 32 | + model = self.SimpleModel() |
| 33 | + x = torch.randn(1, 10) |
| 34 | + |
| 35 | + exported_model = export(model, (x,)) |
| 36 | + |
| 37 | + # Capture log output to check for deprecation warning |
| 38 | + log_capture_string = io.StringIO() |
| 39 | + ch = logging.StreamHandler(log_capture_string) |
| 40 | + ch.setLevel(logging.WARNING) |
| 41 | + |
| 42 | + logger = logging.getLogger( |
| 43 | + "executorch.backends.xnnpack.partition.xnnpack_partitioner" |
| 44 | + ) |
| 45 | + logger.addHandler(ch) |
| 46 | + logger.setLevel(logging.WARNING) |
| 47 | + |
| 48 | + edge = to_edge(exported_model) |
| 49 | + partitioner = XnnpackPartitioner() |
| 50 | + |
| 51 | + edge.to_backend(partitioner) |
| 52 | + |
| 53 | + log_contents = log_capture_string.getvalue() |
| 54 | + self.assertIn("DEPRECATION WARNING", log_contents) |
| 55 | + self.assertIn("to_edge() + to_backend()", log_contents) |
| 56 | + self.assertIn("to_edge_transform_and_lower()", log_contents) |
| 57 | + |
| 58 | + def test_no_warning_for_to_edge_transform_and_lower_workflow(self): |
| 59 | + """ |
| 60 | + Test that the recommended to_edge_transform_and_lower workflow does NOT show a deprecation warning. |
| 61 | + """ |
| 62 | + |
| 63 | + model = self.SimpleModel() |
| 64 | + x = torch.randn(1, 10) |
| 65 | + |
| 66 | + exported_model = export(model, (x,)) |
| 67 | + |
| 68 | + # Capture log output to check for deprecation warning |
| 69 | + log_capture_string = io.StringIO() |
| 70 | + ch = logging.StreamHandler(log_capture_string) |
| 71 | + ch.setLevel(logging.WARNING) |
| 72 | + |
| 73 | + logger = logging.getLogger( |
| 74 | + "executorch.backends.xnnpack.partition.xnnpack_partitioner" |
| 75 | + ) |
| 76 | + logger.addHandler(ch) |
| 77 | + logger.setLevel(logging.WARNING) |
| 78 | + |
| 79 | + partitioner = XnnpackPartitioner() |
| 80 | + |
| 81 | + to_edge_transform_and_lower(exported_model, partitioner=[partitioner]) |
| 82 | + |
| 83 | + log_contents = log_capture_string.getvalue() |
| 84 | + self.assertNotIn("DEPRECATION WARNING", log_contents) |
0 commit comments