|
| 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 | +from executorch.backends.xnnpack.test.tester import Export, Tester |
| 11 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 12 | +from torch.export import Dim |
| 13 | + |
| 14 | + |
| 15 | +class TestSqueeze(unittest.TestCase): |
| 16 | + class Squeeze(torch.nn.Module): |
| 17 | + def __init__(self, dims): |
| 18 | + super().__init__() |
| 19 | + self.dims = dims |
| 20 | + |
| 21 | + def forward(self, x): |
| 22 | + return torch.squeeze(x, self.dims) |
| 23 | + |
| 24 | + def test_fp32_squeeze(self): |
| 25 | + inputs = (torch.randn(1, 2, 1, 4, 1),) |
| 26 | + squeeze_dims = (0, 2, 4) |
| 27 | + |
| 28 | + for dims in squeeze_dims: |
| 29 | + ( |
| 30 | + Tester(self.Squeeze(dims), inputs) |
| 31 | + .export() |
| 32 | + .check_node_count( |
| 33 | + { |
| 34 | + torch.ops.aten.squeeze.dim: 1, |
| 35 | + } |
| 36 | + ) |
| 37 | + .to_edge_transform_and_lower() |
| 38 | + .check_node_count( |
| 39 | + { |
| 40 | + exir_ops.edge.aten.squeeze_copy.dim: 0, |
| 41 | + exir_ops.edge.aten.view_copy.default: 0, |
| 42 | + torch.ops.higher_order.executorch_call_delegate: 1, |
| 43 | + } |
| 44 | + ) |
| 45 | + .run_method_and_compare_outputs() |
| 46 | + ) |
| 47 | + |
| 48 | + def test_fp16_squeeze(self): |
| 49 | + inputs = (torch.randn(1, 2, 1, 4, 1).to(torch.float16),) |
| 50 | + squeeze_dims = (0, 2, 4) |
| 51 | + |
| 52 | + for dims in squeeze_dims: |
| 53 | + ( |
| 54 | + Tester(self.Squeeze(dims), inputs) |
| 55 | + .export() |
| 56 | + .check_node_count( |
| 57 | + { |
| 58 | + torch.ops.aten.squeeze.dim: 1, |
| 59 | + } |
| 60 | + ) |
| 61 | + .to_edge_transform_and_lower() |
| 62 | + .check_node_count( |
| 63 | + { |
| 64 | + exir_ops.edge.aten.squeeze_copy.dim: 0, |
| 65 | + exir_ops.edge.aten.view_copy.default: 0, |
| 66 | + torch.ops.higher_order.executorch_call_delegate: 1, |
| 67 | + } |
| 68 | + ) |
| 69 | + .run_method_and_compare_outputs() |
| 70 | + ) |
| 71 | + |
| 72 | + def test_fp32_squeeze_dynamic(self): |
| 73 | + inputs = (torch.randn(1, 2, 1, 4, 1),) |
| 74 | + squeeze_dims = (0, 2, 4) |
| 75 | + dynamic_shapes = {"x": {1: Dim("x_1", min=1, max=10)}} |
| 76 | + |
| 77 | + for dims in squeeze_dims: |
| 78 | + ( |
| 79 | + Tester(self.Squeeze(dims), inputs) |
| 80 | + .export(Export(dynamic_shapes=dynamic_shapes)) |
| 81 | + .check_node_count( |
| 82 | + { |
| 83 | + torch.ops.aten.squeeze.dim: 1, |
| 84 | + } |
| 85 | + ) |
| 86 | + .to_edge_transform_and_lower() |
| 87 | + .check_node_count( |
| 88 | + { |
| 89 | + exir_ops.edge.aten.squeeze_copy.dim: 0, |
| 90 | + exir_ops.edge.aten.view_copy.default: 0, |
| 91 | + torch.ops.higher_order.executorch_call_delegate: 1, |
| 92 | + } |
| 93 | + ) |
| 94 | + .run_method_and_compare_outputs() |
| 95 | + ) |
| 96 | + |
| 97 | + def test_fp32_squeeze_unsupported_dynamism(self): |
| 98 | + inputs = (torch.randn(1, 2, 1, 4, 1),) |
| 99 | + squeeze_dims = (0, 2, 4) |
| 100 | + # Only one dynamic dimension is supported. |
| 101 | + dynamic_shapes = { |
| 102 | + "x": { |
| 103 | + 1: Dim("x_1", min=1, max=10), |
| 104 | + 3: Dim("x_3", min=1, max=10), |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for dims in squeeze_dims: |
| 109 | + ( |
| 110 | + Tester(self.Squeeze(dims), inputs) |
| 111 | + .export(Export(dynamic_shapes=dynamic_shapes)) |
| 112 | + .check_node_count( |
| 113 | + { |
| 114 | + torch.ops.aten.squeeze.dim: 1, |
| 115 | + } |
| 116 | + ) |
| 117 | + .to_edge_transform_and_lower() |
| 118 | + .check_node_count( |
| 119 | + { |
| 120 | + exir_ops.edge.aten.squeeze_copy.dims: 1, |
| 121 | + torch.ops.higher_order.executorch_call_delegate: 0, |
| 122 | + } |
| 123 | + ) |
| 124 | + .run_method_and_compare_outputs() |
| 125 | + ) |
0 commit comments