From 0a057dff99152a9e1340d91c42e157953c73503c Mon Sep 17 00:00:00 2001 From: Prawal Pokharel <57414987+prawalpokharel@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:36:22 -0600 Subject: [PATCH 1/3] Faster affects_comparison with an exact numeric check --- .../make_tile_shapes.py | 59 +++++++++++++++++ tests/test_affects_comparison.py | 64 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 tests/test_affects_comparison.py diff --git a/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py b/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py index 7d565034..482d07d5 100755 --- a/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py +++ b/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py @@ -2,6 +2,7 @@ from functools import lru_cache from math import ceil, prod import copy +import random import re import resource import signal @@ -835,10 +836,68 @@ def f_slash_other_f(f: Expr, symbols_enumerated: set[Symbol]): return f.xreplace(fs) / f > 1 +_PROBE_TRIALS = 4 + + +def _exact_value(f: Expr, point: dict): + try: + v = f.xreplace(point) + except (TypeError, ValueError): + # e.g. a pole inside Max/Min: Max(2, zoo) raises + return None + return v if v.is_Rational else None + + +def _provably_changes_with(f: Expr, s: Symbol, symbols_enumerated: set[Symbol]): + """ + Fast path for affects_comparison. Plugs a few positive integers into f and checks + whether f(E_2) - f(E) and f(E_2) / f(E) both change when only s changes. If they + do, s can't cancel out of either expansion, so affects_comparison is True. + Returns False if this doesn't settle it. Floats are compared as exact rationals, + so if expand() rounds a term in s to exactly zero, this keeps s where the full + check would drop it. + """ + if not all(x.is_positive for x in f.free_symbols): + return False + # Exact rationals, so float rounding can't make two values look different + floats = f.atoms(sympy.Float) + if floats: + f = f.xreplace({c: sympy.Rational(c) for c in floats}) + rng = random.Random(0) # same points on every call + syms = sorted(f.free_symbols, key=str) + enumerated = [x for x in syms if x in symbols_enumerated] + shared = [x for x in syms if x not in symbols_enumerated and x != s] + s_enumerated = s in symbols_enumerated + minus_changes = ratio_changes = False + for _ in range(_PROBE_TRIALS): + e1 = {x: sympy.Integer(rng.randint(1, 97)) for x in enumerated} + e2 = {x: sympy.Integer(rng.randint(1, 97)) for x in enumerated} + sh = {x: sympy.Integer(rng.randint(1, 97)) for x in shared} + a, b = (sympy.Integer(v) for v in rng.sample(range(1, 97), 2)) + if s_enumerated: # E_2 has its own copy of s, fixed at e2[s] + f2a = f2b = _exact_value(f, {**e2, **sh}) + else: + f2a = _exact_value(f, {**e2, **sh, s: a}) + f2b = _exact_value(f, {**e2, **sh, s: b}) + f1a = _exact_value(f, {**e1, **sh, s: a}) + f1b = _exact_value(f, {**e1, **sh, s: b}) + if any(v is None for v in (f1a, f1b, f2a, f2b)): + continue + minus_changes |= f2a - f1a != f2b - f1b + if min(f1a, f1b, f2a, f2b) > 0: + ratio_changes |= f2a / f1a != f2b / f1b + if minus_changes and ratio_changes: + return True + return False + + @lru_cache(maxsize=10000) def affects_comparison(f: Expr, s: Symbol, symbols_enumerated: set[Symbol]): if not isinstance(f, sympy.Expr): return False + # Most calls end up True, and getting there through expand() is slow + if _provably_changes_with(f, s, symbols_enumerated): + return True delta = simplify(f_minus_other_f(f, symbols_enumerated)) if s not in getattr(delta, "free_symbols", set()): return False diff --git a/tests/test_affects_comparison.py b/tests/test_affects_comparison.py new file mode 100644 index 00000000..0a0b1aee --- /dev/null +++ b/tests/test_affects_comparison.py @@ -0,0 +1,64 @@ +""" +Checks the fast path in affects_comparison against the full symbolic check. +""" + +import unittest +from unittest import mock + +import sympy + +from accelforge.mapper.FFM._make_pmappings.make_pmappings_from_templates import ( + make_tile_shapes as mts, +) + +x, y, z, n = sympy.symbols("x y z n", integer=True, positive=True) + + +def symbolic_only(f, s, symbols_enumerated): + # affects_comparison with the fast path turned off + with mock.patch.object(mts, "_provably_changes_with", return_value=False): + return mts.affects_comparison.__wrapped__(f, s, symbols_enumerated) + + +class TestAffectsComparisonFastPath(unittest.TestCase): + formulas = [ + x * y + 3 * z, + sympy.Max(x * n, y) + sympy.Float(0.37) * z * x, + sympy.ceiling(n / x) * y + sympy.Min(x, z) * sympy.Float(1.5e-12), + (x + y) ** 2 / z + n, + x + y, # y cancels in f(E_2) - f(E) + x * y, # y cancels in f(E_2) / f(E) + sympy.Max(x, 4) * n + y, + ] + + def test_matches_symbolic_check(self): + for enumerated in (frozenset(), frozenset({x, z}), frozenset({x, y, z, n})): + for f in self.formulas: + # n is missing from some formulas, which covers s not in f + for s in sorted(f.free_symbols | {n}, key=str): + with self.subTest(f=f, s=s, enumerated=enumerated): + self.assertEqual( + mts.affects_comparison.__wrapped__(f, s, enumerated), + symbolic_only(f, s, enumerated), + ) + + def test_never_claims_a_cancelled_symbol(self): + self.assertFalse(mts._provably_changes_with(x + y, y, frozenset({x}))) + self.assertFalse(mts._provably_changes_with(x * y, y, frozenset({x}))) + + def test_proves_a_symbol_that_matters(self): + f = sympy.Max(x, y) * y + x + self.assertTrue(mts._provably_changes_with(f, y, frozenset({x}))) + + def test_skips_points_where_f_is_undefined(self): + # y == x puts a pole inside Max, which sympy refuses to compare + f = sympy.Max(2, 1 / (y - x)) + self.assertTrue(mts.affects_comparison.__wrapped__(f, y, frozenset({x}))) + + def test_declines_without_positive_assumptions(self): + w = sympy.Symbol("w") + self.assertFalse(mts._provably_changes_with(x * w + w**2, w, frozenset({x}))) + + +if __name__ == "__main__": + unittest.main() From fa0f19956c09d188d6861f9daff9ef4f780c07d1 Mon Sep 17 00:00:00 2001 From: Prawal Pokharel <57414987+prawalpokharel@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:28:43 -0600 Subject: [PATCH 2/3] Explain the affects_comparison fast path and use fixed values Rename _provably_changes_with to _has_witness_that_s_affects_comparison and document what a True and a False mean and why a witness proves the result for every input. Replace the seeded random values with a fixed list of primes, giving s one small and one large value, and name the variables after what they hold. Name the tests after what they check, and add tests for an enumerated symbol and for _exact_value. --- .../make_tile_shapes.py | 155 +++++++++++++----- tests/test_affects_comparison.py | 73 ++++++--- 2 files changed, 168 insertions(+), 60 deletions(-) diff --git a/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py b/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py index 482d07d5..7fcd3847 100755 --- a/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py +++ b/accelforge/mapper/FFM/_make_pmappings/make_pmappings_from_templates/make_tile_shapes.py @@ -2,7 +2,6 @@ from functools import lru_cache from math import ceil, prod import copy -import random import re import resource import signal @@ -836,57 +835,132 @@ def f_slash_other_f(f: Expr, symbols_enumerated: set[Symbol]): return f.xreplace(fs) / f > 1 -_PROBE_TRIALS = 4 +# Values given to the symbols of f when looking for a witness in +# _has_witness_that_s_affects_comparison. Any positive integers would do, since a +# witness found at any values proves the result. Varied primes make it unlikely that +# terms cancel by accident at the chosen values, which would only make the search give +# up and leave the call to the symbolic check. +_WITNESS_PRIMES = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61) +_WITNESS_PRIMES += (67, 71, 73, 79, 83, 89, 97) +# How many sets of values to try before leaving the call to the symbolic check. +_WITNESS_TRIES = 6 -def _exact_value(f: Expr, point: dict): + +def _prime(index: int) -> sympy.Integer: + """Returns the prime at index in _WITNESS_PRIMES, wrapping around at the end.""" + return sympy.Integer(_WITNESS_PRIMES[index % len(_WITNESS_PRIMES)]) + + +def _prime_values(symbols: list[Symbol], first: int) -> dict[Symbol, sympy.Integer]: + """Gives the symbols consecutive primes from _WITNESS_PRIMES, starting at first.""" + return {symbol: _prime(first + i) for i, symbol in enumerate(symbols)} + + +def _exact_value(f: Expr, values: dict[Symbol, sympy.Integer]) -> Expr | None: + """Returns f at the given values if the result is an exact rational number. + + Returns None when the result can't be compared exactly. That happens when f is + undefined at these values (sympy raises on Max(2, zoo), for example, when a pole is + inside a Max), or when the result is not rational (a square root, for example). + """ try: - v = f.xreplace(point) + value = f.xreplace(values) except (TypeError, ValueError): - # e.g. a pole inside Max/Min: Max(2, zoo) raises return None - return v if v.is_Rational else None + return value if value.is_Rational else None -def _provably_changes_with(f: Expr, s: Symbol, symbols_enumerated: set[Symbol]): - """ - Fast path for affects_comparison. Plugs a few positive integers into f and checks - whether f(E_2) - f(E) and f(E_2) / f(E) both change when only s changes. If they - do, s can't cancel out of either expansion, so affects_comparison is True. - Returns False if this doesn't settle it. Floats are compared as exact rationals, - so if expand() rounds a term in s to exactly zero, this keeps s where the full - check would drop it. +def _has_witness_that_s_affects_comparison( + f: Expr, s: Symbol, symbols_enumerated: set[Symbol] +) -> bool: + """Tries to show, without expanding, that affects_comparison returns True. + + affects_comparison(f, s, symbols_enumerated) returns True when s is still a free + symbol after sympy.expand of both + + f(E_2) - f(E) > 0 and f(E_2) / f(E) > 1, + + where f(E_2) is f with every enumerated symbol replaced by a new copy of it. + Expanding is the slow part, so this function first looks for a witness: values of + the symbols where changing only s changes f(E_2) - f(E), and values where changing + only s changes f(E_2) / f(E). + + A witness proves the result for every input: + + 1. expand() returns an expression with the same value, and an expression that does + not contain s has the same value for any two values of s. So if the value + changes when only s changes, s is a free symbol of the expanded expression. + 2. Neither relational can simplify to True or False, which would leave it with no + free symbols. Both are False when E_2 equals E, so neither is always True. + Swapping E and E_2 negates the difference and inverts the ratio, so if either + one were always False, f(E_2) would equal f(E) at the witness values, and it + doesn't. + + Values are computed exactly. Floats in f are first replaced by their exact rational + value, so rounding can't make two values look different. This is where the answer + can differ from the symbolic check: if expand() rounds a term in s to exactly + zero, the symbolic check drops s and this function keeps it. Keeping a symbol is + the safe direction, because it only means less pruning. + + Only positive integers are tried, so this returns False right away unless every + symbol in f is positive. Otherwise a value might be outside a symbol's domain. + + Args: + f: The formula being compared. + s: The symbol that might affect the comparison. + symbols_enumerated: The symbols that E and E_2 each have their own copy of. + + Returns: + True if a witness was found, which means affects_comparison is True. False if + none was found. False says nothing about s, and the symbolic check decides. """ - if not all(x.is_positive for x in f.free_symbols): + if not all(symbol.is_positive for symbol in f.free_symbols): return False - # Exact rationals, so float rounding can't make two values look different + floats = f.atoms(sympy.Float) if floats: f = f.xreplace({c: sympy.Rational(c) for c in floats}) - rng = random.Random(0) # same points on every call - syms = sorted(f.free_symbols, key=str) - enumerated = [x for x in syms if x in symbols_enumerated] - shared = [x for x in syms if x not in symbols_enumerated and x != s] - s_enumerated = s in symbols_enumerated - minus_changes = ratio_changes = False - for _ in range(_PROBE_TRIALS): - e1 = {x: sympy.Integer(rng.randint(1, 97)) for x in enumerated} - e2 = {x: sympy.Integer(rng.randint(1, 97)) for x in enumerated} - sh = {x: sympy.Integer(rng.randint(1, 97)) for x in shared} - a, b = (sympy.Integer(v) for v in rng.sample(range(1, 97), 2)) - if s_enumerated: # E_2 has its own copy of s, fixed at e2[s] - f2a = f2b = _exact_value(f, {**e2, **sh}) + + symbols = sorted(f.free_symbols, key=str) + enumerated = [x for x in symbols if x in symbols_enumerated] + shared = [x for x in symbols if x not in symbols_enumerated and x != s] + + difference_changes = False + ratio_changes = False + for attempt in range(_WITNESS_TRIES): + # Each attempt starts E, E_2 and the shared symbols at a different place in + # the list, and the offsets keep each enumerated symbol's values in E and E_2 + # apart. s gets one small and one large value, so that a term in s that only + # wins a Max or Min (or changes a ceiling) at one end still shows up. + values_e = _prime_values(enumerated, 5 * attempt + 2) + values_e_2 = _prime_values(enumerated, 5 * attempt + 12) + values_shared = _prime_values(shared, 5 * attempt + 7) + s_1, s_2 = _prime(attempt), _prime(-1 - attempt) + + if s in symbols_enumerated: + # E_2 has its own copy of s, so f(E_2) is the same for both values of s. + f_e_2 = _exact_value(f, {**values_e_2, **values_shared}) + f_e_2_at_s_1 = f_e_2_at_s_2 = f_e_2 else: - f2a = _exact_value(f, {**e2, **sh, s: a}) - f2b = _exact_value(f, {**e2, **sh, s: b}) - f1a = _exact_value(f, {**e1, **sh, s: a}) - f1b = _exact_value(f, {**e1, **sh, s: b}) - if any(v is None for v in (f1a, f1b, f2a, f2b)): + f_e_2_at_s_1 = _exact_value(f, {**values_e_2, **values_shared, s: s_1}) + f_e_2_at_s_2 = _exact_value(f, {**values_e_2, **values_shared, s: s_2}) + f_e_at_s_1 = _exact_value(f, {**values_e, **values_shared, s: s_1}) + f_e_at_s_2 = _exact_value(f, {**values_e, **values_shared, s: s_2}) + + values = (f_e_at_s_1, f_e_at_s_2, f_e_2_at_s_1, f_e_2_at_s_2) + if any(value is None for value in values): continue - minus_changes |= f2a - f1a != f2b - f1b - if min(f1a, f1b, f2a, f2b) > 0: - ratio_changes |= f2a / f1a != f2b / f1b - if minus_changes and ratio_changes: + + if f_e_2_at_s_1 - f_e_at_s_1 != f_e_2_at_s_2 - f_e_at_s_2: + difference_changes = True + # The ratio needs every value to be positive. A zero would divide by zero, and + # step 2 of the argument above only holds for positive values. + if all(value > 0 for value in values): + if f_e_2_at_s_1 / f_e_at_s_1 != f_e_2_at_s_2 / f_e_at_s_2: + ratio_changes = True + + if difference_changes and ratio_changes: return True return False @@ -895,8 +969,9 @@ def _provably_changes_with(f: Expr, s: Symbol, symbols_enumerated: set[Symbol]): def affects_comparison(f: Expr, s: Symbol, symbols_enumerated: set[Symbol]): if not isinstance(f, sympy.Expr): return False - # Most calls end up True, and getting there through expand() is slow - if _provably_changes_with(f, s, symbols_enumerated): + # A witness settles the common True case without expanding. Without one, the + # symbolic check below decides. + if _has_witness_that_s_affects_comparison(f, s, symbols_enumerated): return True delta = simplify(f_minus_other_f(f, symbols_enumerated)) if s not in getattr(delta, "free_symbols", set()): diff --git a/tests/test_affects_comparison.py b/tests/test_affects_comparison.py index 0a0b1aee..1fc2592a 100644 --- a/tests/test_affects_comparison.py +++ b/tests/test_affects_comparison.py @@ -1,5 +1,9 @@ -""" -Checks the fast path in affects_comparison against the full symbolic check. +"""Tests for the witness search in affects_comparison. + +affects_comparison first looks for a witness that s affects the comparison +(_has_witness_that_s_affects_comparison), and only expands symbolically when it +finds none. These tests check that the answer is the same as the symbolic check's +alone, and that the witness search never claims a symbol that cancels out. """ import unittest @@ -14,50 +18,79 @@ x, y, z, n = sympy.symbols("x y z n", integer=True, positive=True) -def symbolic_only(f, s, symbols_enumerated): - # affects_comparison with the fast path turned off - with mock.patch.object(mts, "_provably_changes_with", return_value=False): +def symbolic_check_only(f, s, symbols_enumerated): + """Runs affects_comparison with the witness search turned off.""" + with mock.patch.object( + mts, "_has_witness_that_s_affects_comparison", return_value=False + ): return mts.affects_comparison.__wrapped__(f, s, symbols_enumerated) -class TestAffectsComparisonFastPath(unittest.TestCase): +class TestAffectsComparisonWitness(unittest.TestCase): formulas = [ + # A plain polynomial. x * y + 3 * z, + # Max, and a Float coefficient. sympy.Max(x * n, y) + sympy.Float(0.37) * z * x, + # ceiling and Min, and a very small Float coefficient. sympy.ceiling(n / x) * y + sympy.Min(x, z) * sympy.Float(1.5e-12), + # A quotient. (x + y) ** 2 / z + n, - x + y, # y cancels in f(E_2) - f(E) - x * y, # y cancels in f(E_2) / f(E) + # With x enumerated, y cancels out of f(E_2) - f(E). + x + y, + # With x enumerated, y cancels out of f(E_2) / f(E). + x * y, + # A Max with a constant. sympy.Max(x, 4) * n + y, ] - def test_matches_symbolic_check(self): + def test_same_answer_as_the_symbolic_check(self): for enumerated in (frozenset(), frozenset({x, z}), frozenset({x, y, z, n})): for f in self.formulas: - # n is missing from some formulas, which covers s not in f + # n is not in every formula, which covers an s that f doesn't use. for s in sorted(f.free_symbols | {n}, key=str): with self.subTest(f=f, s=s, enumerated=enumerated): self.assertEqual( mts.affects_comparison.__wrapped__(f, s, enumerated), - symbolic_only(f, s, enumerated), + symbolic_check_only(f, s, enumerated), ) - def test_never_claims_a_cancelled_symbol(self): - self.assertFalse(mts._provably_changes_with(x + y, y, frozenset({x}))) - self.assertFalse(mts._provably_changes_with(x * y, y, frozenset({x}))) + def test_no_witness_for_a_symbol_that_cancels(self): + witness = mts._has_witness_that_s_affects_comparison + self.assertFalse(witness(x + y, y, frozenset({x}))) + self.assertFalse(witness(x * y, y, frozenset({x}))) - def test_proves_a_symbol_that_matters(self): + def test_witness_for_a_shared_symbol(self): f = sympy.Max(x, y) * y + x - self.assertTrue(mts._provably_changes_with(f, y, frozenset({x}))) + self.assertTrue( + mts._has_witness_that_s_affects_comparison(f, y, frozenset({x})) + ) + + def test_witness_for_an_enumerated_symbol(self): + # E_2 has its own copy of x, so only f(E) changes with x. + f = x * y + y + self.assertTrue( + mts._has_witness_that_s_affects_comparison(f, x, frozenset({x})) + ) + + def test_exact_value_rejects_undefined_and_irrational_values(self): + three, two = sympy.Integer(3), sympy.Integer(2) + # y equal to x puts a pole inside the Max, and sympy raises. + self.assertIsNone( + mts._exact_value(sympy.Max(2, 1 / (y - x)), {x: three, y: three}) + ) + self.assertIsNone(mts._exact_value(sympy.sqrt(x), {x: two})) + self.assertEqual(mts._exact_value(x / 3, {x: two}), sympy.Rational(2, 3)) - def test_skips_points_where_f_is_undefined(self): - # y == x puts a pole inside Max, which sympy refuses to compare + def test_undefined_values_fall_back_to_the_symbolic_check(self): f = sympy.Max(2, 1 / (y - x)) self.assertTrue(mts.affects_comparison.__wrapped__(f, y, frozenset({x}))) - def test_declines_without_positive_assumptions(self): + def test_no_search_without_positive_assumptions(self): w = sympy.Symbol("w") - self.assertFalse(mts._provably_changes_with(x * w + w**2, w, frozenset({x}))) + self.assertFalse( + mts._has_witness_that_s_affects_comparison(x * w + w**2, w, frozenset({x})) + ) if __name__ == "__main__": From 85d3548802d73da6629e8540716039d97ebb1597 Mon Sep 17 00:00:00 2001 From: Prawal Pokharel <57414987+prawalpokharel@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:20:17 -0600 Subject: [PATCH 3/3] Use positive-only symbols in the witness tests, like the mapper --- tests/test_affects_comparison.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_affects_comparison.py b/tests/test_affects_comparison.py index 1fc2592a..46dc593e 100644 --- a/tests/test_affects_comparison.py +++ b/tests/test_affects_comparison.py @@ -15,7 +15,8 @@ make_tile_shapes as mts, ) -x, y, z, n = sympy.symbols("x y z n", integer=True, positive=True) +# Positive like the mapper's symbols, with no integer=True. Ctrl-F SYMPY_BUG. +x, y, z, n = sympy.symbols("x y z n", positive=True) def symbolic_check_only(f, s, symbols_enumerated):