GitHub Repo
PyTorch development build reported to reuse the wrong graph for closures; proposed patch adds operator checks
In a minimal example, addition and multiplication closures both return the addition result after being compiled in sequence. A patch has been proposed but remains unmerged; official CI, the impact on stable releases, and precompilation compatibility still await verification.

The PyTorch community reported a compilation correctness issue on September 20: two closures created by the same factory function may incorrectly reuse the same computation graph when they capture different tensor methods. A proposed fix appeared on September 21 but remained unmerged at the time of review. Because this type of error can silently return plausible numerical results, it warrants attention from teams whose model code uses dynamic function factories. [Issue report](https://github.com/pytorch/pytorch/issues/197811), [proposed fix](https://github.com/pytorch/pytorch/pull/197845)
The minimal example creates two closures that capture tensor addition and multiplication descriptors, respectively. Given inputs of 6 and 3, direct execution returns 9 and 18, but compiling the closures in sequence makes both return 9. The reported environment used a September 19 development build of version 2.15, running on CPU with Python 3.11. The reporter said the issue reproduced in three independent runs without warnings or exceptions. Using top-level addition and multiplication functions instead worked correctly. [Reproduction conditions](https://github.com/pytorch/pytorch/issues/197811)
The issue concerns compilation cache granularity. Official documentation explains that `torch.compile` caches results per code object, so dynamically created copies of a function may share a cache. Whether a cached result can be reused depends on guards that check execution conditions. If the operator captured by a closure is not checked, matching tensor shapes and types alone cannot guarantee identical computational semantics. [Cache mechanism](https://docs.pytorch.org/docs/2.14/generated/torch.compile.html)
According to the patch author, the existing construction path skips guards for descriptors. The proposal adds identity checks for method descriptors and wrapper descriptors. The author chose `BUILTIN_MATCH` because it also supports the guard serialization required for precompilation; `ID_MATCH` alone can check identity but would prevent serialization. The added tests cover switching operators, cache reuse, and accepting the original operator while rejecting the other after restoration. [Patch design](https://github.com/pytorch/pytorch/pull/197845)
Verification remains limited. The author supplied local test results, excluding an accelerator synchronization test that crashed in their environment. The public page showed that official CI was awaiting approval and that no review outcome was available. At this stage, there is therefore no basis to claim that stable releases are broadly affected or that an official fixed release is available. [Verification status](https://github.com/pytorch/pytorch/pull/197845)
Based on this example, engineering teams can interleave execution of closures that use different operators within the same process, compare their outputs with uncompiled results, and preserve the cache across calls. This can detect cache contamination across calls, particularly in workflows that generate multiple model variants from configuration and repeatedly compile them in long-running services. Teams should then track the patch’s merge status, supported versions, and tests for restoring precompiled artifacts before deciding whether to upgrade or adopt an alternative implementation they have independently validated.