|
15 | 15 | from executorch.exir.pass_base import ExportPass, PassResult
|
16 | 16 | from torch.fx import GraphModule, Node
|
17 | 17 |
|
| 18 | +q_op = exir_ops.edge.quantized_decomposed.quantize_per_tensor.default |
| 19 | +dq_op = exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default |
| 20 | + |
18 | 21 |
|
19 | 22 | def get_input_qparams(node: Node) -> dict[int, QuantArgs]:
|
20 | 23 | """
|
@@ -77,8 +80,6 @@ def __init__(self, targeted_ops: Iterable[Callable]):
|
77 | 80 | self.targeted_ops = targeted_ops
|
78 | 81 |
|
79 | 82 | def call(self, graph_module: GraphModule) -> PassResult:
|
80 |
| - q_op = exir_ops.edge.quantized_decomposed.quantize_per_tensor.default |
81 |
| - dq_op = exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default |
82 | 83 |
|
83 | 84 | # Loop over the graph nodes and find any node in the 'targeted_ops' list.
|
84 | 85 | for n in graph_module.graph.nodes:
|
@@ -145,3 +146,36 @@ def call(self, graph_module: GraphModule) -> PassResult:
|
145 | 146 |
|
146 | 147 | graph_module.recompile()
|
147 | 148 | return PassResult(graph_module, True)
|
| 149 | + |
| 150 | + |
| 151 | +class QuantizeFullArgument(ExportPass): |
| 152 | + """ |
| 153 | + Make sure the fill_value for full.default is quantized. This pass needs to be run before |
| 154 | + the folding pass above to make sure that the retraced output of the full.default op is |
| 155 | + the right dtype. |
| 156 | + """ |
| 157 | + |
| 158 | + def call(self, graph_module: GraphModule) -> PassResult: |
| 159 | + modified = False |
| 160 | + # Loop over the graph nodes and find any node in the 'targeted_ops' list. |
| 161 | + for n in graph_module.graph.nodes: |
| 162 | + n = cast(Node, n) |
| 163 | + if n.target != exir_ops.edge.aten.full.default: |
| 164 | + continue |
| 165 | + |
| 166 | + # Make sure we have a quantized operator |
| 167 | + user = list(n.users)[0] |
| 168 | + if user.target != q_op: |
| 169 | + continue |
| 170 | + |
| 171 | + qargs = QuantArgs.from_operator(user.target, user.args) |
| 172 | + if "dtype" not in n.kwargs.keys() or n.kwargs["dtype"] != qargs.dtype: |
| 173 | + # replace the node arg with a quantized dito and also set dtype |
| 174 | + # to get the right output according to the Edge IR specification: |
| 175 | + # exir/dialects/edge/edge.yaml:3596 |
| 176 | + quantized_full_value = qargs.quantize_value(n.args[1]).item() |
| 177 | + n.update_arg(1, quantized_full_value) |
| 178 | + n.update_kwarg("dtype", qargs.dtype) |
| 179 | + modified = True |
| 180 | + |
| 181 | + return PassResult(graph_module, modified) |
0 commit comments