Skip to content

Commit aeeb7a6

Browse files
DHAKED440CarloLucibelloclaude
authored
Prevent inconsistent indicators when growing batched graphs (#737)
* Guard node growth on batched graphs * Suggest unbatching in the batched node-growth error Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: CarloLucibello <carlo.lucibello@gmail.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent dbc1f38 commit aeeb7a6

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎GNNGraphs/src/transform.jl‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ function remove_nodes(g::GNNGraph, p::AbstractFloat)
280280
return remove_nodes(g, nodes_to_remove)
281281
end
282282

283+
function _graph_indicator_after_node_growth(g::GNNGraph, n_new_nodes::Integer)
284+
n_new_nodes <= 0 && return g.graph_indicator
285+
g.num_graphs > 1 && throw(ArgumentError(
286+
"Cannot add nodes to a batched graph without specifying which graph they belong to. " *
287+
"Use `unbatch`, modify the component graphs, and `batch` them again.",
288+
))
289+
isnothing(g.graph_indicator) && return nothing
290+
return cat_features(g.graph_indicator, ones_like(g.graph_indicator, n_new_nodes))
291+
end
292+
283293
"""
284294
add_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
285295
add_edges(g::GNNGraph, (s, t); [edata])
@@ -290,7 +300,8 @@ Optionally, pass the edge weight `w` and the features `edata` for the new edges
290300
Returns a new graph sharing part of the underlying data with `g`.
291301
292302
If the `s` or `t` contain nodes that are not already present in the graph,
293-
they are added to the graph as well.
303+
they are added to the graph as well. Adding new nodes to a batched graph is
304+
not supported because their graph assignment is ambiguous.
294305
295306
# Examples
296307
@@ -335,6 +346,9 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
335346
end
336347
@assert minimum(snew) >= 1
337348
@assert minimum(tnew) >= 1
349+
num_nodes = max(maximum(snew), maximum(tnew), g.num_nodes)
350+
graph_indicator = _graph_indicator_after_node_growth(g, num_nodes - g.num_nodes)
351+
338352
num_new = length(snew)
339353
edata = normalize_graphdata(edata, default_name = :e, n = num_new)
340354
edata = cat_features(g.edata, edata)
@@ -345,7 +359,6 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
345359
w = get_edge_weight(g)
346360
w = cat_features(w, wnew, g.num_edges, num_new)
347361

348-
num_nodes = max(maximum(snew), maximum(tnew), g.num_nodes)
349362
if num_nodes > g.num_nodes
350363
ndata_new = normalize_graphdata((;), default_name = :x, n = num_nodes - g.num_nodes)
351364
ndata = cat_features(g.ndata, ndata_new)
@@ -355,7 +368,7 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
355368

356369
return GNNGraph((s, t, w),
357370
num_nodes, length(s), g.num_graphs,
358-
g.graph_indicator,
371+
graph_indicator,
359372
ndata, edata, g.gdata)
360373
end
361374

@@ -555,15 +568,17 @@ end
555568
556569
Add `n` new nodes to graph `g`. In the
557570
new graph, these nodes will have indexes from `g.num_nodes + 1`
558-
to `g.num_nodes + n`.
571+
to `g.num_nodes + n`. Adding nodes to a batched graph is not supported
572+
because their graph assignment is ambiguous.
559573
"""
560574
function add_nodes(g::GNNGraph{<:COO_T}, n::Integer; ndata = (;))
575+
graph_indicator = _graph_indicator_after_node_growth(g, n)
561576
ndata = normalize_graphdata(ndata, default_name = :x, n = n)
562577
ndata = cat_features(g.ndata, ndata)
563578

564579
GNNGraph(g.graph,
565580
g.num_nodes + n, g.num_edges, g.num_graphs,
566-
g.graph_indicator,
581+
graph_indicator,
567582
ndata, g.edata, g.gdata)
568583
end
569584

‎GNNGraphs/test/transform.jl‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,40 @@ end
467467
end
468468
end
469469

470+
@testitem "node growth and batched graph indicators" setup=[GraphsTestModule] begin
471+
using .GraphsTestModule
472+
473+
g1 = GNNGraph(([1, 2], [2, 3]); num_nodes = 3)
474+
g2 = GNNGraph(([1, 2], [2, 1]); num_nodes = 4)
475+
g = batch([g1, g2])
476+
477+
@test g.num_nodes == length(g.graph_indicator) == 7
478+
@test_throws ArgumentError add_nodes(g, 2)
479+
@test_throws ArgumentError add_edges(g, [8], [1])
480+
@test add_nodes(g, 0).graph_indicator == g.graph_indicator
481+
@test add_edges(g, Int[], Int[]) === g
482+
483+
# Adding edges between existing nodes does not grow the batched graph.
484+
g_with_edge = add_edges(g, [1], [2])
485+
@test g_with_edge.num_nodes == length(g_with_edge.graph_indicator) == 7
486+
487+
# A single graph with explicit membership can safely extend that membership.
488+
single = GNNGraph(([1], [2]); num_nodes = 2, graph_indicator = [1, 1])
489+
single_with_nodes = add_nodes(single, 2)
490+
single_with_edge = add_edges(single, [3], [1])
491+
@test single_with_nodes.graph_indicator == [1, 1, 1, 1]
492+
@test single_with_edge.graph_indicator == [1, 1, 1]
493+
494+
# Unbatched graphs represent membership implicitly, so their stored field stays `nothing`.
495+
unbatched = GNNGraph(([1], [2]); num_nodes = 2)
496+
unbatched_with_nodes = add_nodes(unbatched, 2)
497+
unbatched_with_edge = add_edges(unbatched, [3], [1])
498+
@test isnothing(unbatched_with_nodes.graph_indicator)
499+
@test length(graph_indicator(unbatched_with_nodes)) == unbatched_with_nodes.num_nodes
500+
@test isnothing(unbatched_with_edge.graph_indicator)
501+
@test length(graph_indicator(unbatched_with_edge)) == unbatched_with_edge.num_nodes
502+
end
503+
470504
@testitem "remove_self_loops" setup=[GraphsTestModule] begin
471505
using .GraphsTestModule
472506
for GRAPH_T in GRAPH_TYPES

0 commit comments

Comments
 (0)