|
17 | 17 | if TYPE_CHECKING: |
18 | 18 | import torch.fx |
19 | 19 |
|
| 20 | +import copy |
| 21 | + |
20 | 22 | import torch |
21 | 23 | from torch.export import ExportedProgram |
22 | 24 |
|
| 25 | +from tico.quantization.passes.insert_quantize_on_dtype_mismatch import qparam_dtype |
| 26 | + |
23 | 27 | from tico.serialize.quant_param import QPARAM_KEY, QuantParam |
24 | 28 | from tico.utils import logging |
| 29 | +from tico.utils.graph import create_node |
25 | 30 | from tico.utils.passes import PassBase, PassResult |
26 | 31 | from tico.utils.trace_decorators import trace_graph_diff_on_pass |
27 | | -from tico.utils.utils import get_quant_dtype |
| 32 | +from tico.utils.utils import get_quant_dtype, quant_min_max, set_new_meta_val |
28 | 33 | from tico.utils.validate_args_kwargs import ( |
29 | 34 | DequantizePerTensorArgs, |
30 | 35 | QuantizePerTensorArgs, |
31 | 36 | ) |
32 | 37 |
|
33 | 38 |
|
| 39 | +def _insert_mx_quantize_op(node, qparam): |
| 40 | + graph = node.graph |
| 41 | + assert qparam.quantized_dimension is not None |
| 42 | + assert qparam.dtype is not None |
| 43 | + |
| 44 | + with graph.inserting_after(node): |
| 45 | + q_args = (node, qparam.dtype, qparam.quantized_dimension) |
| 46 | + quantize = create_node( |
| 47 | + graph, |
| 48 | + torch.ops.circle_custom.quantize_mx_decomposed.default, |
| 49 | + args=q_args, |
| 50 | + ) |
| 51 | + |
| 52 | + node.replace_all_uses_with(quantize, propagate_meta=True) |
| 53 | + quantize.replace_input_with(quantize, node) |
| 54 | + |
| 55 | + quantize.meta[QPARAM_KEY] = copy.deepcopy(qparam) |
| 56 | + |
| 57 | + return quantize |
| 58 | + |
| 59 | + |
| 60 | +def _insert_quantize_op(node, qparam): |
| 61 | + graph = node.graph |
| 62 | + min_, max_ = quant_min_max(qparam.dtype) |
| 63 | + dtype = getattr(torch, qparam.dtype) |
| 64 | + |
| 65 | + with graph.inserting_after(node): |
| 66 | + q_args = (node, qparam.scale[0], qparam.zero_point[0], min_, max_, dtype) |
| 67 | + quantize = create_node( |
| 68 | + graph, |
| 69 | + torch.ops.quantized_decomposed.quantize_per_tensor.default, |
| 70 | + args=q_args, |
| 71 | + ) |
| 72 | + |
| 73 | + node.replace_all_uses_with(quantize, propagate_meta=True) |
| 74 | + quantize.replace_input_with(quantize, node) |
| 75 | + |
| 76 | + quantize.meta[QPARAM_KEY] = copy.deepcopy(qparam) |
| 77 | + |
| 78 | + return quantize |
| 79 | + |
| 80 | + |
34 | 81 | @trace_graph_diff_on_pass |
35 | 82 | class FoldQuantOps(PassBase): |
36 | 83 | """ |
@@ -114,6 +161,15 @@ def call(self, exported_program: ExportedProgram) -> PassResult: |
114 | 161 | dq.replace_all_uses_with(op, propagate_meta=False) |
115 | 162 |
|
116 | 163 | logger.debug(f"{q.name} and {dq.name} are folded to {op.name}.") |
| 164 | + assert ( |
| 165 | + QPARAM_KEY not in dq.meta |
| 166 | + ) # we should not abandon quantization calibrated parameters |
| 167 | + # if QPARAM_KEY in dq.meta: #right now it's not needed |
| 168 | + # if (qparam_dtype(op) == "int16" or qparam_dtype(op) == "uint8") and qparam_dtype(dq) == "mxint8": |
| 169 | + # #need to insert requantization |
| 170 | + # assert(False) |
| 171 | + # _insert_mx_quantize_op(op, dq.meta[QPARAM_KEY]) |
| 172 | + |
117 | 173 | # ─────────────────────────────────────────── |
118 | 174 | # Case 2: op already quantized |
119 | 175 | # 2.1 same dtype → nothing to do |
@@ -145,6 +201,78 @@ def call(self, exported_program: ExportedProgram) -> PassResult: |
145 | 201 | dq.replace_all_uses_with(op, propagate_meta=False) |
146 | 202 | logger.debug(f"Removed redundant {dq.name}") |
147 | 203 |
|
| 204 | + for dq in graph.nodes: |
| 205 | + if dq.op != "call_function": |
| 206 | + continue |
| 207 | + if dq.target != torch.ops.circle_custom.dequantize_mx_decomposed.default: |
| 208 | + continue |
| 209 | + |
| 210 | + dq_args = dq.args |
| 211 | + |
| 212 | + q = dq_args[0] # type: ignore[index] |
| 213 | + if q.target != torch.ops.circle_custom.quantize_mx_decomposed.default: |
| 214 | + continue |
| 215 | + q_args = q.args |
| 216 | + op = q_args[0] # type: ignore[index] |
| 217 | + |
| 218 | + # Check if Q and DQ have same parameters |
| 219 | + if q_args[1] != dq_args[1]: # type: ignore[index] |
| 220 | + continue |
| 221 | + if q_args[2] != dq_args[2]: # type: ignore[index] |
| 222 | + continue |
| 223 | + |
| 224 | + # ─────────────────────────────────────────── |
| 225 | + # Case 1: op not yet quantized |
| 226 | + # ─────────────────────────────────────────── |
| 227 | + if QPARAM_KEY not in op.meta: |
| 228 | + # TODO |
| 229 | + qparam = QuantParam() |
| 230 | + qparam.dtype = "mxint8" # q_args[1] #TODO |
| 231 | + qparam.quantized_dimension = q_args[2] # type: ignore[index] |
| 232 | + op.meta[QPARAM_KEY] = qparam |
| 233 | + |
| 234 | + dq.replace_all_uses_with(op, propagate_meta=False) |
| 235 | + |
| 236 | + logger.debug(f"{q.name} and {dq.name} are folded to {op.name}.") |
| 237 | + if QPARAM_KEY in dq.meta: |
| 238 | + if qparam_dtype(op) == "mxint8" and ( |
| 239 | + qparam_dtype(dq) == "int16" or qparam_dtype(dq) == "uint8" |
| 240 | + ): |
| 241 | + # need to insert requantization |
| 242 | + _insert_quantize_op(op, dq.meta[QPARAM_KEY]) |
| 243 | + |
| 244 | + # ─────────────────────────────────────────── |
| 245 | + # Case 2: op already quantized |
| 246 | + # 2.1 same dtype → nothing to do |
| 247 | + # 2.2 diff dtype → leave Q in place |
| 248 | + # ─────────────────────────────────────────── |
| 249 | + else: |
| 250 | + op_qparam: QuantParam = op.meta[QPARAM_KEY] # type: ignore[no-redef] |
| 251 | + qdq_dtype = "mxint8" # q_args[1] #TODO |
| 252 | + |
| 253 | + if op_qparam.dtype != qdq_dtype: |
| 254 | + # Attach QPARAM to Q once |
| 255 | + if QPARAM_KEY not in q.meta: |
| 256 | + qparam = QuantParam() |
| 257 | + qparam.dtype = qdq_dtype |
| 258 | + qparam.quantized_dimension = q_args[2] # type: ignore[index] |
| 259 | + q.meta[QPARAM_KEY] = qparam |
| 260 | + assert len(q.users) == 1, "Fix me unless" |
| 261 | + |
| 262 | + dq.replace_all_uses_with(q, propagate_meta=False) |
| 263 | + logger.debug(f"{dq.name} is folded ({q.name} is left).") |
| 264 | + else: |
| 265 | + # Same dtype → the Quantize–Dequantize pair is redundant. |
| 266 | + assert not op_qparam.scale |
| 267 | + assert not op_qparam.zero_point |
| 268 | + assert op_qparam.dtype and op_qparam.dtype == "mxint8" # TODO |
| 269 | + assert ( |
| 270 | + op_qparam.quantized_dimension is not None |
| 271 | + and op_qparam.quantized_dimension == q_args[2] # type: ignore[index] |
| 272 | + ) |
| 273 | + dq.replace_all_uses_with(op, propagate_meta=False) |
| 274 | + logger.debug(f"Removed redundant {dq.name}") |
| 275 | + |
148 | 276 | graph.eliminate_dead_code() |
149 | 277 | graph.lint() |
150 | 278 | graph_module.recompile() |
|
0 commit comments