Skip to content

[precompile] Address review cleanups on PrecompileSession - #199065

Closed
bobrenjc93 wants to merge 7 commits into
gh/bobrenjc93/1529/basefrom
gh/bobrenjc93/1529/head
Closed

bobrenjc93 wants to merge 7 commits into
gh/bobrenjc93/1529/basefrom
gh/bobrenjc93/1529/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, and the summary() comment gains the matching caveat:
the varied-value rule only compares variants some kept guard split apart, so a
dropped slot that is the sole difference between two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: init takes Callable[_P, _R],
enter returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 [email protected]

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 64cf555 with merge base be4d660 (image):

🟡 GREEN LIGHT - PR requires human review:

  • Typing changes fall outside the Type annotations class
    • Existing annotations are edited, not added or narrowed from Any: Callable[..., object] becomes Callable[_P, _R] on __init__, _compiled, __enter__
    • PrecompileSession gains a runtime base, Generic[_P, _R], and _call is retyped to _P.args/_P.kwargs
  • Docstring reverses a documented behavior claim
    • precompile_capture and the DynamoTracer docstring used to promise that runtime guards stay intact; they now say guard_filter_fn prunes them during capture
    • The new text matches CheckFunctionManager in torch/_dynamo/guards.py, which filters sorted_guards before compile_check_fn, but whether the docs or the code is wrong is for a human to decide
  • Low-risk parts
    • The PackageError messages drop the calling cap(model, x) clause, and no .py file in the tree matches on it
    • torch/_precompile.py has from __future__ import annotations, so PrecompileSession[_P, _R] is never evaluated at runtime

reason: not_trivial
Reviewed commit: 64cf555

Inference job

🐛 Report a wrong verdict

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

@claude review

@claude

claude Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 1m 2s —— View job


The PR's own changes are correct, but it misses a second copy of the wrong guard-filter claim: the public DynamoTracer docstring still says it.

PR Review: #199065

Summary

The PR fixes the precompile_capture docstring about guard_filter_fn and runtime guards, adds a matching caveat to the summary() comment, drops the cap(model, x) clause from two PackageError messages, and makes PrecompileSession Generic[_P, _R]. I checked the new docstring against CheckFunctionManager (torch/_dynamo/guards.py:5125-5160), which filters sorted_guards before the final build_guards and compile_check_fn, so it is accurate. The typing changes are consistent. _DynamoCapture(session, ...) at torch/_precompile.py:3271 now infers _P and _R from the session, and there are no other users of PrecompileSession. One problem remains.

API Design

  • torch/_precompile.py:410-412: the public DynamoTracer docstring still makes the claim this PR removes. It says guard_filter_fn filters "the guards kept in the SERIALIZED artifact (runtime capture guards are always retained; ...)". tracer.guard_filter_fn is passed straight to precompile_capture (torch/_precompile.py:3262-3265), which hands it to torch._dynamo.optimize(guard_filter_fn=...). So the filter also prunes the runtime guard manager during capture, and a dropped guard never causes a recompile. This is the user-facing doc for the torch.compiler.precompile Dynamo tracer, so it matters more than the internal precompile_capture docstring this PR fixes. It should be reworded to match the new text at torch/_dynamo/precompile_package.py:1935-1938.
    Fix this →

Recommendation

Request Changes: fix the DynamoTracer docstring as well, since it is the public copy of the same wrong claim. Nothing else blocks approval.

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

bobrenjc93 added a commit that referenced this pull request Sep 29, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 8bdd4bb
Pull-Request: #199065
@claude

claude Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 51s —— View job


I found no issues. The updated PR fixes the DynamoTracer docstring that the previous review flagged, and the rest of the diff is correct.

PR Review: #199065

Summary

