Skip to content

[no-ci] [dynamo] Derive a package's uncovered frames from its entries - #198093

Closed
bobrenjc93 wants to merge 4 commits into
gh/bobrenjc93/1430/basefrom
gh/bobrenjc93/1430/head
Closed

bobrenjc93 wants to merge 4 commits into
gh/bobrenjc93/1430/basefrom
gh/bobrenjc93/1430/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem. A frame that entered Dynamo and came out with no guarded code is a hole in the artifact: install() skips that code object, so after a load the frame runs eagerly for the life of the process, and nothing on CompilePackage says which frames those are. The one report that exists, the summary in torch/_dynamo/precompile_package.py, recomputes the predicate off the cache entry and keys it on the bare co_name, so two frames that happen to share a name are two entries a reader cannot tell apart. Recording the gap the way the commit below records a truncated frame would get the common case wrong: a frame's first call may take a graph-less branch and a later call may compile a real variant, so a recorded gap would have to be un-recorded at the second call, and with the variants in the other order it would never be recorded at all.

Fix. Adds a read-only uncovered_frames property to CompilePackage that walks the package's entries and reports each one that has a compile id, holds no guarded code and is not bypassed, as a frozenset of "co_name (filename:firstlineno)" strings. Nothing is recorded and nothing is serialized; every read recomputes the set from the same entries install() consumes. One test compiles a function that returns its input untouched through torch._dynamo.optimize with a package and watches the gap appear and then vanish once a variant with guarded code is added under code_context; the other registers two entries whose code objects share a name and differ only in filename and checks that both are reported.

Summary. The predicate is exactly the set install() calls skip_code() on minus the entries that never entered Dynamo: it skips every entry that is not bypassed and has no guarded code whether or not the frame ran, and the ones without a compile id are resume functions generated at a graph break and never executed (registered by add_resume_function; only leaving code_context sets the compile id), which are not gaps because no call reached them. A bypassed entry is excluded because it is not skipped but re-traced on the next call and is already reported through its bypass flag and reason, and a frame that hit the recompile limit keeps its working variants installed, so it has guarded code and shows up only in truncated_frames. The key matches the one the commit below uses for truncated frames, so the two sets compose and two frames sharing a name stay two, which the summary's bare-name key cannot tell apart; that summary can read this property instead of recomputing the predicate. Deriving rather than recording is the point: the set is a function of the entries, so whichever order a graph-less variant and a compiling variant ran in, the frame is a gap exactly while it has no guarded code, and add_guarded_code closes it with no bookkeeping. The property is runtime-only and changes nothing about what is saved or installed. Carved out of #195913.

A graph-less frame is a gap until a variant compiles

import torch
from torch._dynamo.package import CompilePackage

def fn(x):
    return x

pkg = CompilePackage(fn)
pkg.uncovered_frames
# parent: AttributeError, CompilePackage has no such property
# now:    frozenset()

torch._dynamo.optimize(backend="eager", package=pkg)(fn)(torch.randn(3))
entry = pkg._codes[fn.__code__]
(entry.has_compile_id, len(entry.guarded_codes), entry.bypassed)
# parent and now: (True, 0, False); the frame entered Dynamo, produced no
#                 graph, and install() would skip_code() it after a load
pkg.uncovered_frames
# now: frozenset({'fn (<this file>:4)'})

with pkg.code_context(fn.__code__):
    pkg.add_guarded_code(b"", fn.__code__)
pkg.uncovered_frames
# now: frozenset(); the set is derived from the entry, so the variant that
#      compiled closed the gap whichever order the two variants ran in

Pinned by test_uncovered_frames_follows_the_entries.

Two frames sharing a co_name stay two

from torch._dynamo.package import _DynamoCodeCacheEntry, SerializedCode

def one(x):
    return x

def two(x):
    return x + 0

code_a = one.__code__.replace(co_name="frame", co_filename="a.py")
code_b = two.__code__.replace(co_name="frame", co_filename="b.py")
pkg = CompilePackage(fn)
for code in (code_a, code_b):
    pkg._codes[code] = _DynamoCodeCacheEntry(
        python_code=SerializedCode.from_code_object(code),
        python_module="m", function_names=[], guarded_codes=[],
        import_sources={}, backend_ids=[], code_source=None,
        install_to_global=False, has_compile_id=True,
    )
