Skip to content

Commit 61d0880

Browse files
authored
[Qualcomm] Partitioner falls back on ops without a node visitor (pytorch#21607)
# [Qualcomm] Partitioner falls back on ops without a node visitor (fix KeyError) ## Summary `QnnOperatorSupport.is_node_supported` indexed `self.node_visitors[node.target.__name__]` directly, so **any op that has no QNN node visitor raised `KeyError` and aborted the entire partition** instead of falling back to CPU. XNNPACK's partitioner returns `False` for unknown ops; QNN should do the same. ## Symptom Lowering **Mamba2** to QNN crashes: ``` File ".../backends/qualcomm/partition/qnn_partitioner.py", line 113, in is_node_supported op_wrapper = self.node_visitors[node.target.__name__].define_node(...) KeyError: 'aten.bitwise_not.default' ``` Mamba2's causal mask uses `~torch.tril(...)`, which traces to `aten.bitwise_not.default` — an op QNN ships no visitor for. This is **not** SSM/conv specific: any graph containing an op without a visitor hits it. ## Fix Guard the lookup and return `False` (→ CPU fallback) for unsupported ops: ```python supported = False if node.target.__name__ not in self.node_visitors: logger.info(f"[{self.phase}] {node.target.__name__} | No node visitor, unsupported") return False op_wrapper = self.node_visitors[node.target.__name__].define_node(...) ``` ## Testing `test_partitioner_falls_back_on_op_without_visitor` (in `test_passes.py`): lowers a module whose graph contains `aten.bitwise_not.default` and asserts `to_executorch()` succeeds. **Fails on the pre-fix code with the exact `KeyError` above; passes after.** Verified by file-swap. ## Notes - Optional follow-up (not in this PR): add `aten.bitwise_not.default` to the `op_logical_not` visitor's target list so it delegates to QNN (on a bool tensor it's equivalent to `logical_not`), putting more of Mamba2 on the HTP instead of CPU. - Surfaced by the HuggingFace transformers ExecuTorch-exporter QNN work (PR #47747), which currently skips SSM with an inaccurate "CanonicalizeConv unsqueezes a conv bias" comment — the real cause is this partitioner KeyError. cc @cbilgin
1 parent e60faa2 commit 61d0880

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎backends/qualcomm/partition/common_defs.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
exir_ops.edge.aten.median.dim,
2626
exir_ops.edge.aten.round.decimals,
2727
exir_ops.edge.aten.le.Scalar,
28+
exir_ops.edge.aten.bitwise_not.default,
2829
]
2930

3031
constant_operator = [

‎backends/qualcomm/partition/qnn_partitioner.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ def is_node_supported(self, _, node: torch.fx.Node) -> bool:
110110
return False
111111

112112
supported = False
113+
if node.target.__name__ not in self.node_visitors:
114+
logger.info(
115+
f"[{self.phase}] {node.target.__name__} | No node visitor, unsupported"
116+
)
117+
return False
113118
op_wrapper = self.node_visitors[node.target.__name__].define_node(
114119
node, self.nodes_to_wrappers
115120
)

‎backends/qualcomm/tests/test_passes.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,46 @@ def test_decompose_hardsigmoid_backend_aware(self):
473473
f"hardsigmoid {'should' if should_decompose else 'should NOT'} be decomposed for {backend.name}",
474474
)
475475

476+
def test_partitioner_falls_back_on_op_without_visitor(self):
477+
"""QnnOperatorSupport must reject an op that has no node visitor by returning
478+
False (CPU fallback), not by KeyError-ing on the node_visitors lookup.
479+
480+
Uses aten.frac.default: it has no QNN node visitor and is not on any partition
481+
operator list, so it exercises the missing-visitor guard directly. (bitwise_not,
482+
the op that first surfaced this on Mamba2, is now in to_be_implemented_operator,
483+
which returns earlier and would not reach the guard.)
484+
"""
485+
486+
class FracModule(torch.nn.Module):
487+
def forward(self, x):
488+
return torch.frac(x) + x
489+
490+
sample_input = (torch.randn(1, 4),)
491+
492+
# Guard against a vacuous test: the visitor-less op must actually be present.
493+
exported = torch.export.export(FracModule().eval(), sample_input)
494+
self.assertTrue(
495+
any(
496+
node.op == "call_function" and "frac" in str(node.target)
497+
for node in exported.graph.nodes
498+
),
499+
"expected aten.frac.default in the traced graph",
500+
)
501+
502+
compiler_specs = generate_qnn_executorch_compiler_spec(
503+
soc_model=QcomChipset.SM8650,
504+
backend_options=generate_htp_compiler_spec(use_fp16=True),
505+
)
506+
try:
507+
# Must not raise KeyError: frac falls back to CPU; the rest may delegate.
508+
edge = to_edge_transform_and_lower_to_qnn(
509+
FracModule().eval(), sample_input, compiler_specs
510+
)
511+
edge.to_executorch()
512+
except RuntimeError as e:
513+
if "QNN" in str(e) or "qnn" in str(e):
514+
self.skipTest(f"QNN SDK not available: {e}")
515+
476516
def test_index_put_int64_value_not_quantized(self):
477517
"""QNN's IndexPut annotator must skip a non-float (int64) value arg.
478518

0 commit comments

Comments
 (0)