Skip to content

Commit f28f5d5

Browse files
Test GatedGraphConv against a reference recursion
Check homogeneous and heterograph outputs (num_layers = 3) against a hand-written loop, including a relation between nodes of the same type, and exercise tuple input in the Lux layer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1661333 commit f28f5d5

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

‎GNNLux/test/layers/conv.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ end
106106
@testset "GatedGraphConv" begin
107107
l = GatedGraphConv(in_dims, 3)
108108
test_lux_layer(rng, l, g, x, outputsize=(in_dims,))
109+
110+
ps, st = LuxCore.setup(rng, l)
111+
@test l(g, (x, x), ps, st)[1] == l(g, x, ps, st)[1]
112+
sg = edge_type_subgraph(rand_bipartite_heterograph(rng, (4, 6), 10), (:A, :to, :B))
113+
y, _ = l(sg, (randn(rng, Float32, 2, 4), randn(rng, Float32, in_dims, 6)), ps, st)
114+
@test size(y) == (in_dims, 6)
109115
end
110116

111117
@testset "GINConv" begin

‎GraphNeuralNetworks/test/layers/conv.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ end
241241
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
242242
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
243243
end
244+
245+
@testset "reference" begin
246+
for g in TEST_GRAPHS
247+
s, t = edge_index(g)
248+
h = vcat(g.x, zeros(Float32, D_OUT - D_IN, g.num_nodes))
249+
for k in 1:num_layers
250+
m = zeros(Float32, D_OUT, g.num_nodes)
251+
for (j, i) in zip(s, t)
252+
m[:, i] .+= l.weight[:, :, k] * h[:, j]
253+
end
254+
_, h = l.gru(m, h)
255+
end
256+
@test l(g, g.x) ≈ h
257+
end
258+
end
244259
end
245260

246261

‎GraphNeuralNetworks/test/layers/heteroconv.jl‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,41 @@
160160
end
161161

162162
@testset "GatedGraphConv" begin
163-
x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3))
164-
layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 2),
165-
(:B, :to, :A) => GatedGraphConv(4, 2));
166-
y = layers(hg, x);
167-
@test size(y.A) == (4, 2) && size(y.B) == (4, 3)
163+
function gated_ref(l, s, t, xj, xi; evolve_src)
164+
pad(x) = vcat(x, zeros(Float32, l.dims - size(x, 1), size(x, 2)))
165+
h, hj = pad(xi), pad(xj)
166+
for k in 1:l.num_layers
167+
src = evolve_src ? h : hj
168+
m = zeros(Float32, l.dims, size(h, 2))
169+
for (j, i) in zip(s, t)
170+
m[:, i] .+= l.weight[:, :, k] * src[:, j]
171+
end
172+
_, h = l.gru(m, h)
173+
end
174+
return h
175+
end
176+
177+
eAB = ([1, 1, 2, 3], [1, 2, 2, 4])
178+
eBA = ([1, 2, 4, 4], [1, 1, 3, 2])
179+
eAA = ([1, 2, 3], [2, 3, 1])
180+
g = GNNHeteroGraph(((:A, :to, :B) => eAB, (:B, :to, :A) => eBA, (:A, :to, :A) => eAA);
181+
num_nodes = Dict(:A => 3, :B => 4))
182+
x = (A = rand(Float32, 3, 3), B = rand(Float32, 2, 4))
183+
layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 3),
184+
(:B, :to, :A) => GatedGraphConv(4, 3),
185+
(:A, :to, :A) => GatedGraphConv(4, 3))
186+
y = layers(g, x)
187+
@test size(y.A) == (4, 3) && size(y.B) == (4, 4)
188+
189+
lAB, lBA, lAA = layers.layers
190+
@test y.B ≈ gated_ref(lAB, eAB..., x.A, x.B; evolve_src = false)
191+
@test y.A ≈ gated_ref(lBA, eBA..., x.B, x.A; evolve_src = false) +
192+
gated_ref(lAA, eAA..., x.A, x.A; evolve_src = true)
193+
194+
loss(x) = sum(layers(g, x).A) + sum(abs2, layers(g, x).B)
195+
dx = gradient(loss, x)[1]
196+
_, ndx = finitediff_withgradient(loss, x)
197+
@test dx.A ≈ ndx[1].A rtol=1e-3
198+
@test dx.B ≈ ndx[1].B rtol=1e-3
168199
end
169200
end

0 commit comments

Comments
 (0)