Repository navigation
Invalidate ShapeEnv caches when an existing replacement is refined - #203255
laithsakka wants to merge 7 commits into
Conversation
PR Status: draftThis 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:
Findings (4): Reviewed commit: 🔗 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 FailuresAs of commit ab0e3ab with merge base 0522dc9 ( 🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:
reason: NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
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
pytorch/torch/fx/experimental/symbolic_shapes.py
Line 4929 in b7b64d8
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, |
There was a problem hiding this comment.
🔴 _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 :2774-2776), which says calling 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 (_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 |
There was a problem hiding this comment.
🔴 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): |
There was a problem hiding this comment.
🔴 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.
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/)
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/)
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/)
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/)
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/)
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/)
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/)
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/)
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:
Use _replacements_version_counter in _get_key() in place of the length, so every replacement change invalidates all versioned caches.
Related change:
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