Skip to content

[no-ci] [dynamo] Record on the package which frame hit the recompile limit - #198092

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

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

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem. When a frame compiled into a CompilePackage hits recompile_limit (or the accumulated cap), _compile logs a warning, marks the frame run-only and returns, and the package never learns that anything happened. The variants captured so far are kept, so nothing on the entry is bypassed, and there is no field a caller can read to tell "this frame's capture is complete" from "this frame stopped capturing at the limit and the inputs past it will never be covered". A caller assembling an artifact from an explicit capture therefore cannot flag the gap, and a production job has no tlparse artifact naming the frame either.

Fix. CompilePackage gains a runtime-only _truncated_frames set with a truncated_frames property (returned as a frozenset), a current_entry property exposing the entry that code_context set, and mark_current_entry_truncated(), which records the current entry's code as co_name (filename:firstlineno). In _compile, right where exceeds_recompile_limit fires and after the existing warning, the package (when one is set and inside its code_context) is marked, and a dynamo_cache_truncated structured-trace artifact is emitted with the limit type and the function's location so tlparse shows it. The test pins that the set is empty after the first variant, names exactly the frame after the second dtype, and that the entry keeps its one guarded code and is not bypassed.

Summary. The record is deliberately not a bypass: the variants captured before the limit are valid, must stay installable, and for a cache a miss simply recompiles, so the entry's guarded codes and bypassed flag are untouched and the new state lives beside them. It is also deliberately not serialized: it describes this capture session rather than the artifact, so it must not change what install() serves, which is why it sits on the package and not on _DynamoCodeCacheEntry. The key is the code object's name plus file and first line rather than the bare name, so two frames that share a co_name stay two entries; the _summarize helper in torch/_dynamo/precompile_package.py already takes a truncated set in exactly this shape, and this commit supplies its producer. The set is a lower bound, because the RUN_ONLY strategy that _compile installs for the limit is recursive: frames called beneath the truncated one also stop capturing, but they never re-enter Dynamo to report it, so only the frame that hit the limit is named. An alternative was to derive truncation from the entries the way commit 11 derives uncovered frames, but nothing on an entry distinguishes "stopped at the limit" from "every variant seen is covered", so the compile path has to record it at the one point that knows. Carved out of #195913.

The frame that hit the limit is named, its captured variant stays

import torch
from torch._dynamo.package import CompilePackage

def fn(x):
    return x + 1

with torch._dynamo.config.patch(recompile_limit=1):
    pkg = CompilePackage(fn, explicit_capture=True)
    compiled = torch._dynamo.optimize(backend="eager", package=pkg)(fn)
    compiled(torch.randn(3))
    pkg.truncated_frames
    # parent: AttributeError, CompilePackage has no truncated_frames
    # now:    frozenset()
    compiled(torch.randint(0, 5, (3,)))  # second dtype, over the limit of 1
    pkg.truncated_frames
    # now:    frozenset({'fn (<file>.py:4)'})   i.e. "co_name (co_filename:co_firstlineno)"
    entry = pkg.cache_entry().codes[0]
    len(entry.guarded_codes), entry.bypassed
    # parent and now: (1, False)  the captured variant is kept, not bypassed
    pkg.current_entry
    # now:    None outside code_context

With TORCH_TRACE set, the same run writes one dynamo_cache_truncated artifact:

{"artifact": {"name": "dynamo_cache_truncated", "encoding": "json"}, "frame_id": 0, "frame_compile_id": 1, ...}
{"reason": "hit recompile_limit", "function": "'fn' (<file>.py:4)"}

Pinned by test_truncated_frames_names_the_frame_that_hit_the_recompile_limit in test/dynamo/test_package.py. A third call after the limit runs the frame run-only and leaves the set unchanged, which is the lower-bound behaviour described above.

Test Plan:

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

The new test fails on the parent (no truncated_frames attribute) and passes 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/198092

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:

❌ 7 New Failures, 1 Cancelled Job, 3 Unclassified Failures

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

NEW FAILURES - The following jobs have failed:

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

CANCELLED JOB - The following job was cancelled. Please retry:

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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@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/1429/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