Skip to content

[precompile] Don't lose a frame to a served artifact installed on a guarded object - #194677

Closed
bobrenjc93 wants to merge 3 commits into
gh/bobrenjc93/1069/basefrom
gh/bobrenjc93/1069/head
Closed

bobrenjc93 wants to merge 3 commits into
gh/bobrenjc93/1069/basefrom
gh/bobrenjc93/1069/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Install a loaded artifact onto a live object, then capture again with that
object in scope, and the frame dies:

torch_dynamo_resume_in_fwd_loss_bwd_at_208
TypeError: cannot pickle '_thread.RLock' object
  reached via: local_scope['pipeline']._precompiled_fwd_loss_bwd
                 ._compiled._inner._state._lock

The lock is ours. _precompiled_fwd_loss_bwd is the handle a previous load
installed, ._inner._state is the session's condition variable, and the guard
tree walks from the guarded pipeline straight into it. convert_frame catches
the PackageError and runs the frame eager, so the capture reports success while
covering less -- and the frame that trips is a resume function of the capture
entry region, so it is on the hot path rather than a corner.

The structural reason it is reachable at all: the pruning in
pickle_guards_state walks the top-level leaves of local_scope, and the
pickler walks everything under them. Anything nested inside an object a guard
DOES read is therefore never pruned and needs a reducer of its own.

Give precompile's own handles one. Nothing can guard on a served artifact, so
it drops the way a pruned value does.

Deliberately narrow. The first version keyed on locks generally, which broke
two existing tests -- and they were right to break: a lock in the CALLER's
model still fails the frame with its path named, because that is something they
can act on. This one is ours, installed by us, and a caller cannot remove it.

Test Plan:

python test/test_precompile.py
python test/dynamo/test_guard_serialization.py
python test/dynamo/test_package.py

The new test pickles a handle nested inside a guarded object and asserts it
comes back as the missing sentinel with its siblings intact, and asserts a bare
lock is NOT treated this way -- the property the two
test_unpicklable_guard_value_names_where_it_lives cases depend on.

Not reproduced end to end: reaching it needs a guard set that walks into an
installed handle, and every synthetic shape I built pruned the handle before
the pickler got there. The path above is from a real capture and names every
step, so the reducer is written against that rather than against a guess.

Authored with an AI assistant.

[ghstack-poisoned]
@bobrenjc93 bobrenjc93 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Aug 24, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 14 New Failures, 1 Unrelated Failure

As of commit 6e923eb with merge base 1b4e490 (image):

NEW FAILURES - The following jobs have failed:

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.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Aug 24, 2026
This was referenced Aug 24, 2026
bobrenjc93 added a commit that referenced this pull request Aug 25, 2026
…uarded object

Install a loaded artifact onto a live object, then capture again with that
object in scope, and the frame dies:

    torch_dynamo_resume_in_fwd_loss_bwd_at_208
    TypeError: cannot pickle '_thread.RLock' object
      reached via: local_scope['pipeline']._precompiled_fwd_loss_bwd
                     ._compiled._inner._state._lock

The lock is ours. `_precompiled_fwd_loss_bwd` is the handle a previous load
installed, `._inner._state` is the session's condition variable, and the guard
tree walks from the guarded pipeline straight into it. `convert_frame` catches
the PackageError and runs the frame eager, so the capture reports success while
covering less -- and the frame that trips is a resume function of the capture
entry region, so it is on the hot path rather than a corner.

The structural reason it is reachable at all: the pruning in
`pickle_guards_state` walks the top-level leaves of `local_scope`, and the
pickler walks everything under them. Anything nested inside an object a guard
DOES read is therefore never pruned and needs a reducer of its own.

Give precompile's own handles one. Nothing can guard on a served artifact, so
it drops the way a pruned value does.

Deliberately narrow. The first version keyed on locks generally, which broke
two existing tests -- and they were right to break: a lock in the CALLER's
model still fails the frame with its path named, because that is something they
can act on. This one is ours, installed by us, and a caller cannot remove it.

Test Plan:

```
python test/test_precompile.py
python test/dynamo/test_guard_serialization.py
python test/dynamo/test_package.py
```

The new test pickles a handle nested inside a guarded object and asserts it
comes back as the missing sentinel with its siblings intact, and asserts a bare
lock is NOT treated this way -- the property the two
`test_unpicklable_guard_value_names_where_it_lives` cases depend on.

Not reproduced end to end: reaching it needs a guard set that walks into an
installed handle, and every synthetic shape I built pruned the handle before
the pickler got there. The path above is from a real capture and names every
step, so the reducer is written against that rather than against a guess.

