Skip to content

Add opt-in interval first-fit memory planner - #22508

Open
seanyang0813 wants to merge 3 commits into
pytorch:mainfrom
seanyang0813:codex/interval-first-fit-memory-planner
Open

Add opt-in interval first-fit memory planner#22508
seanyang0813 wants to merge 3 commits into
pytorch:mainfrom
seanyang0813:codex/interval-first-fit-memory-planner

Conversation

@seanyang0813

@seanyang0813 seanyang0813 commented Sep 3, 2026

Copy link
Copy Markdown

Summary

Current ExecuTorch greedy() already sorts allocations by decreasing size, but its shared-object placement can retain holes when a lower aligned legal offset exists. This adds an opt-in interval first-fit candidate and a bounded conditional portfolio that returns the smaller of current upstream greedy and the candidate.

Default greedy behavior is unchanged. The opt-in policy uses per-memory-ID aligned peak-live lower bounds, inclusive lifetime overlap, hard and soft work limits, and whole-planning-unit fallback for unsupported storage-backed aliases. Equal-size baseline ties are deterministic: allocation-relevant keys are used first, with graph order only for exact key collisions.

Correctness and safety

  • The selected layout is never larger than the deterministic upstream-greedy baseline.
  • A lower-bound early exit is used only for supported planning units and only when the aligned per-arena bound equals greedy.
  • Unsupported aliases return upstream greedy for the entire planning unit.
  • Seven focused tests cover interval improvement, lower-bound semantics, cross-order determinism, work limits, and alias fallback.
  • A 10,000-case randomized differential run passed with zero failures, including 1,000 alias cases.

Current-head evidence

Measurements are pinned to upstream 457a2a8b9f7d103765d73752c5d2efc6b2e8c8bc, PR head 4e5a3906f456120dffd2ec31ea097b50dde17303, and cumulative patch SHA-256 7ad25503ce5d048defd193be39ca08ce38d477f100c3b1d53ec3700d9a91241b.

Frozen holdout Upstream bytes Selected bytes Saved Wins / ties / regressions
10 model cases 156,064,272 136,608,768 19,455,504 (12.466%) 8 / 2 / 0

All ten layouts were valid and deterministic, and all ten serialized upstream-versus-candidate runtime replays were exact. Eight selected layouts reached the sound lower bound. Median-across-case planner times were 17.104 ms for upstream, 45.710 ms for candidate-only, 62.828 ms for always-both, and 65.253 ms for conditional execution; the largest conditional median was 1,077.919 ms on SwinV2-S. Timing is local export-time evidence, not production latency evidence.

ResNet152 and Wide-ResNet101-2 have exact upstream-versus-candidate portable-runtime equality but nonconfirmatory eager diagnostics. Both remain in the results. Emformer and MobileBERT no-gain controls stayed below the frozen 100 ms added-time blocker.

The external benchmark repository contains every model row, exact commands, four-mode timings, retained failures, provenance, and checksums. Immutable snapshot: 07ca869a88c27105b0ae322f90bca259bd95cd32.

Validation and known environment failure

Changed-file lint and diff checks pass. The memory-planning test file reports 51 passes and one local failure because the compiled llama.sdpa_with_kv_cache custom-op library is absent; pristine pinned upstream fails the same test identically.

Scope and review order

The PR changes only exir/memory_planning.py and exir/tests/test_memory_planning.py. No benchmark driver, corpus, generated result, or agent report is included.

Review exir/memory_planning.py first for interval placement, lower-bound soundness, work exits, deterministic baseline ordering, alias fallback, and memory-object IDs. Then review the seven focused tests.

AI disclosure

This patch and evaluation were produced with OpenAI Codex assistance and require maintainer review.

@pytorch-bot

pytorch-bot Bot commented Sep 3, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 15 Awaiting Approval

As of commit 4e5a390 with merge base 457a2a8 (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 Sep 3, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 3, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: seanyang0813 / name: seanyang0813 (a66bd3b)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-03T07:21:30.940062Z a66bd3b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a66bd3b1dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread exir/memory_planning.py
Comment on lines +1374 to +1375
if lower_bound is None or greedy_result.bufsizes == lower_bound:
return greedy_result

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve stable ordering on lower-bound ties

When the declared Set[TensorSpec] input contains equal-size tensors and greedy already reaches the lower bound, this return bypasses _stable_spec_order and preserves greedy's set-iteration-dependent tie ordering. Because greedy inserts equal-size specs without a secondary key, equivalent planning runs in different processes can assign those specs different offsets and produce nondeterministic serialized programs; the added determinism test only repeats the same set instance and therefore cannot detect this. Normalize the baseline's ordering or use the stable candidate layout before returning this tie.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid finding, thanks. Addressed in 3e3016c: the opt-in conditional path now computes graph-stable order once and passes that ordered iterable to upstream greedy before lower-bound, hard-cap, and soft-benefit returns. The test now compares adversarial forward/reverse set iterators instead of repeating one set instance. Default greedy and unsupported-alias fallback remain unchanged. Validation and model reruns for this new head are still pending.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3e3016c and refined in 4e5a390. The opt-in conditional baseline is now ordered deterministically by (allocated size, lifetime start, lifetime end, memory ID), consulting graph order only for exact key collisions; the default greedy path is unchanged. The regression test uses adversarial forward/reverse Set iteration orders rather than repeating one Set instance. Final validation on 4e5a390 includes 10,000 randomized cases with zero failures and exact deterministic replay on all 10 frozen holdout models.

@github-actions

github-actions Bot commented Sep 3, 2026

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.

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.

2 participants