Skip to content

Commit 2af52d3

Browse files
committed
fix: key an operator by what it does, not by which object it is
Anti-unification takes a descriptor's operator to be an attribute no substitution may change, and compared the operator objects themselves. The frontend builds one per use, so two max reductions of the same shape carry two `MaxOperator` instances, and their bodies never compare equal. SeisSol's damage step is where that shows. It states one chunk of 362 operations four times and every chunk holds two such reductions, so the run covering 81 % of the kernel was not even a candidate: the list rolled into 22 loops of period 1 and 77, out of 1787 operations. Keyed by class and state it rolls into 7, the largest of period 362 and count 4, and the families found cover 93 % of the list instead of 72 %. An operator that does carry state keeps it in the key, and one whose state cannot be hashed keeps its identity -- calling two bodies the same when they are not is worse than missing a run. An `Operation` member, which is what an elementwise descriptor carries, is already its own key. AI-generated. Model: Opus 5
1 parent 16e3bf3 commit 2af52d3

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎src/tensorforge/analysis/antiunify.py‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from typing import Dict, List, Optional, Sequence, Tuple, Union
4343

4444
from tensorforge.common.matrix.boundingbox import BoundingBox
45+
from tensorforge.common.operation import Operator
4546
from tensorforge.generators.descriptions import (BarrierDescription,
4647
ElementwiseDescr,
4748
MultilinearDescr,
@@ -166,6 +167,33 @@ def _tuple2(rows) -> Tuple:
166167
return tuple(tuple(r) for r in rows) if rows is not None else None
167168

168169

170+
def _op_key(op):
171+
"""An operator keyed by what it does, not by which instance it is.
172+
173+
The operators are value objects and the frontend builds one per use, so
174+
two max reductions of the same shape carry two `MaxOperator` objects.
175+
Keyed by identity those are two different bodies, and that is enough to
176+
hide a repetition entirely: SeisSol's damage step states one chunk of 362
177+
operations four times, every chunk holds two such reductions, and the run
178+
covering 81 % of the kernel was not even a candidate.
179+
180+
An operator that does carry state keeps it in the key, and one whose
181+
state cannot be hashed keeps its identity -- calling two bodies the same
182+
when they are not is worse than missing a run. Anything that is not an
183+
operator, an `Operation` member in particular, is already its own key.
184+
"""
185+
if not isinstance(op, Operator):
186+
return op
187+
state = tuple(sorted((name, _op_key(value)) for name, value
188+
in getattr(op, '__dict__', {}).items()))
189+
key = (type(op).__qualname__, state)
190+
try:
191+
hash(key)
192+
except TypeError:
193+
return op
194+
return key
195+
196+
169197
def _attrs(descr) -> Tuple:
170198
"""The part of a descriptor that no substitution may change.
171199
@@ -184,11 +212,11 @@ def _attrs(descr) -> Tuple:
184212
bool(getattr(descr, 'prefer_align', False)))
185213
if isinstance(descr, ElementwiseDescr):
186214
scalars = tuple(descr.scalar_srcs())
187-
return ('elementwise', descr.op, scalars,
215+
return ('elementwise', _op_key(descr.op), scalars,
188216
bool(getattr(descr, 'strict_match', False)),
189217
bool(getattr(descr, 'prefer_align', False)))
190218
if isinstance(descr, ReductionDescr):
191-
return ('reduction', descr.op, tuple(descr.dims),
219+
return ('reduction', _op_key(descr.op), tuple(descr.dims),
192220
bool(getattr(descr, 'prefer_align', False)))
193221
if isinstance(descr, BarrierDescription):
194222
return ('barrier', bool(descr.trueBarrier()))

‎tests/test_families.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
from tensorforge.common.basic_types import Addressing, Datatype
1616
from tensorforge.common.matrix.boundingbox import BoundingBox
1717
from tensorforge.common.matrix.tensor import SubTensor, Tensor
18+
from tensorforge.common.operation import MaxOperator
1819
from tensorforge.generators.descriptions import (GemmDescr, GridBarrierDescr,
19-
GridFenceDescr)
20+
GridFenceDescr,
21+
ReductionDescr)
2022

2123
DTYPE = Datatype.F32
2224

@@ -45,6 +47,13 @@ def face(i):
4547
tensor('Q', [56, 9]))]
4648

4749

50+
def reduce_max(k):
51+
"""``bound{k} = max(deriv{k+1})`` -- with this step's own operator."""
52+
return [ReductionDescr(tensor(f'bound{k}', [1]),
53+
tensor(f'deriv{k + 1}', [56, 9]), [0, 1],
54+
MaxOperator())]
55+
56+
4857
def flat(chunks):
4958
return [d for chunk in chunks for d in chunk]
5059

@@ -117,6 +126,21 @@ def test_equal_shapes_over_unrelated_tensors_are_one_run():
117126
assert [(r.start, r.count) for r in bounded] == [(1, 3)]
118127

119128

129+
def test_an_operator_built_per_step_does_not_break_the_run():
130+
"""Every step carries its own `MaxOperator`, and they are one operator.
131+
132+
The frontend builds one operator object per use, so a run that reduces
133+
once per step holds a different instance in every chunk while nothing
134+
about what the body computes differs. Compared by identity there is no
135+
run at all -- which is how the chunk of 362 operations that SeisSol's
136+
damage step states four times stayed written out in full.
137+
"""
138+
descrs = flat([step(k) + reduce_max(k) for k in range(4)])
139+
runs = find_repeats(descrs)
140+
assert len(runs) == 1
141+
assert (runs[0].start, runs[0].period, runs[0].count) == (0, 2, 4)
142+
143+
120144
def test_nothing_repeats():
121145
descrs = [gemm(tensor('A', [56, 56]), tensor('I', [56, 9]),
122146
tensor('Q', [56, 9])),

0 commit comments

Comments
 (0)