|
| 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 unittest |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +from executorch.backends.xnnpack.test.tester import Tester |
| 12 | + |
| 13 | + |
| 14 | +class TestChannelsLastTaggedReshapePass(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + torch._dynamo.reset() |
| 17 | + |
| 18 | + def run_tester(self, module, inputs): |
| 19 | + tester = Tester( |
| 20 | + module.eval(), |
| 21 | + inputs, |
| 22 | + ) |
| 23 | + tester.export().to_edge_transform_and_lower().check_not( |
| 24 | + ["executorch_exir_dialects_edge__ops_aten__to_copy_default"] |
| 25 | + ).to_executorch().serialize().run_method_and_compare_outputs() |
| 26 | + |
| 27 | + class ChannelLastBeforeLinear(torch.nn.Module): |
| 28 | + def __init__(self): |
| 29 | + super().__init__() |
| 30 | + self.linear = torch.nn.Linear(3, 3) |
| 31 | + |
| 32 | + def forward(self, x): |
| 33 | + y = x.to(memory_format=torch.channels_last) |
| 34 | + return self.linear(y) |
| 35 | + |
| 36 | + ChannelLastBeforeLinearModule = ChannelLastBeforeLinear() |
| 37 | + |
| 38 | + def test_channel_last_before_linear(self): |
| 39 | + self.run_tester(self.ChannelLastBeforeLinearModule, (torch.randn(1, 3, 3, 3),)) |
| 40 | + |
| 41 | + class ContiguousBeforeConv(torch.nn.Module): |
| 42 | + def __init__(self): |
| 43 | + super().__init__() |
| 44 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 45 | + |
| 46 | + def forward(self, x): |
| 47 | + y = x.to(memory_format=torch.contiguous_format) |
| 48 | + return self.conv(y) |
| 49 | + |
| 50 | + ContiguousBeforeConvModule = ContiguousBeforeConv() |
| 51 | + |
| 52 | + def test_contiguous_before_conv(self): |
| 53 | + self.run_tester(self.ContiguousBeforeConvModule, (torch.randn(1, 3, 6, 6),)) |
| 54 | + |
| 55 | + class DtypeAndMemoryFormatConversion(torch.nn.Module): |
| 56 | + def __init__(self): |
| 57 | + super().__init__() |
| 58 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 59 | + |
| 60 | + def forward(self, x): |
| 61 | + y = x.to(torch.float, memory_format=torch.channels_last) |
| 62 | + return self.conv(y) |
| 63 | + |
| 64 | + DtypeAndMemoryFormatConversionModule = DtypeAndMemoryFormatConversion() |
| 65 | + |
| 66 | + def test_dtype_and_memory_format_conversion(self): |
| 67 | + self.run_tester( |
| 68 | + self.DtypeAndMemoryFormatConversionModule, |
| 69 | + (torch.randint(0, 10, (1, 3, 6, 6), dtype=torch.int32),), |
| 70 | + ) |
| 71 | + |
| 72 | + class DtypeAndMemoryFormatWithLinear(torch.nn.Module): |
| 73 | + def __init__(self): |
| 74 | + super().__init__() |
| 75 | + self.linear = torch.nn.Linear(3, 3) |
| 76 | + |
| 77 | + def forward(self, x): |
| 78 | + y = x.to(torch.float, memory_format=torch.channels_last) |
| 79 | + return self.linear(y) |
| 80 | + |
| 81 | + DtypeAndMemoryFormatWithLinearModule = DtypeAndMemoryFormatWithLinear() |
| 82 | + |
| 83 | + def test_dtype_and_memory_format_with_linear(self): |
| 84 | + self.run_tester( |
| 85 | + self.DtypeAndMemoryFormatWithLinearModule, |
| 86 | + (torch.randint(0, 10, (1, 3, 3, 3), dtype=torch.int16),), |
| 87 | + ) |
| 88 | + |
| 89 | + class QuantizedToCopy(torch.nn.Module): |
| 90 | + def __init__(self): |
| 91 | + super().__init__() |
| 92 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 93 | + self.conv2 = torch.nn.Conv2d(3, 3, 3) |
| 94 | + |
| 95 | + def forward(self, x): |
| 96 | + y = self.conv(x) |
| 97 | + y = y.to(memory_format=torch.contiguous_format) |
| 98 | + return self.conv2(y) |
| 99 | + |
| 100 | + QuantizedToCopyModule = QuantizedToCopy() |
| 101 | + |
| 102 | + def test_quantized_to_copy(self): |
| 103 | + tester = Tester( |
| 104 | + self.QuantizedToCopyModule.eval(), |
| 105 | + (torch.randn(1, 3, 9, 9),), |
| 106 | + ) |
| 107 | + |
| 108 | + tester.quantize().export().to_edge_transform_and_lower().check_not( |
| 109 | + [ |
| 110 | + "executorch_exir_dialects_edge__ops_aten__to_copy_default", |
| 111 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default", |
| 112 | + ] |
| 113 | + ).to_executorch().serialize().run_method_and_compare_outputs(qtol=0.01) |
0 commit comments