Skip to content

Commit 6b36e47

Browse files
committed
Add handle= to all GraphNode subclass __repr__ for debugging
Every subclass repr now starts with handle=0x... (the CUgraphNode pointer) followed by type-specific identity/parameter data. Dynamic queries (pred counts, subnode counts) are removed in favor of deterministic, cheap fields. This makes set comparison failures in test output readable when debugging graph mutation tests. Made-with: Cursor
1 parent 64d6c2d commit 6b36e47

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

cuda_core/cuda/core/_graph/_graph_def/_subclasses.pyx

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ cdef class EmptyNode(GraphNode):
5858
return n
5959

6060
def __repr__(self) -> str:
61-
cdef Py_ssize_t n = len(self.pred)
62-
return f"<EmptyNode with {n} {'pred' if n == 1 else 'preds'}>"
61+
return f"<EmptyNode handle=0x{as_intptr(self._h_node):x}>"
6362

6463

6564
cdef class KernelNode(GraphNode):
@@ -108,7 +107,8 @@ cdef class KernelNode(GraphNode):
108107
h_kernel)
109108

110109
def __repr__(self) -> str:
111-
return (f"<KernelNode grid={self._grid} block={self._block}>")
110+
return (f"<KernelNode handle=0x{as_intptr(self._h_node):x}"
111+
f" kernel=0x{as_intptr(self._h_kernel):x}>")
112112

113113
@property
114114
def grid(self) -> tuple:
@@ -207,7 +207,8 @@ cdef class AllocNode(GraphNode):
207207
<int>params.poolProps.location.id, memory_type, tuple(peer_ids))
208208

209209
def __repr__(self) -> str:
210-
return f"<AllocNode dptr=0x{self._dptr:x} size={self._bytesize}>"
210+
return (f"<AllocNode handle=0x{as_intptr(self._h_node):x}"
211+
f" dptr=0x{self._dptr:x} size={self._bytesize}>")
211212

212213
@property
213214
def dptr(self) -> int:
@@ -273,7 +274,7 @@ cdef class FreeNode(GraphNode):
273274
return FreeNode._create_with_params(h_node, dptr)
274275

275276
def __repr__(self) -> str:
276-
return f"<FreeNode dptr=0x{self._dptr:x}>"
277+
return f"<FreeNode handle=0x{as_intptr(self._h_node):x} dptr=0x{self._dptr:x}>"
277278

278279
@property
279280
def dptr(self) -> int:
@@ -328,8 +329,8 @@ cdef class MemsetNode(GraphNode):
328329
params.elementSize, params.width, params.height, params.pitch)
329330

330331
def __repr__(self) -> str:
331-
return (f"<MemsetNode dptr=0x{self._dptr:x} "
332-
f"value={self._value} elem={self._element_size}>")
332+
return (f"<MemsetNode handle=0x{as_intptr(self._h_node):x}"
333+
f" dptr=0x{self._dptr:x} value={self._value}>")
333334

334335
@property
335336
def dptr(self) -> int:
@@ -416,8 +417,8 @@ cdef class MemcpyNode(GraphNode):
416417
def __repr__(self) -> str:
417418
cdef str dt = "H" if self._dst_type == cydriver.CU_MEMORYTYPE_HOST else "D"
418419
cdef str st = "H" if self._src_type == cydriver.CU_MEMORYTYPE_HOST else "D"
419-
return (f"<MemcpyNode dst=0x{self._dst:x}({dt}) "
420-
f"src=0x{self._src:x}({st}) size={self._size}>")
420+
return (f"<MemcpyNode handle=0x{as_intptr(self._h_node):x}"
421+
f" dst=0x{self._dst:x}({dt}) src=0x{self._src:x}({st}) size={self._size}>")
421422

422423
@property
423424
def dst(self) -> int:
@@ -465,12 +466,8 @@ cdef class ChildGraphNode(GraphNode):
465466
return ChildGraphNode._create_with_params(h_node, h_child)
466467

467468
def __repr__(self) -> str:
468-
cdef cydriver.CUgraph g = as_cu(self._h_child_graph)
469-
cdef size_t num_nodes = 0
470-
with nogil:
471-
HANDLE_RETURN(cydriver.cuGraphGetNodes(g, NULL, &num_nodes))
472-
cdef Py_ssize_t n = <Py_ssize_t>num_nodes
473-
return f"<ChildGraphNode with {n} {'subnode' if n == 1 else 'subnodes'}>"
469+
return (f"<ChildGraphNode handle=0x{as_intptr(self._h_node):x}"
470+
f" child=0x{as_intptr(self._h_child_graph):x}>")
474471

475472
@property
476473
def child_graph(self) -> "GraphDef":
@@ -507,7 +504,8 @@ cdef class EventRecordNode(GraphNode):
507504
return EventRecordNode._create_with_params(h_node, h_event)
508505

509506
def __repr__(self) -> str:
510-
return f"<EventRecordNode event=0x{as_intptr(self._h_event):x}>"
507+
return (f"<EventRecordNode handle=0x{as_intptr(self._h_node):x}"
508+
f" event=0x{as_intptr(self._h_event):x}>")
511509

512510
@property
513511
def event(self) -> Event:
@@ -544,7 +542,8 @@ cdef class EventWaitNode(GraphNode):
544542
return EventWaitNode._create_with_params(h_node, h_event)
545543

546544
def __repr__(self) -> str:
547-
return f"<EventWaitNode event=0x{as_intptr(self._h_event):x}>"
545+
return (f"<EventWaitNode handle=0x{as_intptr(self._h_node):x}"
546+
f" event=0x{as_intptr(self._h_event):x}>")
548547

549548
@property
550549
def event(self) -> Event:
@@ -591,8 +590,10 @@ cdef class HostCallbackNode(GraphNode):
591590
def __repr__(self) -> str:
592591
if self._callable is not None:
593592
name = getattr(self._callable, '__name__', '?')
594-
return f"<HostCallbackNode callback={name}>"
595-
return f"<HostCallbackNode cfunc=0x{<uintptr_t>self._fn:x}>"
593+
return (f"<HostCallbackNode handle=0x{as_intptr(self._h_node):x}"
594+
f" callback={name}>")
595+
return (f"<HostCallbackNode handle=0x{as_intptr(self._h_node):x}"
596+
f" cfunc=0x{<uintptr_t>self._fn:x}>")
596597

597598
@property
598599
def callback_fn(self):
@@ -672,7 +673,7 @@ cdef class ConditionalNode(GraphNode):
672673
return n
673674

674675
def __repr__(self) -> str:
675-
return "<ConditionalNode>"
676+
return f"<ConditionalNode handle=0x{as_intptr(self._h_node):x}>"
676677

677678
@property
678679
def condition(self) -> Condition | None:
@@ -709,7 +710,8 @@ cdef class IfNode(ConditionalNode):
709710
"""An if-conditional node (1 branch, executes when condition is non-zero)."""
710711

711712
def __repr__(self) -> str:
712-
return f"<IfNode condition=0x{<unsigned long long>self._condition._c_handle:x}>"
713+
return (f"<IfNode handle=0x{as_intptr(self._h_node):x}"
714+
f" condition=0x{<unsigned long long>self._condition._c_handle:x}>")
713715

714716
@property
715717
def then(self) -> "GraphDef":
@@ -721,7 +723,8 @@ cdef class IfElseNode(ConditionalNode):
721723
"""An if-else conditional node (2 branches)."""
722724

723725
def __repr__(self) -> str:
724-
return f"<IfElseNode condition=0x{<unsigned long long>self._condition._c_handle:x}>"
726+
return (f"<IfElseNode handle=0x{as_intptr(self._h_node):x}"
727+
f" condition=0x{<unsigned long long>self._condition._c_handle:x}>")
725728

726729
@property
727730
def then(self) -> "GraphDef":
@@ -738,7 +741,8 @@ cdef class WhileNode(ConditionalNode):
738741
"""A while-loop conditional node (1 branch, repeats while condition is non-zero)."""
739742

740743
def __repr__(self) -> str:
741-
return f"<WhileNode condition=0x{<unsigned long long>self._condition._c_handle:x}>"
744+
return (f"<WhileNode handle=0x{as_intptr(self._h_node):x}"
745+
f" condition=0x{<unsigned long long>self._condition._c_handle:x}>")
742746

743747
@property
744748
def body(self) -> "GraphDef":
@@ -750,6 +754,5 @@ cdef class SwitchNode(ConditionalNode):
750754
"""A switch conditional node (N branches, selected by condition value)."""
751755

