Skip to content

Allow bypasses for Precompile when guards, etc. cannot be serialized - #160902

Closed
jamesjwu wants to merge 5 commits into
gh/jamesjwu/185/basefrom
gh/jamesjwu/185/head
Closed

jamesjwu wants to merge 5 commits into
gh/jamesjwu/185/basefrom
gh/jamesjwu/185/head

Conversation

@jamesjwu

@jamesjwu jamesjwu commented Aug 18, 2025 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

This adds a new function bypass_package and CompilePackage.bypass_current_entry(). This allows us to safely bypass if there are models with unserializable or incompatible parts. When we encounter something incompatible, we'll raise a bypass and ignore that particular code in DynamoCodeEntry.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames @Lucaskabela

Differential Revision: D80725139

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 18, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit ee17c50 with merge base 5ee464d (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

jamesjwu added a commit that referenced this pull request Aug 18, 2025
ghstack-source-id: 8727f80
Pull-Request: #160902
[ghstack-poisoned]
jamesjwu added a commit that referenced this pull request Aug 18, 2025
ghstack-source-id: 385853b
Pull-Request: #160902
@jamesjwu jamesjwu added topic: not user facing topic category ciflow/trunk Trigger trunk jobs on your pull request labels Aug 18, 2025
[ghstack-poisoned]
jamesjwu added a commit that referenced this pull request Aug 19, 2025
ghstack-source-id: 354f0dc
Pull-Request: #160902
[ghstack-poisoned]
jamesjwu added a commit that referenced this pull request Aug 19, 2025
ghstack-source-id: 351d616
Pull-Request: #160902
@jamesjwu
jamesjwu marked this pull request as ready for review August 19, 2025 20:50
@jamesjwu
jamesjwu requested a review from zhxchen17 August 19, 2025 20:50
@jamesjwu jamesjwu changed the title Precompile bypassing feature Allow bypasses for Precompile when guards, etc. cannot be serialized Aug 19, 2025
@jamesjwu

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -b main

@pytorch-bot

pytorch-bot Bot commented Aug 20, 2025

Copy link
Copy Markdown

❌ 🤖 pytorchbot command failed:

@pytorchbot: error: unrecognized arguments: -b main

usage: @pytorchbot [-h] {merge,revert,rebase,label,drci,cherry-pick} ...

Try @pytorchbot --help for more info.

@jamesjwu

Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase -b main

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased gh/jamesjwu/185/orig onto refs/remotes/origin/main, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/160902)

pytorchmergebot pushed a commit that referenced this pull request Aug 20, 2025
ghstack-source-id: 360b860
Pull-Request: #160902
@jamesjwu
jamesjwu requested a review from oulgen August 21, 2025 18:10
@jamesjwu

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@jamesjwu

Copy link
Copy Markdown
Contributor Author

@jamesjwu has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
…ytorch#160902)

This adds a new function `bypass_package` and `CompilePackage.bypass_current_entry()`. This allows us to safely bypass if there are models with unserializable or incompatible parts. When we encounter something incompatible, we'll raise a bypass and ignore that particular code in DynamoCodeEntry.

Pull Request resolved: pytorch#160902
Approved by: https://github.com/zhxchen17
@github-actions
github-actions Bot deleted the gh/jamesjwu/185/head branch September 21, 2025 02:15
pytorchmergebot pushed a commit that referenced this pull request Sep 11, 2026
…t the whole entry (#196505)

A `CompilePackage` keeps one `_DynamoCodeCacheEntry` per code object, and
every compile of that code object -- the first and each later recompile for a
new guard variant -- appends its guarded code and backend id into that one
entry. When a compile could not be packaged, `bypass_current_entry` set
`entry.bypassed = True` and `install()` then skipped the WHOLE entry on
reload: variants of the same frame that had serialized fine were thrown away
with the one that had not, and a later serializable recompile could not
un-poison the entry.

There is no correctness reason for that granularity. Each variant's guards are
sound for its own bytecode, so installing the variants that serialized while
dropping the one that did not is safe: inputs that would have matched the
dropped variant miss every installed guard and are traced fresh in the loading
process, which is exactly what the whole frame did before. The whole-entry
flag came from #160902 and reflected the usual case (an unpicklable guarded
value is typically a default or closure that every variant guards on) plus the
fact that nothing attributed backend ids to the compile that registered them.