Authored with an AI assistant.


ghstack-source-id: 78994dc
Pull-Request: #194677
bobrenjc93 added a commit that referenced this pull request Aug 25, 2026
…uarded object

Install a loaded artifact onto a live object, then capture again with that
object in scope, and the frame dies:

    torch_dynamo_resume_in_fwd_loss_bwd_at_208
    TypeError: cannot pickle '_thread.RLock' object
      reached via: local_scope['pipeline']._precompiled_fwd_loss_bwd
                     ._compiled._inner._state._lock

The lock is ours. `_precompiled_fwd_loss_bwd` is the handle a previous load
installed, `._inner._state` is the session's condition variable, and the guard
tree walks from the guarded pipeline straight into it. `convert_frame` catches
the PackageError and runs the frame eager, so the capture reports success while
covering less -- and the frame that trips is a resume function of the capture
entry region, so it is on the hot path rather than a corner.

The structural reason it is reachable at all: the pruning in
`pickle_guards_state` walks the top-level leaves of `local_scope`, and the
pickler walks everything under them. Anything nested inside an object a guard
DOES read is therefore never pruned and needs a reducer of its own.

Give precompile's own handles one. Nothing can guard on a served artifact, so
it drops the way a pruned value does.

Deliberately narrow. The first version keyed on locks generally, which broke
two existing tests -- and they were right to break: a lock in the CALLER's
model still fails the frame with its path named, because that is something they
can act on. This one is ours, installed by us, and a caller cannot remove it.

Test Plan:

```
python test/test_precompile.py
python test/dynamo/test_guard_serialization.py
python test/dynamo/test_package.py
```

The new test pickles a handle nested inside a guarded object and asserts it
comes back as the missing sentinel with its siblings intact, and asserts a bare
lock is NOT treated this way -- the property the two
`test_unpicklable_guard_value_names_where_it_lives` cases depend on.

Not reproduced end to end: reaching it needs a guard set that walks into an
installed handle, and every synthetic shape I built pruned the handle before
the pickler got there. The path above is from a real capture and names every
step, so the reducer is written against that rather than against a guess.

Authored with an AI assistant.


ghstack-source-id: 78994dc
Pull-Request: #194677
bobrenjc93 added a commit that referenced this pull request Aug 25, 2026
…uarded object

Install a loaded artifact onto a live object, then capture again with that
object in scope, and the frame dies:

    torch_dynamo_resume_in_fwd_loss_bwd_at_208
    TypeError: cannot pickle '_thread.RLock' object
      reached via: local_scope['pipeline']._precompiled_fwd_loss_bwd
                     ._compiled._inner._state._lock

The lock is ours. `_precompiled_fwd_loss_bwd` is the handle a previous load
installed, `._inner._state` is the session's condition variable, and the guard
tree walks from the guarded pipeline straight into it. `convert_frame` catches
the PackageError and runs the frame eager, so the capture reports success while
covering less -- and the frame that trips is a resume function of the capture
entry region, so it is on the hot path rather than a corner.

The structural reason it is reachable at all: the pruning in
`pickle_guards_state` walks the top-level leaves of `local_scope`, and the
pickler walks everything under them. Anything nested inside an object a guard
DOES read is therefore never pruned and needs a reducer of its own.

Give precompile's own handles one. Nothing can guard on a served artifact, so
it drops the way a pruned value does.

Deliberately narrow. The first version keyed on locks generally, which broke
two existing tests -- and they were right to break: a lock in the CALLER's
model still fails the frame with its path named, because that is something they
can act on. This one is ours, installed by us, and a caller cannot remove it.

Test Plan:

```
python test/test_precompile.py
python test/dynamo/test_guard_serialization.py
python test/dynamo/test_package.py
```

The new test pickles a handle nested inside a guarded object and asserts it
comes back as the missing sentinel with its siblings intact, and asserts a bare
lock is NOT treated this way -- the property the two
`test_unpicklable_guard_value_names_where_it_lives` cases depend on.

Not reproduced end to end: reaching it needs a guard set that walks into an
installed handle, and every synthetic shape I built pruned the handle before
the pickler got there. The path above is from a real capture and names every
step, so the reducer is written against that rather than against a guess.

Authored with an AI assistant.


ghstack-source-id: 78994dc
Pull-Request: #194677
[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

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

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by the canonical precompile stack #195353-#195362; this change lives in #195362 (with the guard-drop policy half in #195360). Closing to consolidate review there. The branch is kept on GitHub.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1069/head branch November 11, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant