Skip to content

Reuse the graph's fake mode when every input is a lifted constant - #22310

Open
msluszniak wants to merge 1 commit into
pytorch:mainfrom
msluszniak:ms/exportpass-reuse-graph-fake-mode
Open

Reuse the graph's fake mode when every input is a lifted constant#22310
msluszniak wants to merge 1 commit into
pytorch:mainfrom
msluszniak:ms/exportpass-reuse-graph-fake-mode

Conversation

@msluszniak

Copy link
Copy Markdown
Contributor

Summary

Fixes #22309.

_ExportPassBase.call picks the FakeTensorMode to retrace under by scanning self.inputs() for a FakeTensor. inputs() unwraps a constant-carrying fake tensor to its real .constant tensor first, so a graph whose placeholders are all lifted constants yields no FakeTensor at all even though the graph itself is faked. The scan then falls through to opening a brand new mode, the pass retraces under it, and the untouched placeholders keep their original fake tensors. One graph, two modes, rejected downstream by detect_fake_mode():

  File "exir/_program_utils.py", line 27, in get_shape_env
    fake_mode = detect_fake_mode(vals)
AssertionError: fake mode (...) from fake tensor input 0 doesn't match mode (...) from fake tensor input 3

Partitioners produce exactly this shape of submodule: an input-independent subgraph whose scalar operands become lifted constant placeholders when it is split out. A sine positional embedding is enough to trigger it. Because the assertion escapes the pass manager, one such submodule aborts the whole lowering, and the message names whichever pass happened to be running rather than anything about fake modes.

This recovers the graph's own fake mode before falling back to a new one. The fallback still applies when the graph carries no fake tensors at all, so passes that genuinely run without one are unaffected.

Test plan

New case in exir/tests/test_pass_infra.py building a graph whose placeholders are constant-carrying fake tensors and asserting the post-pass graph holds a single mode. It fails on main with AssertionError: 2 != 1 and passes with this change.

pytest exir/tests/test_pass_infra.py
18 passed

No regressions across the core pass suites. Same run on main and on this branch:

pytest exir/tests/test_passes.py exir/tests/test_pass_infra.py \
       exir/tests/test_dynamic_shape_propagation.py exir/tests/test_delegate.py \
       exir/tests/test_memory_planning.py

main:       17 failed, 125 passed, 1 xfailed
this branch: 17 failed, 126 passed, 1 xfailed

The 17 failures are identical on both (byte-identical failure lists) and are pre-existing in my environment, which lacks the compiled portable lib.

pytest backends/vulkan/test/test_vulkan_passes.py \
       backends/vulkan/test/test_serialization.py \
       backends/vulkan/test/test_vulkan_tensor_repr.py
102 passed

End to end: RF-DETR nano now lowers to the Vulkan backend. It needs #22307 and #22308 as well, since it hits all three; with the three applied together, to_edge_transform_and_lower completes over its 15 delegate submodules.

_ExportPassBase.call picks the FakeTensorMode to retrace under by
scanning self.inputs() for a FakeTensor. inputs() unwraps a
constant-carrying FakeTensor to its real .constant tensor:

    fake = node.meta["val"]
    if hasattr(fake, "constant") and fake.constant is not None:
        return fake.constant

so a graph whose placeholders are all lifted constants yields no
FakeTensor at all, even though the graph itself is faked. The scan then
falls through to opening a brand new FakeTensorMode, and the pass
retraces under it while the placeholders keep their original fake
tensors. The result is one graph holding tensors from two modes, which
detect_fake_mode() rejects downstream:

  File "exir/_program_utils.py", line 27, in get_shape_env
    fake_mode = detect_fake_mode(vals)
  AssertionError: fake mode (...) from fake tensor input 0 doesn't match
  mode (...) from fake tensor input 3

Partitioners produce exactly this shape of submodule: an input
independent subgraph, whose scalar operands are lifted to constant
placeholders when it is split out. A sine positional embedding is enough
to trigger it, and it aborts the whole lowering because the assertion
escapes the pass manager. Seen with the Vulkan backend on RF-DETR.

Recover the graph's own fake mode before falling back to a new one. The
fallback still applies when the graph carries no fake tensors at all, so
passes that genuinely run without one are unaffected.
@pytorch-bot

pytorch-bot Bot commented Aug 29, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22310

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 14 Awaiting Approval

As of commit 754fc0f with merge base c27baa8 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 29, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

msluszniak added a commit to software-mansion-labs/executorch that referenced this pull request Aug 30, 2026
_ExportPassBase.call scans self.inputs() for a FakeTensor to pick the
mode to retrace under, but inputs() unwraps a constant-carrying
FakeTensor to its real .constant tensor. A graph whose placeholders are
all lifted constants therefore yields no FakeTensor, falls through to a
brand new FakeTensorMode, and ends up holding tensors from two modes,
which detect_fake_mode() rejects downstream.

Partitioners produce exactly this shape of submodule. Seen with the
Vulkan backend on RF-DETR.

Backport of upstream pytorch/executorch#22310.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ExportPass opens a second FakeTensorMode when every input is a lifted constant, aborting lowering

2 participants