Skip to content

Invalidate caches after axiom growth - #203253

Closed
laithsakka wants to merge 8 commits into
gh/laithsakka/545/basefrom
gh/laithsakka/545/head
Closed

laithsakka wants to merge 8 commits into
gh/laithsakka/545/basefrom
gh/laithsakka/545/head

Conversation

@laithsakka

@laithsakka laithsakka commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Include axiom growth in the existing cache-version key and advance the version when guards or derived facts add axioms. This prevents stale static-evaluation, simplification and floor-division results after learning new constraints.
Add regression tests for each function that fail before the fix and pass afterward.

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

Differential Revision: D128259287

Differential Revision: D128259287

[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: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - Ready for human review:

The PR adds `len(self.axioms)` to `ShapeEnv._get_key` and bumps the version
after `evaluate_expr` records a guard and after the default-axioms
FloorDivCleanDiv pass, so cached `simplify`, `_maybe_evaluate_static`,
`get_axioms` and `_simplify_floor_div` results no longer go stale.
Recommendation: Approve. The earlier blocking findings are fixed or answered at
HEAD, and every remaining point is non-blocking.

Findings (5):

[minor] torch/fx/experimental/symbolic_shapes.py:9124
Non-blocking, low confidence: `get_axioms` can still miss a guard. `get_axioms`
(`:7308-7337`) is built from `self.guards`, but the new key tracks
`len(self.axioms)` (`:4885`). If a guard is recorded at `:9122` and every key
from its `get_implications` is already in `self.axioms`, the call at `:9124`
does not bump, so `get_axioms()` returns a tuple without that guard. This takes
a new guard expression that simplifies to an existing one, for example
`Eq(Max(0, s0*s1), 30)` after `Eq(s0*s1, 30)`. The callers (`sizevars.py:402`,
`index_propagation.py:241`) re-derive implications through the explicit-axioms
path, so no wrong result was found, and before this PR it was strictly worse.
Adding `len(self.guards)` to `_get_key` would make
`test_guard_updates_get_axioms_result`'s claim hold in general.

[minor] torch/fx/experimental/symbolic_shapes.py:7553
Non-blocking: the explicit-axioms re-simplification at `:7553-7554` looks
redundant. The flag describes `self.axioms`, not the local `subst`. `subst`
comes from `get_implications(self.simplify(e))` (`:7551`), which already uses
the current `divisible` set, and `get_implications`/`canonicalize_bool_expr`
never add FloorDiv atoms. The flag now stays set until a default-axioms call, so
each explicit-axiom cache miss in between repeats this work. Either drop the
call (and the assertion at `test_dynamic_shapes.py:7733`) or add a comment
explaining when it matters.

[minor] torch/fx/experimental/symbolic_shapes.py:4885
Non-blocking, code this PR does not change: the cleanup promised in the author's
reply is not in HEAD. `patch_source_specialization` still runs
`self.axioms.pop(k, None)` on every key in `new_axioms`
(`symbolic_shapes.py:4452-4453`), including keys that existed before entry. The
bump at `:4456` prevents stale caches, but facts proven before the context are
lost for good. If this is meant for a later PR in the stack, a follow-up note
would keep it from getting lost.

[minor] test/test_dynamic_shapes.py:7744
Non-blocking, still open from the last review: `_check_adds_only_axioms`
(`:7744-7763`) never checks that `_version_counter` went up. That bump is the
property under test, and neighbouring tests assert it directly (`:7474-7477`,
`:7660`). Record `v0` before `torch._check` and add
`assertGreater(shape_env._version_counter, v0)`.

[minor] test/test_dynamic_shapes.py:7733
Non-blocking: in
`test_explicit_axioms_preserve_pending_floor_div_resimplification`
(`:7716-7741`), the pending flag comes from `ShapeEnv.__init__`
(`symbolic_shapes.py:4288`), not from `_add_divisible`. Nothing before the
`assertTrue` at `:7733` clears it, so the test still passes if setting the flag
when `divisible` grows (`symbolic_shapes.py:4905-4906`) breaks. Call
`_maybe_evaluate_static` once with default axioms before `_add_divisible`, then
assert the flag is False before and True after. Both subTest cases, `()` and
`(sympy.true,)`, run the same path because neither has a FloorDiv key, so one
case is enough.

Reviewed commit: fd72ae4

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

✅ No Failures

As of commit fd72ae4 with merge base 0522dc9 (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR requires human review:

  • Triviality
    • Not a single-cause bugfix: it bundles two fixes in torch/fx/experimental/symbolic_shapes.py
    • Fix 1 adds len(self.axioms) to _get_key and bumps the version after each guard updates self.axioms
    • Fix 2 stops calls with explicit axioms from clearing _resimplify_floor_div_axioms; the description never explains this cause
  • Scope of behavior change
    • Each new guard implication now bumps _version_counter, clearing every _lru_cache ShapeEnv cache (simplify, _maybe_evaluate_static)
    • Static evaluation can now prove facts it used to leave undecided, so compiled guards and graphs can change
  • Evidence
    • New ShapeEnvAxiomCacheInvalidationTest in test/test_dynamic_shapes.py covers each stale-cache path
    • Every self.axioms write in symbolic_shapes.py is followed by a version update, so validate_shape_env_version_key holds

reason: not_trivial
Reviewed commit: fd72ae4

Inference job

🐛 Report a wrong verdict

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

github-actions[bot]

This comment was marked as outdated.

@laithsakka
laithsakka marked this pull request as draft November 3, 2026 20:05
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pytorchgreenlight[bot]
pytorchgreenlight Bot previously approved these changes Nov 13, 2026
@pytorchgreenlight pytorchgreenlight Bot added GL/approved Approved by GreenLight and removed GL/human GreenLight marked as needing human review labels Nov 13, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 13, 2026
[ghstack-poisoned]
laithsakka added a commit that referenced this pull request Nov 13, 2026
Pull Request resolved: #203253

Include axiom growth in the existing cache-version key and advance the version when guards or derived facts add axioms. This prevents stale static-evaluation, simplification and floor-division results after learning new constraints.
Add regression tests for each function that fail before the fix and pass afterward.

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

Differential Revision: [D128259287](https://our.internmc.facebook.com/intern/diff/D128259287/)
@pytorchgreenlight
pytorchgreenlight Bot dismissed their stale review November 13, 2026 23:27

Superseded by a newer greenlight verdict.

@pytorchgreenlight pytorchgreenlight Bot removed the GL/approved Approved by GreenLight label Nov 13, 2026
@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Nov 13, 2026

@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 fd72ae4: nothing blocking; ready for human review.

The PR adds len(self.axioms) to ShapeEnv._get_key and bumps the version after evaluate_expr records a guard and after the default-axioms FloorDivCleanDiv pass, so cached simplify, _maybe_evaluate_static, get_axioms and _simplify_floor_div results no longer go stale. Recommendation: Approve. The earlier blocking findings are fixed or answered at HEAD, and every remaining point is non-blocking.

⚪ 5 non-blocking findings

self._update_version_counter()

Non-blocking, low confidence: get_axioms can still miss a guard. get_axioms (:7308-7337) is built from self.guards, but the new key tracks len(self.axioms) (:4885). If a guard is recorded at :9122 and every key from its get_implications is already in self.axioms, the call at :9124 does not bump, so get_axioms() returns a tuple without that guard. This takes a new guard expression that simplifies to an existing one, for example Eq(Max(0, s0*s1), 30) after Eq(s0*s1, 30). The callers (sizevars.py:402, index_propagation.py:241) re-derive implications through the explicit-axioms path, so no wrong result was found, and before this PR it was strictly worse. Adding len(self.guards) to _get_key would make test_guard_updates_get_axioms_result's claim hold in general.

An earlier comment near this line has replies: #203253 (comment)


if self._resimplify_floor_div_axioms:

Non-blocking: the explicit-axioms re-simplification at :7553-7554 looks redundant. The flag describes self.axioms, not the local subst. subst comes from get_implications(self.simplify(e)) (:7551), which already uses the current divisible set, and get_implications/canonicalize_bool_expr never add FloorDiv atoms. The flag now stays set until a default-axioms call, so each explicit-axiom cache miss in between repeats this work. Either drop the call (and the assertion at test_dynamic_shapes.py:7733) or add a comment explaining when it matters.


Non-blocking, code this PR does not change: the cleanup promised in the author's reply is not in HEAD. patch_source_specialization still runs self.axioms.pop(k, None) on every key in new_axioms (symbolic_shapes.py:4452-4453), including keys that existed before entry. The bump at :4456 prevents stale caches, but facts proven before the context are lost for good. If this is meant for a later PR in the stack, a follow-up note would keep it from getting lost.

An earlier comment near this line has replies: #203253 (comment)


def _check_adds_only_axioms(self, shape_env, condition):

Non-blocking, still open from the last review: _check_adds_only_axioms (:7744-7763) never checks that _version_counter went up. That bump is the property under test, and neighbouring tests assert it directly (:7474-7477, :7660). Record v0 before torch._check and add assertGreater(shape_env._version_counter, v0).


self.assertTrue(shape_env._resimplify_floor_div_axioms)

Non-blocking: in test_explicit_axioms_preserve_pending_floor_div_resimplification (:7716-7741), the pending flag comes from ShapeEnv.__init__ (symbolic_shapes.py:4288), not from _add_divisible. Nothing before the assertTrue at :7733 clears it, so the test still passes if setting the flag when divisible grows (symbolic_shapes.py:4905-4906) breaks. Call _maybe_evaluate_static once with default axioms before _add_divisible, then assert the flag is False before and True after. Both subTest cases, () and (sympy.true,), run the same path because neither has a FloorDiv key, so one case is enough.

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

@github-actions
github-actions Bot dismissed their stale review November 13, 2026 23:39

Withdrawn: the automated review of fd72ae4 found nothing blocking.

laithsakka added a commit that referenced this pull request Nov 13, 2026
Pull Request resolved: #203253

Include axiom growth in the existing cache-version key and advance the version when guards or derived facts add axioms. This prevents stale static-evaluation, simplification and floor-division results after learning new constraints.
Add regression tests for each function that fail before the fix and pass afterward.

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

Differential Revision: [D128259287](https://our.internmc.facebook.com/intern/diff/D128259287/)
@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 14, 2026
@laithsakka

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

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

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request GL/human GreenLight marked as needing human review Merged ready for review Automated review passed, ready for a final review by a maintainer. release notes: fx release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants