Repository navigation
Do not persist evaluate_expr fallback results across ShapeEnv changes - #203254
laithsakka wants to merge 7 commits into
Conversation
PR Status: draftThis PR is waiting for its author to mark it ready for review. 🟡 AUTOMATED REVIEW - OUTDATED (earlier commit) - Changes requested:
Findings (9): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/203254
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New FailuresAs of commit 65a96a0 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 73fdfa1: changes requested before human review.
The PR splits evaluate_expr's cache so decided results persist across version bumps while guard_or_true/guard_or_false fallback answers go in a version-cleared _lru_cache that later facts can refine. The review finds no correctness, replay or BC defects. It recommends Request Changes: the unguarded _get_stack_summary call in _log_suppressed_dde needs fixing, and compile-time evidence is needed that recomputing fallback queries after each bump is acceptable.
🔴 2 blocking findings are attached to the code below.
⚪ 7 non-blocking findings
pytorch/torch/fx/experimental/symbolic_shapes.py
Line 4430 in 73fdfa1
The new WARNING in patch_source_specialization is partly wrong and out of scope. The technical claim holds: replacements/axioms change without _update_version_counter. But "only used by mark_dynamic(..., specialize_on=...)" is wrong, since mark_unbacked also takes specialize_on. "No known users" is also wrong: the feature is documented in mark_dynamic's docstring and tested in test_torchinductor.py, test_aot_autograd_cache.py and test_precompile.py. "We should probably deprecate it" is a policy statement about a documented public API. And the leak predates this PR. Correct the facts and turn the deprecation remark into a TODO with an issue link, or move it to its own PR.
pytorch/torch/fx/experimental/symbolic_shapes.py
Line 8881 in 73fdfa1
The NOTE says _cached_evaluate_expr "keeps decided results for the life of the ShapeEnv". That is not true: it is the file's lru_cache(256) on the class function, a single 256-entry LRU shared by every ShapeEnv and keyed on self, so entries can be evicted. Say "until evicted from the shared 256-entry LRU; never cleared by version bumps". The note also breaks the file's # Note \[...\] naming convention (e.g. # Note \[guard_or_\]). The reference in the _cached_evaluate_expr docstring (8926-8927) splits the title across two lines, so grepping for it fails. Use # Note \[evaluate_expr caching with fallback_value\] and keep each reference on one line.
_UseFallback has no docstring and no __repr__, and is now the return value of the recorded _inner_evaluate_expr. record_shapeenv_event's retlog (recording.py:298-300) logs that value to trace_shape_events. Those lines used to read -> False; they will now read -> <torch.fx.experimental.symbolic_shapes._UseFallback object at 0x...>. Its neighbours _ShapeEnvGuardError and _FallbackTaken both have docstrings. Add a one-line docstring and a __repr__ returning "_USE_FALLBACK".
pytorch/test/test_dynamic_shapes.py
Line 7570 in 73fdfa1
Tests only use fallback_value=False. _cached_evaluate_expr_with_fallback is now the only cache whose key includes fallback_value; _cached_evaluate_expr keys on allow_fallback=True for both directions. If a regression dropped fallback_value from the outer key, guard_or_true(u0 > 0) would return a cached False after guard_or_false(u0 > 0) in the same version, and no new test would fail. Also, the first assertFalse(...) in test_range_update_refines_fallback_result cannot tell "fallback taken" from "proved False". Add assertTrue(shape_env.evaluate_expr(query, fallback_value=True)) before refining; that covers both points.
pytorch/torch/fx/experimental/symbolic_shapes.py
Line 9110 in 73fdfa1
Neither aggressive_guard_free_semantics branch is tested (9109-9119). The PR rewrote both: SKIP_RANGE_ANALYSIS now returns _USE_FALLBACK, and level 1 now maps _maybe_evaluate_range_only(...) is None to the sentinel. No file under test/ sets this config. Add a test under torch.fx.experimental._config.patch(aggressive_guard_free_semantics=...) for levels 1 and 2. Level 1 should check that a range-decidable unbacked query returns the proven value; level 2 should check that the fallback is returned and then refined after constrain_symbol_range.
pytorch/torch/fx/experimental/symbolic_shapes.py
Line 8910 in 73fdfa1
Nothing checks that _log_suppressed_dde still fires after it moved from three return sites in _evaluate_expr into the except _FallbackTaken handler (8910-8911). No test matches "could not evaluate ... due to data dependency". Add an assertLogs("torch.fx.experimental.symbolic_shapes", "INFO") check.
pytorch/test/test_dynamic_shapes.py
Line 7598 in 73fdfa1
test_proven_fallback_query_survives_version_change uses mock.patch.object(..., wraps=...) and adds the file's only mock import (line 9). The sibling cache tests in this file assert cache_info().hits deltas instead. shape_env._cached_evaluate_expr.cache_info() is available, so assert a +1 hit there and drop the mock import.
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.
| allow_fallback=True, | ||
| ) | ||
| except _FallbackTaken: | ||
| self._log_suppressed_dde(orig_expr, fallback_value) |
There was a problem hiding this comment.
🔴 Expensive, unguarded log call now runs far more often. _log_suppressed_dde (8975-8983) calls self._get_stack_summary(True) before log.info even when INFO is disabled. That walks user frames, calls TracingContext.extract_stack() and runs traceback.format_list over the full user stack. Before this PR it ran once per key until LRU eviction. Now it runs on every miss of the version-cleared fallback cache, i.e. once per (query, fallback_value, _version_counter value), and the same INFO line repeats after every bump. The file already guards this pattern at 8716 (if self.log.isEnabledFor(logging.INFO):). Apply that guard inside _log_suppressed_dde.
| # catches _FallbackTaken and returns fallback_value, so an assumed answer | ||
| # is only reused until the ShapeEnv learns something new. | ||
| @functools.partial(_lru_cache, maxsize=256) | ||
| def _cached_evaluate_expr_with_fallback( |
There was a problem hiding this comment.
🔴 Fallback queries are now recomputed after every ShapeEnv version bump, and there are no measurements. Before, an undecided guard_or_* query stayed in the 256-entry LRU until evicted; now it is never stored persistently. _update_version_counter runs on every range refinement, replacement, divisibility update, guard and deferred runtime assert, and each one empties the fallback cache, so every unknown query re-runs the full _inner_evaluate_expr _evaluate_expr path. The _lru_cache prior_version closure is shared across ShapeEnvs, so alternating between two ShapeEnvs also empties it. In unbacked-heavy traces, cost grows from about one evaluation per query to about one per query per bump. With event recording on, each recomputation also records a new evaluate_expr event with a tracked-fakes snapshot, which contradicts the PR body's claim that event recording/replay are unaffected (replay itself is still correct). Provide compile-time numbers on an unbacked-heavy workload (e.g. pr_time_benchmarks) and correct that sentence in the PR description.
There was a problem hiding this comment.
correctness>>> perf + pr time benchmarks already enabled.
There was a problem hiding this comment.
Persistently caching an undecided fallback is incorrect once ShapeEnv learns new facts, so invalidation is intentional. PR-time benchmarks are already enabled; I’ll check their results for regressions. Agreed on the description: recording/replay correctness is preserved, but recomputation can produce additional recorded events—I’ll clarify that wording.
Stack from ghstack (oldest at bottom):
With fallback_value set (guard_or_true/guard_or_false), evaluate_expr returns the fallback when the expression is unknown. The outer lru_cache stored that answer permanently, so after new facts arrived (a guard, a range refinement) the same query kept returning the stale fallback instead of the now-provable result.
Split the cache in two:
Calls without fallback_value go straight to the persistent layer, as before. The recorded _inner_evaluate_expr replaces its optional fallback_value parameter with a required allow_fallback: bool and returns the sentinel instead of raising, so ShapeEnv event recording and replay are unaffected.
Authored with an AI assistant.
@exported-using-ghexport
Differential Revision: D128538854
Differential Revision: D128538854