Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New State for Attribute Replacements #1296

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dace/frontend/python/newast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4612,7 +4612,12 @@ def visit_Attribute(self, node: ast.Attribute):
# Try to find sub-SDFG attribute
func = oprepo.Replacements.get_attribute(type(arr), node.attr)
if func is not None:
return func(self, self.sdfg, self.last_state, result)
# A new state is likely needed here, e.g., for transposition (ndarray.T)
self._add_state('%s_%d' % (type(node).__name__, node.lineno))
self.last_state.set_default_lineinfo(self.current_lineinfo)
result = func(self, self.sdfg, self.last_state, result)
self.last_state.set_default_lineinfo(None)
return result

# Otherwise, try to find compile-time attribute (such as shape)
try:
Expand Down
22 changes: 22 additions & 0 deletions tests/numpy/attribute_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ def test_attribute_in_ranged_loop_symbolic():
assert np.allclose(a, regression)


def test_attribute_new_state():

N, F_in, F_out, heads = 2, 3, 4, 5

@dace.program
def fn(a: dace.float64[N, F_in], b: dace.float64[N, heads, F_out], c: dace.float64[heads * F_out, F_in]):
tmp = a.T @ np.reshape(b, (N, heads * F_out))
c[:] = tmp.T

rng = np.random.default_rng(42)

a = rng.random((N, F_in))
b = rng.random((N, heads, F_out))
c_expected = np.zeros((heads * F_out, F_in))
c = np.zeros((heads * F_out, F_in))

fn.f(a, b, c_expected)
fn(a, b, c)
assert np.allclose(c, c_expected)


if __name__ == '__main__':
test_attribute_in_ranged_loop()
test_attribute_in_ranged_loop_symbolic()
test_attribute_new_state()