|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
9 | 9 | import inspect |
| 10 | +import operator |
10 | 11 | from typing import TYPE_CHECKING |
11 | 12 |
|
12 | 13 | import pytest |
|
26 | 27 | QCOM_PASS_ACTIVATE_KEY, |
27 | 28 | QCOM_QUANT_ATTRS, |
28 | 29 | ) |
| 30 | +from executorch.exir.delegate import executorch_call_delegate |
29 | 31 | from executorch.exir.dialects._ops import ops as exir_ops |
30 | 32 | from torch.fx.passes.utils.source_matcher_utils import get_source_partitions |
31 | 33 |
|
@@ -4478,14 +4480,36 @@ class _Relu(torch.nn.Module): |
4478 | 4480 | def forward(self, x): |
4479 | 4481 | return torch.relu(x) |
4480 | 4482 |
|
| 4483 | + @staticmethod |
| 4484 | + def _delegate_output_arity(gm: torch.fx.GraphModule) -> dict[torch.fx.Node, int]: |
| 4485 | + """Map every delegate users to the number of users, so we can ensure |
| 4486 | + both the single-output and multi-output scenarios are covered.""" |
| 4487 | + arity = {} |
| 4488 | + for n in gm.graph.nodes: |
| 4489 | + if n.op == "call_function" and n.target is executorch_call_delegate: |
| 4490 | + getitems = [u for u in n.users if u.target is operator.getitem] |
| 4491 | + arity.update(dict.fromkeys(getitems, len(getitems))) |
| 4492 | + return arity |
| 4493 | + |
4481 | 4494 | @staticmethod |
4482 | 4495 | @unpack_pass_fixtures |
4483 | | - def test(): |
| 4496 | + def test( |
| 4497 | + compile_spec, |
| 4498 | + backend_type: QnnExecuTorchBackendType, |
| 4499 | + ): |
| 4500 | + from executorch.backends.qualcomm._passes.qnn_pass_manager import ( |
| 4501 | + get_qnn_pass_manager_cls, |
| 4502 | + ) |
| 4503 | + from executorch.backends.qualcomm.tests.models import SkipSplitToConcat |
4484 | 4504 | from executorch.backends.qualcomm.utils.constants import ( |
| 4505 | + QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY, |
4485 | 4506 | QCOM_QUANT_ATTRS, |
4486 | 4507 | QCOM_QUANT_ATTRS_MAP, |
4487 | 4508 | QCOM_QUANTIZED_IO, |
4488 | 4509 | ) |
| 4510 | + from executorch.backends.qualcomm.utils.utils import ( |
| 4511 | + to_edge_transform_and_lower_to_qnn, |
| 4512 | + ) |
4489 | 4513 |
|
4490 | 4514 | module = TagQuantIO._Relu() |
4491 | 4515 | inputs = (torch.randn(3, 4),) |
@@ -4529,3 +4553,59 @@ def get_dtype(node): |
4529 | 4553 | f"expected {num_returned} entries in QCOM_QUANT_ATTRS_MAP, " |
4530 | 4554 | f"got {len(n.meta[QCOM_QUANT_ATTRS_MAP])}" |
4531 | 4555 | ) |
| 4556 | + |
| 4557 | + # Trait 3: QCOM_QUANTIZED_IO tags applied before to_backend survive on |
| 4558 | + # delegate-output getitems. Keeping split/cat on CPU splits the graph so |
| 4559 | + # that both the single-output and multi-output scenarios are covered. |
| 4560 | + skip_targets = { |
| 4561 | + exir_ops.edge.aten.split_with_sizes_copy.default, |
| 4562 | + exir_ops.edge.aten.cat.default, |
| 4563 | + } |
| 4564 | + |
| 4565 | + def get_boundary_dtype(node): |
| 4566 | + targets = {node.target, *(u.target for u in node.users)} |
| 4567 | + return torch.uint8 if targets & skip_targets else None |
| 4568 | + |
| 4569 | + passes_job = get_qnn_pass_manager_cls(backend_type).get_capture_program_passes() |
| 4570 | + passes_job[_passes.TagQuantIO][QCOM_PASS_ACTIVATE_KEY] = True |
| 4571 | + passes_job[_passes.TagQuantIO][QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY][ |
| 4572 | + "get_quant_io_dtype_fn" |
| 4573 | + ] = get_boundary_dtype |
| 4574 | + |
| 4575 | + module = SkipSplitToConcat() |
| 4576 | + inputs = ( |
| 4577 | + torch.randn(1, 4, 4, 6), |
| 4578 | + torch.randn(1, 4, 4, 2), |
| 4579 | + torch.randn(1, 4, 4, 2), |
| 4580 | + torch.randn(1, 4, 4, 2), |
| 4581 | + ) |
| 4582 | + edge_prog_mgr = to_edge_transform_and_lower_to_qnn( |
| 4583 | + module, |
| 4584 | + inputs, |
| 4585 | + compile_spec, |
| 4586 | + passes_job=passes_job, |
| 4587 | + skip_node_op_set={t.__name__ for t in skip_targets}, |
| 4588 | + ) |
| 4589 | + |
| 4590 | + gm = edge_prog_mgr.exported_program().graph_module |
| 4591 | + arity = TagQuantIO._delegate_output_arity(gm) |
| 4592 | + |
| 4593 | + # get_boundary_dtype tagged the nodes feeding the CPU-side split/cat, so |
| 4594 | + # after lowering, those delegate outputs must carry the QCOM_QUANTIZED_IO tag. |
| 4595 | + expected = {n for n in arity if any(u.target in skip_targets for u in n.users)} |
| 4596 | + assert expected, "no delegate-output getitem feeds the skipped nodes" |
| 4597 | + |
| 4598 | + tagged = { |
| 4599 | + n: n.meta[QCOM_QUANTIZED_IO] for n in arity if QCOM_QUANTIZED_IO in n.meta |
| 4600 | + } |
| 4601 | + assert tagged == dict.fromkeys(expected, torch.uint8), ( |
| 4602 | + f"delegate outputs should be carrying {QCOM_QUANTIZED_IO!r} {tagged} " |
| 4603 | + f"!= boundaries tagged uint8 before to_backend " |
| 4604 | + f"{dict.fromkeys(expected, torch.uint8)}" |
| 4605 | + ) |
| 4606 | + |
| 4607 | + arities = {arity[n] for n in expected} |
| 4608 | + assert 1 in arities and arities != {1}, ( |
| 4609 | + f"expected both single-output and multi-output delegates to be covered, " |
| 4610 | + f"got output counts {sorted(arities)}" |
| 4611 | + ) |
0 commit comments