Skip to content

Invalidate ShapeEnv caches when an existing replacement is refined - #203255

Draft
laithsakka wants to merge 7 commits into
gh/laithsakka/547/basefrom
gh/laithsakka/547/head
Draft

laithsakka wants to merge 7 commits into
gh/laithsakka/547/basefrom
gh/laithsakka/547/head

Conversation

@laithsakka

@laithsakka laithsakka commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:

  • _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
@exported-using-ghexport

Differential Revision: D128735596

Differential Revision: D128735596

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/inductor release notes: fx release notes category labels Nov 3, 2026
@pytorch-bot

pytorch-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

PR Status: draft

This PR is waiting for its author to mark it ready for review. Pre-review does not advance while the PR is a draft.

🟡 AUTOMATED REVIEW - OUTDATED (earlier commit) - Changes requested:

The PR makes ShapeEnv._get_key() use _replacements_version_counter instead of
len(self.replacements), so refining a replacement invalidates versioned caches,
and stops _find's path compression from bumping counters. The core fix is right,
but _constrain_unify's direct writes to replacements now go undetected and the
path_compression contract overclaims. Recommendation: Request Changes.

Findings (4):

[major] torch/fx/experimental/symbolic_shapes.py:4929
`_constrain_unify`'s direct writes no longer affect the cache key.
`ShapeEnv._constrain_unify` (behind the public `constrain_unify`) writes
`self.replacements[...] = ...` directly (~:4727, :4737, :4744) and never bumps a
counter. Before this PR, adding a key changed `len(self.replacements)`, so any
later `_update_version_counter()` (path compression, `_update_divisible`, the
guard path) cleared the `_version_counter` caches, and the next compression
bumped `_replacements_version_counter`. Now those calls see an unchanged key and
compression bumps nothing, so the `u1 -> u0` write stays invisible to every
cache until some unrelated real change. `config.validate_shape_env_version_key`
can no longer catch this bug class because the counter is now the key. The PR
description doesn't account for this path. Fix: route `_constrain_unify` through
`_set_replacement`, or add `self._replacements_version_counter += 1;
self._update_version_counter()` after each write, or keep
`len(self.replacements)` in the key next to the counter. Also update the
`_lru_cache` docstring (~:2774-2776), which says calling
`_update_version_counter` is enough; for replacements you must now also bump
`_replacements_version_counter`.

[major] torch/fx/experimental/symbolic_shapes.py:8172
The `path_compression` flag's contract is unchecked and its wording claims more
than it delivers. It turns off cache invalidation in a general-purpose mutator,
and nothing checks that "tgt is an equivalent, more resolved form"; a wrong
`path_compression=True` would silently leave stale `replace`, `_find`,
`SymNode.expr` and symbolic-op-memo results, undetectable by validation mode.
"Cached results stay valid" / "no cache is cleared" are also inaccurate:
compression still runs `_update_var_to_range` on `a` (:8203) and possibly `b`
(:8231), bumping `_var_to_range_version_counter` and `_version_counter` when
ranges tighten (e.g. `a  [0,100]`, `a -> b+1`, `b -> c+2`, `c  [0,10]` narrows
`a` to `[3,13]`). Direct readers of `self.replacements` see a different form,
e.g. `_size_hinting.py:366`. Fix: say exactly what the flag skips (only the
`_replacements_version_counter` bump) and that the guarantee covers only
`_find`/`replace` consumers; rename the flag after its effect or move
compression into a dedicated private helper; consider asserting equivalence when
`validate_shape_env_version_key` is on.

[major] test/test_dynamic_shapes.py:7497
The new tests miss the behaviour and the regression path.
`test_path_compression_preserves_versions` asserts only counter values; its
`_version_counter` assertion also passes on pre-PR code (len stays 2, symbols
unbounded), so only the last assertion distinguishes old from new. It never
shows that a cached `simplify()` or `_maybe_evaluate_static()` result survives
compression; use the `fn.cache_info().hits` pattern already used in this class
(:7437-7440, :7480-7482). Its setup repeats the a/b/c chain from
`test_transitive_replacement_and_noop`; fold the counter checks into that test
or a shared helper. No test covers `constrain_unify` with SymInts followed by
`simplify` or `_maybe_evaluate_static`, the path this PR stops invalidating. Add
a test that cached results survive compression and that `constrain_unify`
invalidates caches.

