Skip to content

Commit 8ec36fe

Browse files
committed
Add unit test
1 parent d88f6e3 commit 8ec36fe

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1010
and the packages adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111
Entries link to the pull request that introduced them.
1212

13+
## GNNlib.jl Unreleased
14+
15+
**Fixed**
16+
- `GATConv` and `GATv2Conv` no longer fail on graphs (or heterograph relations) without edges for some edge categories, or with zero source/destination nodes: the forward pass crashed reshaping the empty output, and the gradient failed with a `DimensionMismatch`.
17+
1318
## GNNGraphs.jl 1.6.0 - 2026-11-04
1419

1520
**Added**

‎GNNlib/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GNNlib"
22
uuid = "a6a84749-d869-43f8-aacc-be26a1996e48"
33
authors = ["Carlo Lucibello and contributors"]
4-
version = "1.4.1"
4+
version = "1.4.2"
55

66
[workspace]
77
projects = ["test", "docs", "../GNNGraphs"]

‎GNNlib/src/layers/conv.jl‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,15 @@ function gat_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix} =
134134
# a hand-written message passing
135135
message = Fix1(gat_message, l)
136136
m = apply_edges(message, g, Wxi, Wxj, e)
137-
α = softmax_edge_neighbors(g, m.logα)
138-
α = dropout(α, l.dropout)
139-
β = α .* m.Wxj
140-
x = aggregate_neighbors(g, +, β)
137+
if isempty(m.logα)
138+
# no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays
139+
x = zero(Wxi)
140+
else
141+
α = softmax_edge_neighbors(g, m.logα)
142+
α = dropout(α, l.dropout)
143+
β = α .* m.Wxj
144+
x = aggregate_neighbors(g, +, β)
145+
end
141146

142147
if !l.concat
143148
x = mean(x, dims = 2)
@@ -189,10 +194,15 @@ function gatv2_conv(l, g::AbstractGNNGraph, x, e::Union{Nothing, AbstractMatrix}
189194

190195
message = Fix1(gatv2_message, l)
191196
m = apply_edges(message, g, Wxi, Wxj, e)
192-
α = softmax_edge_neighbors(g, m.logα)
193-
α = dropout(α, l.dropout)
194-
β = α .* m.Wxj
195-
x = aggregate_neighbors(g, +, β)
197+
if isempty(m.logα)
198+
# no edges: Zygote computes a wrong-sized gradient for `α .* m.Wxj` on empty arrays
199+
x = zero(Wxi)
200+
else
201+
α = softmax_edge_neighbors(g, m.logα)
202+
α = dropout(α, l.dropout)
203+
β = α .* m.Wxj
204+
x = aggregate_neighbors(g, +, β)
205+
end
196206

197207
if !l.concat
198208
x = mean(x, dims = 2)

‎GraphNeuralNetworks/test/layers/heteroconv.jl‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,28 @@
146146
x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3))
147147
layers = HeteroGraphConv((:A, :to, :B) => GATv2Conv(4 => 2),
148148
(:B, :to, :A) => GATv2Conv(4 => 2));
149-
y = layers(hg, x);
149+
y = layers(hg, x);
150150
@test size(y.A) == (2, 2) && size(y.B) == (2, 3)
151151
end
152-
152+
153+
@testset "$L with empty relations" for L in (GATConv, GATv2Conv)
154+
g = GNNHeteroGraph(((:A, :to, :B) => ([1, 2], [3, 4]),
155+
(:B, :to, :A) => (Int[], Int[]),
156+
(:C, :to, :A) => (Int[], Int[]),
157+
(:A, :to, :C) => (Int[], Int[]));
158+
num_nodes = Dict(:A => 3, :B => 5, :C => 0))
159+
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])
161+
y = layers(g, x)
162+
@test size(y.A) == (4, 3) && size(y.B) == (4, 5) && size(y.C) == (4, 0)
163+
@test y.A == zeros(Float32, 4, 3)
164+
165+
f(x) = sum(sum, layers(g, x))
166+
dx = gradient(f, x)[1]
167+
_, (ndx,) = finitediff_withgradient(f, x)
168+
@test dx.A ≈ ndx.A rtol = 1e-3
169+
end
170+
153171
@testset "GCNConv" begin
154172
g = rand_bipartite_heterograph((2,3), 6)
155173
x = (A = rand(Float32, 4,2), B = rand(Float32, 4, 3))

0 commit comments

Comments
 (0)