|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +import executorch.backends.vulkan.utils as utils |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +from executorch.backends.vulkan.patterns.pattern_registry import ( |
| 14 | + PatternMatch, |
| 15 | + register_pattern_detector, |
| 16 | + register_pattern_replacement, |
| 17 | +) |
| 18 | + |
| 19 | +from executorch.exir import ExportedProgram |
| 20 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 21 | + |
| 22 | + |
| 23 | +class QuantizedUnaryMatch(PatternMatch): |
| 24 | + def __init__(self, unary_node: torch.fx.Node) -> None: |
| 25 | + self.anchor_node = unary_node |
| 26 | + self.match_found = False |
| 27 | + self.all_nodes = [self.anchor_node] |
| 28 | + |
| 29 | + # The unary op takes a single input which must be a dequantize node |
| 30 | + if len(unary_node.args) < 1: |
| 31 | + return |
| 32 | + |
| 33 | + input_node = unary_node.args[0] |
| 34 | + assert isinstance(input_node, torch.fx.Node) |
| 35 | + |
| 36 | + if not utils.is_dequant_node(input_node): |
| 37 | + return |
| 38 | + |
| 39 | + self.dequantize_input_node = input_node |
| 40 | + |
| 41 | + # Extract quantization parameters for the input |
| 42 | + self.quantize_input_node = self.dequantize_input_node.args[0] |
| 43 | + self.input_scales_node = self.dequantize_input_node.args[1] |
| 44 | + self.input_zeros_node = self.dequantize_input_node.args[2] |
| 45 | + |
| 46 | + self.all_nodes.append(self.dequantize_input_node) |
| 47 | + |
| 48 | + # The unary op output must have exactly one user: a quantize node |
| 49 | + self.output_node = self.anchor_node |
| 50 | + |
| 51 | + if len(self.output_node.users) != 1: |
| 52 | + return |
| 53 | + |
| 54 | + cur_node = list(self.output_node.users)[0] |
| 55 | + |
| 56 | + if not utils.is_quant_node(cur_node): |
| 57 | + return |
| 58 | + |
| 59 | + self.quantize_output_node = cur_node |
| 60 | + self.output_scales_node = self.quantize_output_node.args[1] |
| 61 | + self.output_zeros_node = self.quantize_output_node.args[2] |
| 62 | + |
| 63 | + self.all_nodes.append(self.quantize_output_node) |
| 64 | + |
| 65 | + self.match_found = True |
| 66 | + |
| 67 | + |
| 68 | +# Unary operation anchor nodes that we support |
| 69 | +unary_anchor_nodes = { |
| 70 | + exir_ops.edge.aten.relu.default, |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +@register_pattern_detector("quantized_unary") |
| 75 | +def find_quantized_unary_patterns( |
| 76 | + node: torch.fx.Node, |
| 77 | +) -> Optional[QuantizedUnaryMatch]: |
| 78 | + if node.target not in unary_anchor_nodes: |
| 79 | + return None |
| 80 | + |
| 81 | + matched_pattern = QuantizedUnaryMatch(node) |
| 82 | + if matched_pattern.match_found: |
| 83 | + return matched_pattern |
| 84 | + |
| 85 | + return None |
| 86 | + |
| 87 | + |
| 88 | +## |
| 89 | +## Pattern Replacement |
| 90 | +## |
| 91 | + |
| 92 | + |
| 93 | +@register_pattern_replacement("quantized_unary") |
| 94 | +def make_q8ta_unary_custom_op( |
| 95 | + ep: ExportedProgram, |
| 96 | + graph_module: torch.fx.GraphModule, |
| 97 | + match: QuantizedUnaryMatch, |
| 98 | +): |
| 99 | + op_target = None |
| 100 | + if match.anchor_node.target == exir_ops.edge.aten.relu.default: |
| 101 | + op_target = exir_ops.edge.et_vk.q8ta_relu.default |
| 102 | + else: |
| 103 | + raise NotImplementedError( |
| 104 | + f"Unsupported unary operation: {match.anchor_node.target}" |
| 105 | + ) |
| 106 | + |
| 107 | + with graph_module.graph.inserting_before(match.output_node): |
| 108 | + qunary_node = graph_module.graph.create_node( |
| 109 | + "call_function", |
| 110 | + op_target, |
| 111 | + args=( |
| 112 | + match.quantize_input_node, |
| 113 | + match.input_scales_node, |
| 114 | + match.input_zeros_node, |
| 115 | + match.output_scales_node, |
| 116 | + match.output_zeros_node, |
| 117 | + ), |
| 118 | + ) |
| 119 | + |
| 120 | + qunary_node.meta["val"] = match.output_node.meta["val"] |
| 121 | + match.quantize_output_node.replace_all_uses_with(qunary_node) |
0 commit comments