|
| 1 | +# Copyright 2024 Arm Limited and/or its 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 | +# |
| 8 | +# Tests the full op which creates a tensor of a given shape filled with a given value. |
| 9 | +# The shape and value are set at compile time, i.e. can't be set by a tensor input. |
| 10 | +# |
| 11 | + |
| 12 | +import unittest |
| 13 | +from typing import Tuple |
| 14 | + |
| 15 | +import torch |
| 16 | +from executorch.backends.arm.test import common |
| 17 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 18 | +from parameterized import parameterized |
| 19 | + |
| 20 | + |
| 21 | +class TestFull(unittest.TestCase): |
| 22 | + class Full(torch.nn.Module): |
| 23 | + # A single full op |
| 24 | + def forward(self): |
| 25 | + return torch.full((3, 3), 4.5) |
| 26 | + |
| 27 | + class AddConstFull(torch.nn.Module): |
| 28 | + # Input + a full with constant value. |
| 29 | + def forward(self, x: torch.Tensor): |
| 30 | + return torch.full((2, 2, 3, 3), 4.5, dtype=torch.float32) + x |
| 31 | + |
| 32 | + class AddVariableFull(torch.nn.Module): |
| 33 | + sizes = [ |
| 34 | + (5), |
| 35 | + (5, 5), |
| 36 | + (5, 5, 5), |
| 37 | + (1, 5, 5, 5), |
| 38 | + ] |
| 39 | + test_parameters = [((torch.randn(n) * 10 - 5, 3.2),) for n in sizes] |
| 40 | + |
| 41 | + def forward(self, x: torch.Tensor, y): |
| 42 | + # Input + a full with the shape from the input and a given value 'y'. |
| 43 | + return x + torch.full(x.shape, y) |
| 44 | + |
| 45 | + def _test_full_tosa_MI_pipeline( |
| 46 | + self, |
| 47 | + module: torch.nn.Module, |
| 48 | + example_data: Tuple, |
| 49 | + test_data: Tuple | None = None, |
| 50 | + ): |
| 51 | + if test_data is None: |
| 52 | + test_data = example_data |
| 53 | + ( |
| 54 | + ArmTester( |
| 55 | + module, |
| 56 | + example_inputs=example_data, |
| 57 | + compile_spec=common.get_tosa_compile_spec(), |
| 58 | + ) |
| 59 | + .export() |
| 60 | + .check_count({"torch.ops.aten.full.default": 1}) |
| 61 | + .to_edge() |
| 62 | + .partition() |
| 63 | + .check_not(["executorch_exir_dialects_edge__ops_aten_full_default"]) |
| 64 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 65 | + .to_executorch() |
| 66 | + .run_method_and_compare_outputs(inputs=test_data) |
| 67 | + ) |
| 68 | + |
| 69 | + def _test_full_tosa_BI_pipeline( |
| 70 | + self, |
| 71 | + module: torch.nn.Module, |
| 72 | + test_data: Tuple, |
| 73 | + permute_memory_to_nhwc: bool, |
| 74 | + ): |
| 75 | + ( |
| 76 | + ArmTester( |
| 77 | + module, |
| 78 | + example_inputs=test_data, |
| 79 | + compile_spec=common.get_tosa_compile_spec( |
| 80 | + permute_memory_to_nhwc=permute_memory_to_nhwc |
| 81 | + ), |
| 82 | + ) |
| 83 | + .quantize() |
| 84 | + .export() |
| 85 | + .check_count({"torch.ops.aten.full.default": 1}) |
| 86 | + .to_edge() |
| 87 | + .partition() |
| 88 | + .check_not(["executorch_exir_dialects_edge__ops_aten_full_default"]) |
| 89 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 90 | + .to_executorch() |
| 91 | + .run_method_and_compare_outputs(inputs=test_data) |
| 92 | + ) |
| 93 | + |
| 94 | + def _test_full_tosa_u55_pipeline(self, module: torch.nn.Module, test_data: Tuple): |
| 95 | + ( |
| 96 | + ArmTester( |
| 97 | + module, |
| 98 | + example_inputs=test_data, |
| 99 | + compile_spec=common.get_u55_compile_spec(), |
| 100 | + ) |
| 101 | + .quantize() |
| 102 | + .export() |
| 103 | + .check_count({"torch.ops.aten.full.default": 1}) |
| 104 | + .to_edge() |
| 105 | + .partition() |
| 106 | + .check_not(["executorch_exir_dialects_edge__ops_aten_full_default"]) |
| 107 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 108 | + .to_executorch() |
| 109 | + ) |
| 110 | + |
| 111 | + def test_only_full_tosa_MI(self): |
| 112 | + self._test_full_tosa_MI_pipeline(self.Full(), ()) |
| 113 | + |
| 114 | + def test_const_full_tosa_MI(self): |
| 115 | + _input = torch.rand((2, 2, 3, 3)) * 10 |
| 116 | + self._test_full_tosa_MI_pipeline(self.AddConstFull(), (_input,)) |
| 117 | + |
| 118 | + def test_const_full_nhwc_tosa_BI(self): |
| 119 | + _input = torch.rand((2, 2, 3, 3)) * 10 |
| 120 | + self._test_full_tosa_BI_pipeline(self.AddConstFull(), (_input,), True) |
| 121 | + |
| 122 | + @parameterized.expand(AddVariableFull.test_parameters) |
| 123 | + def test_full_tosa_MI(self, test_tensor: Tuple): |
| 124 | + self._test_full_tosa_MI_pipeline( |
| 125 | + self.AddVariableFull(), example_data=test_tensor |
| 126 | + ) |
| 127 | + |
| 128 | + @parameterized.expand(AddVariableFull.test_parameters) |
| 129 | + def test_full_tosa_BI(self, test_tensor: Tuple): |
| 130 | + self._test_full_tosa_BI_pipeline(self.AddVariableFull(), test_tensor, False) |
| 131 | + |
| 132 | + @parameterized.expand(AddVariableFull.test_parameters) |
| 133 | + def test_full_u55_BI(self, test_tensor: Tuple): |
| 134 | + self._test_full_tosa_u55_pipeline( |
| 135 | + self.AddVariableFull(), |
| 136 | + test_tensor, |
| 137 | + ) |
| 138 | + |
| 139 | + # This fails since full outputs int64 by default if 'fill_value' is integer, which our backend doesn't support. |
| 140 | + @unittest.expectedFailure |
| 141 | + def test_integer_value(self): |
| 142 | + _input = torch.ones((2, 2)) |
| 143 | + integer_fill_value = 1 |
| 144 | + self._test_full_tosa_MI_pipeline( |
| 145 | + self.AddVariableFull(), example_data=(_input, integer_fill_value) |
| 146 | + ) |
| 147 | + |
| 148 | + # This fails since the fill value in the full tensor is set at compile time by the example data (1.). |
| 149 | + # Test data tries to set it again at runtime (to 2.) but it doesn't do anything. |
| 150 | + # In eager mode, the fill value can be set at runtime, causing the outputs to not match. |
| 151 | + @unittest.expectedFailure |
| 152 | + def test_set_value_at_runtime(self): |
| 153 | + _input = torch.ones((2, 2)) |
| 154 | + example_fill_value = 1.0 |
| 155 | + test_fill_value = 2.0 |
| 156 | + self._test_full_tosa_MI_pipeline( |
| 157 | + self.AddVariableFull(), |
| 158 | + example_data=(_input, example_fill_value), |
| 159 | + test_data=(_input, test_fill_value), |
| 160 | + ) |
0 commit comments