|
| 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 TestCloneMemoryFormat(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_clone_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.clone(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.clone(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 CloneChannelsLastToContiguous(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 | + # Start with channels_last input |
| 62 | + x_channels_last = x.to(memory_format=torch.channels_last) |
| 63 | + # Clone to contiguous format |
| 64 | + y = x_channels_last.clone(memory_format=torch.contiguous_format) |
| 65 | + return self.conv(y) |
| 66 | + |
| 67 | + CloneChannelsLastToContiguousModule = CloneChannelsLastToContiguous() |
| 68 | + |
| 69 | + def test_clone_channels_last_to_contiguous(self): |
| 70 | + self.run_tester( |
| 71 | + self.CloneChannelsLastToContiguousModule, (torch.randn(1, 3, 6, 6),) |
| 72 | + ) |
| 73 | + |
| 74 | + class CloneContiguousToChannelsLast(torch.nn.Module): |
| 75 | + def __init__(self): |
| 76 | + super().__init__() |
| 77 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 78 | + |
| 79 | + def forward(self, x): |
| 80 | + # Clone contiguous input to channels_last format |
| 81 | + y = x.clone(memory_format=torch.channels_last) |
| 82 | + return self.conv(y) |
| 83 | + |
| 84 | + CloneContiguousToChannelsLastModule = CloneContiguousToChannelsLast() |
| 85 | + |
| 86 | + def test_clone_contiguous_to_channels_last(self): |
| 87 | + self.run_tester( |
| 88 | + self.CloneContiguousToChannelsLastModule, (torch.randn(1, 3, 6, 6),) |
| 89 | + ) |
| 90 | + |
| 91 | + class SimpleClone(torch.nn.Module): |
| 92 | + def __init__(self): |
| 93 | + super().__init__() |
| 94 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 95 | + |
| 96 | + def forward(self, x): |
| 97 | + # Simple clone without memory format (should default to contiguous) |
| 98 | + y = x.clone() |
| 99 | + return self.conv(y) |
| 100 | + |
| 101 | + SimpleCloneModule = SimpleClone() |
| 102 | + |
| 103 | + def test_simple_clone(self): |
| 104 | + self.run_tester(self.SimpleCloneModule, (torch.randn(1, 3, 6, 6),)) |
| 105 | + |
| 106 | + class QuantizedClone(torch.nn.Module): |
| 107 | + def __init__(self): |
| 108 | + super().__init__() |
| 109 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 110 | + self.conv2 = torch.nn.Conv2d(3, 3, 3) |
| 111 | + |
| 112 | + def forward(self, x): |
| 113 | + y = self.conv(x) |
| 114 | + y = y.clone(memory_format=torch.contiguous_format) |
| 115 | + return self.conv2(y) |
| 116 | + |
| 117 | + QuantizedCloneModule = QuantizedClone() |
| 118 | + |
| 119 | + def test_quantized_clone(self): |
| 120 | + tester = Tester( |
| 121 | + self.QuantizedCloneModule.eval(), |
| 122 | + (torch.randn(1, 3, 9, 9),), |
| 123 | + ) |
| 124 | + |
| 125 | + tester.quantize().export().to_edge_transform_and_lower().check_not( |
| 126 | + [ |
| 127 | + "executorch_exir_dialects_edge__ops_aten_clone_default", |
| 128 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default", |
| 129 | + ] |
| 130 | + ).to_executorch().serialize().run_method_and_compare_outputs(qtol=1) |
| 131 | + |
| 132 | + class ChainedClone(torch.nn.Module): |
| 133 | + def __init__(self): |
| 134 | + super().__init__() |
| 135 | + self.conv = torch.nn.Conv2d(3, 3, 3) |
| 136 | + |
| 137 | + def forward(self, x): |
| 138 | + # Chain multiple clones with different memory formats |
| 139 | + y = x.clone(memory_format=torch.channels_last) |
| 140 | + z = y.clone(memory_format=torch.contiguous_format) |
| 141 | + return self.conv(z) |
| 142 | + |
| 143 | + ChainedCloneModule = ChainedClone() |
| 144 | + |
| 145 | + def test_chained_clone(self): |
| 146 | + self.run_tester(self.ChainedCloneModule, (torch.randn(1, 3, 6, 6),)) |
0 commit comments