Skip to content

Commit 8bf7ffb

Browse files
committed
Add graph_sizes to GNNGraph
graph_sizes holds the number of nodes in each graph of a batch. It duplicates graph_indicator, but its length is the number of graphs, which makes that number part of the array shapes a compiled Reactant function checks. A compiled model called on a batch with a different number of graphs now raises an error instead of silently returning wrong results. The positional constructor derives graph_sizes from graph_indicator, and transformations that keep the graph indicator pass graph_sizes through.
1 parent 64904d3 commit 8bf7ffb

7 files changed

Lines changed: 72 additions & 31 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Entries link to the pull request that introduced them.
1515
**Added**
1616
- Graphs can be compiled with Reactant.jl: a `GNNGraph` can now be moved to a Reactant device (e.g. `g |> reactant_device()`) and traced. The COO index vectors and the `graph_indicator` of a `GNNGraph` are no longer required to have an `Integer` eltype, so that traced arrays (eltype `TracedRNumber{Int}`) can be stored in them, and a new `Reactant` package extension makes `degree` work inside a trace ([#730]).
1717
- Added `pad_graph`, which pads a graph to given numbers of nodes, edges and graphs, putting the padding in extra graphs. Padding every mini-batch to the same size lets Reactant.jl compile a model or a training step once and reuse it for all batches ([#730]).
18+
- Added the `graph_sizes` field to `GNNGraph`, holding the number of nodes in each graph of a batch (`nothing` for a single graph). Its length is the number of graphs, so a model compiled with Reactant.jl now raises an error when called on a batch with a different number of graphs, instead of silently returning wrong results ([#730]).
1819

1920
## GNNlib.jl — Unreleased (towards 1.5.0)
2021

‎GNNGraphs/src/convert.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ end
125125
function _to_coo_graph(g::GNNGraph{<:SPARSE_T})
126126
s, t, v = findnz(g.graph)
127127
return GNNGraph((s, t, v), g.num_nodes, g.num_edges, g.num_graphs,
128-
g.graph_indicator, g.ndata, g.edata, g.gdata)
128+
g.graph_indicator, g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
129129
end
130130

131131
function _to_coo_graph(g::GNNGraph{<:ADJMAT_T})
132132
A = g.graph
133133
s, t, _ = _findnz_idx(A)
134134
v = _edge_values(A, s, t)
135135
return GNNGraph((s, t, v), g.num_nodes, g.num_edges, g.num_graphs,
136-
g.graph_indicator, g.ndata, g.edata, g.gdata)
136+
g.graph_indicator, g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
137137
end
138138

139139
### CONVERT TO ADJACENCY MATRIX ################

‎GNNGraphs/src/gnngraph.jl‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ as well, unless explicitly set by the keyword arguments
2929
A `GNNGraph` can also represent multiple graphs batched together
3030
(see [`MLUtils.batch`](@ref) or [`SparseArrays.blockdiag`](@ref)).
3131
The field `g.graph_indicator` contains the graph membership
32-
of each node.
32+
of each node, and `g.graph_sizes` the number of nodes in each graph.
33+
Both are `nothing` for a single graph.
3334
3435
`GNNGraph`s are always directed graphs, therefore each edge is defined
3536
by a source node and a target node (see [`edge_index`](@ref)).
@@ -110,23 +111,33 @@ struct GNNGraph{T <: Union{COO_T, ADJMAT_T}} <: AbstractGNNGraph{T}
110111
num_edges::Int
111112
num_graphs::Int
112113
graph_indicator::Union{Nothing, AVecI} # vector of ints or nothing
114+
graph_sizes::Union{Nothing, AVecI} # number of nodes in each graph, or nothing
113115
ndata::DataStore
114116
edata::DataStore
115117
gdata::DataStore
116118
is_coalesced::Bool # only for :coo, true if the graph is coalesced, i.e., no multi edges and indices ordered by target, then source
117119
end
118120

119-
# GNNGraph constructor setting the is_coalesced field to false
121+
# GNNGraph constructor deriving the graph_sizes field from the graph indicator.
122+
# graph_sizes duplicates the graph indicator, but its length is the number of graphs,
123+
# which makes the number of graphs part of the array shapes seen by compilers like Reactant.
120124
function GNNGraph(graph::T,
121125
num_nodes::Int,
122126
num_edges::Int,
123127
num_graphs::Int,
124128
graph_indicator::Union{Nothing, AVecI},
125129
ndata::DataStore,
126130
edata::DataStore,
127-
gdata::DataStore) where {T <: Union{COO_T, ADJMAT_T}}
131+
gdata::DataStore,
132+
is_coalesced::Bool = false;
133+
graph_sizes = _graph_sizes(graph_indicator, num_graphs)) where {T <: Union{COO_T, ADJMAT_T}}
128134
return GNNGraph{T}(graph, num_nodes, num_edges, num_graphs,
129-
graph_indicator, ndata, edata, gdata, false)
135+
graph_indicator, graph_sizes, ndata, edata, gdata, is_coalesced)
136+
end
137+
138+
_graph_sizes(graph_indicator::Nothing, num_graphs) = nothing
139+
function _graph_sizes(graph_indicator::AVecI, num_graphs)
140+
return _scatter(+, ones_like(graph_indicator), graph_indicator, num_graphs)
130141
end
131142

132143
function GNNGraph(data::D;
@@ -233,7 +244,7 @@ function GNNGraph(g::GNNGraph; ndata = g.ndata, edata = g.edata, gdata = g.gdata
233244
return GNNGraph(graph,
234245
g.num_nodes, g.num_edges, g.num_graphs,
235246
g.graph_indicator,
236-
ndata, edata, gdata)
247+
ndata, edata, gdata; graph_sizes = g.graph_sizes)
237248
end
238249

239250
"""
@@ -247,12 +258,12 @@ function Base.copy(g::GNNGraph; deep = false)
247258
GNNGraph(deepcopy(g.graph),
248259
g.num_nodes, g.num_edges, g.num_graphs,
249260
deepcopy(g.graph_indicator),
250-
deepcopy(g.ndata), deepcopy(g.edata), deepcopy(g.gdata))
261+
deepcopy(g.ndata), deepcopy(g.edata), deepcopy(g.gdata); graph_sizes = deepcopy(g.graph_sizes))
251262
else
252263
GNNGraph(g.graph,
253264
g.num_nodes, g.num_edges, g.num_graphs,
254265
g.graph_indicator,
255-
g.ndata, g.edata, g.gdata)
266+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
256267
end
257268
end
258269

@@ -345,14 +356,14 @@ MLUtils.getobs(g::GNNGraph, i) = getgraph(g, i)
345356
function Base.:(==)(g1::GNNGraph, g2::GNNGraph)
346357
g1 === g2 && return true
347358
for k in fieldnames(typeof(g1))
348-
k === :graph_indicator && continue
359+
k in (:graph_indicator, :graph_sizes) && continue
349360
getfield(g1, k) != getfield(g2, k) && return false
350361
end
351362
return true
352363
end
353364

354365
function Base.hash(g::T, h::UInt) where {T <: GNNGraph}
355-
fs = (getfield(g, k) for k in fieldnames(T) if k !== :graph_indicator)
366+
fs = (getfield(g, k) for k in fieldnames(T) if k ∉ (:graph_indicator, :graph_sizes))
356367
return foldl((h, f) -> hash(f, h), fs, init = hash(T, h))
357368
end
358369

‎GNNGraphs/src/sampling.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function sample_neighbors(g::GNNGraph{<:COO_T}, nodes, K = -1;
202202
gnew = GNNGraph(graph,
203203
g.num_nodes, num_edges, g.num_graphs,
204204
g.graph_indicator,
205-
g.ndata, edata, g.gdata)
205+
g.ndata, edata, g.gdata; graph_sizes = g.graph_sizes)
206206
else
207207
nodes_other = dir == :in ? setdiff(s, nodes) : setdiff(t, nodes)
208208
nodes_all = [nodes; nodes_other]

‎GNNGraphs/src/transform.jl‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function add_self_loops(g::GNNGraph{<:COO_T})
2424
return GNNGraph((s, t, ew),
2525
g.num_nodes, length(s), g.num_graphs,
2626
g.graph_indicator,
27-
g.ndata, g.edata, g.gdata)
27+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
2828
end
2929

3030
function add_self_loops(g::GNNGraph{<:ADJMAT_T})
@@ -35,7 +35,7 @@ function add_self_loops(g::GNNGraph{<:ADJMAT_T})
3535
return GNNGraph(A,
3636
g.num_nodes, num_edges, g.num_graphs,
3737
g.graph_indicator,
38-
g.ndata, g.edata, g.gdata)
38+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
3939
end
4040

4141
"""
@@ -60,7 +60,7 @@ function remove_self_loops(g::GNNGraph{<:COO_T})
6060
GNNGraph((s, t, w),
6161
g.num_nodes, length(s), g.num_graphs,
6262
g.graph_indicator,
63-
g.ndata, edata, g.gdata)
63+
g.ndata, edata, g.gdata; graph_sizes = g.graph_sizes)
6464
end
6565

6666
function remove_self_loops(g::GNNGraph{<:ADJMAT_T})
@@ -74,7 +74,7 @@ function remove_self_loops(g::GNNGraph{<:ADJMAT_T})
7474
return GNNGraph(A,
7575
g.num_nodes, num_edges, g.num_graphs,
7676
g.graph_indicator,
77-
g.ndata, g.edata, g.gdata)
77+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
7878
end
7979

8080
"""
@@ -137,7 +137,7 @@ function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove::AbstractVector{<:In
137137
return GNNGraph((s, t, w),
138138
g.num_nodes, length(s), g.num_graphs,
139139
g.graph_indicator,
140-
g.ndata, edata, g.gdata)
140+
g.ndata, edata, g.gdata; graph_sizes = g.graph_sizes)
141141
end
142142

143143

@@ -185,7 +185,7 @@ function Base.coalesce(g::GNNGraph{<:COO_T}; aggr = +)
185185
return GNNGraph((s, t, w),
186186
g.num_nodes, num_edges, g.num_graphs,
187187
g.graph_indicator,
188-
g.ndata, edata, g.gdata, true)
188+
g.ndata, edata, g.gdata, true; graph_sizes = g.graph_sizes)
189189
end
190190

191191
"""
@@ -252,7 +252,7 @@ function remove_nodes(g::GNNGraph{<:COO_T}, nodes_to_remove::AbstractVector)
252252
return GNNGraph((s, t, w),
253253
num_nodes, length(s), g.num_graphs,
254254
g.graph_indicator,
255-
ndata, edata, g.gdata)
255+
ndata, edata, g.gdata; graph_sizes = g.graph_sizes)
256256
end
257257

258258
"""
@@ -353,7 +353,7 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
353353
return GNNGraph((s, t, w),
354354
num_nodes, length(s), g.num_graphs,
355355
g.graph_indicator,
356-
ndata, edata, g.gdata)
356+
ndata, edata, g.gdata; graph_sizes = g.graph_sizes)
357357
end
358358

359359
"""
@@ -507,7 +507,7 @@ function to_bidirected(g::GNNGraph{<:COO_T})
507507
g = GNNGraph((snew, tnew, w),
508508
g.num_nodes, length(snew), g.num_graphs,
509509
g.graph_indicator,
510-
g.ndata, edata, g.gdata)
510+
g.ndata, edata, g.gdata; graph_sizes = g.graph_sizes)
511511

512512
return coalesce(g; aggr = mean)
513513
end
@@ -527,7 +527,7 @@ function to_unidirected(g::GNNGraph{<:COO_T})
527527
g = GNNGraph((snew, tnew, w),
528528
g.num_nodes, g.num_edges, g.num_graphs,
529529
g.graph_indicator,
530-
g.ndata, g.edata, g.gdata)
530+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
531531

532532
return coalesce(g; aggr = mean)
533533
end
@@ -561,7 +561,7 @@ function add_nodes(g::GNNGraph{<:COO_T}, n::Integer; ndata = (;))
561561
GNNGraph(g.graph,
562562
g.num_nodes + n, g.num_edges, g.num_graphs,
563563
g.graph_indicator,
564-
ndata, g.edata, g.gdata)
564+
ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
565565
end
566566

567567
"""
@@ -576,7 +576,9 @@ weights, are padded with zeros.
576576
577577
Padding every mini-batch to the same size lets a compiler like Reactant.jl compile a
578578
model, or a training step, once and reuse it for all batches. The original graphs are
579-
the first `g.num_graphs` ones: mask out the others, e.g. in the loss.
579+
the first `g.num_graphs` ones: mask out the others, e.g. in the loss. A compiled function
580+
raises an error when called on a graph padded to different sizes, including a different
581+
number of graphs.
580582
581583
Requires `num_graphs > g.num_graphs`, and `num_nodes > g.num_nodes` if edges are added.
582584
@@ -660,7 +662,7 @@ function set_edge_weight(g::GNNGraph, w::AbstractVector)
660662
return GNNGraph((s, t, w),
661663
g.num_nodes, g.num_edges, g.num_graphs,
662664
g.graph_indicator,
663-
g.ndata, g.edata, g.gdata)
665+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
664666
end
665667

666668
function SparseArrays.blockdiag(g1::GNNGraph, g2::GNNGraph)
@@ -1159,5 +1161,5 @@ function ppr_diffusion(g::GNNGraph{<:COO_T}; alpha = 0.85f0)
11591161
return GNNGraph((s, t, new_w),
11601162
g.num_nodes, length(s), g.num_graphs,
11611163
g.graph_indicator,
1162-
g.ndata, g.edata, g.gdata)
1164+
g.ndata, g.edata, g.gdata; graph_sizes = g.graph_sizes)
11631165
end

‎GNNGraphs/test/gnngraph.jl‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,36 @@ end
413413
end
414414
end
415415

416+
@testitem "graph_sizes" setup=[GraphsTestModule] begin
417+
using .GraphsTestModule
418+
@test rand_graph(3, 4).graph_sizes === nothing
419+
for GRAPH_T in GRAPH_TYPES
420+
g = MLUtils.batch([rand_graph(n, m, graph_type = GRAPH_T) for (n, m) in [(3, 4), (5, 6)]])
421+
@test g.graph_sizes == [3, 5]
422+
@test add_self_loops(g).graph_sizes == [3, 5]
423+
@test GNNGraph(g, ndata = rand(2, 8)).graph_sizes == [3, 5]
424+
@test copy(g, deep = true).graph_sizes == [3, 5]
425+
@test getgraph(g, 2).graph_sizes == [5]
426+
end
427+
g = MLUtils.batch([rand_graph(3, 4), rand_graph(5, 6)])
428+
@test pad_graph(g, 10, 12, 4).graph_sizes == [3, 5, 2, 0]
429+
# the nodes of a graph need not be contiguous
430+
g = GNNGraph([1, 2, 3], [2, 3, 4], graph_indicator = [1, 2, 1, 2])
431+
@test g.graph_sizes == [2, 2]
432+
end
433+
434+
@testitem "graph_sizes GPU" setup=[GraphsTestModule] tags=[:gpu] begin
435+
using .GraphsTestModule
436+
dev = gpu_device(force=true)
437+
gs = [rand_graph(3, 4), rand_graph(5, 6)]
438+
g = MLUtils.batch(dev.(gs))
439+
@test get_device(g.graph_sizes) isa AbstractGPUDevice
440+
@test Array(g.graph_sizes) == [3, 5]
441+
@test Array(dev(MLUtils.batch(gs)).graph_sizes) == [3, 5]
442+
@test Array(add_self_loops(g).graph_sizes) == [3, 5]
443+
@test Array(pad_graph(g, 10, 12, 4).graph_sizes) == [3, 5, 2, 0]
444+
end
445+
416446
@testitem "show" begin
417447
# no throw when global data is not an array
418448
g = rand_graph(10, 20, gdata = "ciao")

‎GNNLux/test/reactant.jl‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,9 @@ end
9090
end
9191

9292
# graphs of another size do not match the compiled shapes
93-
for g in (batches[2], pad_graph(batches[1], 41, 80, 5), pad_graph(batches[1], 40, 82, 5))
93+
for g in (batches[2], pad_graph(batches[1], 41, 80, 5), pad_graph(batches[1], 40, 82, 5),
94+
pad_graph(batches[1], 40, 80, 6))
9495
gd = dev(g)
9596
@test_throws "RuntimeProgramInputMismatch" compiled(gd, gd.x, dev(ps), dev(st))
9697
end
97-
# the number of graphs is not a shape but a constant baked into the compiled model,
98-
# so a different one goes unnoticed
99-
gp6 = dev(pad_graph(batches[1], 40, 80, 6))
100-
@test_broken size(first(compiled(gp6, gp6.x, dev(ps), dev(st)))) == (2, 6)
10198
end

0 commit comments

Comments
 (0)