`code_context` now records the backend ids the compile inside it NEWLY added
to the entry, and `bypass_current_compile` (renamed from `bypass_current_entry`)
removes exactly those from the entry and from `cached_backends`; a backend an
earlier, installed variant still needs can never be stripped. Earlier variants
stay. Reading `output.package` also means a bypassed compile no longer records
its inlined sources or updates the package's device type (both used to run
regardless), and that a later successful compile of the same code object
records its inlined sources again, which the sticky per-entry flag used to
suppress. Import sources and the resume function a bypassed compile registered
stay on the entry (both pre-existing and inert on load); only its backend ids
are dropped.

For the compile's own guarded code never to be recorded, `_compile` has to
consult the package on the OUTPUT GRAPH, which `bypass_package` clears, rather
than its local `package` variable. Reading the local also tripped an assertion
on the missing `guards_state` whenever guards themselves failed to serialize;
here it matters for a bypass raised before guards are built (a graph holding a
named parameter under `prepare_freezing`): with the old `bypassed` early
returns gone, the local-variable check would record the bypassed compile's
guarded code and re-derive a backend id nothing cached, and the save would
fail or drop the frame. So the `output.package` switch lands in this commit.

`bypassed` is kept but becomes derived state, "this entry currently has
nothing installable": set when a bypass leaves the entry with no guarded code,
cleared when a compile records one. It still has to exist because `install()`
treats an entry with zero guarded codes as a trivial function and `skip_code`s
it, which would stop the frame from ever being traced again after a reload.
The other writer, `PrecompileCacheEntry.from_cache_entry` (run by
`PrecompileContext.create_cache_entries` at SAVE time, on the live entries,
when a backend artifact is missing), is unchanged and still flags the whole
entry while keeping its stale guarded codes; making that path per-variant too
is a follow-up.

Test Plan:

```
python test/dynamo/test_package.py -k test_bypass_drops_only_the_current_compiles_backend -k test_bypass_of_every_compile_marks_the_entry_bypassed -k test_bypassed_guards_keep_the_frames_earlier_variant -k test_bypass_before_guards_keeps_the_frames_earlier_variant
```

Two end-to-end tests compile two variants of one frame, save, reload, and hit
the first variant under `fail_on_recompile`. In the first the second variant's
guards cannot be serialized (a guarded value whose `__reduce__` raises
`AttributeError`, the one exception the pickler maps to `PackageError` at this
point in the stack); on main that compile crashes on the `guards_state`
assertion. In the second the bypass fires before guards are built (the
named-parameter path); without the convert_frame change the saved entry
carries a backend id nothing cached (the count assertion fails) and the
caching_precompile save then drops the whole frame.

Authored with an AI assistant.

FAQ

Q: `from_cache_entry` still sets `code.bypassed = True` on a missing backend
but leaves the id in `backend_ids`; now that `add_guarded_code` clears
`bypassed`, does a same-process recompile re-expose the stale id?

A: Yes, and the steady state is the same as before this change.
`from_cache_entry` runs at save time on the live entries, so a save after such
a recompile re-flags the entry before writing it (before this change it also
carried the new compile's id and stayed `bypassed=True`); the written entry
carries `bypassed=True`, and the next process's `install()` skips it before
the missing-backend `RuntimeError`, so the frame is never served from cache and
never crashes. Dropping
only the id at load is not a safe fix: the stale guarded codes whose bytecode
references that `__compiled_fn` would survive, be saved `bypassed=False` after
a recompile, and be installed with no backend in the next process, giving a
`NameError` on a guard hit. Making the load path per-variant means pruning
those guarded codes (via `_backend_ids_from_code` on the deserialized
bytecode) together with the id and deriving `bypassed = not guarded_codes`;
that is the follow-up the message names, kept out of this change to keep it
to the save side.

Pull Request resolved: #196505
Approved by: https://github.com/pytorchgreenlight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants