Skip to content

Commit 4acaf6d

Browse files
i-Amoghclaude
andauthored
Add GatedGraphConv support for HeteroGraphConv (#653)
GatedGraphConv accepts a tuple (xj, xi) of source and target node features, so it can be used in HeteroGraphConv. Source features of a different node type stay fixed across the num_layers steps, while target states evolve through the GRU. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6a9ec35 commit 4acaf6d

8 files changed

Lines changed: 109 additions & 11 deletions

File tree

‎GNNLux/docs/src/api/conv.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The table below lists all graph convolutional layers implemented in the *GNNLux.
2424
| [`EdgeConv`](@ref) | | | | ✓ | |
2525
| [`GATConv`](@ref) | | | ✓ | ✓ | ✓ |
2626
| [`GATv2Conv`](@ref) | | | ✓ | ✓ | ✓ |
27-
| [`GatedGraphConv`](@ref) | ✓ | | | | ✓ |
27+
| [`GatedGraphConv`](@ref) | ✓ | | | ✓ | ✓ |
2828
| [`GCNConv`](@ref) | ✓ | ✓ | | ✓ | |
2929
| [`GINConv`](@ref) | ✓ | | | ✓ | ✓ |
3030
| [`GMMConv`](@ref) | | | ✓ | | |

‎GNNLux/src/layers/conv.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,23 @@ where ``\mathbf{h}^{(l)}_i`` denotes the ``l``-th hidden variables passing throu
12321232
- `aggr`: Aggregation operator for the incoming messages (e.g. `+`, `*`, `max`, `min`, and `mean`).
12331233
- `init_weight`: Weights' initializer. Default `glorot_uniform`.
12341234
1235+
# Forward
1236+
1237+
(::GatedGraphConv)(g, x, ps, st) -> (y, st)
1238+
1239+
- `g`: A `GNNGraph`, or a `GNNHeteroGraph` with a single edge type.
1240+
- `x`: A node feature matrix of size `(in, num_nodes)` with `in <= out`,
1241+
or a tuple `(xj, xi)` of source and target node features of sizes `(in_j, num_src_nodes)` and `(in_i, num_dst_nodes)`,
1242+
with `in_j, in_i <= out`.
1243+
- `ps`, `st`: The parameters and state of the layer.
1244+
1245+
Returns the target node features `y` of size `(out, num_dst_nodes)` and the state.
1246+
1247+
When `xj` and `xi` are distinct arrays (e.g. source and target nodes of different types),
1248+
the source nodes have no hidden state evolving in the recursion: their padded features ``[\mathbf{x}_j; \mathbf{0}]``
1249+
are used in place of ``\mathbf{h}^{(l-1)}_j`` at every step, while the target states ``\mathbf{h}^{(l)}_i`` evolve through the GRU.
1250+
If `xj` and `xi` are the same array, the source states evolve as well, as in the homogeneous case.
1251+
12351252
# Examples:
12361253
12371254
```julia

‎GNNLux/test/layers/conv.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ end
115115
@testset "GatedGraphConv" begin
116116
l = GatedGraphConv(in_dims, 3)
117117
test_lux_layer(rng, l, g, x, outputsize=(in_dims,))
118+
119+
ps, st = LuxCore.setup(rng, l)
120+
@test l(g, (x, x), ps, st)[1] == l(g, x, ps, st)[1]
121+
sg = edge_type_subgraph(rand_bipartite_heterograph(rng, (4, 6), 10), (:A, :to, :B))
122+
y, _ = l(sg, (randn(rng, Float32, 2, 4), randn(rng, Float32, in_dims, 6)), ps, st)
123+
@test size(y) == (in_dims, 6)
118124
end
119125

120126
@testset "GINConv" begin

‎GNNlib/src/layers/conv.jl‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,21 @@ end
215215

216216
####################### GatedGraphConv ######################################
217217

218-
function gated_graph_conv(l, g::GNNGraph, x::AbstractMatrix)
219-
check_num_nodes(g, x)
218+
function _gated_graph_pad(x, dims)
220219
m, n = size(x)
221-
@assert m <= l.dims "number of input features must be less or equal to output features."
222-
if m < l.dims
223-
xpad = zeros_like(x, (l.dims - m, n))
224-
x = vcat(x, xpad)
225-
end
226-
h = x
220+
@assert m <= dims "number of input features must be less or equal to output features."
221+
return m < dims ? vcat(x, zeros_like(x, (dims - m, n))) : x
222+
end
223+
224+
function gated_graph_conv(l, g::AbstractGNNGraph, x)
225+
check_num_nodes(g, x)
226+
xj, xi = expand_srcdst(g, x)
227+
h = _gated_graph_pad(xi, l.dims)
228+
# Distinct source features (e.g. another node type) have no hidden state in the recursion.
229+
fixed_src = xj !== xi
230+
hj = fixed_src ? _gated_graph_pad(xj, l.dims) : h
227231
for i in 1:(l.num_layers)
228-
m = view(l.weight, :, :, i) * h
232+
m = view(l.weight, :, :, i) * (fixed_src ? hj : h)
229233
m = propagate(copy_xj, g, l.aggr; xj = m)
230234
_, h = l.gru(m, h)
231235
end

‎GraphNeuralNetworks/docs/src/api/conv.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The table below lists all graph convolutional layers implemented in the *GraphNe
2525
| [`EdgeConv`](@ref) | | | | ✓ | |
2626
| [`GATConv`](@ref) | | | ✓ | ✓ | ✓ |
2727
| [`GATv2Conv`](@ref) | | | ✓ | ✓ | ✓ |
28-
| [`GatedGraphConv`](@ref) | ✓ | | | | ✓ |
28+
| [`GatedGraphConv`](@ref) | ✓ | | | ✓ | ✓ |
2929
| [`GCNConv`](@ref) | ✓ | ✓ | | ✓ | |
3030
| [`GINConv`](@ref) | ✓ | | | ✓ | ✓ |
3131
| [`GMMConv`](@ref) | | | ✓ | | |

‎GraphNeuralNetworks/src/layers/conv.jl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,23 @@ where ``\mathbf{h}^{(l)}_i`` denotes the ``l``-th hidden variables passing throu
495495
- `aggr`: Aggregation operator for the incoming messages (e.g. `+`, `*`, `max`, `min`, and `mean`).
496496
- `init`: Weight initialization function.
497497
498+
# Forward
499+
500+
(::GatedGraphConv)(g, x) -> AbstractMatrix
501+
502+
- `g`: A `GNNGraph`, or a `GNNHeteroGraph` with a single edge type (e.g. within a [`HeteroGraphConv`](@ref)).
503+
- `x`: A node feature matrix of size `(in, num_nodes)` with `in <= out`,
504+
or a tuple `(xj, xi)` of source and target node features of sizes `(in_j, num_src_nodes)` and `(in_i, num_dst_nodes)`,
505+
with `in_j, in_i <= out`.
506+
507+
Returns the target node features of size `(out, num_dst_nodes)`.
508+
509+
When `xj` and `xi` are distinct arrays (e.g. source and target nodes of different types),
510+
the source nodes have no hidden state evolving in the recursion: their padded features ``[\mathbf{x}_j; \mathbf{0}]``
511+
are used in place of ``\mathbf{h}^{(l-1)}_j`` at every step, while the target states ``\mathbf{h}^{(l)}_i`` evolve through the GRU.
512+
If `xj` and `xi` are the same array (as passed by [`HeteroGraphConv`](@ref) for relations whose source and target
513+
node types coincide), the source states evolve as well, as in the homogeneous case.
514+
498515
# Examples:
499516
500517
```julia

‎GraphNeuralNetworks/test/layers/conv.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ end
262262
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
263263
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
264264
end
265+
266+
@testset "reference" begin
267+
for g in TEST_GRAPHS
268+
s, t = edge_index(g)
269+
h = vcat(g.x, zeros(Float32, D_OUT - D_IN, g.num_nodes))
270+
for k in 1:num_layers
271+
m = zeros(Float32, D_OUT, g.num_nodes)
272+
for (j, i) in zip(s, t)
273+
m[:, i] .+= l.weight[:, :, k] * h[:, j]
274+
end
275+
_, h = l.gru(m, h)
276+
end
277+
@test l(g, g.x) ≈ h
278+
end
279+
end
265280
end
266281

267282

‎GraphNeuralNetworks/test/layers/heteroconv.jl‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,43 @@
179179
y = HeteroGraphConv((:A, :to, :A) => AGNNConv())(hg_AA, (; A = x))
180180
@test y.A ≈ AGNNConv()(GNNGraph(s, t), x)
181181
end
182+
183+
@testset "GatedGraphConv" begin
184+
function gated_ref(l, s, t, xj, xi; evolve_src)
185+
pad(x) = vcat(x, zeros(Float32, l.dims - size(x, 1), size(x, 2)))
186+
h, hj = pad(xi), pad(xj)
187+
for k in 1:l.num_layers
188+
src = evolve_src ? h : hj
189+
m = zeros(Float32, l.dims, size(h, 2))
190+
for (j, i) in zip(s, t)
191+
m[:, i] .+= l.weight[:, :, k] * src[:, j]
192+
end
193+
_, h = l.gru(m, h)
194+
end
195+
return h
196+
end
197+
198+
eAB = ([1, 1, 2, 3], [1, 2, 2, 4])
199+
eBA = ([1, 2, 4, 4], [1, 1, 3, 2])
200+
eAA = ([1, 2, 3], [2, 3, 1])
201+
g = GNNHeteroGraph(((:A, :to, :B) => eAB, (:B, :to, :A) => eBA, (:A, :to, :A) => eAA);
202+
num_nodes = Dict(:A => 3, :B => 4))
203+
x = (A = rand(Float32, 3, 3), B = rand(Float32, 2, 4))
204+
layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 3),
205+
(:B, :to, :A) => GatedGraphConv(4, 3),
206+
(:A, :to, :A) => GatedGraphConv(4, 3))
207+
y = layers(g, x)
208+
@test size(y.A) == (4, 3) && size(y.B) == (4, 4)
209+
210+
lAB, lBA, lAA = layers.layers
211+
@test y.B ≈ gated_ref(lAB, eAB..., x.A, x.B; evolve_src = false)
212+
@test y.A ≈ gated_ref(lBA, eBA..., x.B, x.A; evolve_src = false) +
213+
gated_ref(lAA, eAA..., x.A, x.A; evolve_src = true)
214+
215+
loss(x) = sum(layers(g, x).A) + sum(abs2, layers(g, x).B)
216+
dx = gradient(loss, x)[1]
217+
_, ndx = finitediff_withgradient(loss, x)
218+
@test dx.A ≈ ndx[1].A rtol=1e-3
219+
@test dx.B ≈ ndx[1].B rtol=1e-3
220+
end
182221
end

0 commit comments

Comments
 (0)