PyTorch fixes mutation region tracking bug in pattern matcher rewrites
A subtle metadata caching bug could allow mutable ops to cross region boundaries incorrectly.
The PyTorch team has merged a critical fix for the pattern matcher in the PyTorch compiler. The pattern matcher rewrites graph operations for optimization, but a bug caused cached mutation_region_id metadata to become stale after certain replacements. Specifically, when a rewrite inserted a new mutable operation (e.g., copy_), the metadata was not refreshed. Subsequent pattern matches could then incorrectly consider ops from different mutation regions as adjacent, leading to illegal graph transformations. The commit, authored by jansel and approved by oulgen, addresses issue #132932.
The fix ensures that after each pattern replacement, the cached mutation_region_id metadata is explicitly refreshed. This prevents later matches from crossing into newly inserted mutable ops. The commit also adds regression coverage specifically for replacements that insert copy_ operations, verifying that the fix holds. For developers using PyTorch's compiler stack (e.g., torch.compile, Inductor), this patch eliminates a class of silent correctness bugs in graph optimizations. While the change is small, its impact on reliability is significant—especially for advanced users relying on custom patterns or aggressive fusion.
- Fixes stale mutation_region_id metadata after pattern matcher rewrites in PyTorch's compiler
- Closes issue #132932, where copy_ insertions could cause incorrect mutation region crossings
- Adds regression tests to prevent future occurrences of similar bugs
Why It Matters
Ensures correct graph transformations in PyTorch's compiler, preventing silent errors in optimized models.