Fix memory planning corruption when multiple HOPs have overlapping output lifetimes - #21494
Fix memory planning corruption when multiple HOPs have overlapping output lifetimes#21494gtyukasz wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21494
Note: Links to docs will display an error until the docs builds have been completed.
|
|
Hi @gtyukasz! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
|
Didn't find following labels among repository labels: Release notes: exir |
|
@pytorchbot label "release notes: exir" |
…tput lifetimes When multiple cond/while nodes exist in a graph and an earlier HOP's outputs are still live when a later HOP executes, the later HOP's submodule would overwrite the earlier HOP's results in the shared arena starting at offset 0. Rewrite _apply_algo_to_submodules to process HOPs in graph order, track output lifetimes, and offset later HOP submodule allocations past earlier HOPs' still-live memory. Map/scan outputs don't live in the submodule arena so they are excluded from overlap tracking.
040a60c to
d0f17ec
Compare
Fixes #21490
Summary
torch.condnodes have overlapping output lifetimes — the second cond's submodule arena now allocates at an offset past the firstcond's still-live outputs, preventing
MoveCallaliasing from corrupting datatorch.cond— propagate the parent submodule'sinput_mem_buffer_sizesdown to nested HOPs so their branches don't allocate atoffset 0 and overwrite the enclosing scope's live outputs
_apply_algo_to_submodulesto process all HOP types (cond/while/map/scan) in a single graph-order pass with lifetime tracking, replacing the previous per-typeiteration that had no cross-HOP awareness
Problem
ExecuTorch's
MoveCallinstruction does a shallow pointer move (not a data copy) to transfer a cond branch's output into the outer scope. If a subsequent cond's submodulearena overlaps the first cond's output memory, executing the second cond overwrites data that the first cond's output pointer still references. This manifests as silent
data corruption at runtime.
Two variants:
torch.condat the same level where cond_0's output is still live when cond_1 executestorch.condinside another cond's branch, where the inner branch allocates at offset 0 and overlaps with the outer cond's outputFix
The rewritten
_apply_algo_to_submodulesnow:_hop_output_last_useinput_mem_buffer_sizesand merges it into the offset for nested HOPsTest plan
test_overlapping_cond_outputs_no_corruption— sequential conds with overlapping output lifetimes, verifies runtime outputs against expected values across 4 statefulcalls
test_non_overlapping_conds_share_memory— confirms non-overlapping conds still share memory (no unnecessary bloat)test_nested_cond_no_corruption— nested cond inside a cond branch, verifies the inner branch doesn't corrupt the outer outputCo-Authored by Claude Opus 4.6