(graph: Graph, quant_config: QuantizationConfig)
| 147 | |
| 148 | |
| 149 | def _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 | |
| 181 | def annotate_placeholder(node: Node, quant_config: QuantizationConfig) -> None: |
no test coverage detected