Skip to content

Commit 580d9bc

Browse files
committed
Qualcomm AI Engine Direct - Order only the passes that run
On HTP, the pass manager runs TagPositionalSinCos after every other pass but the terminal ones, like ResolveDebugHandle, whatever dep_table says, through _get_fixed_passes_dependency. It built that order from every pass in passes_job, including the ones turned off. So with TagPositionalSinCos turned off the documented way, a dep_table that orders another pass after it still failed: passes_job[TagPositionalSinCos][QCOM_PASS_ACTIVATE_KEY] = False dep_table = {RemoveRedundancy: [TagPositionalSinCos]} # RuntimeError: Circular dependency detected A pass that is turned off does not run, so the fixed order is now built from the active passes only. A new test turns TagPositionalSinCos off with such a dep_table and checks that the passes build, without it and with ResolveDebugHandle last. It fails without this change.
1 parent 3f181d3 commit 580d9bc

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

‎backends/qualcomm/_passes/backends/htp/qnn_htp_pass_manager.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def get_passes_dependency_for_capture_program(cls):
4141

4242
@classmethod
4343
def _get_fixed_passes_dependency(cls, passes: List[type]) -> Dict:
44-
# After every pass but the terminal ones, so it tags the graph the
45-
# partitioner sees, with any dep_table.
44+
# After every active pass but the terminal ones, so it tags the graph
45+
# the partitioner sees, with any dep_table.
4646
if TagPositionalSinCos not in passes:
4747
return {}
4848
last = (TagPositionalSinCos, *cls._get_terminal_passes())

‎backends/qualcomm/_passes/qnn_pass_manager.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,14 @@ def get_to_edge_transform_passes(
411411
if dep_table is not None
412412
else self.get_passes_dependency_for_capture_program()
413413
)
414-
fixed_dep_table = self._get_fixed_passes_dependency(list(passes_job))
414+
# A pass that is turned off does not run, so it needs no fixed order.
415+
fixed_dep_table = self._get_fixed_passes_dependency(
416+
[
417+
p
418+
for p, entry in passes_job.items()
419+
if isinstance(entry, dict) and entry.get(QCOM_PASS_ACTIVATE_KEY)
420+
]
421+
)
415422
for that, these in (*dep_table.items(), *fixed_dep_table.items()):
416423
for this in these:
417424
self.add_constraint(this_before_that_pass_constraint(this, that))
@@ -440,8 +447,9 @@ def get_to_edge_transform_passes(
440447
@classmethod
441448
def _get_fixed_passes_dependency(cls, passes: List[type]) -> Dict:
442449
"""
443-
Pass order that a backend needs whatever dep_table the caller gives,
444-
in the same form as get_passes_dependency_for_capture_program().
450+
Order that a backend needs among the active passes, whatever
451+
dep_table the caller gives, in the same form as
452+
get_passes_dependency_for_capture_program().
445453
"""
446454
return {}
447455

‎backends/qualcomm/tests/test_passes.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,6 +3441,23 @@ def test_to_edge_passes_keep_tag_positional_sin_cos_last(self):
34413441
[TagPositionalSinCos, ResolveDebugHandle],
34423442
)
34433443

3444+
def test_to_edge_passes_order_nothing_after_a_turned_off_pass(self):
3445+
# TagPositionalSinCos turned off does not run, so a dep_table may
3446+
# order another pass after it.
3447+
program = torch.export.export(
3448+
torch.nn.Linear(8, 8), (torch.rand(4, 8),), strict=True
3449+
)
3450+
pass_manager = get_qnn_pass_manager_cls()()
3451+
passes_job = pass_manager.get_capture_program_passes()
3452+
passes_job[TagPositionalSinCos][QCOM_PASS_ACTIVATE_KEY] = False
3453+
passes = pass_manager.get_to_edge_transform_passes(
3454+
program,
3455+
passes_job=passes_job,
3456+
dep_table={RemoveRedundancy: [TagPositionalSinCos]},
3457+
)
3458+
self.assertNotIn(TagPositionalSinCos, [type(p) for p in passes])
3459+
self.assertIsInstance(passes[-1], ResolveDebugHandle)
3460+
34443461
@stubbed_recipe_setup()
34453462
def test_qnn_recipe_can_turn_off_tag_positional_sin_cos(self):
34463463
program = torch.export.export(

0 commit comments

Comments
 (0)