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

Fixes replacement of dict keys #1845

Merged
merged 3 commits into from
Jan 7, 2025
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
9 changes: 9 additions & 0 deletions dace/sdfg/sdfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def _nested_arrays_from_json(obj, context=None):


def _replace_dict_keys(d, old, new):
if old == new:
warnings.warn(f"Trying to replace key with the same name {old} ... skipping.")
return
if old in d:
if new in d:
warnings.warn('"%s" already exists in SDFG' % new)
Expand Down Expand Up @@ -734,6 +737,12 @@ def replace_dict(self,
:param replace_in_graph: Whether to replace in SDFG nodes / edges.
:param replace_keys: If True, replaces in SDFG property names (e.g., array, symbol, and constant names).
"""

repldict = {k: v for k, v in repldict.items() if k != v}
if symrepl:
symrepl = {k: v for k, v in symrepl.items() if str(k) != str(v)}


symrepl = symrepl or {
symbolic.pystr_to_symbolic(k): symbolic.pystr_to_symbolic(v) if isinstance(k, str) else v
for k, v in repldict.items()
Expand Down
36 changes: 36 additions & 0 deletions tests/sdfg/interstate_assignment_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019-2025 ETH Zurich and the DaCe authors. All rights reserved.

import dace
import numpy as np


def test_key_replacement_same_name():

sdfg = dace.SDFG('key_replacement_same_name')
sdfg.add_array('inp', [1], dace.int32)
sdfg.add_array('out', [1], dace.int32)

first = sdfg.add_state('first_state')
second = sdfg.add_state('second_state')
edge = sdfg.add_edge(first, second, dace.InterstateEdge(assignments={'s': 'inp[0]'}))

task = second.add_tasklet('t', {}, {'__out'}, '__out = s')
access = second.add_access('out')
second.add_edge(task, '__out', access, None, dace.Memlet('out[0]'))

sdfg.replace('s', 's')
assert 's' in edge.data.assignments
sdfg.replace_dict({'s': 's'})
assert 's' in edge.data.assignments

rng = np.random.default_rng()
inp = rng.integers(1, 100, 1)
inp = np.array(inp, dtype=np.int32)
out = np.zeros([1], dtype=np.int32)

sdfg(inp=inp, out=out)
assert out[0] == inp[0]


if __name__ == '__main__':
test_key_replacement_same_name()
Loading