sorted(pkg.uncovered_frames)
# now: ['frame (a.py:7)', 'frame (b.py:10)']
#      keyed on the bare co_name, as the precompile_package.py summary does
#      today, this reads ('frame', 'frame'): two entries nobody can tell apart

pkg._codes[code_a].has_compile_id = False  # generated, never-executed resume code
sorted(pkg.uncovered_frames)
# now: ['frame (b.py:10)']; install() still skip_code()s code_a, but no call
#      ever reached it, so it is not a gap

pkg._codes[code_b].bypassed = True
sorted(pkg.uncovered_frames)
# now: []; a bypassed entry is re-traced on the next call and is reported
#      through bypassed / bypass_reason instead

The first result and the bypass exclusion are pinned by test_uncovered_frames_distinguish_frames_that_share_a_name; the compile-id exclusion follows from the predicate and was checked by running the snippet.

Test Plan:

python test/dynamo/test_package.py -k uncovered_frames
python test/dynamo/test_package.py

The new tests fail on the parent, which has no uncovered_frames attribute, and pass here.

Authored with an AI assistant.

Co-Authored-By: Claude Fable 5.1 [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 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 11 New Failures, 1 Unclassified Failure

As of commit 3cb994c with merge base 618de55 (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

  • TSan / get-label-type / runner-determinator (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    ##[error]CI is disabled by the [no-ci] PR title prefix. Remove the prefix and push a new commit to run CI.

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

@pytorch-bot pytorch-bot Bot added ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Sep 22, 2026
@bobrenjc93 bobrenjc93 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Sep 22, 2026
@bobrenjc93 bobrenjc93 changed the title [dynamo] Derive a package's uncovered frames from its entries [no-ci] [dynamo] Derive a package's uncovered frames from its entries Sep 22, 2026
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Sep 22, 2026
**Problem.** A frame that entered Dynamo and came out with no guarded code is a hole in the artifact: `install()` skips that code object, so after a load the frame runs eagerly for the life of the process, and nothing on `CompilePackage` says which frames those are. The one report that exists, the summary in torch/_dynamo/precompile_package.py, recomputes the predicate off the cache entry and keys it on the bare `co_name`, so two frames that happen to share a name are two entries a reader cannot tell apart. Recording the gap the way the commit below records a truncated frame would get the common case wrong: a frame's first call may take a graph-less branch and a later call may compile a real variant, so a recorded gap would have to be un-recorded at the second call, and with the variants in the other order it would never be recorded at all.

**Fix.** Adds a read-only `uncovered_frames` property to `CompilePackage` that walks the package's entries and reports each one that has a compile id, holds no guarded code and is not bypassed, as a frozenset of `"co_name (filename:firstlineno)"` strings. Nothing is recorded and nothing is serialized; every read recomputes the set from the same entries `install()` consumes. One test compiles a function that returns its input untouched through `torch._dynamo.optimize` with a package and watches the gap appear and then vanish once a variant with guarded code is added under `code_context`; the other registers two entries whose code objects share a name and differ only in filename and checks that both are reported.

**Summary.** The predicate is exactly the set `install()` calls `skip_code()` on minus the entries that never entered Dynamo: it skips every entry that is not bypassed and has no guarded code whether or not the frame ran, and the ones without a compile id are resume functions generated at a graph break and never executed (registered by `add_resume_function`; only leaving `code_context` sets the compile id), which are not gaps because no call reached them. A bypassed entry is excluded because it is not skipped but re-traced on the next call and is already reported through its bypass flag and reason, and a frame that hit the recompile limit keeps its working variants installed, so it has guarded code and shows up only in `truncated_frames`. The key matches the one the commit below uses for truncated frames, so the two sets compose and two frames sharing a name stay two, which the summary's bare-name key cannot tell apart; that summary can read this property instead of recomputing the predicate. Deriving rather than recording is the point: the set is a function of the entries, so whichever order a graph-less variant and a compiling variant ran in, the frame is a gap exactly while it has no guarded code, and `add_guarded_code` closes it with no bookkeeping. The property is runtime-only and changes nothing about what is saved or installed. Carved out of #195913.

## A graph-less frame is a gap until a variant compiles

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

def fn(x):
    return x

pkg = CompilePackage(fn)
pkg.uncovered_frames
# parent: AttributeError, CompilePackage has no such property
# now:    frozenset()

torch._dynamo.optimize(backend="eager", package=pkg)(fn)(torch.randn(3))
entry = pkg._codes[fn.__code__]
(entry.has_compile_id, len(entry.guarded_codes), entry.bypassed)
# parent and now: (True, 0, False); the frame entered Dynamo, produced no
#                 graph, and install() would skip_code() it after a load
pkg.uncovered_frames
# now: frozenset({'fn (<this file>:4)'})

with pkg.code_context(fn.__code__):
    pkg.add_guarded_code(b"", fn.__code__)
pkg.uncovered_frames
# now: frozenset(); the set is derived from the entry, so the variant that
#      compiled closed the gap whichever order the two variants ran in
```

Pinned by `test_uncovered_frames_follows_the_entries`.

## Two frames sharing a co_name stay two

```python
from torch._dynamo.package import _DynamoCodeCacheEntry, SerializedCode

def one(x):
    return x

def two(x):
    return x + 0

code_a = one.__code__.replace(co_name="frame", co_filename="a.py")
code_b = two.__code__.replace(co_name="frame", co_filename="b.py")
pkg = CompilePackage(fn)
for code in (code_a, code_b):
    pkg._codes[code] = _DynamoCodeCacheEntry(
        python_code=SerializedCode.from_code_object(code),
        python_module="m", function_names=[], guarded_codes=[],
        import_sources={}, backend_ids=[], code_source=None,
        install_to_global=False, has_compile_id=True,
    )
sorted(pkg.uncovered_frames)
# now: ['frame (a.py:7)', 'frame (b.py:10)']
#      keyed on the bare co_name, as the precompile_package.py summary does
#      today, this reads ('frame', 'frame'): two entries nobody can tell apart

pkg._codes[code_a].has_compile_id = False  # generated, never-executed resume code
sorted(pkg.uncovered_frames)
# now: ['frame (b.py:10)']; install() still skip_code()s code_a, but no call
#      ever reached it, so it is not a gap

pkg._codes[code_b].bypassed = True
sorted(pkg.uncovered_frames)
# now: []; a bypassed entry is re-traced on the next call and is reported
#      through bypassed / bypass_reason instead
```

The first result and the bypass exclusion are pinned by `test_uncovered_frames_distinguish_frames_that_share_a_name`; the compile-id exclusion follows from the predicate and was checked by running the snippet.

Test Plan:
```
python test/dynamo/test_package.py -k uncovered_frames
python test/dynamo/test_package.py
```
The new tests fail on the parent, which has no `uncovered_frames` attribute, and pass here.

Authored with an AI assistant.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

ghstack-source-id: 065f207
Pull-Request: #198093
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Sep 22, 2026
**Problem.** A frame that entered Dynamo and came out with no guarded code is a hole in the artifact: `install()` skips that code object, so after a load the frame runs eagerly for the life of the process, and nothing on `CompilePackage` says which frames those are. The one report that exists, the summary in torch/_dynamo/precompile_package.py, recomputes the predicate off the cache entry and keys it on the bare `co_name`, so two frames that happen to share a name are two entries a reader cannot tell apart. Recording the gap the way the commit below records a truncated frame would get the common case wrong: a frame's first call may take a graph-less branch and a later call may compile a real variant, so a recorded gap would have to be un-recorded at the second call, and with the variants in the other order it would never be recorded at all.

**Fix.** Adds a read-only `uncovered_frames` property to `CompilePackage` that walks the package's entries and reports each one that has a compile id, holds no guarded code and is not bypassed, as a frozenset of `"co_name (filename:firstlineno)"` strings. Nothing is recorded and nothing is serialized; every read recomputes the set from the same entries `install()` consumes. One test compiles a function that returns its input untouched through `torch._dynamo.optimize` with a package and watches the gap appear and then vanish once a variant with guarded code is added under `code_context`; the other registers two entries whose code objects share a name and differ only in filename and checks that both are reported.

**Summary.** The predicate is exactly the set `install()` calls `skip_code()` on minus the entries that never entered Dynamo: it skips every entry that is not bypassed and has no guarded code whether or not the frame ran, and the ones without a compile id are resume functions generated at a graph break and never executed (registered by `add_resume_function`; only leaving `code_context` sets the compile id), which are not gaps because no call reached them. A bypassed entry is excluded because it is not skipped but re-traced on the next call and is already reported through its bypass flag and reason, and a frame that hit the recompile limit keeps its working variants installed, so it has guarded code and shows up only in `truncated_frames`. The key matches the one the commit below uses for truncated frames, so the two sets compose and two frames sharing a name stay two, which the summary's bare-name key cannot tell apart; that summary can read this property instead of recomputing the predicate. Deriving rather than recording is the point: the set is a function of the entries, so whichever order a graph-less variant and a compiling variant ran in, the frame is a gap exactly while it has no guarded code, and `add_guarded_code` closes it with no bookkeeping. The property is runtime-only and changes nothing about what is saved or installed. Carved out of #195913.

## A graph-less frame is a gap until a variant compiles

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

def fn(x):
    return x

pkg = CompilePackage(fn)
pkg.uncovered_frames
# parent: AttributeError, CompilePackage has no such property
# now:    frozenset()

torch._dynamo.optimize(backend="eager", package=pkg)(fn)(torch.randn(3))
entry = pkg._codes[fn.__code__]
(entry.has_compile_id, len(entry.guarded_codes), entry.bypassed)
# parent and now: (True, 0, False); the frame entered Dynamo, produced no
#                 graph, and install() would skip_code() it after a load
pkg.uncovered_frames
# now: frozenset({'fn (<this file>:4)'})

with pkg.code_context(fn.__code__):
    pkg.add_guarded_code(b"", fn.__code__)
pkg.uncovered_frames
# now: frozenset(); the set is derived from the entry, so the variant that
#      compiled closed the gap whichever order the two variants ran in
```

Pinned by `test_uncovered_frames_follows_the_entries`.

## Two frames sharing a co_name stay two

```python
from torch._dynamo.package import _DynamoCodeCacheEntry, SerializedCode

def one(x):
    return x

def two(x):
    return x + 0

code_a = one.__code__.replace(co_name="frame", co_filename="a.py")
code_b = two.__code__.replace(co_name="frame", co_filename="b.py")
pkg = CompilePackage(fn)
for code in (code_a, code_b):
    pkg._codes[code] = _DynamoCodeCacheEntry(
        python_code=SerializedCode.from_code_object(code),
        python_module="m", function_names=[], guarded_codes=[],
        import_sources={}, backend_ids=[], code_source=None,
        install_to_global=False, has_compile_id=True,
    )
sorted(pkg.uncovered_frames)
# now: ['frame (a.py:7)', 'frame (b.py:10)']
#      keyed on the bare co_name, as the precompile_package.py summary does
#      today, this reads ('frame', 'frame'): two entries nobody can tell apart

pkg._codes[code_a].has_compile_id = False  # generated, never-executed resume code
sorted(pkg.uncovered_frames)
# now: ['frame (b.py:10)']; install() still skip_code()s code_a, but no call
#      ever reached it, so it is not a gap

pkg._codes[code_b].bypassed = True
sorted(pkg.uncovered_frames)
# now: []; a bypassed entry is re-traced on the next call and is reported
#      through bypassed / bypass_reason instead
```

The first result and the bypass exclusion are pinned by `test_uncovered_frames_distinguish_frames_that_share_a_name`; the compile-id exclusion follows from the predicate and was checked by running the snippet.

Test Plan:
```
python test/dynamo/test_package.py -k uncovered_frames
python test/dynamo/test_package.py
```
The new tests fail on the parent, which has no `uncovered_frames` attribute, and pass here.

Authored with an AI assistant.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

ghstack-source-id: 5a96458
Pull-Request: #198093
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Sep 22, 2026
**Problem.** A frame that entered Dynamo and came out with no guarded code is a hole in the artifact: `install()` skips that code object, so after a load the frame runs eagerly for the life of the process, and nothing on `CompilePackage` says which frames those are. The one report that exists, the summary in torch/_dynamo/precompile_package.py, recomputes the predicate off the cache entry and keys it on the bare `co_name`, so two frames that happen to share a name are two entries a reader cannot tell apart. Recording the gap the way the commit below records a truncated frame would get the common case wrong: a frame's first call may take a graph-less branch and a later call may compile a real variant, so a recorded gap would have to be un-recorded at the second call, and with the variants in the other order it would never be recorded at all.

**Fix.** Adds a read-only `uncovered_frames` property to `CompilePackage` that walks the package's entries and reports each one that has a compile id, holds no guarded code and is not bypassed, as a frozenset of `"co_name (filename:firstlineno)"` strings. Nothing is recorded and nothing is serialized; every read recomputes the set from the same entries `install()` consumes. One test compiles a function that returns its input untouched through `torch._dynamo.optimize` with a package and watches the gap appear and then vanish once a variant with guarded code is added under `code_context`; the other registers two entries whose code objects share a name and differ only in filename and checks that both are reported.

**Summary.** The predicate is exactly the set `install()` calls `skip_code()` on minus the entries that never entered Dynamo: it skips every entry that is not bypassed and has no guarded code whether or not the frame ran, and the ones without a compile id are resume functions generated at a graph break and never executed (registered by `add_resume_function`; only leaving `code_context` sets the compile id), which are not gaps because no call reached them. A bypassed entry is excluded because it is not skipped but re-traced on the next call and is already reported through its bypass flag and reason, and a frame that hit the recompile limit keeps its working variants installed, so it has guarded code and shows up only in `truncated_frames`. The key matches the one the commit below uses for truncated frames, so the two sets compose and two frames sharing a name stay two, which the summary's bare-name key cannot tell apart; that summary can read this property instead of recomputing the predicate. Deriving rather than recording is the point: the set is a function of the entries, so whichever order a graph-less variant and a compiling variant ran in, the frame is a gap exactly while it has no guarded code, and `add_guarded_code` closes it with no bookkeeping. The property is runtime-only and changes nothing about what is saved or installed. Carved out of #195913.

## A graph-less frame is a gap until a variant compiles

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

def fn(x):
    return x

pkg = CompilePackage(fn)
pkg.uncovered_frames
# parent: AttributeError, CompilePackage has no such property
# now:    frozenset()

torch._dynamo.optimize(backend="eager", package=pkg)(fn)(torch.randn(3))
entry = pkg._codes[fn.__code__]
(entry.has_compile_id, len(entry.guarded_codes), entry.bypassed)
# parent and now: (True, 0, False); the frame entered Dynamo, produced no
#                 graph, and install() would skip_code() it after a load
pkg.uncovered_frames
# now: frozenset({'fn (<this file>:4)'})

with pkg.code_context(fn.__code__):
    pkg.add_guarded_code(b"", fn.__code__)
pkg.uncovered_frames
# now: frozenset(); the set is derived from the entry, so the variant that
#      compiled closed the gap whichever order the two variants ran in
```

Pinned by `test_uncovered_frames_follows_the_entries`.

## Two frames sharing a co_name stay two

```python
from torch._dynamo.package import _DynamoCodeCacheEntry, SerializedCode

def one(x):
    return x

def two(x):
    return x + 0

code_a = one.__code__.replace(co_name="frame", co_filename="a.py")
code_b = two.__code__.replace(co_name="frame", co_filename="b.py")
pkg = CompilePackage(fn)
for code in (code_a, code_b):
    pkg._codes[code] = _DynamoCodeCacheEntry(
        python_code=SerializedCode.from_code_object(code),
        python_module="m", function_names=[], guarded_codes=[],
        import_sources={}, backend_ids=[], code_source=None,
        install_to_global=False, has_compile_id=True,
    )
sorted(pkg.uncovered_frames)
# now: ['frame (a.py:7)', 'frame (b.py:10)']
#      keyed on the bare co_name, as the precompile_package.py summary does
#      today, this reads ('frame', 'frame'): two entries nobody can tell apart

pkg._codes[code_a].has_compile_id = False  # generated, never-executed resume code
sorted(pkg.uncovered_frames)
# now: ['frame (b.py:10)']; install() still skip_code()s code_a, but no call
#      ever reached it, so it is not a gap

pkg._codes[code_b].bypassed = True
sorted(pkg.uncovered_frames)
# now: []; a bypassed entry is re-traced on the next call and is reported
#      through bypassed / bypass_reason instead
```

The first result and the bypass exclusion are pinned by `test_uncovered_frames_distinguish_frames_that_share_a_name`; the compile-id exclusion follows from the predicate and was checked by running the snippet.

Test Plan:
```
python test/dynamo/test_package.py -k uncovered_frames
python test/dynamo/test_package.py
```
The new tests fail on the parent, which has no `uncovered_frames` attribute, and pass here.

Authored with an AI assistant.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

ghstack-source-id: ae9d0d2
Pull-Request: #198093
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Abandoning this stack for now; it may be reopened later.

@bobrenjc93 bobrenjc93 closed this Sep 22, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1430/head branch November 28, 2026 01:51
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