|
15 | 15 | from executorch.backends.qualcomm._passes.qnn_pass_manager import ( |
16 | 16 | get_qnn_pass_manager_cls, |
17 | 17 | ) |
| 18 | +from executorch.backends.qualcomm.builders.node_visitor import ( |
| 19 | + NodeVisitor, |
| 20 | + to_dq_op, |
| 21 | + to_q_op, |
| 22 | +) |
18 | 23 | from executorch.backends.qualcomm.builders.op_custom_op import ( |
19 | 24 | _resolve_qnn_data_type, |
20 | 25 | CustomOp, |
21 | 26 | ) |
22 | 27 | from executorch.backends.qualcomm.builders.qnn_constants import OpContextLoader |
| 28 | +from executorch.backends.qualcomm.builders.utils import is_parameter |
23 | 29 | from executorch.backends.qualcomm.partition.qnn_partitioner import QnnOperatorSupport |
24 | 30 | from executorch.backends.qualcomm.qnn_preprocess import QnnBackend |
25 | 31 | from executorch.backends.qualcomm.quantizer.quantizer import QnnQuantizer, QuantDtype |
|
34 | 40 | Reciprocal, |
35 | 41 | TopKandIndex, |
36 | 42 | ) |
| 43 | +from executorch.backends.qualcomm.utils.constants import ( |
| 44 | + QCOM_ENCODING, |
| 45 | + QCOM_QUANT_ATTRS, |
| 46 | + QCOM_SCALE, |
| 47 | + QCOM_ZERO_POINTS, |
| 48 | +) |
37 | 49 | from executorch.backends.qualcomm.utils.utils import ( |
38 | 50 | generate_htp_compiler_spec, |
39 | 51 | generate_qnn_executorch_compiler_spec, |
@@ -212,6 +224,100 @@ def test_insert_io_qdq_no_revisit(self): |
212 | 224 | # one quantize (input) and one dequantize (output) = +2 nodes. |
213 | 225 | self.assertEqual(node_count_after, node_count_before + 2) |
214 | 226 |
|
| 227 | + def test_q_dq_map_pins_per_channel_group_pairs(self): |
| 228 | + """to_q_op / to_dq_op must map per_channel_group q<->dq to each other. |
| 229 | +
|
| 230 | + ``to_dq_op`` short-circuits for targets already in ``dq_ops``, so the |
| 231 | + map is only consulted when converting across the pair; a wrong entry |
| 232 | + here would surface later as an assertion in ``insert_quant_node``. |
| 233 | + """ |
| 234 | + quantize_per_channel_group = ( |
| 235 | + exir_ops.edge.quantized_decomposed.quantize_per_channel_group.default |
| 236 | + ) |
| 237 | + dequantize_per_channel_group = ( |
| 238 | + exir_ops.edge.quantized_decomposed.dequantize_per_channel_group.default |
| 239 | + ) |
| 240 | + self.assertIs(to_q_op(dequantize_per_channel_group), quantize_per_channel_group) |
| 241 | + self.assertIs( |
| 242 | + to_dq_op(quantize_per_channel_group), dequantize_per_channel_group |
| 243 | + ) |
| 244 | + self.assertIs(to_q_op(quantize_per_channel_group), quantize_per_channel_group) |
| 245 | + self.assertIs( |
| 246 | + to_dq_op(dequantize_per_channel_group), dequantize_per_channel_group |
| 247 | + ) |
| 248 | + |
| 249 | + def test_insert_io_qdq_per_channel_group_dequantizes_output(self): |
| 250 | + """InsertIOQDQ must insert a dequantize_per_channel_group before output. |
| 251 | +
|
| 252 | + A pre-quantized group-wise (e.g. int4 LLM) weight parameter that feeds |
| 253 | + the graph output takes the dequantize-before-output branch. The |
| 254 | + insert-quantize-after-input branch is skipped for parameters, so the |
| 255 | + output branch is the only place the encoding is resolved. |
| 256 | + """ |
| 257 | + graph_module, exported_program = self._build_quantized_graph() |
| 258 | + |
| 259 | + parameter_node = next( |
| 260 | + node |
| 261 | + for node in graph_module.graph.nodes |
| 262 | + if node.op == "placeholder" and is_parameter(node, exported_program) |
| 263 | + ) |
| 264 | + output_node = next( |
| 265 | + node for node in graph_module.graph.nodes if node.op == "output" |
| 266 | + ) |
| 267 | + |
| 268 | + # Annotate the parameter as a group-wise quantized weight. |
| 269 | + scales = torch.ones(4, 1) |
| 270 | + parameter_node.meta[QCOM_QUANT_ATTRS] = { |
| 271 | + QCOM_ENCODING: exir_ops.edge.quantized_decomposed.dequantize_per_channel_group.default, |
| 272 | + QCOM_SCALE: scales, |
| 273 | + "scales": scales, |
| 274 | + "zero_points": None, |
| 275 | + "quant_min": -8, |
| 276 | + "quant_max": 7, |
| 277 | + "dtype": torch.int8, |
| 278 | + "group_size": 1, |
| 279 | + "output_dtype": torch.float32, |
| 280 | + } |
| 281 | + |
| 282 | + existing_outputs = output_node.args[0] |
| 283 | + if not isinstance(existing_outputs, tuple): |
| 284 | + existing_outputs = (existing_outputs,) |
| 285 | + output_node.args = (existing_outputs + (parameter_node,),) |
| 286 | + graph_module.graph.lint() |
| 287 | + graph_module.recompile() |
| 288 | + |
| 289 | + InsertIOQDQ(exported_program)._insert(graph_module) |
| 290 | + |
| 291 | + dequantize_target = ( |
| 292 | + exir_ops.edge.quantized_decomposed.dequantize_per_channel_group.default |
| 293 | + ) |
| 294 | + dequantize_nodes_feeding_output = [ |
| 295 | + node |
| 296 | + for node in graph_module.graph.nodes |
| 297 | + if node.op == "call_function" |
| 298 | + and node.target == dequantize_target |
| 299 | + and any(user.op == "output" for user in node.users.keys()) |
| 300 | + ] |
| 301 | + self.assertEqual(len(dequantize_nodes_feeding_output), 1) |
| 302 | + |
| 303 | + def test_make_qnn_per_block_config_rejects_asymmetric(self): |
| 304 | + """QNN blockwise expansion is symmetric-only; non-zero zero_points must |
| 305 | + raise instead of silently lowering to a symmetric encoding.""" |
| 306 | + graph_module, exported_program = self._build_quantized_graph() |
| 307 | + parameter_node = next( |
| 308 | + node |
| 309 | + for node in graph_module.graph.nodes |
| 310 | + if node.op == "placeholder" and is_parameter(node, exported_program) |
| 311 | + ) |
| 312 | + |
| 313 | + visitor = NodeVisitor({}, exported_program, enable_tensor_dump=False) |
| 314 | + quant_attrs = { |
| 315 | + QCOM_ENCODING: exir_ops.edge.quantized_decomposed.dequantize_per_channel_group.default, |
| 316 | + QCOM_ZERO_POINTS: torch.tensor([0, 1, 0, 0]), |
| 317 | + } |
| 318 | + with self.assertRaisesRegex(ValueError, "symmetric"): |
| 319 | + visitor.make_qnn_per_block_config(parameter_node, quant_attrs) |
| 320 | + |
215 | 321 | def test_insert_reshape_for_argmax(self): |
216 | 322 | class ArgmaxModule(torch.nn.Module): |
217 | 323 | def forward(self, x): |
|
0 commit comments