auto stripping of equal graph branches added

This commit is contained in:
Nicolas Kruse 2025-12-19 18:20:29 +01:00
parent 83e74a8ae9
commit 3c5f01db7f
2 changed files with 13 additions and 10 deletions

View File

@ -105,13 +105,13 @@ class value(Generic[TNum], Net):
assert dtype, 'For source type Node a dtype argument is required.'
self.dtype = dtype
elif isinstance(source, float):
self.source = CPConstant(source)
self.source = CPConstant(source, False)
self.dtype = 'float'
elif isinstance(source, bool):
self.source = CPConstant(source)
self.source = CPConstant(source, False)
self.dtype = 'bool'
else:
self.source = CPConstant(source)
self.source = CPConstant(source, False)
self.dtype = 'int'
self.volatile = volatile
@ -332,11 +332,11 @@ class value(Generic[TNum], Net):
class CPConstant(Node):
def __init__(self, value: int | float):
def __init__(self, value: int | float, constant: bool = True):
self.dtype, self.value = _get_data_and_dtype(value)
self.name = 'const_' + self.dtype
self.args = tuple()
self.node_hash = id(self)
self.node_hash = hash(value) if constant else id(self)
class Write(Node):

View File

@ -102,11 +102,14 @@ def get_all_dag_edges(nodes: Iterable[Node]) -> Generator[tuple[Node, Node], Non
Tuples of (source_node, target_node) representing edges in the DAG
"""
emitted_edges: set[tuple[Node, Node]] = set()
used_nets: set[Net] = set()
node_list: list[Node] = [n for n in nodes]
while(node_list):
node = node_list.pop()
for net in node.args:
if net not in used_nets:
used_nets.add(net)
edge = (net.source, node)
if edge not in emitted_edges:
yield edge