Skip to content

Commit

Permalink
Add UUIDs to SDFG elements (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
phschaad authored Aug 28, 2024
1 parent 9b18c83 commit d3a1c57
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
8 changes: 8 additions & 0 deletions dace/memlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import warnings

import dace
from dace.sdfg.graph import generate_element_id
import dace.serialize
from dace import subsets, dtypes, symbolic
from dace.frontend.operations import detect_reduction_type
Expand Down Expand Up @@ -54,6 +55,8 @@ class Memlet(object):
'(non-atomic) writes in resulting code')
allow_oob = Property(dtype=bool, default=False, desc='Bypass out-of-bounds validation')

guid = Property(dtype=str, allow_none=False)

def __init__(self,
expr: Optional[str] = None,
data: Optional[str] = None,
Expand Down Expand Up @@ -137,6 +140,9 @@ def __init__(self,
self.debuginfo = debuginfo
self.allow_oob = allow_oob

self.guid = generate_element_id(self)


@staticmethod
def from_memlet(memlet: 'Memlet') -> 'Memlet':
sbs = subsets.Range(memlet.subset.ndrange()) if memlet.subset is not None else None
Expand Down Expand Up @@ -207,6 +213,8 @@ def __deepcopy__(self, memo):
node._allow_oob = self._allow_oob
node._is_data_src = self._is_data_src

node._guid = generate_element_id(node)

# Nullify graph references
node._sdfg = None
node._state = None
Expand Down
5 changes: 5 additions & 0 deletions dace/sdfg/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections import deque, OrderedDict
import itertools
import uuid
import networkx as nx
from dace.dtypes import deduplicate
import dace.serialize
Expand Down Expand Up @@ -825,3 +826,7 @@ def edges_between(self, source: NodeT, destination: NodeT) -> List[MultiConnecto

def is_multigraph(self) -> bool:
return True


def generate_element_id(element) -> str:
return str(uuid.uuid4())
9 changes: 9 additions & 0 deletions dace/sdfg/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Node(object):
out_connectors = DictProperty(key_type=str,
value_type=dtypes.typeclass,
desc="A set of output connectors for this node.")
guid = Property(dtype=str, allow_none=False)

def __init__(self, in_connectors=None, out_connectors=None):
# Convert connectors to typed connectors with autodetect type
Expand All @@ -46,6 +47,8 @@ def __init__(self, in_connectors=None, out_connectors=None):
self.in_connectors = in_connectors or {}
self.out_connectors = out_connectors or {}

self.guid = graph.generate_element_id(self)

def __str__(self):
if hasattr(self, 'label'):
return self.label
Expand Down Expand Up @@ -253,6 +256,9 @@ def __deepcopy__(self, memo):
node._in_connectors = dcpy(self._in_connectors, memo=memo)
node._out_connectors = dcpy(self._out_connectors, memo=memo)
node._debuginfo = dcpy(self._debuginfo, memo=memo)

node._guid = graph.generate_element_id(node)

return node

@property
Expand Down Expand Up @@ -574,6 +580,9 @@ def __deepcopy__(self, memo):
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
# Skip GUID.
if k in ('guid',):
continue
setattr(result, k, dcpy(v, memo))
if result._sdfg is not None:
result._sdfg.parent_nsdfg_node = result
Expand Down
10 changes: 7 additions & 3 deletions dace/sdfg/sdfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import warnings

import dace
from dace.sdfg.graph import generate_element_id
import dace.serialize
from dace import (data as dt, hooks, memlet as mm, subsets as sbs, dtypes, symbolic)
from dace.sdfg.replace import replace_properties_dict
Expand Down Expand Up @@ -173,6 +174,7 @@ class InterstateEdge(object):
assignments = Property(dtype=dict,
desc="Assignments to perform upon transition (e.g., 'x=x+1; y = 0')")
condition = CodeProperty(desc="Transition condition", default=CodeBlock("1"))
guid = Property(dtype=str, allow_none=False)

def __init__(self,
condition: Optional[Union[CodeBlock, str, ast.AST, list]] = None,
Expand All @@ -195,6 +197,8 @@ def __init__(self,
self._cond_sympy = None
self._uncond = None

self.guid = generate_element_id(self)

def __setattr__(self, name: str, value: Any) -> None:
if name == 'condition' or name == '_condition':
super().__setattr__('_cond_sympy', None)
Expand Down Expand Up @@ -512,9 +516,9 @@ def __deepcopy__(self, memo):
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
# Skip derivative attributes
# Skip derivative attributes and GUID
if k in ('_cached_start_block', '_edges', '_nodes', '_parent', '_parent_sdfg', '_parent_nsdfg_node',
'_cfg_list', '_transformation_hist'):
'_cfg_list', '_transformation_hist', 'guid'):
continue
setattr(result, k, copy.deepcopy(v, memo))
# Copy edges and nodes
Expand Down Expand Up @@ -638,7 +642,7 @@ def keyword_remover(json_obj: Any, last_keyword=""):
for key, value in json_obj.items():
if (isinstance(key, str)
and (key.startswith('_meta_')
or key in ['name', 'hash', 'orig_sdfg', 'transformation_hist', 'instrument'])):
or key in ['name', 'hash', 'orig_sdfg', 'transformation_hist', 'instrument', 'guid'])):
keys_to_delete.append(key)
else:
kv_to_recurse.append((key, value))
Expand Down
9 changes: 7 additions & 2 deletions dace/sdfg/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from dace.properties import (CodeBlock, DebugInfoProperty, DictProperty, EnumProperty, Property, SubsetProperty, SymbolicProperty,
CodeProperty, make_properties)
from dace.sdfg import nodes as nd
from dace.sdfg.graph import MultiConnectorEdge, OrderedMultiDiConnectorGraph, SubgraphView, OrderedDiGraph, Edge
from dace.sdfg.graph import (MultiConnectorEdge, OrderedMultiDiConnectorGraph, SubgraphView, OrderedDiGraph, Edge,
generate_element_id)
from dace.sdfg.propagation import propagate_memlet
from dace.sdfg.validation import validate_state
from dace.subsets import Range, Subset
Expand Down Expand Up @@ -1099,6 +1100,8 @@ def replace_dict(self,
@make_properties
class ControlFlowBlock(BlockGraphView, abc.ABC):

guid = Property(dtype=str, allow_none=False)

is_collapsed = Property(dtype=bool, desc='Show this block as collapsed', default=False)

pre_conditions = DictProperty(key_type=str, value_type=list, desc='Pre-conditions for this block')
Expand All @@ -1122,6 +1125,8 @@ def __init__(self, label: str = '', sdfg: Optional['SDFG'] = None, parent: Optio
self.post_conditions = {}
self.invariant_conditions = {}

self.guid = generate_element_id(self)

def nodes(self):
return []

Expand Down Expand Up @@ -1169,7 +1174,7 @@ def __deepcopy__(self, memo):
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
if k in ('_parent_graph', '_sdfg'): # Skip derivative attributes
if k in ('_parent_graph', '_sdfg', 'guid'): # Skip derivative attributes and GUID
continue
setattr(result, k, copy.deepcopy(v, memo))

Expand Down

0 comments on commit d3a1c57

Please sign in to comment.