MapFusionVertical refuses to fuse `map i { for j }` producer and consumer when the producer also reads the intermediate (row recurrence), even with permissive=True

Open
#2,725 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

Start in dace/transformation/dataflow/map_fusion_vertical.py, especially MapFusionVertical.can_be_applied, and trace the checks for nested SDFG access patterns and producer self-dependencies. Use the provided rows reproducer to identify which check rejects fusion with permissive=True. Done means the two maps fuse for the row recurrence while retaining p if removing the intermediate is not valid; verify the transformed SDFG against the stated expected behavior.

Written by the indexing model from the issue text.

Description

Version: extended at 801cafd17 (dace 2.0.0a10); map_fusion_vertical.py unchanged up to 017f54b9d.

Summary. Two maps over rows i, each with a sequential loop over j inside (a nested SDFG). The first computes a recurrence along the row on a transient p; the second reads p along the same row. Iteration i of each map reads and writes only row i, so fusing them into map i { for j (p); for j (q) } is legal and keeps each row in cache. The pattern matcher finds the pair, but MapFusionVertical never applies, also with permissive=True.

import numpy as np
import dace
from dace.transformation.dataflow.map_fusion_vertical import MapFusionVertical

N = dace.symbol("N")

@dace.program
def rows(p0: dace.float64[N], q: dace.float64[N, N]):
    p = np.empty((N, N), dtype=np.float64)
    for i in dace.map[0:N]:
        p[i, 0] = p0[i]
        for j in range(1, N):
            p[i, j] = 0.5 * p[i, j - 1] + 1.0
    for i in dace.map[0:N]:
        for j in range(1, N):
            q[i, j] = q[i, j - 1] + p[i, j - 1]

sdfg = rows.to_sdfg(simplify=True)
print(sdfg.apply_transformations_repeated(MapFusionVertical))                   # 0
print(sdfg.apply_transformations_repeated(MapFusionVertical, permissive=True))  # 0

What the transformation sees (one state, both map bodies are nested SDFGs):

Edge Subset of p
p (read) -> first map 0:N, 0:N - 1 (the recurrence reads p[i, j - 1])
first map -> p (write) 0:N, 0:N
p -> second map 0:N, 0:N - 1

Reading p[i, j] instead of p[i, j - 1] in the second map (subset 0:N, 1:N) is refused the same way, so it is not the offset between write and read.

Expected. The two maps fuse: per iteration i the producer writes p[i, 0:N] and the consumer reads p[i, 0:N - 1] (or p[i, 1:N]), both within the row the same iteration produced. If removing the intermediate is not possible (it is read by the producer itself), keeping p in memory and only merging the two map scopes would still be valid.

Likely cause (not confirmed). The producer map reads the intermediate it writes (a read-after-write chain through p inside the first map), which the vertical-fusion checks treat as a dependency between the two maps; or the nested-SDFG bodies hide the per-iteration access, so the producer's write is seen as the whole array. permissive=True does not lift it, so the refusal is in a structural check.

Impact. On Polybench ADI's column sweep (GPU, N = 1600, 25 steps) the program with these maps fused by hand runs in 0.160 s, against 1.180 s for the best schedule without the fusion.

Where it matters. Any alternating-direction / line-sweep code after a loop interchange (Polybench ADI: the p, q, u column sweeps after MoveLoopIntoMap are exactly this shape). Found through NestForge, which applies DaCe MapFusionVertical as its map-fusion move.

Not yet checked. Which condition in MapFusionVertical.can_be_applied returns False (the cause above is a guess), and a hand-fused SDFG validated against NumPy for this exact reproducer. For the full ADI column sweep, a source written with the three row loops in one map gives the same results as the NumPy oracle (rtol 1e-8) on CPU and GPU.

🤖 Generated with Claude Code

Dominant language
Python
Stars
602
Forks
171
Avg merge
1d 16h
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from spcl/dace

All issues in spcl/dace

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.