Skip to content

[dynamo] Guard autocast objects by value rather than identity - #193976

Closed
bobrenjc93 wants to merge 2 commits into
gh/bobrenjc93/992/basefrom
gh/bobrenjc93/992/head
Closed

bobrenjc93 wants to merge 2 commits into
gh/bobrenjc93/992/basefrom
gh/bobrenjc93/992/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Fifth of six fixes the precompile PR at the top of this stack needs, and a
correctness fix for plain torch.compile on its own.

A torch.amp.autocast object the user holds and reuses reaches the trace as
four specialized values -- device, dtype, enabled, cache_enabled -- but the
guard installed on it was ID_MATCH on the object. That is wrong in both
directions. It is too strong for serialization: an address cannot be written to
an artifact, so a precompile drops the guard, and a sibling instance holding a
different autocast object then silently selects this graph. It is also too weak
for the live case, because mutating the object in place leaves the address
alone and the stale graph keeps serving.

Guard the four fields the trace already specialized on instead. That is what
the graph actually depends on, so it is the precise condition, and it
serializes.

Test Plan:

The regression test compiles a module holding an autocast object, then mutates
fast_dtype on that same object; an id() guard cannot see the change, so
without this the second call keeps returning bf16.

python test/dynamo/test_ctx_manager.py
python test/dynamo/test_misc.py
python test/dynamo/test_guard_serialization.py

Authored with an AI assistant.

Differential Revision: D116528371

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 1 Unrelated Failure

As of commit 601fded with merge base 1b4e490 (image):

NEW FAILURE - The following job has 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.

@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 added a commit that referenced this pull request Aug 19, 2026
Fifth of six fixes the precompile PR at the top of this stack needs, and a
correctness fix for plain torch.compile on its own.

A `torch.amp.autocast` object the user holds and reuses reaches the trace as
four specialized values -- device, dtype, enabled, cache_enabled -- but the
guard installed on it was `ID_MATCH` on the object. That is wrong in both
directions. It is too strong for serialization: an address cannot be written to
an artifact, so a precompile drops the guard, and a sibling instance holding a
different autocast object then silently selects this graph. It is also too weak
for the live case, because mutating the object in place leaves the address
alone and the stale graph keeps serving.

Guard the four fields the trace already specialized on instead. That is what
the graph actually depends on, so it is the precise condition, and it
serializes.

Test Plan:

The regression test compiles a module holding an autocast object, then mutates
`fast_dtype` on that same object; an id() guard cannot see the change, so
without this the second call keeps returning bf16.

```
python test/dynamo/test_ctx_manager.py
python test/dynamo/test_misc.py
python test/dynamo/test_guard_serialization.py
```

Authored with an AI assistant.


ghstack-source-id: e285672
Pull-Request: #193976
bobrenjc93 added a commit that referenced this pull request Aug 20, 2026
Fifth of six fixes the precompile PR at the top of this stack needs, and a
correctness fix for plain torch.compile on its own.

A `torch.amp.autocast` object the user holds and reuses reaches the trace as
four specialized values -- device, dtype, enabled, cache_enabled -- but the
guard installed on it was `ID_MATCH` on the object. That is wrong in both
directions. It is too strong for serialization: an address cannot be written to
an artifact, so a precompile drops the guard, and a sibling instance holding a
different autocast object then silently selects this graph. It is also too weak
for the live case, because mutating the object in place leaves the address
alone and the stale graph keeps serving.

Guard the four fields the trace already specialized on instead. That is what
the graph actually depends on, so it is the precise condition, and it
serializes.

Test Plan:

The regression test compiles a module holding an autocast object, then mutates
`fast_dtype` on that same object; an id() guard cannot see the change, so
without this the second call keeps returning bf16.

```
python test/dynamo/test_ctx_manager.py
python test/dynamo/test_misc.py
python test/dynamo/test_guard_serialization.py
```

Authored with an AI assistant.


ghstack-source-id: e285672
Pull-Request: #193976
bobrenjc93 added a commit that referenced this pull request Aug 20, 2026
Fifth of six fixes the precompile PR at the top of this stack needs, and a
correctness fix for plain torch.compile on its own.

A `torch.amp.autocast` object the user holds and reuses reaches the trace as
four specialized values -- device, dtype, enabled, cache_enabled -- but the
guard installed on it was `ID_MATCH` on the object. That is wrong in both
directions. It is too strong for serialization: an address cannot be written to
an artifact, so a precompile drops the guard, and a sibling instance holding a
different autocast object then silently selects this graph. It is also too weak
for the live case, because mutating the object in place leaves the address
alone and the stale graph keeps serving.

Guard the four fields the trace already specialized on instead. That is what
the graph actually depends on, so it is the precise condition, and it
serializes.

Test Plan:

The regression test compiles a module holding an autocast object, then mutates
`fast_dtype` on that same object; an id() guard cannot see the change, so
without this the second call keeps returning bf16.

```
python test/dynamo/test_ctx_manager.py
python test/dynamo/test_misc.py
python test/dynamo/test_guard_serialization.py
```

Authored with an AI assistant.


ghstack-source-id: e285672
Pull-Request: #193976
This was referenced Aug 22, 2026
[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

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

1 similar comment
@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 #195356. 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/992/head branch November 11, 2026 01:43
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