Skip to content

Commit 4fe0253

Browse files
authored
Arm backend: Fix provenance for GroupNorm. (#24240)
Fix provenance for GroupNorm. cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Elena Zhelezina <elena.zhelezina@arm.com>
1 parent 337aa81 commit 4fe0253

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

‎backends/arm/_passes/decompose_groupnorm_pass.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,17 @@ def call(self, graph_module: torch.fx.GraphModule):
139139
from_node=node,
140140
)
141141
mean = create_node(
142-
graph_module.graph, mean_op, args=(x_reshaped, dims, keepdim)
142+
graph_module.graph,
143+
mean_op,
144+
args=(x_reshaped, dims, keepdim),
145+
from_node=node,
146+
)
147+
sub = create_node(
148+
graph_module.graph,
149+
sub_op,
150+
args=(x_reshaped, mean),
151+
from_node=node,
143152
)
144-
sub = create_node(graph_module.graph, sub_op, args=(x_reshaped, mean))
145153
var = create_node(
146154
graph_module.graph,
147155
var_op,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)