Skip to content

[no-ci] [dynamo] Build a package's guards under its serialization filter and capture mode - #198088

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

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

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem. The commit below gives CompilePackage a serialization guard filter and an explicit-capture flag, and the commit two below teaches CheckFunctionManager to honour both, but nothing connects them: _compile never reads either attribute off the package, and DynamoOutput.build_guards has no way to hand them on. So a package constructed with a filter still serializes exactly the guards its runtime cache checks, and an explicit capture still gets its live guards out of a save build, with the caching-precompile filter rewrite applied to them as if it were the ambient cache.

Fix. DynamoOutput.build_guards and DynamoOutput._build_guards in convert_frame.py grow two keyword arguments, serialization_guard_filter_fn and explicit_capture, and pass them straight through to the CheckFunctionManager constructor (both paths, with and without translation validation). The guard-building block in _compile reads them off the package it was handed: the filter is the package's serialization_guard_filter_fn when a package exists and None otherwise, and explicit_capture is true only when a package exists and says so. A test in test_package.py pins the observable split between live and saved guards.

Summary. This is the wiring step of the stack: the two commits below each add one half of the mechanism as pure data or pure guard-manager logic, and this commit is the only place the two meet, so a reviewer can check the threading here without re-reading the guard builder. The kwargs are read from the package rather than from Hooks because they describe the artifact being recorded, not the caller's compile, and a package survives across many frames while hooks are per-call. Passing them as keyword arguments through both build_guards layers, instead of letting CheckFunctionManager look at the package itself, keeps the guard builder free of any knowledge of CompilePackage, as it is today. Nothing else changes: save is still output.package is not None, strict_error is still the default (the commit above ties it to the capture mode), and GraphCaptureOutput.build_guards, which never has a package, is untouched. A package with neither flag set builds exactly the single save build it built before, so the ambient caching-precompile path is unaffected. Carved out of #195913.

The filter applies to the saved copy only

The live cache still recompiles on a dtype change, because the runtime guards keep TENSOR_MATCH; only the pickled guards state drops it. Verified from the top of the stack (nothing here depends on the commits above).

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

def fn(x):
    return x + 1

def drop_tensor_match(entries):
    return [e.guard_type != "TENSOR_MATCH" for e in entries]

def saved_names(pkg):
    names = set()
    for guarded in pkg.cache_entry().codes[0].guarded_codes:
        state = load_guards_state(guarded.guards_state)
        names |= {g.create_fn_name() for g in state.output_graph.guards}
    return names

pkg = CompilePackage(fn, serialization_guard_filter_fn=drop_tensor_match)
counter = CompileCounter()
compiled = torch._dynamo.optimize(backend=counter, package=pkg)(fn)
compiled(torch.randn(3))
compiled(torch.randint(0, 5, (3,)))
counter.frame_count
# parent: 2 (the filter never reached the guard builder, so the live guards were unfiltered either way)
# now:    2 (the live guards are still unfiltered; the dtype change recompiles)
"TENSOR_MATCH" in saved_names(pkg)
# parent: True (the saved copy was the runtime copy; the filter was ignored)
# now:    False (only the serialized copy is filtered; 7 other guard kinds remain)

The same holds with explicit_capture=True on that package. test_serialization_filter_applies_to_the_saved_guards_only in test/dynamo/test_package.py pins both the recompile and the missing TENSOR_MATCH, for the explicit and the ambient package.

An explicit capture without a filter saves its guards unfiltered

The capture mode alone changes how the guards are built (a strict runtime build plus a separate save build), not which guards are saved.

bare = CompilePackage(fn, explicit_capture=True)
torch._dynamo.optimize(backend="eager", package=bare)(fn)(torch.randn(3))
"TENSOR_MATCH" in saved_names(bare)
# parent: True (single save build, same as an ambient package)
# now:    True (separate save build over the unfiltered guards)

The same test's final block pins this; a plain CompilePackage(fn) saves TENSOR_MATCH too.

Test Plan:

python test/dynamo/test_package.py -k test_serialization_filter_applies_to_the_saved_guards_only
python test/dynamo/test_package.py
python test/dynamo/test_guard_serialization.py

The new test fails on the parent (the saved copy still carries TENSOR_MATCH) 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/198088

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 67d3e8a 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/1425/head branch November 28, 2026 01:50
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