From 8351c74a93c77d2d389c8be68b7cf3890bb0e1d4 Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Sat, 17 Jan 2026 22:03:21 -0800 Subject: [PATCH 1/5] fix: Gate empty result in GATConv --- GNNlib/src/layers/conv.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index 8297734dc..2e8262ff7 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -139,11 +139,12 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = α = dropout(α, l.dropout) β = α .* m.Wxj x = aggregate_neighbors(g, +, β) + width = size(x, 1) if !l.concat x = mean(x, dims = 2) end - x = reshape(x, :, size(x, 3)) # return a matrix + x = reshape(x, width, size(x, 3)) # return a matrix x = l.σ.(x .+ l.bias) return x From 1f8c60a27ca6c1f49b40b7941d4da0714359f1fc Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Sun, 18 Jan 2026 00:44:22 -0800 Subject: [PATCH 2/5] fix: Handle multiple heads --- GNNlib/src/layers/conv.jl | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index 2e8262ff7..8f4d9ec41 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -124,13 +124,10 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = _, chout = l.channel heads = l.heads - Wxi = Wxj = l.dense_x(xj) - Wxi = Wxj = reshape(Wxj, chout, heads, :) - - if xi !== xj - Wxi = l.dense_x(xi) - Wxi = reshape(Wxi, chout, heads, :) - end + Wxj = l.dense_x(xj) + Wxj = reshape(Wxj, chout, heads, :) + Wxi = l.dense_x(xi) + Wxi = reshape(Wxi, chout, heads, :) # a hand-written message passing message = Fix1(gat_message, l) @@ -139,10 +136,12 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = α = dropout(α, l.dropout) β = α .* m.Wxj x = aggregate_neighbors(g, +, β) - width = size(x, 1) if !l.concat x = mean(x, dims = 2) + width = size(x, 1) + else + width = size(x, 1) * size(x, 2) end x = reshape(x, width, size(x, 3)) # return a matrix x = l.σ.(x .+ l.bias) @@ -195,8 +194,11 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} if !l.concat x = mean(x, dims = 2) + width = size(x, 1) + else + width = size(x, 1) * size(x, 2) end - x = reshape(x, :, size(x, 3)) + x = reshape(x, width, size(x, 3)) x = l.σ.(x .+ l.bias) return x end From 279f59237f94a2fab0e51592b6c0055f01c437a5 Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Wed, 2 Sep 2026 23:01:27 -0700 Subject: [PATCH 3/5] fix: Construct stack size --- GNNlib/src/layers/conv.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index 8f4d9ec41..791869d00 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -121,13 +121,15 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = g = add_self_loops(g) end + _, n_stackj = size(xj) + _, n_stacki = size(xi) _, chout = l.channel heads = l.heads Wxj = l.dense_x(xj) - Wxj = reshape(Wxj, chout, heads, :) + Wxj = reshape(Wxj, chout, heads, n_stackj) Wxi = l.dense_x(xi) - Wxi = reshape(Wxi, chout, heads, :) + Wxi = reshape(Wxi, chout, heads, n_stacki) # a hand-written message passing message = Fix1(gat_message, l) From 270f97437e3ae25a3b7f1d5ee2dbb7e91eb96acd Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Fri, 6 Nov 2026 15:33:03 -0800 Subject: [PATCH 4/5] Add unit test --- GNNlib/src/layers/conv.jl | 26 +++++++++++++------ GraphNeuralNetworks/test/layers/heteroconv.jl | 22 ++++++++++++++-- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index 791869d00..e4cc03dee 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -134,10 +134,15 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = # a hand-written message passing message = Fix1(gat_message, l) m = apply_edges(message, g, Wxi, Wxj, e) - α = softmax_edge_neighbors(g, m.logα) - α = dropout(α, l.dropout) - β = α .* m.Wxj - x = aggregate_neighbors(g, +, β) + if isempty(m.logα) + # no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays + x = zero(Wxi) + else + α = softmax_edge_neighbors(g, m.logα) + α = dropout(α, l.dropout) + β = α .* m.Wxj + x = aggregate_neighbors(g, +, β) + end if !l.concat x = mean(x, dims = 2) @@ -189,10 +194,15 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} message = Fix1(gatv2_message, l) m = apply_edges(message, g, Wxi, Wxj, e) - α = softmax_edge_neighbors(g, m.logα) - α = dropout(α, l.dropout) - β = α .* m.Wxj - x = aggregate_neighbors(g, +, β) + if isempty(m.logα) + # no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays + x = zero(Wxi) + else + α = softmax_edge_neighbors(g, m.logα) + α = dropout(α, l.dropout) + β = α .* m.Wxj + x = aggregate_neighbors(g, +, β) + end if !l.concat x = mean(x, dims = 2) diff --git a/GraphNeuralNetworks/test/layers/heteroconv.jl b/GraphNeuralNetworks/test/layers/heteroconv.jl index fe9a5bc38..b9e74d0ac 100644 --- a/GraphNeuralNetworks/test/layers/heteroconv.jl +++ b/GraphNeuralNetworks/test/layers/heteroconv.jl @@ -146,10 +146,28 @@ x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3)) layers = HeteroGraphConv((:A, :to, :B) => GATv2Conv(4 => 2), (:B, :to, :A) => GATv2Conv(4 => 2)); - y = layers(hg, x); + y = layers(hg, x); @test size(y.A) == (2, 2) && size(y.B) == (2, 3) end - + + @testset "$L with empty relations" for L in (GATConv, GATv2Conv) + g = GNNHeteroGraph(((:A, :to, :B) => ([1, 2], [3, 4]), + (:B, :to, :A) => (Int[], Int[]), + (:C, :to, :A) => (Int[], Int[]), + (:A, :to, :C) => (Int[], Int[])); + num_nodes = Dict(:A => 3, :B => 5, :C => 0)) + x = (A = rand(Float32, 4, 3), B = rand(Float32, 4, 5), C = rand(Float32, 4, 0)) + layers = HeteroGraphConv([et => L(4 => 2; heads = 2, dropout = 0) for et in g.etypes]) + y = layers(g, x) + @test size(y.A) == (4, 3) && size(y.B) == (4, 5) && size(y.C) == (4, 0) + @test y.A == zeros(Float32, 4, 3) + + f(x) = sum(sum, layers(g, x)) + dx = gradient(f, x)[1] + _, (ndx,) = finitediff_withgradient(f, x) + @test dx.A ≈ ndx.A rtol = 1e-3 + end + @testset "GCNConv" begin g = rand_bipartite_heterograph((2,3), 6) x = (A = rand(Float32, 4,2), B = rand(Float32, 4, 3)) From ef9ca358e57a60e15071c4d503cca35f4db62762 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Sun, 29 Nov 2026 15:11:05 +0100 Subject: [PATCH 5/5] Apply dense_x once on homogeneous GATConv inputs, test edgeless graphs Choose shared vs. separate source/destination projections by dispatch on the input type: this avoids the `xi !== xj` branch that Zygote < 0.7.17 differentiates wrongly, without computing dense_x twice on homogeneous graphs. Drop the explicit stack sizes, which `reshape(_, chout, heads, :)` already handles for zero nodes, and add tests on graphs without edges. Co-Authored-By: Claude Opus 5.5 --- GNNlib/src/layers/conv.jl | 23 ++++--------------- GNNlib/src/utils.jl | 6 +++++ GraphNeuralNetworks/test/layers/conv.jl | 14 +++++++++++ GraphNeuralNetworks/test/layers/heteroconv.jl | 4 ++-- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index e4cc03dee..aff802a0b 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -114,28 +114,21 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = @assert !((e === nothing) && (l.dense_e !== nothing)) "Input edge features required for this layer" @assert !((e !== nothing) && (l.dense_e === nothing)) "Input edge features were not specified in the layer constructor" - xj, xi = expand_srcdst(g, x) - if l.add_self_loops @assert e===nothing "Using edge features and setting add_self_loops=true at the same time is not yet supported." g = add_self_loops(g) end - _, n_stackj = size(xj) - _, n_stacki = size(xi) _, chout = l.channel heads = l.heads - Wxj = l.dense_x(xj) - Wxj = reshape(Wxj, chout, heads, n_stackj) - Wxi = l.dense_x(xi) - Wxi = reshape(Wxi, chout, heads, n_stacki) + Wxj, Wxi = map_srcdst(y -> reshape(l.dense_x(y), chout, heads, :), g, x) # a hand-written message passing message = Fix1(gat_message, l) m = apply_edges(message, g, Wxi, Wxj, e) if isempty(m.logα) - # no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays + # Zygote's broadcast pullback mis-sizes gradients of empty arrays (FluxML/Zygote.jl#1530) x = zero(Wxi) else α = softmax_edge_neighbors(g, m.logα) @@ -146,11 +139,8 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} = if !l.concat x = mean(x, dims = 2) - width = size(x, 1) - else - width = size(x, 1) * size(x, 2) end - x = reshape(x, width, size(x, 3)) # return a matrix + x = reshape(x, size(x, 1) * size(x, 2), :) # return a matrix x = l.σ.(x .+ l.bias) return x @@ -195,7 +185,7 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} message = Fix1(gatv2_message, l) m = apply_edges(message, g, Wxi, Wxj, e) if isempty(m.logα) - # no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays + # Zygote's broadcast pullback mis-sizes gradients of empty arrays (FluxML/Zygote.jl#1530) x = zero(Wxi) else α = softmax_edge_neighbors(g, m.logα) @@ -206,11 +196,8 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} if !l.concat x = mean(x, dims = 2) - width = size(x, 1) - else - width = size(x, 1) * size(x, 2) end - x = reshape(x, width, size(x, 3)) + x = reshape(x, size(x, 1) * size(x, 2), :) x = l.σ.(x .+ l.bias) return x end diff --git a/GNNlib/src/utils.jl b/GNNlib/src/utils.jl index 4967e151f..dbe31f7d0 100644 --- a/GNNlib/src/utils.jl +++ b/GNNlib/src/utils.jl @@ -138,6 +138,12 @@ expand_srcdst(g::AbstractGNNGraph, x) = throw(ArgumentError("Invalid input type, expand_srcdst(g::AbstractGNNGraph, x::AbstractMatrix) = (x, x) expand_srcdst(g::AbstractGNNGraph, x::Tuple{<:AbstractMatrix, <:AbstractMatrix}) = x +# Like `map(f, expand_srcdst(g, x))`, but applies `f` only once to shared features. +# Dispatches instead of branching on `xj === xi`: Zygote < 0.7.17 mixes up +# the cotangents of such a branch (FluxML/Zygote.jl#1747). +map_srcdst(f, g::AbstractGNNGraph, x) = map(f, expand_srcdst(g, x)) +map_srcdst(f, g::AbstractGNNGraph, x::AbstractMatrix) = (y = f(x); (y, y)) + # Replacement for Base.Fix1 to allow for multiple arguments struct Fix1{F,X} f::F diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index 7eeaddc94..c09fb70ba 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -190,6 +190,13 @@ end test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW) end + @testset "no edges" begin + g = GNNGraph(Int[], Int[]; num_nodes = 3, ndata = rand(Float32, D_IN, 3)) + l = GATConv(D_IN => D_OUT; heads = 2, add_self_loops = false) + @test l(g, g.x) == zeros(Float32, 2 * D_OUT, 3) + test_gradients(l, g, g.x, rtol = RTOL_LOW) + end + @testset "num params" begin l = GATConv(2 => 3, add_self_loops = false) @test length(Flux.trainables(l)) == 3 @@ -230,6 +237,13 @@ end test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, atol=ATOL_LOW) end + @testset "no edges" begin + g = GNNGraph(Int[], Int[]; num_nodes = 3, ndata = rand(Float32, D_IN, 3)) + l = GATv2Conv(D_IN => D_OUT; heads = 2, add_self_loops = false) + @test l(g, g.x) == zeros(Float32, 2 * D_OUT, 3) + test_gradients(l, g, g.x, rtol = RTOL_LOW, atol = ATOL_LOW) + end + @testset "num params" begin l = GATv2Conv(2 => 3, add_self_loops = false) @test length(Flux.trainables(l)) == 5 diff --git a/GraphNeuralNetworks/test/layers/heteroconv.jl b/GraphNeuralNetworks/test/layers/heteroconv.jl index b9e74d0ac..497f06298 100644 --- a/GraphNeuralNetworks/test/layers/heteroconv.jl +++ b/GraphNeuralNetworks/test/layers/heteroconv.jl @@ -157,10 +157,10 @@ (:A, :to, :C) => (Int[], Int[])); num_nodes = Dict(:A => 3, :B => 5, :C => 0)) x = (A = rand(Float32, 4, 3), B = rand(Float32, 4, 5), C = rand(Float32, 4, 0)) - layers = HeteroGraphConv([et => L(4 => 2; heads = 2, dropout = 0) for et in g.etypes]) + layers = HeteroGraphConv([et => L(4 => 2; heads = 2) for et in g.etypes]) y = layers(g, x) @test size(y.A) == (4, 3) && size(y.B) == (4, 5) && size(y.C) == (4, 0) - @test y.A == zeros(Float32, 4, 3) + @test y.A == zeros(Float32, 4, 3) # only edgeless relations point to A f(x) = sum(sum, layers(g, x)) dx = gradient(f, x)[1]