Skip to content

[no-ci] [dynamo] Let an explicit package's recompile_limit lift the accumulated cap - #198091

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

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

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem. An explicit capture is a caller asking Dynamo to record up to N variants of a frame into a CompilePackage, and the per-region recompile_limit passed to torch._dynamo.optimize is how it states N. But exceeds_recompile_limit in cache_size.py applies two caps, not one: besides the per-region limit it stops compiling once the code object's total entry count across all regions, or the frame's frame_compile_id, reaches config.accumulated_recompile_limit. That second cap is a process-wide safety net against runaway recompilation in ordinary torch.compile, and it knows nothing about the capture. When it is lower than the requested N (a test or a deployment that pins it low, or a large capture on the default of 256), the capture silently stops short, the package records fewer variants than asked for, and nothing tells the caller that its own budget was never the binding one.

Fix. ConvertFrameAssert.__call__ already wraps the compile of a frame in config.patch(recompile_limit=...) when a per-region limit was given. This commit extends that patch, for an explicit package only, to also set accumulated_recompile_limit to the larger of the ambient value and the requested limit. The patch is the same context manager around the same _compile call, so it covers both accumulated checks in exceeds_recompile_limit (the total-entry count and the frame_compile_id check), lasts only for the compile of that frame, and never lowers the cap. A package that is not an explicit capture, or an explicit capture with no recompile_limit, is unaffected: the accumulated_recompile_limit value written into the patch is the ambient one.

Summary. The design point is that the two limits mean different things: recompile_limit is a per-frame budget the user chose, while the accumulated cap is a global guard for code the user did not annotate, so only a caller that has opted in (an explicit capture that names its budget) should be able to move it, and only for its own compiles. Using max rather than replacing the value keeps a deliberately high ambient cap in force, and keeping the lift inside the existing per-frame config.patch means a plain torch.compile frame in the same process, or the same package's compile without a limit, still sees the ambient cap. The alternative from the source PR of also subtracting the explicit region's own entries from the per-code total was left out: main's exceeds_recompile_limit compares frame_compile_id against the same cap, so a lower total alone would never let another variant through, and the region registry it relied on does not exist on main. A frame that still hits a limit is not bypassed and keeps the variants it captured; commit 10 above records which frame that was on the package. Nothing changes for torch.compile without a package or for a serving package. Carved out of #195913.

An explicit capture's budget beats a lower accumulated cap

import torch, torch._dynamo, torch._dynamo.testing
from torch._dynamo.package import CompilePackage

def fn(x):
    return x + 1

variants = (torch.randn(3), torch.randint(0, 5, (3,)), torch.randn(3, dtype=torch.float64))

with torch._dynamo.config.patch(accumulated_recompile_limit=1):
    counter = torch._dynamo.testing.CompileCounter()
    plain = torch._dynamo.optimize(backend=counter, recompile_limit=2)(fn)
    for x in variants:
        plain(x)
    counter.frame_count
    # parent: 1 (the accumulated cap of 1 stops the second dtype)
    # now:    1 (ordinary torch.compile keeps the ambient cap)

    torch._dynamo.reset()
    pkg = CompilePackage(fn, explicit_capture=True)
    counter = torch._dynamo.testing.CompileCounter()
    compiled = torch._dynamo.optimize(backend=counter, package=pkg, recompile_limit=2)(fn)
    for x in variants:
        compiled(x)
    counter.frame_count, len(pkg.cache_entry().codes[0].guarded_codes)
    # parent: (1, 1)  the same cap of 1 truncates the capture after one variant
    # now:    (2, 2)  the cap is lifted to recompile_limit=2 for this frame; the
    #                 third dtype is stopped at the budget; the lifted cap equals it, so the
    #                 log names accumulated_recompile_limit (2)

torch._dynamo.config.accumulated_recompile_limit
# now: 256 (the lift lived only inside the compile of that frame)

The same script with a non-explicit CompilePackage(fn) and recompile_limit=2, or with an explicit package and no recompile_limit, compiles once under a cap of 1: neither path has opted in to lifting it. test_explicit_package_recompile_limit_lifts_the_accumulated_cap in test/dynamo/test_package.py pins the plain-compiles-once and explicit-compiles-twice claims together with the two recorded guarded codes.

Test Plan:

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

The new test fails on the parent (the explicit capture stops at one variant) 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/198091

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:

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/1428/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