Skip to content

[no-ci] [dynamo] Let a serving package recompile an uncovered frame without recording it - #198090

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

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

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem. _compile treats every package it is handed as one being captured. The guard build serializes its guards whenever the compile still has a package, and the guard bytes, the inlined source and the device type are then appended to the package's entry for the frame. A package that serves a loaded artifact, which is what the serving flag introduced three commits below means, gets nothing from this: nothing will ever save it, so the serialized guards are dead weight. Worse, the pickling can still fail on a frame the caller never asked to capture, bypassing the entry or, for a strict capture, raising PackageError out of an ordinary recompile. Refusing to compile such a frame is not an option either: the artifact simply does not cover it, and the program has to keep running.

Fix. _compile_inner computes record = package is not None and not package.serving next to the existing capture flag and gates three things on it. The save= argument to the guard build is now on only when the compile records, so a serving package never runs the serialization build. The strict_error= argument is likewise on only for a recording explicit capture, since raise-versus-bypass only means something when a save build can fail. The block that calls add_guarded_code, then adds the inlined source and updates the device type, is entered only when the compile records; its assertion that the guard bytes are present stays sound because the save that would have produced them was skipped under the same condition.

Summary. The three gates are one decision, not three: the save build produces the guard bytes the recording block consumes, and strictness is a property of that save build, so a single record flag keeps them from drifting apart and makes the None assertion in the block a real invariant rather than a coincidence. Two alternatives were rejected: letting a serving package record anyway (wasted serialization on every recompile, plus a pickling failure that would bypass or raise for a frame nobody intends to persist) and making it refuse uncovered frames (the frame must run; with save off the compile is exactly a plain torch.compile, a single strict runtime guard build with the backend installed as usual). Everything else about the frame is unchanged: it still goes through code_context, so its entry exists and has has_compile_id set, it still counts toward the recompile limit through cache_entries, and a non-serving package still records every variant as before. The entry this leaves behind, compiled but with no guarded codes and not bypassed, is precisely the shape the top of the stack reads back as an uncovered frame, so the two commits are a pair. Carved out of #195913.

A serving package compiles the frame twice and records neither variant

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

def fn(x):
    return x + 1

pkg = CompilePackage(fn, serving=True)
counter = torch._dynamo.testing.CompileCounter()
compiled = torch._dynamo.optimize(backend=counter, package=pkg)(fn)
compiled(torch.randn(3))
compiled(torch.randint(0, 5, (3,)))
counter.frame_count                        # 2 on both: the dtype change recompiles as usual
entry = pkg.cache_entry().codes[0]
entry.guarded_codes
# parent: two _GuardedCodeCacheEntry objects, serialized guards and all
# now:    []  (nothing was saved, so nothing was recorded)
entry.bypassed                             # False on both: this is not a bypass
entry.has_compile_id                       # True: code_context still ran for the frame

Dropping serving=True from the constructor gives two guarded codes, one inlined source and a cpu device type on the same run, which is what the parent produced for the serving package too. Pinned by test_serving_package_records_nothing_and_still_recompiles.

Strictness is moot when nothing is saved

class UnpicklableConfig:
    scale = 2.0
    def __reduce__(self):
        raise RuntimeError("config cannot pickle")

def g(x, cfg=UnpicklableConfig()):
    return x.sin() * cfg.scale

with torch._dynamo.config.patch(strict_precompile=False):
    explicit = CompilePackage(g, explicit_capture=True)
    torch._dynamo.optimize(backend="eager", package=explicit)(g)(torch.randn(3))
    # parent and now: PackageError "config cannot pickle" (the commit below)

    both = CompilePackage(g, explicit_capture=True, serving=True)
    torch._dynamo.optimize(backend="eager", package=both)(g)(torch.randn(3))
    # parent: PackageError, strict_error was explicit_capture alone
    # now:    compiles; guarded_codes == [] and bypassed is False,
    #         the serializer that would have hit the config never ran

The raise on the capturing side is pinned by test_explicit_capture_raises_where_the_ambient_cache_bypasses from the commit below; the serving side is pinned by test_serving_package_is_not_strict_about_unserializable_guards, which runs with strict_precompile on and still compiles.

Test Plan:

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

The new tests fail on the parent, where the serving package records both variants and the strict one raises, 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 added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Sep 22, 2026
@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/198090

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:

❌ 10 New Failures, 1 Unclassified Failure

As of commit 383c6b7 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.

[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/1427/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