Work-depth drops the add of a loop-nest accumulation; MatMul counts it

Open
#2,755 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
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
data

Research direction

Read work_depth.py, especially scope_work_depth and its MapEntry and NestedSDFG branches, alongside the existing WCR handling in the tasklet branch. Check the reported work formulas against the proposed accumulation count of volume / subset.num_elements(). Done means loop and MatMul forms count the accumulation adds consistently without double-counting tasklet WCRs.

Written by the indexing model from the issue text.

Description

Summary

A multiply-add written as a loop is cheaper, in the work formula, than the same multiply-add written as a MatMul. The missing piece is the add of the accumulation.

How it was found

On DaCe 8a4d824, after canonicalization, three ways of writing the same products:

Form Work formula Ops per multiply-add
upd += kDivM * I * star 2*NB*NBF*NQ*(NBF*ND*NQ + 1) 2, and the step has two multiplies and one add
tmp += I * star, then upd += kDivM * tmp NB*NBF*NQ*(NBF*ND + ND*NQ + 2) 1
the same products as MatMul nodes 2*M*N*K per product 2, which is what count_work_matmul documents

count_work_matmul is explicit: the 2 is one multiply and one add. The loop forms drop the add.

Work-depth adds a write-conflict resolution (the lambda a, b: a + b DaCe uses for +=) to work in one place only, the tasklet branch of scope_work_depth:

elif isinstance(node, nd.Tasklet):
    t_work, t_depth = analyze_tasklet(node, state)
    for e in state.out_edges(node):
        if e.data.wcr is not None:
            t_work += count_arithmetic_ops_code(e.data.wcr)

A += inside a map is usually a WCR on the edge that leaves the map exit, not on the tasklet. That edge is never visited here. The depth walk does see it, and uses volume / subset.num_elements() as the number of contributions, then deliberately does not charge that into a MapExit. The same work_depth.py is unchanged on extended at 6106f3257.

The formulas above were measured. The exact edge on those SDFGs was not opened again, so this is the counting site that matches the missing add.

Proposed fix

When a map's interior has been counted, add the exit WCRs. One application of lambda a, b: a + b is one operation, and volume / num_elements is how many times it runs. The depth walk already uses that ratio.

if isinstance(node, nd.MapEntry):
    exit_node = state.exit_node(node)
    for edge in state.out_edges(exit_node):
        if edge.data.wcr is None or not edge.data.volume:
            continue
        locations = pystr_to_symbolic(edge.data.subset.num_elements())
        if locations == 0:
            continue
        times = pystr_to_symbolic(edge.data.volume) / locations
        s_work += count_arithmetic_ops_code(edge.data.wcr) * times

Put this next to the recursive call that produces s_work, before work += s_work. A WCR that already sits on the tasklet is still counted in the tasklet branch, and this loop does not see that edge, so the add is not counted twice.

A WCR on an edge leaving a nested SDFG has the same hole. The same three lines, on state.out_edges(node) for a NestedSDFG, cover it.

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.