This PR fixes the guard_filter_fn wording in precompile_capture and the public DynamoTracer docstrings, adds a matching caveat to the summary() comment, drops the cap(model, x) clause from two PackageError messages, and makes PrecompileSession Generic[_P, _R]. I found no issues.

  • Docstrings: both now describe CheckFunctionManager correctly: the filter is applied before the runtime guard manager is built. The torch/_precompile.py:407-411 wording matches torch/_dynamo/precompile_package.py:1935-1938.
  • Typing: the _P/_R threading through __init__, _compiled, __enter__, _call, precompile_capture and _DynamoCapture.__init__ is consistent.
  • Other files: I found no other copies of the old claim in the precompile tests or docs.
  • Leftover cap(model, x) in torch/_precompile.py: two PrecompileError messages there (lines 2197 and 2206) still say cap(model, x). That is fine: in that module cap is the documented with precompile.capture(...) as cap: name (line 3168).

Recommendation

Approve: nothing blocks landing.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

/easycla

bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 34c7875
Pull-Request: #199065
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude

@claude

claude Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 34s —— View job


The code is correct, but the DynamoTracer docstring wording fix from my last review hasn't been made yet.

PR Review: #199065

Summary

This PR does five things:

  • fixes the guard_filter_fn wording in the precompile_capture and DynamoTracer docstrings
  • adds a caveat to the summary() comment
  • drops the stray cap(model, x) clause from two PackageError messages
  • makes PrecompileSession Generic[_P, _R]
  • passes the type parameters through precompile_capture and _DynamoCapture

No REVIEW.md files apply. The one remaining issue is the docstring wording below, which my 2026-10-08 review also raised.

API Design

  • torch/_precompile.py:408-412: the parenthetical is in the wrong place, and the paragraph was not reflowed.

    • "(the default drops only what cannot be serialized)" comes right after "a dropped guard never triggers a recompile during capture", so it reads as a condition on recompiles. It actually describes the default filter.
    • The line serialized); ``recompile_limit`` caps is left short.
    • Suggested rewrite:

      It filters the runtime guards during capture as well as the serialized ones (the default drops only what cannot be serialized), so a dropped guard never triggers a recompile during capture; recompile_limit caps recompilations per frame, ...

    Fix this →

Recommendation

Approve: the wording fix above is small and doesn't need another review round.

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 26s —— View job


I found no issues. The DynamoTracer docstring wording from my last two reviews is now fixed, and the rest of the diff is correct.

PR Review: #199065

Summary

This PR fixes the guard_filter_fn wording in the precompile_capture and DynamoTracer docstrings, adds a caveat to the summary() comment, drops the stray cap(model, x) clause from two PackageError messages, and makes PrecompileSession Generic[_P, _R], passing the type parameters through precompile_capture and _DynamoCapture. No REVIEW.md files apply.

The DynamoTracer docstring (torch/_precompile.py:407-419) now puts "(the default drops only what cannot be serialized)" right after "as well as the serialized ones", and the paragraph is reflowed. That resolves what my 2026-10-08 and 2026-10-10 reviews raised. The new module-level _P/_R in torch/_dynamo/precompile_package.py:1591-1592 don't clash with any existing names in that module.

Recommendation

Approve: nothing blocks landing.

bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: a506835
Pull-Request: #199065
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: a506835
Pull-Request: #199065
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: a506835
Pull-Request: #199065
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: a506835
Pull-Request: #199065
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: a506835
Pull-Request: #199065
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
Follow-up to the review of #198137, which has landed; this does not amend it.

The precompile_capture docstring claimed runtime guards stay intact during
capture and that guard_filter_fn only shapes the serialized guard state. That
is backwards: CheckFunctionManager applies the filter to sorted_guards before
it builds the runtime guard manager, so a dropped guard never triggers a
recompile during capture, just as it cannot pick a graph at serve time. The
docstring now says so, as does the public DynamoTracer docstring in
torch/_precompile.py, which carried the same claim. The summary() comment
gains the matching caveat: the varied-value rule only compares variants some
kept guard split apart, so a dropped slot that is the sole difference between
two calls is never reported.

The nn.Module and bound-method PackageError messages ended with "calling
cap(model, x)", but nothing in this module is named cap (the session hands back
its _call). The clause is dropped.

PrecompileSession is now Generic[_P, _R]: __init__ takes Callable[_P, _R],
__enter__ returns Callable[_P, _R], and _call is typed with _P.args/_P.kwargs,
so the callable from the with block keeps fn's signature instead of returning
object. precompile_capture returns PrecompileSession[_P, _R], and
_DynamoCapture, already Capture[_P, _R], now takes a PrecompileSession[_P, _R].

