Skip to content

Fix control flow output memory aliasing - #21818

Open
JacobSzwejbka wants to merge 2 commits into
mainfrom
fix/21490-hop-output-memory-planning
Open

Fix control flow output memory aliasing#21818
JacobSzwejbka wants to merge 2 commits into
mainfrom
fix/21490-hop-output-memory-planning

Conversation

@JacobSzwejbka

@JacobSzwejbka JacobSzwejbka commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #21490.

Control-flow branches intentionally share a memory arena, but their tensor outputs escape through MoveCall. The planner previously allowed a later higher-order operation to reuse branch storage while the earlier logical output was still live.

Memory planning now assigns each higher-order-op submodule arena a lifespan at its call site. For cond, that lifespan extends through the last use of its logical outputs. The true and false branches retain the same arena location because they are mutually exclusive, while consecutive conditionals receive non-overlapping storage when their outputs are simultaneously live. The emitter continues to use MoveCall; no tensor copy is added.

Regression coverage checks the original nested-condition and mutable-state failure, verifies non-overlapping allocations for consecutive conditional outputs, and asserts that branch results are emitted with MoveCall.

Test plan

pytest -q exir/tests/test_memory_planning.py::TestCond::test_consecutive_cond_output_lifetime exir/emit/test/test_emit.py::TestEmit::test_emit_cond exir/emit/test/test_emit.py::TestEmit::test_emit_cond_output_lifetime (3 passed)

pytest -q exir/tests/test_memory_planning.py -k "not custom_kv_cache_shared_buffers" (37 passed, 1 deselected; the installed custom-op library is binary-incompatible with the local Torch build)

pytest -q exir/emit/test/test_emit.py::TestEmit -k "not test_emit_sym_ite and not test_emit_sym_not" (68 passed, 2 skipped, 2 deselected; the installed ExecuTorch 1.3.1 runtime does not contain those current prim kernels)

python -m ufmt check exir/memory_planning.py exir/tests/test_memory_planning.py exir/emit/_emitter.py exir/emit/test/test_emit.py

python -m flake8 exir/memory_planning.py exir/tests/test_memory_planning.py exir/emit/_emitter.py exir/emit/test/test_emit.py

Authored with Codex.

@pytorch-bot

pytorch-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 58f7f6a with merge base 914705c (image):
💚 Looks good so far! There are no failures yet. 💚

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 13, 2026
@JacobSzwejbka JacobSzwejbka added the release notes: exir Changes to any dialects and passes on these dialects, such as memory planning label Aug 13, 2026
@JacobSzwejbka

Copy link
Copy Markdown
Contributor Author

This approach is wrong gonna try to fix the memory planning issue rather then just deepcopying

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. release notes: exir Changes to any dialects and passes on these dialects, such as memory planning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Higher Order Op Output Memory Planning

1 participant