From 7beb4b806051c43449c80b27d60ae300cf99df9e Mon Sep 17 00:00:00 2001 From: Amogh Date: Sun, 22 Mar 2026 07:43:02 +0530 Subject: [PATCH 1/5] feat: Add GatedGraphConv support for HeteroGraphConv --- GNNlib/src/layers/conv.jl | 25 +++++++++++++------ GraphNeuralNetworks/test/layers/heteroconv.jl | 8 ++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index b62938454..295551fa4 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -215,19 +215,28 @@ end ####################### GatedGraphConv ###################################### -function gated_graph_conv(l, g::GNNGraph, x::AbstractMatrix) +function gated_graph_conv(l, g::AbstractGNNGraph, x) check_num_nodes(g, x) - m, n = size(x) + xj, xi = expand_srcdst(g, x) + + h = xi + m, n = size(h) @assert m <= l.dims "number of input features must be less or equal to output features." if m < l.dims - xpad = zeros_like(x, (l.dims - m, n)) - x = vcat(x, xpad) + xpad = zeros_like(h, (l.dims - m, n)) + h = vcat(h, xpad) end - h = x + + mj, nj = size(xj) + if mj < l.dims + xpad = zeros_like(xj, (l.dims - mj, nj)) + xj = vcat(xj, xpad) + end + for i in 1:(l.num_layers) - m = view(l.weight, :, :, i) * h - m = propagate(copy_xj, g, l.aggr; xj = m) - _, h = l.gru(m, h) + msg = view(l.weight, :, :, i) * xj + msg = propagate(copy_xj, g, l.aggr; xj = msg) + _, h = l.gru(msg, h) end return h end diff --git a/GraphNeuralNetworks/test/layers/heteroconv.jl b/GraphNeuralNetworks/test/layers/heteroconv.jl index 8b3b1b58f..a8d9bb870 100644 --- a/GraphNeuralNetworks/test/layers/heteroconv.jl +++ b/GraphNeuralNetworks/test/layers/heteroconv.jl @@ -179,4 +179,12 @@ y = HeteroGraphConv((:A, :to, :A) => AGNNConv())(hg_AA, (; A = x)) @test y.A ≈ AGNNConv()(GNNGraph(s, t), x) end + + @testset "GatedGraphConv" begin + x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3)) + layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 2), + (:B, :to, :A) => GatedGraphConv(4, 2)); + y = layers(hg, x); + @test size(y.A) == (4, 2) && size(y.B) == (4, 3) + end end From 25c11411ad82a73f70333612fff6ab2789168282 Mon Sep 17 00:00:00 2001 From: Amogh Date: Sun, 22 Mar 2026 22:05:25 +0530 Subject: [PATCH 2/5] Optimizations --- GNNlib/src/layers/conv.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index 295551fa4..12f2ec7a0 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -227,10 +227,14 @@ function gated_graph_conv(l, g::AbstractGNNGraph, x) h = vcat(h, xpad) end - mj, nj = size(xj) - if mj < l.dims - xpad = zeros_like(xj, (l.dims - mj, nj)) - xj = vcat(xj, xpad) + if xj !== xi + mj, nj = size(xj) + if mj < l.dims + xpad = zeros_like(xj, (l.dims - mj, nj)) + xj = vcat(xj, xpad) + end + else + xj = h end for i in 1:(l.num_layers) From 0f0198bbaa1c54b83206b5592514076fa083d80d Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Sun, 29 Nov 2026 09:31:26 +0100 Subject: [PATCH 3/5] Fix GatedGraphConv hidden-state recursion for num_layers > 1 The heterograph change built the messages of every step from the initial padded features, so homogeneous inputs with num_layers > 1 no longer matched the GGNN recursion. Messages again come from the evolving hidden state, unless distinct source features are given (e.g. another node type): those have no hidden state in the relation and stay fixed, padded to `dims`. Source features are now also checked against `dims`. Co-Authored-By: Claude Opus 5.5 --- GNNlib/src/layers/conv.jl | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index 12f2ec7a0..cec87d8e3 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -215,32 +215,23 @@ end ####################### GatedGraphConv ###################################### +function _gated_graph_pad(x, dims) + m, n = size(x) + @assert m <= dims "number of input features must be less or equal to output features." + return m < dims ? vcat(x, zeros_like(x, (dims - m, n))) : x +end + function gated_graph_conv(l, g::AbstractGNNGraph, x) check_num_nodes(g, x) xj, xi = expand_srcdst(g, x) - - h = xi - m, n = size(h) - @assert m <= l.dims "number of input features must be less or equal to output features." - if m < l.dims - xpad = zeros_like(h, (l.dims - m, n)) - h = vcat(h, xpad) - end - - if xj !== xi - mj, nj = size(xj) - if mj < l.dims - xpad = zeros_like(xj, (l.dims - mj, nj)) - xj = vcat(xj, xpad) - end - else - xj = h - end - + h = _gated_graph_pad(xi, l.dims) + # Distinct source features (e.g. another node type) have no hidden state in the recursion. + fixed_src = xj !== xi + hj = fixed_src ? _gated_graph_pad(xj, l.dims) : h for i in 1:(l.num_layers) - msg = view(l.weight, :, :, i) * xj - msg = propagate(copy_xj, g, l.aggr; xj = msg) - _, h = l.gru(msg, h) + m = view(l.weight, :, :, i) * (fixed_src ? hj : h) + m = propagate(copy_xj, g, l.aggr; xj = m) + _, h = l.gru(m, h) end return h end From 651c7bddacf59edb716cb62bcad42ad642d83b0f Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Sun, 29 Nov 2026 09:31:26 +0100 Subject: [PATCH 4/5] Document GatedGraphConv tuple input and heterograph support Co-Authored-By: Claude Opus 5.5 --- GNNLux/docs/src/api/conv.md | 2 +- GNNLux/src/layers/conv.jl | 17 +++++++++++++++++ GraphNeuralNetworks/docs/src/api/conv.md | 2 +- GraphNeuralNetworks/src/layers/conv.jl | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/GNNLux/docs/src/api/conv.md b/GNNLux/docs/src/api/conv.md index 2e4a00b9f..ae106648a 100644 --- a/GNNLux/docs/src/api/conv.md +++ b/GNNLux/docs/src/api/conv.md @@ -24,7 +24,7 @@ The table below lists all graph convolutional layers implemented in the *GNNLux. | [`EdgeConv`](@ref) | | | | ✓ | | | [`GATConv`](@ref) | | | ✓ | ✓ | ✓ | | [`GATv2Conv`](@ref) | | | ✓ | ✓ | ✓ | -| [`GatedGraphConv`](@ref) | ✓ | | | | ✓ | +| [`GatedGraphConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GCNConv`](@ref) | ✓ | ✓ | | ✓ | | | [`GINConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GMMConv`](@ref) | | | ✓ | | | diff --git a/GNNLux/src/layers/conv.jl b/GNNLux/src/layers/conv.jl index f46cd01e6..6a7be0664 100644 --- a/GNNLux/src/layers/conv.jl +++ b/GNNLux/src/layers/conv.jl @@ -1232,6 +1232,23 @@ where ``\mathbf{h}^{(l)}_i`` denotes the ``l``-th hidden variables passing throu - `aggr`: Aggregation operator for the incoming messages (e.g. `+`, `*`, `max`, `min`, and `mean`). - `init_weight`: Weights' initializer. Default `glorot_uniform`. +# Forward + + (::GatedGraphConv)(g, x, ps, st) -> (y, st) + +- `g`: A `GNNGraph`, or a `GNNHeteroGraph` with a single edge type. +- `x`: A node feature matrix of size `(in, num_nodes)` with `in <= out`, + or a tuple `(xj, xi)` of source and target node features of sizes `(in_j, num_src_nodes)` and `(in_i, num_dst_nodes)`, + with `in_j, in_i <= out`. +- `ps`, `st`: The parameters and state of the layer. + +Returns the target node features `y` of size `(out, num_dst_nodes)` and the state. + +When `xj` and `xi` are distinct arrays (e.g. source and target nodes of different types), +the source nodes have no hidden state evolving in the recursion: their padded features ``[\mathbf{x}_j; \mathbf{0}]`` +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. +If `xj` and `xi` are the same array, the source states evolve as well, as in the homogeneous case. + # Examples: ```julia diff --git a/GraphNeuralNetworks/docs/src/api/conv.md b/GraphNeuralNetworks/docs/src/api/conv.md index 41836f136..fc89b8f48 100644 --- a/GraphNeuralNetworks/docs/src/api/conv.md +++ b/GraphNeuralNetworks/docs/src/api/conv.md @@ -25,7 +25,7 @@ The table below lists all graph convolutional layers implemented in the *GraphNe | [`EdgeConv`](@ref) | | | | ✓ | | | [`GATConv`](@ref) | | | ✓ | ✓ | ✓ | | [`GATv2Conv`](@ref) | | | ✓ | ✓ | ✓ | -| [`GatedGraphConv`](@ref) | ✓ | | | | ✓ | +| [`GatedGraphConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GCNConv`](@ref) | ✓ | ✓ | | ✓ | | | [`GINConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GMMConv`](@ref) | | | ✓ | | | diff --git a/GraphNeuralNetworks/src/layers/conv.jl b/GraphNeuralNetworks/src/layers/conv.jl index bd0c78aa6..2bbba36b9 100644 --- a/GraphNeuralNetworks/src/layers/conv.jl +++ b/GraphNeuralNetworks/src/layers/conv.jl @@ -495,6 +495,23 @@ where ``\mathbf{h}^{(l)}_i`` denotes the ``l``-th hidden variables passing throu - `aggr`: Aggregation operator for the incoming messages (e.g. `+`, `*`, `max`, `min`, and `mean`). - `init`: Weight initialization function. +# Forward + + (::GatedGraphConv)(g, x) -> AbstractMatrix + +- `g`: A `GNNGraph`, or a `GNNHeteroGraph` with a single edge type (e.g. within a [`HeteroGraphConv`](@ref)). +- `x`: A node feature matrix of size `(in, num_nodes)` with `in <= out`, + or a tuple `(xj, xi)` of source and target node features of sizes `(in_j, num_src_nodes)` and `(in_i, num_dst_nodes)`, + with `in_j, in_i <= out`. + +Returns the target node features of size `(out, num_dst_nodes)`. + +When `xj` and `xi` are distinct arrays (e.g. source and target nodes of different types), +the source nodes have no hidden state evolving in the recursion: their padded features ``[\mathbf{x}_j; \mathbf{0}]`` +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. +If `xj` and `xi` are the same array (as passed by [`HeteroGraphConv`](@ref) for relations whose source and target +node types coincide), the source states evolve as well, as in the homogeneous case. + # Examples: ```julia From 436170555e0395470fc8a23431685648b745cb82 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Sun, 29 Nov 2026 09:31:26 +0100 Subject: [PATCH 5/5] 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 --- GNNLux/test/layers/conv.jl | 6 +++ GraphNeuralNetworks/test/layers/conv.jl | 15 +++++++ GraphNeuralNetworks/test/layers/heteroconv.jl | 41 ++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/GNNLux/test/layers/conv.jl b/GNNLux/test/layers/conv.jl index 27e5d299d..a44e0df77 100644 --- a/GNNLux/test/layers/conv.jl +++ b/GNNLux/test/layers/conv.jl @@ -115,6 +115,12 @@ end @testset "GatedGraphConv" begin l = GatedGraphConv(in_dims, 3) test_lux_layer(rng, l, g, x, outputsize=(in_dims,)) + + ps, st = LuxCore.setup(rng, l) + @test l(g, (x, x), ps, st)[1] == l(g, x, ps, st)[1] + sg = edge_type_subgraph(rand_bipartite_heterograph(rng, (4, 6), 10), (:A, :to, :B)) + y, _ = l(sg, (randn(rng, Float32, 2, 4), randn(rng, Float32, in_dims, 6)), ps, st) + @test size(y) == (in_dims, 6) end @testset "GINConv" begin diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index bd3a94318..bcb62f8ca 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -241,6 +241,21 @@ end @test size(l(g, g.x)) == (D_OUT, g.num_nodes) test_gradients(l, g, g.x, rtol = RTOL_HIGH) end + + @testset "reference" begin + for g in TEST_GRAPHS + s, t = edge_index(g) + h = vcat(g.x, zeros(Float32, D_OUT - D_IN, g.num_nodes)) + for k in 1:num_layers + m = zeros(Float32, D_OUT, g.num_nodes) + for (j, i) in zip(s, t) + m[:, i] .+= l.weight[:, :, k] * h[:, j] + end + _, h = l.gru(m, h) + end + @test l(g, g.x) ≈ h + end + end end diff --git a/GraphNeuralNetworks/test/layers/heteroconv.jl b/GraphNeuralNetworks/test/layers/heteroconv.jl index a8d9bb870..5614fcbd1 100644 --- a/GraphNeuralNetworks/test/layers/heteroconv.jl +++ b/GraphNeuralNetworks/test/layers/heteroconv.jl @@ -181,10 +181,41 @@ end @testset "GatedGraphConv" begin - x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3)) - layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 2), - (:B, :to, :A) => GatedGraphConv(4, 2)); - y = layers(hg, x); - @test size(y.A) == (4, 2) && size(y.B) == (4, 3) + function gated_ref(l, s, t, xj, xi; evolve_src) + pad(x) = vcat(x, zeros(Float32, l.dims - size(x, 1), size(x, 2))) + h, hj = pad(xi), pad(xj) + for k in 1:l.num_layers + src = evolve_src ? h : hj + m = zeros(Float32, l.dims, size(h, 2)) + for (j, i) in zip(s, t) + m[:, i] .+= l.weight[:, :, k] * src[:, j] + end + _, h = l.gru(m, h) + end + return h + end + + eAB = ([1, 1, 2, 3], [1, 2, 2, 4]) + eBA = ([1, 2, 4, 4], [1, 1, 3, 2]) + eAA = ([1, 2, 3], [2, 3, 1]) + g = GNNHeteroGraph(((:A, :to, :B) => eAB, (:B, :to, :A) => eBA, (:A, :to, :A) => eAA); + num_nodes = Dict(:A => 3, :B => 4)) + x = (A = rand(Float32, 3, 3), B = rand(Float32, 2, 4)) + layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 3), + (:B, :to, :A) => GatedGraphConv(4, 3), + (:A, :to, :A) => GatedGraphConv(4, 3)) + y = layers(g, x) + @test size(y.A) == (4, 3) && size(y.B) == (4, 4) + + lAB, lBA, lAA = layers.layers + @test y.B ≈ gated_ref(lAB, eAB..., x.A, x.B; evolve_src = false) + @test y.A ≈ gated_ref(lBA, eBA..., x.B, x.A; evolve_src = false) + + gated_ref(lAA, eAA..., x.A, x.A; evolve_src = true) + + loss(x) = sum(layers(g, x).A) + sum(abs2, layers(g, x).B) + dx = gradient(loss, x)[1] + _, ndx = finitediff_withgradient(loss, x) + @test dx.A ≈ ndx[1].A rtol=1e-3 + @test dx.B ≈ ndx[1].B rtol=1e-3 end end