Test Plan:

```
lintrunner -a torch/_dynamo/precompile_package.py torch/_precompile.py
python test/dynamo/test_precompile_package.py
python test/test_precompile.py
```

Both suites pass (99 and 297 tests); no test asserts on the dropped message
clause.

This PR was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: a506835
Pull-Request: #199065
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

/easycla

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 25s —— View job


I found no issues. The diff is the same as at my 2026-10-10 review, which approved it.

PR Review: #199065

Summary

This PR makes these changes:

  • It corrects the guard_filter_fn wording in the precompile_capture and DynamoTracer docstrings: the filter applies to the runtime guards during capture as well as the serialized ones.
  • It adds the matching caveat to the summary() comment.
  • It drops the stray cap(model, x) clause from two PackageError messages.
  • It makes PrecompileSession Generic[_P, _R] and passes the type parameters through precompile_capture and _DynamoCapture.

No REVIEW.md files apply.

Recommendation

Approve: nothing blocks landing.

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #198777

pytorchmergebot pushed a commit that referenced this pull request Oct 13, 2026
…roperty (#198777)

Part of the set of changes the two-phase precompile workflow needs at pytorch tip, on top of the open precompile stack that ends in #198558. In that workflow, one GPU captures the model against a fake process group of the serving world size, and then every rank replays the saved artifact with recompilation refused. The set was found by running the workflow end to end at tip on a large production FSDP2 recommendation model. Each change here was the first blocker at its step: the run got past that step once the change was applied. Each was then confirmed by removing it alone from the full set, which made the run fail again at the same point. With the full set, the replay completed on all 16 ranks for 100 batches with zero serve-time compilation. Its losses matched an uncompiled run of the same build: the first step was bit-identical, and after 100 steps the differences were within run-to-run noise.

**Why precompile needs this.** A precompile artifact is loaded by a different process from the one that captured it. So each compiled frame's code object is recorded as a location, a module plus a qualname, that the loading process can look up and match against the frame Dynamo sees. Plain `torch.compile` never needs this, because it keys its cache on the live code object. A code object that cannot be located cannot be packaged, so the whole capture fails. Getters defined with `@property` are ordinary model code (a `forward` that reads `self.scale`, for example), so precompile has to locate them.

In the end-to-end run: The model compiles a frame whose code is defined under a `@property`. At tip, capture failed at package save with exactly the error above. With this change, capture got past package save.

**What.** `_get_code_source` records a code object's location by walking its `co_qualname` from the module with `getattr`. On a class, `getattr` returns the `property` or `functools.cached_property` descriptor, not the function inside it. So a code object that is a property getter, or a function nested in one (`C.prop.<locals>.inner`), could not be resolved. Packaging then aborted with:

    PackageError: Cannot resolve a fully qualified name for <code object ...>. Lookup scope: <property object ...>

This change:
- stops the qualname walk at a descriptor;
- descends through `fget`, `fset`, `fdel` or `func`, emitting a path the loader replays with plain `getattr` (for example `.fget`);
- lets the class search in `_find_code_source` look inside descriptors too. Python 3.10 has no `co_qualname`, so there this search is the only path, and before this change it skipped descriptors entirely.

**Example.**

```python
# owner.py
import functools

class Owner:
    @Property
    def prop(self):
        def inner():
            return 1
        return inner

    @functools.cached_property
    def cached(self):
        return 2
```

```python
from torch._dynamo.package import CompilePackage
from owner import Owner

getter = vars(Owner)["prop"].fget.__code__
package = CompilePackage(None)
with package.code_context(getter):
    pass
# before: PackageError: Cannot resolve a fully qualified name for <code object prop ...>.
#         Lookup scope: <property object ...>
# after:  recorded under Owner.prop and found again through .fget; the same holds
#         for the nested `inner` and for Owner.cached through .func
```

Pull Request resolved: #198777
Approved by: https://github.com/pytorchgreenlight, https://github.com/MengjiaoZhou
ghstack dependencies: #198558, #198920, #199065
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 Merged module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants