Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions GNNlib/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ 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)
Expand All @@ -124,26 +122,25 @@ 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, 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)
α = softmax_edge_neighbors(g, m.logα)
α = dropout(α, l.dropout)
β = α .* m.Wxj
x = aggregate_neighbors(g, +, β)
if isempty(m.logα)
# Zygote's broadcast pullback mis-sizes gradients of empty arrays (FluxML/Zygote.jl#1530)
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)
end
x = reshape(x, :, 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
Expand Down Expand Up @@ -187,15 +184,20 @@ 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α)
# Zygote's broadcast pullback mis-sizes gradients of empty arrays (FluxML/Zygote.jl#1530)
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)
end
x = reshape(x, :, size(x, 3))
x = reshape(x, size(x, 1) * size(x, 2), :)
x = l.σ.(x .+ l.bias)
return x
end
Expand Down
6 changes: 6 additions & 0 deletions GNNlib/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions GraphNeuralNetworks/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions GraphNeuralNetworks/test/layers/heteroconv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) 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) # only edgeless relations point to A

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))
Expand Down