(1 more not shown, see the review run)

Reviewed commit: b7b64d8 (NOT the current head ab0e3ab)

Review run

🐛 Report a bad review

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/203255

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures

As of commit ab0e3ab with merge base 0522dc9 (image):

🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:

  • Triviality
    • Changes ShapeEnv._get_key() from len(self.replacements) to _replacements_version_counter, the key gating every versioned ShapeEnv cache
    • Also adds path_compression=True in _find, which now skips both counter bumps in _set_replacement; that is a second behavior change
  • Review questions
    • Whether the compressed replacement in _find is always equivalent for cached replace(), SymNode.expr and symop memo results needs a symbolic-shapes reviewer
    • Invalidating on every replacement refinement can change dynamo compile-time cache hit rates; nothing in the diff measures it
  • Tests
    • test_replacement_refinement_invalidates_simplify and test_path_compression_preserves_versions cover both paths, but core cache semantics need a human look

reason: not_trivial
Reviewed commit: b7b64d8 (NOT the current head ab0e3ab)

Inference job

🐛 Report a wrong verdict

NEW FAILURES - The following jobs have failed:

  • BC Lint / bc_linter (gh)
    ##[error]API rate limit exceeded for installation. If you reach out to GitHub Support for help, please include the request ID C410:335735:A846D:B2FB7:6AF749A6 and timestamp 2026-11-13 17:32:22 UTC. For more on scraping GitHub and how it may affect your rights, please review our Terms of Service (https://docs.github.com/en/site-policy/github-terms/github-terms-of-service) - https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api#rate-limiting
  • pull / dynamo-cpython-test / test (dynamo_wrapped, 4, 7, lf-l-x86iavx512-8-64) (gh)
    test/test_dynamic_shapes.py::ShapeEnvAxiomCacheInvalidationTest::test_guard_updates_get_axioms_result

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of b7b64d8: changes requested before human review.

The PR makes ShapeEnv._get_key() use _replacements_version_counter instead of len(self.replacements), so refining a replacement invalidates versioned caches, and stops _find's path compression from bumping counters. The core fix is right, but _constrain_unify's direct writes to replacements now go undetected and the path_compression contract overclaims. Recommendation: Request Changes.

🔴 3 blocking findings are attached to the code below.

⚪ 1 non-blocking finding

self._replacements_version_counter,

Inductor's SizeVarAllocator still has the same refinement bug (torch/_inductor/sizevars.py:330-337, :354-361, :1271-1278). It aliases shape_env.replacements (:296) and invalidates simplify_with_ranges, _simplify_loops and its _lru_cache-wrapped stride and hint caches on len(self.replacements). The PR's own scenario (s0 -> 2*s1 refined to s0 -> 2*s2) leaves those caches stale, so the claim that "every replacement change invalidates all versioned caches" holds only inside ShapeEnv. Fix: key these caches on (shape_env._replacements_version_counter, len(self.replacements)), which also covers the direct writes, here or in a follow-up linked from the description.

Please address the findings and push; a new commit normally gets a fresh automated review.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