752756
def __repr__(self) -> str:
753-
cdef Py_ssize_t n = len(self._branches)
754-
return (f"<SwitchNode condition=0x{<unsigned long long>self._condition._c_handle:x}"
755-
f" with {n} {'branch' if n == 1 else 'branches'}>")
757+
return (f"<SwitchNode handle=0x{as_intptr(self._h_node):x}"
758+
f" condition=0x{<unsigned long long>self._condition._c_handle:x}>")

cuda_core/tests/test_object_protocols.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -685,20 +685,20 @@ def sample_switch_node_alt(sample_graphdef):
685685
("sample_graphdef", r"<GraphDef handle=0x[0-9a-f]+>"),
686686
("sample_condition", r"<Condition handle=0x[0-9a-f]+>"),
687687
("sample_root_node", r"<GraphNode entry>"),
688-
("sample_empty_node", r"<EmptyNode with \d+ preds?>"),
689-
("sample_alloc_node", r"<AllocNode dptr=0x[0-9a-f]+ size=\d+>"),
690-
("sample_kernel_node", r"<KernelNode grid=\(\d+, \d+, \d+\) block=\(\d+, \d+, \d+\)>"),
691-
("sample_free_node", r"<FreeNode dptr=0x[0-9a-f]+>"),
692-
("sample_memset_node", r"<MemsetNode dptr=0x[0-9a-f]+ value=\d+ elem=\d+>"),
693-
("sample_memcpy_node", r"<MemcpyNode dst=0x[0-9a-f]+\([DH]\) src=0x[0-9a-f]+\([DH]\) size=\d+>"),
694-
("sample_child_graph_node", r"<ChildGraphNode with \d+ subnodes?>"),
695-
("sample_event_record_node", r"<EventRecordNode event=0x[0-9a-f]+>"),
696-
("sample_event_wait_node", r"<EventWaitNode event=0x[0-9a-f]+>"),
697-
("sample_host_callback_node", r"<HostCallbackNode callback=\w+>"),
698-
("sample_if_node", r"<IfNode condition=0x[0-9a-f]+>"),
699-
("sample_if_else_node", r"<IfElseNode condition=0x[0-9a-f]+>"),
700-
("sample_while_node", r"<WhileNode condition=0x[0-9a-f]+>"),
701-
("sample_switch_node", r"<SwitchNode condition=0x[0-9a-f]+ with \d+ branches?>"),
688+
("sample_empty_node", r"<EmptyNode handle=0x[0-9a-f]+>"),
689+
("sample_alloc_node", r"<AllocNode handle=0x[0-9a-f]+ dptr=0x[0-9a-f]+ size=\d+>"),
690+
("sample_kernel_node", r"<KernelNode handle=0x[0-9a-f]+ kernel=0x[0-9a-f]+>"),
691+
("sample_free_node", r"<FreeNode handle=0x[0-9a-f]+ dptr=0x[0-9a-f]+>"),
692+
("sample_memset_node", r"<MemsetNode handle=0x[0-9a-f]+ dptr=0x[0-9a-f]+ value=\d+>"),
693+
("sample_memcpy_node", r"<MemcpyNode handle=0x[0-9a-f]+ dst=0x[0-9a-f]+\([DH]\) src=0x[0-9a-f]+\([DH]\) size=\d+>"),
694+
("sample_child_graph_node", r"<ChildGraphNode handle=0x[0-9a-f]+ child=0x[0-9a-f]+>"),
695+
("sample_event_record_node", r"<EventRecordNode handle=0x[0-9a-f]+ event=0x[0-9a-f]+>"),
696+
("sample_event_wait_node", r"<EventWaitNode handle=0x[0-9a-f]+ event=0x[0-9a-f]+>"),
697+
("sample_host_callback_node", r"<HostCallbackNode handle=0x[0-9a-f]+ callback=\w+>"),
698+
("sample_if_node", r"<IfNode handle=0x[0-9a-f]+ condition=0x[0-9a-f]+>"),
699+
("sample_if_else_node", r"<IfElseNode handle=0x[0-9a-f]+ condition=0x[0-9a-f]+>"),
700+
("sample_while_node", r"<WhileNode handle=0x[0-9a-f]+ condition=0x[0-9a-f]+>"),
701+
("sample_switch_node", r"<SwitchNode handle=0x[0-9a-f]+ condition=0x[0-9a-f]+>"),
702702
]
703703

704704

0 commit comments

Comments
 (0)