MCPcopy Create free account
hub / github.com/pytorch/executorch / _annotate_rmsnorm_pattern

Function _annotate_rmsnorm_pattern

backends/mediatek/quantizer/annotator.py:149–178  ·  view source on GitHub ↗
(graph: Graph, quant_config: QuantizationConfig)

Source from the content-addressed store, hash-verified

147
148
149def _annotate_rmsnorm_pattern(graph: Graph, quant_config: QuantizationConfig) -> None:
150 class ExecuTorchPattern(torch.nn.Module):
151 def forward(self, x):
152 norm = x * torch.rsqrt((x * x).mean(-1, keepdim=True) + 1e-6)
153 return norm, {}
154
155 class MTKPattern(torch.nn.Module):
156 def forward(self, x):
157 norm = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + 1e-6)
158 return norm, {}
159
160 for pattern_cls in (ExecuTorchPattern, MTKPattern):
161 pattern_gm = export(pattern_cls(), (torch.randn(3, 3),), strict=True).module()
162 matcher = SubgraphMatcherWithNameNodeMap(
163 pattern_gm, ignore_literals=True, remove_overlapping_matches=False
164 )
165 matches = matcher.match(graph)
166 for match in matches:
167 target_nodes = []
168 for node in match.nodes_map.values():
169 if node in match.placeholder_nodes:
170 continue
171 if node.op == "call_function" and node.target in OP_TO_ANNOTATOR:
172 target_nodes.append(node)
173
174 if any(_is_annotated(node) for node in target_nodes):
175 continue
176 _mark_as_annotated(target_nodes)
177 for node in match.returning_nodes:
178 _annotate_output_qspec(node, quant_config.activation)
179
180
181def annotate_placeholder(node: Node, quant_config: QuantizationConfig) -> None:

Callers 1

annotateFunction · 0.85

Calls 7

exportFunction · 0.90
_mark_as_annotatedFunction · 0.85
moduleMethod · 0.80
valuesMethod · 0.80
_is_annotatedFunction · 0.70
matchMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected