Repository navigation
Do not memoize symbolic mod; key the op memo on resolved operands - #203252
laithsakka wants to merge 6 commits into
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - Ready for human review:
Findings (5): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/203252
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit bd6673e with merge base e1c7456 ( 🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:
reason: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
Code Review Summary this github actions reviewer must be a nerd or nit picker lol |
There was a problem hiding this comment.
Automated review of bd6673e: nothing blocking; ready for human review.
The PR stops memoizing symbolic mod, keys the symbolic op memo on the resolved operands (op, lhs.expr, rhs.expr), clears the memo lazily when replacements change, and trusts _optimized_summation only when expr is _expr. Recommendation: Approve. Nothing blocks. The review marks every open point non-blocking: two missing regression tests, the unmeasured hit-path cost, operands being resolved twice, and comment/description wording.
⚪ 5 non-blocking findings
pytorch/test/test_dynamic_shapes.py
Line 7559 in bd6673e
The right-operand flag gate (sym_node.py:1611) has no regression test (non-blocking). Every new test puts the flagged, replaced operand on the left (p + s2, q + s2, t + s2). Suppose :1611 went back to other._optimized_summation. Then with p = s0 + s1, s0 -> s1, computing s2 + p would call _binary_search_insert_arg(\[2, s1\], s2) and return s1 + s2 + 2 instead of 2*s1 + s2. That is the same wrong-result bug this PR fixes, with the operands swapped. The memo can't hide it, because the key ("add", s2, 2*s1) is new. Fix: add self.assertEqual((s2 + p).node.expr, 2 * s1.node.expr + s2.node.expr) to test_optimized_summation_flag_after_replacement.
An earlier comment near this line has replies: #203252 (comment)
pytorch/test/test_dynamic_shapes.py
Line 7548 in bd6673e
The fetch-after-resolve ordering (sym_node.py:1565-1568) is untested (non-blocking). In test_range_change_refreshes_symop_memo, the second node.add(one) bumps the replacement counter while resolving node.expr. The test asserts only _expr, which is the same under either ordering. Moving the fetch back above the key would keep every test green; the only effect would be a lost hit. Fix: install _count_symop_hits before the second add, assert hits == 0 after it, then run a third node.add(one) and assert hits == 1.
pytorch/torch/fx/experimental/sym_node.py
Line 1565 in bd6673e
Operands are resolved twice (non-blocking). The key reads self.expr/other.expr, and the computation (:1607-1611, :1623-1640, :1642-1646) reads them again. Resolving one operand can call _find -> _set_replacement(..., "find") (symbolic_shapes.py:8322), which bumps the range and/or replacement version. The memo is fetched after the key is built, so an entry can be stored under a less resolved key than its value. The value is still correct, but the entry can never be hit, and the miss path makes extra replace() calls. Fix: bind lhs, rhs = self.expr, other.expr once and use them for the key, the computation and the gate (lhs is self._expr). This carries over unchanged from the last review.
An earlier comment near this line has replies: #203252 (comment)
pytorch/torch/fx/experimental/symbolic_shapes.py
Line 4263 in bd6673e
Comments and description don't match the code (non-blocking). :4263-4264 says the memo "is also emptied when it changes", and the Note at :4274-4275 says _get_symop_cache "empties the memo whenever replacements change". The clear actually happens lazily, on the next _get_symop_cache() call (:4894-4896). The comment at sym_node.py:1566-1567 already describes this correctly; use the same wording. The PR description still says the memo is emptied "only to bound its size". In fact the memo is an uncapped dict between replacement changes, and the Note now gives a different reason. The resolution table says this was fixed, but only the in-code comments were changed.
pytorch/torch/fx/experimental/sym_node.py
Line 1565 in bd6673e
The hit path's extra cost is still not measured against the base (non-blocking). A memo hit used to key on the plain _expr attribute; it now calls the SymNode.expr property on both operands. Once the ShapeEnv has a replacement, that property calls replace() whenever either counter has moved or the node has never resolved. Nodes built on a hit start with _expr_cache=None (:132-134), so each link in a chain of hits pays one replace(). The posted numbers compare mod memoized vs. unmemoized on the stack top, not this PR vs. its base. symint_sum, sum_floordiv and update_hint_regression are mostly unbacked, so they barely reach this path. Ask: pr_time_benchmarks instruction counts, base vs. head, on backed-dynamic workloads such as diffusion_dit_hierarchical_dynamic or the add_loop dynamic variants. Low confidence, secondary: infer_size (torch/_prims_common/init.py:1034) also runs numel % newsize without going through a comparator, so the reply's reasoning for why % is rare is incomplete. The code comment at :1563 is accurate.
An earlier comment near this line has replies: #203252 (comment)
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
Withdrawn: the automated review of bd6673e found nothing blocking.
|
@pytorchbot merge |
Merge startedYour 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 |
Stack from ghstack (oldest at bottom):
The symbolic op memo in
binary_magic_impl(added in #193219) was keyed on(op, lhs._expr, rhs._expr, _replacements_version_counter)and never cleared. This had three problems:a % bpicksModorPythonModfrombound_sympy, which readsvar_to_range, but ranges weren't in the key. After the operands' ranges narrowed to non-negative, the memo kept returningPythonMod(same value, less simplified).lhs.expr/rhs.expr, which go throughreplace()/_findand also depend on value ranges. After a range change let_findresolve further, the memo kept returning the less resolved result.modis no longer memoized. The key is now(op, lhs.expr, rhs.expr), the resolved operands, whichSymNode.expralready caches on both the replacement and range counters, so a hit never depends on outdated replacements or ranges.ShapeEnv._get_symop_cache()empties the memo when replacements change, only to bound its size.The add path passed each operand's
_optimized_summationflag, which describes its unresolved_expr, together with the resolvedexpr. After a replacement such ass0 -> s1,s0 + s1resolves to2*s1but keeps the flag, and addings2spliced it intoMul(2, s1)'s args, givings1 + s2 + 2instead of2*s1 + s2. This predates the memo, but the resolved key would share that result with every node that resolves to2*s1. The flag is now trusted only whenexpr is _expr.Authored with an AI assistant.
@exported-using-ghexport
Differential Revision: D129028772
Differential Revision: D129028772