Skip to content

[no-ci] [dynamo] Register the sources of aotautograd guards on a package's saved guards - #198125

Closed
bobrenjc93 wants to merge 1 commit into
gh/bobrenjc93/1442/basefrom
gh/bobrenjc93/1442/head
Closed

bobrenjc93 wants to merge 1 commit into
gh/bobrenjc93/1442/basefrom
gh/bobrenjc93/1442/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Problem. Since the commit below, the saved copy of a package's guards is built by its own GuardBuilder, over the serialization-filtered guard list, and value pruning keys off that builder's guard_tree_values. The aotautograd guards, DuplicateInputs and StorageOverlap, are subject to neither filter: compile_check_fn installs them on the runtime build unconditionally and load rebuilds them unconditionally from output_graph._aotautograd_guards. But the tensors they name reach guard_tree_values only through get_guard_manager_from_source calls that compile_check_fn makes on the runtime builder (and under use_lamba_guard_for_object_aliasing, DUPLICATE_INPUT names its second input through additional_used_local_vars without registering it at all). When the saved copy holds no other guard on an aliased input, because its TENSOR_MATCH was dropped by the serialization filter, pickle_guards_state prunes that tensor to _Missing while the loaded build reinstalls an aliasing guard rooted at its source.

Fix. Right after the save build, CheckFunctionManager.__init__ walks output_graph.aotautograd_guards and calls serialization_builder.get_guard_manager_from_source for each DuplicateInputs input source and each StorageOverlap overlapping and non-overlapping source, which registers those tensors on the saved copy's guard tree. test_duplicate_input_survives_separate_save_build pins it through a _post_trace hook in the test helper that injects a DuplicateInputs after tracing.

Summary. Registering the sources directly is the shape that names exactly what the saved copy must carry: the aotautograd guards are the unfilterable part of the guard set, so what they reach is guarded in the artifact by construction. Two alternatives were rejected. Inheriting the runtime builder's whole guard_tree_values would keep every value the live guards reach, including values whose guards the filter dropped on purpose, and since membership in that dict is the prune gate in pickle_guards_state and reducer_override, a non-portable value would be pickled instead of pruned; the commit below pins the pruning with test_value_reached_only_by_a_dropped_guard_is_pruned. Inheriting only the entries compile_check_fn added would depend on whether an unrelated runtime guard had already registered the same tensor during the runtime build, which excludes it from the delta, so the artifact would keep or lose an aliased input depending on which other guards happened to exist. Nothing changes for the ambient caching-precompile path, whose single build already runs compile_check_fn on the builder it serializes from. Carved out of #195913.

The aliased input stays in the artifact even when nothing else in the saved copy reaches it

def fn(x, x_):
    return x + x_

def inject(output):  # what AOTAutograd records for two aliased inputs
    output.tracing_context.guards_context.aotautograd_guards.append(
        DuplicateInputs(LocalSource("x"), LocalSource("x_"))
    )

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

with torch._dynamo.config.patch(use_lamba_guard_for_object_aliasing=True):
    ref, loaded = self._test_serialization(
        "TENSOR_MATCH", fn, x, x,
        _explicit_capture=True,
        _serialization_guard_filter_fn=drop_tensor_match,  # the saved copy has no TENSOR_MATCH
        _post_trace=inject,
    )
load_guards_state(self._cached_guards_state).output_graph.local_scope["x_"]
# parent: _Missing(missing values); no guard of the saved copy reached x_
# now:    a tensor; the DuplicateInputs sources were registered on the saved copy's tree
ref.check({"x": x, "x_": x}), loaded.check({"x": x, "x_": x})            # (True, True)
ref.check({"x": x, "x_": torch.randn(3, 2)}), loaded.check(...)         # (False, False)

Pinned by test_duplicate_input_survives_separate_save_build, which fails on the parent with _Missing(missing values) is not an instance of <class 'torch.Tensor'>.

Test Plan:

python test/dynamo/test_guard_serialization.py -k separate_save_build
python test/dynamo/test_guard_serialization.py

The new test fails on the parent, where the aliased input is pruned from the saved copy, 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 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/198125

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 e9afcd3 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.

@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/1442/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