|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from typing import Tuple |
| 7 | + |
| 8 | +import pytest |
| 9 | +import torch |
| 10 | +from executorch.backends.arm._passes.decompose_groupnorm_pass import ( |
| 11 | + DecomposeGroupNormPass, |
| 12 | +) |
| 13 | +from executorch.backends.arm.quantizer import ( |
| 14 | + get_symmetric_a16w8_quantization_config, |
| 15 | + get_symmetric_quantization_config, |
| 16 | + TOSAQuantizer, |
| 17 | +) |
| 18 | +from executorch.backends.arm.test.tester.test_pipeline import QuantizationPipeline |
| 19 | +from executorch.backends.arm.tosa import TosaSpecification |
| 20 | +from torchao.quantization.pt2e.quantizer import QuantizationSpec |
| 21 | + |
| 22 | +input_t = Tuple[torch.Tensor] |
| 23 | + |
| 24 | + |
| 25 | +class GroupNorm(torch.nn.Module): |
| 26 | + """A module instance whose FX provenance must survive decomposition.""" |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + super().__init__() |
| 30 | + self.group_norm = torch.nn.GroupNorm(num_groups=2, num_channels=4) |
| 31 | + |
| 32 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 33 | + return self.group_norm(x) |
| 34 | + |
| 35 | + def get_inputs(self) -> input_t: |
| 36 | + return (torch.rand(1, 4, 4, 4),) |
| 37 | + |
| 38 | + |
| 39 | +def test_group_norm_decomposition_preserves_module_metadata(): |
| 40 | + """Decomposed mean/sub must retain the original GroupNorm module stack.""" |
| 41 | + model = GroupNorm().eval() |
| 42 | + graph_module = torch.export.export(model, model.get_inputs()).module() |
| 43 | + source_nodes = [ |
| 44 | + node |
| 45 | + for node in graph_module.graph.nodes |
| 46 | + if node.target == torch.ops.aten.group_norm.default |
| 47 | + ] |
| 48 | + assert len(source_nodes) == 1 |
| 49 | + original_stack = source_nodes[0].meta.get("nn_module_stack") |
| 50 | + assert original_stack, "Export must retain GroupNorm module provenance" |
| 51 | + |
| 52 | + result = DecomposeGroupNormPass(tfa_pass=True)(graph_module) |
| 53 | + nodes = list(result.graph_module.graph.nodes) |
| 54 | + for op in (torch.ops.aten.mean.dim, torch.ops.aten.sub.Tensor): |
| 55 | + generated = [node for node in nodes if node.target == op] |
| 56 | + assert len(generated) == 1, f"Expected exactly one generated {op}" |
| 57 | + assert generated[0].meta.get("nn_module_stack") == original_stack |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("use_composable_quantizer", [False, True]) |
| 61 | +def test_group_norm_decomposition_a16w8_module_override_metadata( |
| 62 | + use_composable_quantizer: bool, |
| 63 | +): |
| 64 | + """The GroupNorm-specific A16W8 override must beat the global A8W8 |
| 65 | + config. |
| 66 | + """ |
| 67 | + model = GroupNorm().eval() |
| 68 | + |
| 69 | + a16w8_config = get_symmetric_a16w8_quantization_config() |
| 70 | + a16w8_qspec = a16w8_config.output_activation |
| 71 | + assert isinstance(a16w8_qspec, QuantizationSpec) |
| 72 | + |
| 73 | + quantizer = TOSAQuantizer( |
| 74 | + TosaSpecification.create_from_string("TOSA-1.0+INT+int16"), |
| 75 | + use_composable_quantizer=use_composable_quantizer, |
| 76 | + ) |
| 77 | + quantizer.set_global(get_symmetric_quantization_config()) |
| 78 | + quantizer.set_module_type(torch.nn.GroupNorm, a16w8_config) |
| 79 | + |
| 80 | + pipeline = QuantizationPipeline[input_t]( |
| 81 | + model, |
| 82 | + model.get_inputs(), |
| 83 | + quantizer=quantizer, |
| 84 | + qspecs={ |
| 85 | + "aten.sub.Tensor": { |
| 86 | + a16w8_qspec: 2, |
| 87 | + }, |
| 88 | + }, |
| 89 | + ) |
| 90 | + pipeline.run() |
0 commit comments