"""
return (
len(self.replacements),
self._replacements_version_counter,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 _constrain_unify's direct writes no longer affect the cache key. ShapeEnv._constrain_unify (behind the public constrain_unify) writes self.replacements\[...\] = ... directly (:4727, :4737, :4744) and never bumps a counter. Before this PR, adding a key changed len(self.replacements), so any later _update_version_counter() (path compression, _update_divisible, the guard path) cleared the _version_counter caches, and the next compression bumped _replacements_version_counter. Now those calls see an unchanged key and compression bumps nothing, so the u1 -> u0 write stays invisible to every cache until some unrelated real change. config.validate_shape_env_version_key can no longer catch this bug class because the counter is now the key. The PR description doesn't account for this path. Fix: route _constrain_unify through _set_replacement, or add self._replacements_version_counter += 1; self._update_version_counter() after each write, or keep len(self.replacements) in the key next to the counter. Also update the _lru_cache docstring (:2774-2776), which says calling _update_version_counter is enough; for replacements you must now also bump _replacements_version_counter.

Adds or updates a replacement for a symbol.
Use this instead of `self.replacements[a] = tgt`.

path_compression: tgt is an equivalent, more resolved form of the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 The path_compression flag's contract is unchecked and its wording claims more than it delivers. It turns off cache invalidation in a general-purpose mutator, and nothing checks that "tgt is an equivalent, more resolved form"; a wrong path_compression=True would silently leave stale replace, _find, SymNode.expr and symbolic-op-memo results, undetectable by validation mode. "Cached results stay valid" / "no cache is cleared" are also inaccurate: compression still runs _update_var_to_range on a (:8203) and possibly b (:8231), bumping _var_to_range_version_counter and _version_counter when ranges tighten (e.g. a \[0,100\], a -> b+1, b -> c+2, c \[0,10\] narrows a to \[3,13\]). Direct readers of self.replacements see a different form, e.g. _size_hinting.py:366. Fix: say exactly what the flag skips (only the _replacements_version_counter bump) and that the guarantee covers only _find/replace consumers; rename the flag after its effect or move compression into a dedicated private helper; consider asserting equivalence when validate_shape_env_version_key is on.

# Previously, simplify kept the result computed with the old replacement.
self.assertEqual(shape_env.simplify(s0 + 1), 2 * s2 + 1)

def test_path_compression_preserves_versions(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 The new tests miss the behaviour and the regression path. test_path_compression_preserves_versions asserts only counter values; its _version_counter assertion also passes on pre-PR code (len stays 2, symbols unbounded), so only the last assertion distinguishes old from new. It never shows that a cached simplify() or _maybe_evaluate_static() result survives compression; use the fn.cache_info().hits pattern already used in this class (:7437-7440, :7480-7482). Its setup repeats the a/b/c chain from test_transitive_replacement_and_noop; fold the counter checks into that test or a shared helper. No test covers constrain_unify with SymInts followed by simplify or _maybe_evaluate_static, the path this PR stops invalidating. Add a test that cached results survive compression and that constrain_unify invalidates caches.

@laithsakka
laithsakka marked this pull request as draft November 3, 2026 20:05
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 3, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 464009150
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 3, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 464132455
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
laithsakka added a commit that referenced this pull request Nov 3, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 464132455
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 4, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 464969760
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 5, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 465370971
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 13, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 472489249
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
laithsakka added a commit that referenced this pull request Nov 13, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 472489249
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 13, 2026
Pull Request resolved: #203255

ShapeEnv._get_key() summarized replacements as len(self.replacements). Refining an existing replacement keeps the length, so _version_counter did not advance and every cache keyed on it (simplify, _maybe_evaluate_static, get_axioms, ...) kept results computed with the old replacement:

```
env = ShapeEnv()
s0 = create_symint(env, 8, duck=False).node.expr
s1 = create_symint(env, 4, duck=False).node.expr
s2 = create_symint(env, 4, duck=False).node.expr
env._set_replacement(s0, 2 * s1, "first")
env.simplify(s0 + 1)                        # 2*s1 + 1, cached
env._set_replacement(s0, 2 * s2, "refine")  # same key, len(replacements) unchanged
env.replace(s0 + 1)                         # 2*s2 + 1 (keyed on _replacements_version_counter)
env.simplify(s0 + 1)                        # 2*s1 + 1: stale, _version_counter did not move
```

Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.

Related change:
- _find's path compression rewrites a replacement into an equivalent, more resolved form (a -> b + 1 becomes a -> c + 3 once b -> c + 2 exists). It changes no result, so it no longer bumps either counter. Without this, a plain simplify() call that compresses a path would clear every ShapeEnv cache.

patch_source_specialization still writes replacements without bumping counters, as its docstring warns; its replacements are always added together with axioms, which len(self.axioms) in the key still tracks.

Authored with an AI assistant.
ghstack-source-id: 473154895
@exported-using-ghexport

Differential Revision: [D128735596](https://our.internmc.facebook.com/intern/diff/D128735596/)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor GL/human GreenLight marked as needing human review in progress PR is being worked on by the author, until the automated review accepts it release notes: fx release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants