Skip to content

Commit 9b5d126

Browse files
Andy-Jostclaude
andcommitted
cuda.core: key child graphs by address and owner node
A child graph is identified within its parent by its CUgraph and the node that owns it. Keying the per-parent registry by that pair makes the owner check part of the lookup. A box whose graph CUDA destroyed before cuda.core retired it keeps a key distinct from a newer child that reuses its address under another owner, so retiring the stale box cannot erase the newer entry. The same-parent registry test is restored; it passes under the pair key. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 974a7ad commit 9b5d126

4 files changed

Lines changed: 67 additions & 19 deletions

File tree

‎cuda_core/cuda/core/_cpp/rt/GRAPH_ATTACHMENTS.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ That invalidates existing handles and preserves the storage until the
174174
hierarchy's last shared owner is released.
175175

176176
Each `GraphBox` holds two registries: its direct child graphs keyed by
177-
`CUgraph` and its nodes keyed by `CUgraphNode`. Both are scoped to the box,
178-
so a raw handle recycled by CUDA in another hierarchy cannot resolve to this
179-
hierarchy's metadata; see
177+
`CUgraph` and owner node, and its nodes keyed by `CUgraphNode`. Both are
178+
scoped to the box, so a raw handle recycled by CUDA in another hierarchy
179+
cannot resolve to this hierarchy's metadata; see
180180
[registry identity](REGISTRY_DESIGN.md#reused-graph-handles). Node handles use
181181
separate `GraphNodeBox::resource` fields and are nulled before the graph box
182182
is retired.

‎cuda_core/cuda/core/_cpp/rt/REGISTRY_DESIGN.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ therefore resolve a recycled address to another hierarchy's metadata.
4343

4444
Scoping the child registry to its parent `GraphBox` removes that case by
4545
construction: `create_child_graph_handle` always knows the parent, and roots
46-
are never looked up. Within one hierarchy, external synchronization and the
47-
retire-before-register order in `graph_commit_child_graph_update` keep the
48-
entry for an address on its live box, as long as every child graph that CUDA
49-
destroys is retired before that address is registered again. The one path
50-
that does not retire is a conditional-body capture that ends invalidated; the
51-
parent graph is already invalid there (issue #2918), and the registry does
52-
not guard it.
46+
are never looked up. Within the parent, an entry is keyed by the child's
47+
`CUgraph` and its owner node, which together identify a child graph. If CUDA
48+
destroys a child before cuda.core retires its box, the owner node is still
49+
alive, so a newer child at the recycled address has a different owner and its
50+
own entry. Retiring the stale box erases only the stale entry. Within one
51+
owner, external synchronization and the retire-before-register order in
52+
`graph_commit_child_graph_update` keep one live entry per key.
5353

5454
## Level 2: Resource Handle -> Python Object (Cython)
5555

‎cuda_core/cuda/core/_cpp/rt/graph.cpp‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <array>
1212
#include <cstddef>
1313
#include <cstdlib>
14+
#include <functional>
1415
#include <list>
1516
#include <map>
1617
#include <memory>
@@ -69,6 +70,29 @@ using GraphAttachmentMap = std::map<CUgraphNode, NodeAttachment *>;
6970

7071
struct GraphHierarchy;
7172

73+
// A child graph is identified by its address and its owner node in the parent.
74+
// A box that CUDA destroyed before cuda.core retired it keeps a key distinct
75+
// from a newer child that reuses its address under another owner node.
76+
struct ChildGraphKey {
77+
CUgraph graph;
78+
CUgraphNode owner_node;
79+
80+
bool
81+
operator==(const ChildGraphKey &other) const noexcept
82+
{
83+
return graph == other.graph && owner_node == other.owner_node;
84+
}
85+
};
86+
87+
struct ChildGraphKeyHash {
88+
std::size_t
89+
operator()(const ChildGraphKey &key) const noexcept
90+
{
91+
std::size_t h = std::hash<const void *>{}(key.graph);
92+
return h ^ (std::hash<const void *>{}(key.owner_node) + 0x9e3779b9 + (h << 6) + (h >> 2));
93+
}
94+
};
95+
7296
// Standard-layout alias target for GraphHandle.
7397
struct GraphBoxBase {
7498
CUgraph resource = nullptr;
@@ -83,7 +107,7 @@ struct GraphBox : GraphBoxBase {
83107
GraphAttachmentMap attachments; // Non-owning attachment index.
84108
HandleRegistry<CUgraphNode, GraphNodeHandle> node_handles;
85109
// See REGISTRY_DESIGN.md (Level 1: Driver Handle -> Resource Handle)
86-
HandleRegistry<CUgraph, GraphHandle> children; // Canonical handles of the direct child graphs.
110+
HandleRegistry<ChildGraphKey, GraphHandle, ChildGraphKeyHash> children; // Direct child graphs.
87111

88112
GraphBox(
89113
CUgraph resource_, GraphHierarchy *hierarchy_, GraphBox *parent_ = nullptr, CUgraphNode owner_node_ = nullptr
@@ -149,7 +173,7 @@ register_child_handles(const std::vector<GraphHandle> &handles)
149173
{
150174
for (const GraphHandle &h : handles) {
151175
GraphBox *box = get_box(h);
152-
box->parent->children.register_handle(box->resource, h);
176+
box->parent->children.register_handle({box->resource, box->owner_node}, h);
153177
}
154178
}
155179

@@ -334,17 +358,15 @@ create_child_graph_handle(CUgraph child_graph, const GraphHandle &h_parent, CUgr
334358
return {};
335359
}
336360
GraphBox *parent = get_box(h_parent);
337-
if (GraphHandle h_graph = parent->children.lookup(child_graph)) {
338-
if (get_box(h_graph)->owner_node == owner_node) {
339-
return h_graph;
340-
}
361+
if (GraphHandle h_graph = parent->children.lookup({child_graph, owner_node})) {
362+
return h_graph;
341363
}
342364

343365
GraphHierarchy *hierarchy = parent->hierarchy;
344366
GraphBox &child = hierarchy->graphs.emplace_back(child_graph, hierarchy, parent, owner_node);
345367

346368
GraphHandle h_child(h_parent, &child.resource);
347-
parent->children.register_handle(child_graph, h_child);
369+
parent->children.register_handle({child_graph, owner_node}, h_child);
348370
return h_child;
349371
}
350372

@@ -665,7 +687,7 @@ retire_graph(GraphHierarchy &hierarchy, std::list<GraphBox>::iterator graph) noe
665687
}
666688
if (graph->resource) {
667689
if (graph->parent) {
668-
graph->parent->children.unregister_handle(graph->resource);
690+
graph->parent->children.unregister_handle({graph->resource, graph->owner_node});
669691
}
670692
graph->resource = nullptr;
671693
}

‎cuda_core/tests/cython/test_graph_registry.pyx‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# distutils: language = c++
55
# distutils: include_dirs = ../../cuda/core
66

7-
"""Deterministic raw-handle reuse tests across independent parent registries.
7+
"""Deterministic raw-handle reuse tests against the per-parent child registries.
88
99
Parents are real empty CUDA graphs. Stack-local addresses stand in for reused
1010
child and owner handles only inside the metadata API; they are never passed to
@@ -56,6 +56,32 @@ def test_child_graph_registry_rejects_foreign_parent(init_cuda):
5656
assert same_child.get() == new_child.get()
5757

5858

59+
@pytest.mark.agent_authored(model="gpt-6")
60+
def test_child_graph_registry_rejects_foreign_owner_in_same_parent(init_cuda):
61+
"""Retiring an old owner preserves the replacement within one hierarchy."""
62+
cdef uintptr_t tokens[3]
63+
cdef CUgraph raw_child = <CUgraph>&tokens[0]
64+
cdef CUgraphNode old_owner = <CUgraphNode>&tokens[1]
65+
cdef CUgraphNode new_owner = <CUgraphNode>&tokens[2]
66+
cdef GraphDefinition parent = GraphDefinition()
67+
cdef GraphHandle old_child = create_child_graph_handle(
68+
raw_child, parent._h_graph, old_owner
69+
)
70+
cdef GraphHandle new_child = create_child_graph_handle(
71+
raw_child, parent._h_graph, new_owner
72+
)
73+
assert new_child.get() != old_child.get()
74+
75+
invalidate_child_graph_state(parent._h_graph, old_owner)
76+
assert as_cu(old_child) == NULL
77+
assert as_cu(new_child) == raw_child
78+
79+
cdef GraphHandle same_child = create_child_graph_handle(
80+
raw_child, parent._h_graph, new_owner
81+
)
82+
assert same_child.get() == new_child.get()
83+
84+
5985
@pytest.mark.agent_authored(model="gpt-6")
6086
def test_graph_hierarchy_deletion_preserves_replacement_mapping(init_cuda):
6187
"""An old tree's destructor must not erase another tree's newer entry."""

0 commit comments

Comments
 (0)