Skip to content

Commit ef9ca35

Browse files
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 <noreply@anthropic.com>
1 parent 270f974 commit ef9ca35

4 files changed

Lines changed: 27 additions & 20 deletions

File tree

‎GNNlib/src/layers/conv.jl‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,21 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} =
114114
@assert !((e === nothing) && (l.dense_e !== nothing)) "Input edge features required for this layer"
115115
@assert !((e !== nothing) && (l.dense_e === nothing)) "Input edge features were not specified in the layer constructor"
116116

117-
xj, xi = expand_srcdst(g, x)
118-
119117
if l.add_self_loops
120118
@assert e===nothing "Using edge features and setting add_self_loops=true at the same time is not yet supported."
121119
g = add_self_loops(g)
122120
end
123121

124-
_, n_stackj = size(xj)
125-
_, n_stacki = size(xi)
126122
_, chout = l.channel
127123
heads = l.heads
128124

129-
Wxj = l.dense_x(xj)
130-
Wxj = reshape(Wxj, chout, heads, n_stackj)
131-
Wxi = l.dense_x(xi)
132-
Wxi = reshape(Wxi, chout, heads, n_stacki)
125+
Wxj, Wxi = map_srcdst(y -> reshape(l.dense_x(y), chout, heads, :), g, x)
133126

134127
# a hand-written message passing
135128
message = Fix1(gat_message, l)
136129
m = apply_edges(message, g, Wxi, Wxj, e)
137130
if isempty(m.logα)
138-
# no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays
131+
# Zygote's broadcast pullback mis-sizes gradients of empty arrays (FluxML/Zygote.jl#1530)
139132
x = zero(Wxi)
140133
else
141134
α = softmax_edge_neighbors(g, m.logα)
@@ -146,11 +139,8 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} =
146139

147140
if !l.concat
148141
x = mean(x, dims = 2)
149-
width = size(x, 1)
150-
else
151-
width = size(x, 1) * size(x, 2)
152142
end
153-
x = reshape(x, width, size(x, 3)) # return a matrix
143+
x = reshape(x, size(x, 1) * size(x, 2), :) # return a matrix
154144
x = l.σ.(x .+ l.bias)
155145

156146
return x
@@ -195,7 +185,7 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix}
195185
message = Fix1(gatv2_message, l)
196186
m = apply_edges(message, g, Wxi, Wxj, e)
197187
if isempty(m.logα)
198-
# no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays
188+
# Zygote's broadcast pullback mis-sizes gradients of empty arrays (FluxML/Zygote.jl#1530)
199189
x = zero(Wxi)
200190
else
201191
α = softmax_edge_neighbors(g, m.logα)
@@ -206,11 +196,8 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix}
206196

207197
if !l.concat
208198
x = mean(x, dims = 2)
209-
width = size(x, 1)
210-
else
211-
width = size(x, 1) * size(x, 2)
212199
end
213-
x = reshape(x, width, size(x, 3))
200+
x = reshape(x, size(x, 1) * size(x, 2), :)
214201
x = l.σ.(x .+ l.bias)
215202
return x
216203
end

‎GNNlib/src/utils.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ expand_srcdst(g::AbstractGNNGraph, x) = throw(ArgumentError("Invalid input type,
138138
expand_srcdst(g::AbstractGNNGraph, x::AbstractMatrix) = (x, x)
139139
expand_srcdst(g::AbstractGNNGraph, x::Tuple{<:AbstractMatrix, <:AbstractMatrix}) = x
140140

141+
# Like `map(f, expand_srcdst(g, x))`, but applies `f` only once to shared features.
142+
# Dispatches instead of branching on `xj === xi`: Zygote < 0.7.17 mixes up
143+
# the cotangents of such a branch (FluxML/Zygote.jl#1747).
144+
map_srcdst(f, g::AbstractGNNGraph, x) = map(f, expand_srcdst(g, x))
145+
map_srcdst(f, g::AbstractGNNGraph, x::AbstractMatrix) = (y = f(x); (y, y))
146+
141147
# Replacement for Base.Fix1 to allow for multiple arguments
142148
struct Fix1{F,X}
143149
f::F

‎GraphNeuralNetworks/test/layers/conv.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ end
190190
test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW)
191191
end
192192

193+
@testset "no edges" begin
194+
g = GNNGraph(Int[], Int[]; num_nodes = 3, ndata = rand(Float32, D_IN, 3))
195+
l = GATConv(D_IN => D_OUT; heads = 2, add_self_loops = false)
196+
@test l(g, g.x) == zeros(Float32, 2 * D_OUT, 3)
197+
test_gradients(l, g, g.x, rtol = RTOL_LOW)
198+
end
199+
193200
@testset "num params" begin
194201
l = GATConv(2 => 3, add_self_loops = false)
195202
@test length(Flux.trainables(l)) == 3
@@ -230,6 +237,13 @@ end
230237
test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, atol=ATOL_LOW)
231238
end
232239

240+
@testset "no edges" begin
241+
g = GNNGraph(Int[], Int[]; num_nodes = 3, ndata = rand(Float32, D_IN, 3))
242+
l = GATv2Conv(D_IN => D_OUT; heads = 2, add_self_loops = false)
243+
@test l(g, g.x) == zeros(Float32, 2 * D_OUT, 3)
244+
test_gradients(l, g, g.x, rtol = RTOL_LOW, atol = ATOL_LOW)
245+
end
246+
233247
@testset "num params" begin
234248
l = GATv2Conv(2 => 3, add_self_loops = false)
235249
@test length(Flux.trainables(l)) == 5

‎GraphNeuralNetworks/test/layers/heteroconv.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@
157157
(:A, :to, :C) => (Int[], Int[]));
158158
num_nodes = Dict(:A => 3, :B => 5, :C => 0))
159159
x = (A = rand(Float32, 4, 3), B = rand(Float32, 4, 5), C = rand(Float32, 4, 0))
160-
layers = HeteroGraphConv([et => L(4 => 2; heads = 2, dropout = 0) for et in g.etypes])
160+
layers = HeteroGraphConv([et => L(4 => 2; heads = 2) for et in g.etypes])
161161
y = layers(g, x)
162162
@test size(y.A) == (4, 3) && size(y.B) == (4, 5) && size(y.C) == (4, 0)
163-
@test y.A == zeros(Float32, 4, 3)
163+
@test y.A == zeros(Float32, 4, 3) # only edgeless relations point to A
164164

165165
f(x) = sum(sum, layers(g, x))
166166
dx = gradient(f, x)[1]

0 commit comments

Comments
 (0)