2727__all__ = [
2828 "REDUCTION_LOOP_ATTR_NAME" ,
2929 "FusionRejected" ,
30- "check_elementwise_separability" ,
3130 "check_legal_fusion_triple" ,
3231 "collect_inner_reduction_generics" ,
3332 "collect_r1_as_elementwise_inputs" ,
@@ -136,29 +135,17 @@ def find_r2_elementwise_operand(r2: ir.OpView, e: ir.OpView) -> ls.Operand:
136135def needs_elementwise_clone (e : ir .OpView , r2 : ir .OpView ) -> bool :
137136 """Whether the fusion must work on a *clone* of `e` rather than `e` itself.
138137
139- Fusing ``E`` moves it into the loop, where each tile is computed against the
140- *running* value of ``R1``'s accumulator rather than its final one. That is
141- exactly what the online correction accounts for -- but only for ``R2``'s
142- accumulator. ``E``'s own full-extent result is therefore stale in every tile
143- but the last, so no consumer outside the loop may read it. For softmax
144- ``E = exp(x - m)``, the tile written at step ``t`` is ``exp(x - m_t)`` and
145- differs from the final ``exp(x - m)`` by ``exp(m - m_t)``; a normalizing
146- divide reading it off the loop would normalize stale numerators and get row
147- sums above one.
148-
149- So whenever ``E`` has another consumer, fuse a clone and leave the original
150- in place, where it still reads ``R1``'s final result off the loop and
151- recomputes the term correctly. That covers both shapes this arises in: an
152- all-parallel consumer (softmax's normalizing divide), and another consumer
153- *reduction* over the same axis (an attention chain's row sum alongside its
154- ``P @ V`` contraction), which is a candidate ``R2`` in its own right and gets
155- fused into the same loop by applying the fusion again.
138+ True if ``E`` has any user besides ``R2``. Fusing ``E`` computes each tile
139+ against the *running* ``R1`` accumulator instead of its final result, so
140+ ``E``'s own output is stale in every tile but the last and no user outside the
141+ loop may read it. The original is left in place for those users, where it still
142+ reads ``R1``'s final result and recomputes the term correctly.
156143 """
157144 r2_op = r2 .operation
158145 return any (user != r2_op for user in op_users (e .results [0 ]))
159146
160147
161- # --- separability ------------------------------------------------------------
148+ # --- separability analysis ------------------------------------------------------------
162149
163150#: `v` is independent of every block argument (a loop-invariant scalar).
164151_CONST = 1 << 0
@@ -356,12 +343,22 @@ def check_inner_reduction_against_elementwise(
356343
357344 ``r1`` must reduce over exactly one innermost loop, and every ``r1`` input
358345 (resolved *through* its tile ``extract_slice`` to the source tensor) must also
359- appear as an ``e`` input -- *except* inputs that are results of a sibling
360- inner reduction (`inner_results`), which are themselves broadcast running
361- accumulators. The shared inputs' indexing maps must agree under a consistent
362- injective dim-mapping phi from ``r1``'s loop dims to ``e``'s loop dims, with
363- phi total over ``r1``'s loops and aligning ``r1``'s reduction dim with the
364- ``e`` dim carrying ``R2``'s reduction axis (`e_tiled_dim`).
346+ appear as an ``e`` input.
347+
348+ Fusion rewrites ``r1``'s indexing maps into ``e``'s iteration space, so the two
349+ ops' loop dims have to correspond. That correspondence ``phi`` is not given but
350+ *derived*, by unifying the maps of the inputs they share: at each position of a
351+ shared input's map, the ``r1`` dim indexing that tensor dim pairs with the ``e``
352+ dim indexing it, and the pairing must be consistent across all shared inputs.
353+ For softmax's ``max`` and ``exp(x - m)``, both read ``x`` as
354+ ``(d0, d1) -> (d0, d1)``, so ``phi = {0: 0, 1: 1}``.
355+
356+ ``phi`` must additionally be
357+
358+ * total over ``r1``'s loop dims, so every ``r1`` map -- its init map included
359+ -- can be translated, and
360+ * reduction-aligned: ``phi[r1_red_dim] == e_tiled_dim``, i.e. ``r1`` reduces
361+ the same axis that ``R2`` reduces through ``e``.
365362
366363 Note the comparison is against ``E``, not ``R2``: with the elementwise term
367364 left unfused, ``E`` is the op sharing ``R1``'s data inputs (e.g. ``x`` in
@@ -377,8 +374,8 @@ def check_inner_reduction_against_elementwise(
377374 if r1_red_dims [0 ] != ls .num_loops (r1 ) - 1 :
378375 raise FusionRejected ("reduction iterator is not the innermost loop in inner R1" )
379376
380- # Every input of R1 must also appear as an input of E, and the two ops' maps
381- # for each shared input must agree up to a consistent injective mapping phi .
377+ # Every input of R1 must also appear as an input of E. Unifying the two ops'
378+ # maps for each shared input derives phi: R1 loop dim -> E loop dim .
382379 phi : dict [int , int ] = {}
383380
384381 def try_add_mapping (r1_dim : int , e_dim : int ) -> bool :
@@ -460,6 +457,7 @@ def check_legal_fusion_triple(
460457 reduction axis, which the retiling step needs, and the tile size.
461458
462459 Raises `FusionRejected` with the reason on any violated condition.
460+ TODO: These conditions are too strict, reconsider and relax later.
463461 """
464462 if len (list (r2 .results )) != 1 or ls .num_dps_inits (r2 ) != 1 :
465463 raise FusionRejected (
0 commit comments