spcl / dace

DaCe - Data Centric Parallel Programming
http://dace.is/fast
BSD 3-Clause "New" or "Revised" License
481 stars 118 forks source link

Python front-end tasklet memlet bug #1588

Open BenWeber42 opened 1 month ago

BenWeber42 commented 1 month ago

When using the explicit dataflow mode of the Python front-end to declare a tasklet with connectors, the body of the tasklet doesn't correctly recognize the variables introduced by memlets.

This example triggers this bug:

@dace.program
def add_tasklet(A: dace.float64[1], B: dace.float64[1], R: dace.float64[1]):

    with dace.tasklet(dace.Language.Python):
        a << A[0]
        b << B[0]
        r >> R[0]
        r = a + b

The whole test is available here: https://github.com/BenWeber42/dace/blob/tasklet-connector-bug/tests/python_frontend/tasklet_connector_test.py

The generated SDFG contains a tasklet with the code r = (None + None). It seems like the memlet declarations (a << A[0] and b << B[0]) are not recognized in the tasklet body (r = a + b).

alexnick83 commented 1 month ago

This is partially a UX issue. a and b in r = a + b (line 15) are considered a and b from lines 18 and 19. This happens due to Python's scoping rules and the frontend's preprocessing actively looking for global variables. A quick fix is to either rename the variables or move the program outside the testing method (in another scope). My guess is that preprocessing is not taking into account definitions with << and >>, so a better solution would address that issue.

BenWeber42 commented 1 month ago

Ok, thanks!, your proposed work-around fixed the issue.

For my understanding, the issue is that Python's scoping rules and DaCe's scoping rules are different in this case. Particularly in a DaCe tasklet, a << A[0], b << B[0] and r >> R[0] can declare new variables (i.e., variables a, b and r), whereas in Python they cannot declare new variables. In Python, they only count as variable usages.

Preferably, preprocessing should take into account, that a << A[0], b << B[0] and r >> R[0] can in fact declare local variables. Thus, a, b and r are not part of the closure and don't become parameters of the SDFG.