Skip to content

Commit a1665c5

Browse files
authored
Adding Test for CadenceWithLayerNormQuantizer
Differential Revision: D88898823 Pull Request resolved: pytorch#16355
1 parent 5ad4da4 commit a1665c5

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

‎backends/cadence/aot/tests/test_quantizer_ops.py‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
from typing import Callable
1212

1313
import torch
14-
from executorch.backends.cadence.aot.graph_builder import GraphBuilder
14+
from executorch.backends.cadence.aot.graph_builder import (
15+
GraphBuilder,
16+
single_op_builder,
17+
)
1518
from executorch.backends.cadence.aot.quantizer import quantizer as quantizer_module
1619
from executorch.backends.cadence.aot.quantizer.patterns import AddmmPattern
1720
from executorch.backends.cadence.aot.quantizer.quantizer import (
@@ -56,7 +59,6 @@
5659
CadenceW8A32MixedQuantizer, # TODO: T247438158 Add test coverage
5760
CadenceRmsNormNopQuantizer, # No-op quantizer, doesn't annotate anything, preserves rms_norm from decomposition
5861
CadenceWakeWordQuantizer, # TODO: T247438162 Add test coverage
59-
CadenceWithLayerNormQuantizer, # TODO: T247438410 Add test coverage
6062
}
6163

6264

@@ -118,6 +120,15 @@
118120
# For softmax: only input_activation
119121
[qconfig_A16.input_activation],
120122
),
123+
(
124+
"layer_norm_A8W8",
125+
lambda self: self._build_layer_norm_graph(),
126+
CadenceWithLayerNormQuantizer(),
127+
torch.ops.aten.layer_norm.default,
128+
qconfig_A8W8.output_activation,
129+
# For layer_norm: only input_activation (weights/bias are passed as others)
130+
[qconfig_A8W8.input_activation],
131+
),
121132
]
122133

123134
# Derive the set of tested quantizer classes from the test cases.
@@ -243,6 +254,31 @@ def _build_softmax_graph(self) -> tuple[torch.fx.GraphModule, torch.fx.Node]:
243254
self.assertEqual(len(softmax_nodes), 1, "Should find exactly one softmax node")
244255
return gm, softmax_nodes[0]
245256

257+
def _build_layer_norm_graph(self) -> tuple[torch.fx.GraphModule, torch.fx.Node]:
258+
"""Build a simple graph with a layer_norm operation."""
259+
# Input shape: (batch, features)
260+
x = torch.randn(1, 10)
261+
# normalized_shape must match the last dimension(s) of input
262+
normalized_shape = [10]
263+
gm = single_op_builder(
264+
placeholders=(x,),
265+
op=torch.ops.aten.layer_norm.default,
266+
args=(x, normalized_shape),
267+
)
268+
269+
layer_norm_nodes = gm.graph.find_nodes(
270+
op="call_function",
271+
target=torch.ops.aten.layer_norm.default,
272+
)
273+
self.assertEqual(
274+
len(layer_norm_nodes), 1, "Should find exactly one layer_norm node"
275+
)
276+
# Add source_fn_stack metadata required by quantizer pattern matching
277+
layer_norm_nodes[0].meta["source_fn_stack"] = [
278+
("layer_norm", torch.ops.aten.layer_norm.default)
279+
]
280+
return gm, layer_norm_nodes[0]
281+
246282
@parameterized.expand(QUANTIZER_ANNOTATION_TEST_CASES)
247283
def test_quantizer_annotation(
248284
self,

0 commit comments

Comments
 (0)