Skip to content

Commit c6213ae

Browse files
Qualcomm AI Engine Direct - Add test for delegate getitem nodes preserving quant IO tags (pytorch#21772)
### Summary Add regression test follow-up by pytorch#20805 ### Test plan ```bash pytest backends/qualcomm/tests/rework/passes/test.py -k "tag_quant_io" -v ```
1 parent 438bc2e commit c6213ae

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

‎backends/qualcomm/tests/rework/src/pattern.py‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import inspect
10+
import operator
1011
from typing import TYPE_CHECKING
1112

1213
import pytest
@@ -26,6 +27,7 @@
2627
QCOM_PASS_ACTIVATE_KEY,
2728
QCOM_QUANT_ATTRS,
2829
)
30+
from executorch.exir.delegate import executorch_call_delegate
2931
from executorch.exir.dialects._ops import ops as exir_ops
3032
from torch.fx.passes.utils.source_matcher_utils import get_source_partitions
3133

@@ -4478,14 +4480,36 @@ class _Relu(torch.nn.Module):
44784480
def forward(self, x):
44794481
return torch.relu(x)
44804482

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+
44814494
@staticmethod
44824495
@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
44844504
from executorch.backends.qualcomm.utils.constants import (
4505+
QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY,
44854506
QCOM_QUANT_ATTRS,
44864507
QCOM_QUANT_ATTRS_MAP,
44874508
QCOM_QUANTIZED_IO,
44884509
)
4510+
from executorch.backends.qualcomm.utils.utils import (
4511+
to_edge_transform_and_lower_to_qnn,
4512+
)
44894513

44904514
module = TagQuantIO._Relu()
44914515
inputs = (torch.randn(3, 4),)
@@ -4529,3 +4553,59 @@ def get_dtype(node):
45294553
f"expected {num_returned} entries in QCOM_QUANT_ATTRS_MAP, "
45304554
f"got {len(n.meta[QCOM_QUANT_ATTRS_MAP])}"
45314555
)
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

Comments
 (0)