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
4 changes: 3 additions & 1 deletion GNNlib/test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
Expand All @@ -22,4 +23,5 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
GNNlib = {path = ".."}

[compat]
NNlib = "0.9.38"
Enzyme = "0.13.218"
NNlib = "0.9.45"
2 changes: 1 addition & 1 deletion GNNlib/test/msgpass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ end
@testset "copy_xj +" begin
for g in TEST_GRAPHS
f(g, x) = propagate(copy_xj, g, +, xj = x)
test_gradients(f, g, g.x; test_grad_f=false)
test_gradients(f, g, g.x; test_grad_f=false, enzyme_dense = false)
end
end

Expand Down
28 changes: 24 additions & 4 deletions GNNlib/test/test_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Reexport: @reexport
@reexport using SparseArrays
@reexport using Test, Random, Statistics
@reexport using MLDataDevices
using Functors: fmapstructure_with_path
using Functors: fmap, fmapstructure_with_path
using FiniteDifferences: FiniteDifferences
using Zygote: Zygote
using Flux: Flux
Expand All @@ -48,9 +48,16 @@ if TEST_MOONCAKE
import Mooncake
end

# Enzyme runs only in the CI job on the latest Julia release, to keep the 1.10
# job's run time down.
const TEST_ENZYME = VERSION >= v"1.12"
if TEST_ENZYME
import Enzyme # loads FluxEnzymeExt, which backs Flux.AutoEnzyme()
end

# from this module
export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS,
test_gradients, finitediff_withgradient,
test_gradients, finitediff_withgradient,
check_equal_leaves, gpu_backend, TEST_MOONCAKE


Expand Down Expand Up @@ -95,15 +102,25 @@ function test_gradients(
test_grad_x = true,
# Reference AD: finite differences for CPU tests, Zygote (on CPU) for GPU tests.
reference = test_gpu ? Flux.AutoZygote() : :finitediff,
ad_backends = test_gpu ? [] : [Flux.AutoZygote(), Flux.AutoMooncake()],
ad_backends = test_gpu ? [] :
[Flux.AutoZygote(), Flux.AutoMooncake(), Flux.AutoEnzyme()],
loss = (f, g, xs...) -> mean(f(g, xs...)),
# false skips Enzyme on :dense graphs, where `propagate(copy_xj, g, +)`
# multiplies by the union-typed `adjacency_matrix(g; weighted = false)`
# and Enzyme throws an IllegalTypeAnalysisException.
enzyme_dense = true,
)

# Mooncake requires Julia >= 1.12 and errors on sparse graph internals.
if !TEST_MOONCAKE || graph.graph isa AbstractSparseMatrix
ad_backends = filter(b -> !(b isa Flux.AutoMooncake), ad_backends)
end

# Other layers Enzyme cannot handle opt out per call site via `ad_backends`.
if !TEST_ENZYME || (!enzyme_dense && get_graph_type(graph) == :dense)
ad_backends = filter(b -> !(b isa Flux.AutoEnzyme), ad_backends)
end

## Let's make sure first that the forward pass works.
l = loss(f, graph, xs...)
@assert l isa Number
Expand Down Expand Up @@ -149,7 +166,10 @@ function test_gradients(
f64 = f |> Flux.f64
ps, re = Flux.destructure(f64)
y_fd, g_fd = finitediff_withgradient(ps -> loss(re(ps), graph, xs...), ps)
y_fd, (re(g_fd[1]),)
# Rebuild from a zeroed model, so non-trainable arrays (e.g. BatchNorm's
# running stats) compare as zero gradients instead of as model values.
_, re0 = Flux.destructure(fmap(x -> x isa AbstractArray ? zero(x) : x, f64))
y_fd, (re0(g_fd[1]),)
else
Flux.withgradient(f -> loss(f, graph, xs...), reference, f)
end
Expand Down
4 changes: 4 additions & 0 deletions GraphNeuralNetworks/test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
Expand All @@ -9,6 +10,7 @@ GraphNeuralNetworks = "cffab07f-9bc2-4db1-8861-388f63bf7694"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand All @@ -21,4 +23,6 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
GraphNeuralNetworks = {path = ".."}

[compat]
Enzyme = "0.13.218"
MLDatasets = "0.7"
NNlib = "0.9.45"
6 changes: 4 additions & 2 deletions GraphNeuralNetworks/test/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

Flux.testmode!(gnn)

test_gradients(gnn, g, x, rtol = 1e-5, ad_backends = [Flux.AutoZygote()])
test_gradients(gnn, g, x, rtol = 1e-5, enzyme_dense = false,
ad_backends = [Flux.AutoZygote(), Flux.AutoEnzyme()])

@testset "constructor with names" begin
m = GNNChain(GCNConv(din => d),
Expand Down Expand Up @@ -53,7 +54,8 @@

Flux.trainmode!(gnn)

test_gradients(gnn, g, x, rtol = 1e-4, atol=1e-4, ad_backends = [Flux.AutoZygote()])
test_gradients(gnn, g, x, rtol = 1e-4, atol=1e-4, enzyme_dense = false,
ad_backends = [Flux.AutoZygote(), Flux.AutoEnzyme()])
end
end

Expand Down
38 changes: 27 additions & 11 deletions GraphNeuralNetworks/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ end
for g in TEST_GRAPHS
g = add_self_loops(g)
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = [Flux.AutoZygote()])
test_gradients(l, g, g.x, rtol = RTOL_LOW,
ad_backends = [Flux.AutoZygote(), Flux.AutoEnzyme()])
end

@testset "bias=false" begin
Expand Down Expand Up @@ -145,7 +146,7 @@ end
l = GraphConv(D_IN => D_OUT)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH, enzyme_dense = false)
end

l = GraphConv(D_IN => D_OUT, tanh, bias = false, aggr = mean)
Expand Down Expand Up @@ -274,7 +275,7 @@ end

for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH, enzyme_dense = false)
end

@testset "reference" begin
Expand Down Expand Up @@ -394,7 +395,7 @@ end
l = SAGEConv(D_IN => D_OUT, tanh, bias = false, aggr = +)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH, enzyme_dense = false)
end
end

Expand Down Expand Up @@ -519,8 +520,8 @@ end
@test size(y[1]) == (D_OUT, g.num_nodes)
@test size(y[2]) == (D_OUT, g.num_edges)
function loss(l, g, x, e)
y = l(g, x, e)
return mean(y[1]) + sum(y[2])
ŷ = l(g, x, e)
return mean(ŷ[1]) + sum(ŷ[2])
end
test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss)
end
Expand Down Expand Up @@ -570,18 +571,24 @@ end

@testitem "SGConv" setup=[TolSnippet, TestModule] begin
using .TestModule
K = [1, 2, 3] # for different number of hops
K = [1, 2, 3] # for different number of hops
# Enzyme is skipped: it fails with an IllegalTypeAnalysisException on every
# storage type, because `degree(g, T; edge_weight = l.use_edge_weight)` returns
# a value whose type is only known at runtime (`Union{Nothing, AbstractVector}`)
# and the k-hop loop propagates that union through each `propagate` call.
for k in K
l = SGConv(D_IN => D_OUT, k, add_self_loops = true)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH,
ad_backends = [Flux.AutoZygote(), Flux.AutoMooncake()])
end

l = SGConv(D_IN => D_OUT, k, add_self_loops = true)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH,
ad_backends = [Flux.AutoZygote(), Flux.AutoMooncake()])
end
end
end
Expand All @@ -600,17 +607,26 @@ end
@testitem "TAGConv" setup=[TolSnippet, TestModule] begin
using .TestModule
K = [1, 2, 3]
# Enzyme runs only on :sparse graphs: the other storage types hit the same
# union-typed `degree(g, T; edge_weight = l.use_edge_weight)` problem as SGConv,
# while Enzyme's type analysis happens to recover on :sparse.
for k in K
l = TAGConv(D_IN => D_OUT, k, add_self_loops = true)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH,
ad_backends = get_graph_type(g) == :sparse ?
[Flux.AutoZygote(), Flux.AutoMooncake(), Flux.AutoEnzyme()] :
[Flux.AutoZygote(), Flux.AutoMooncake()])
end

l = TAGConv(D_IN => D_OUT, k, add_self_loops = true)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
test_gradients(l, g, g.x, rtol = RTOL_HIGH,
ad_backends = get_graph_type(g) == :sparse ?
[Flux.AutoZygote(), Flux.AutoMooncake(), Flux.AutoEnzyme()] :
[Flux.AutoZygote(), Flux.AutoMooncake()])
end
end
end
Expand Down
30 changes: 25 additions & 5 deletions GraphNeuralNetworks/test/layers/temporalconv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ end
# sigmoid gates (`Dense(_, sigmoid)`) on a fraction of inputs, so the check is
# flaky (Zygote and finite differences agree). Upstream Mooncake bug with
# NNlib.sigmoid: https://github.com/chalk-lab/Mooncake.jl/issues/1257.
# Reference against Zygote only, matching the other temporal cells.
# Enzyme is also skipped: compiling the gradient of the packaged cell hangs
# (25+ minutes with no output), so enabling it would stall the whole suite.
# Reference against Zygote only.
ad_backends = [Flux.AutoZygote()]

# Test with default activation function
Expand Down Expand Up @@ -62,7 +64,8 @@ end

# Mooncake is skipped here (see the TGCNCell test item and
# https://github.com/chalk-lab/Mooncake.jl/issues/1257): it returns wrong
# gradients through the cell's sigmoid gates on some inputs. Zygote only.
# gradients through the cell's sigmoid gates on some inputs. Enzyme is also
# skipped: it inherits TGCNCell's compile hang. Zygote only.
ad_backends = [Flux.AutoZygote()]

# Test with default activation function
Expand Down Expand Up @@ -104,6 +107,9 @@ end
@test y === h
@test size(h) == (out_channel, g.num_nodes)
@test size(c) == (out_channel, g.num_nodes)
# Enzyme is skipped for the ChebConv-based recurrent layers (GConvLSTM, GConvGRU):
# compiling their gradient fails when Enzyme links the derivative, with
# "LLVM error: Referring to an instruction in another function!". Zygote only.
# with no initial state
test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
Expand All @@ -118,6 +124,7 @@ end
state0 = (rand(Float32, out_channel, g.num_nodes), rand(Float32, out_channel, g.num_nodes))
y = layer(g, x)
@test size(y) == (out_channel, timesteps, g.num_nodes)
# Enzyme is skipped (see the GConvLSTMCell test item). Zygote only.
# with no initial state
test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
Expand All @@ -136,6 +143,7 @@ end
y, h = cell(g, g.x)
@test y === h
@test size(h) == (out_channel, g.num_nodes)
# Enzyme is skipped (see the GConvLSTMCell test item). Zygote only.
# with no initial state
test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
Expand All @@ -151,6 +159,7 @@ end
state0 = rand(Float32, out_channel, g.num_nodes)
y = layer(g, x)
@test size(y) == (out_channel, timesteps, g.num_nodes)
# Enzyme is skipped (see the GConvLSTMCell test item). Zygote only.
# with no initial state
test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
Expand All @@ -169,6 +178,11 @@ end
y, h = cell(g, g.x)
@test y === h
@test size(h) == (out_channel, g.num_nodes)
# Enzyme is skipped: the cell's diffusion convolution (`d_conv`) builds the
# transposed graph with the keyword `GNNGraph` constructor, whose locals are
# union-typed across its `graph_type` branches, and Enzyme fails there with an
# IllegalTypeAnalysisException (it does not treat `ignore_derivatives` as
# inactive). Zygote only.
# with no initial state
test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
Expand All @@ -183,6 +197,7 @@ end
state0 = rand(Float32, out_channel, g.num_nodes)
y = layer(g, x)
@test size(y) == (out_channel, timesteps, g.num_nodes)
# Enzyme is skipped (see the DCGRUCell test item). Zygote only.
# with no initial state
test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
Expand All @@ -201,9 +216,14 @@ end
y, state = cell(g, g.x)
@test size(y) == (out_channel, g.num_nodes)
# with no initial state
test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
# with initial state
test_gradients(cell, g, g.x, state, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()])
test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW,
ad_backends = [Flux.AutoZygote(), Flux.AutoEnzyme()])
# with initial state: `state.weight` and `state.lstm[1]` are the same array
# (`LSTMCell` returns `h, (h, c)`). Enzyme gives aliased inputs one shadow that sums
# both slots' gradients (EnzymeAD/Enzyme.jl#3408), while Zygote and finite
# differences report each slot separately.
test_gradients(cell, g, g.x, state, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW,
ad_backends = [Flux.AutoZygote()])
end

@testitem "EvolveGCNO" setup=[TemporalConvTestModule, TestModule] begin
Expand Down
28 changes: 24 additions & 4 deletions GraphNeuralNetworks/test/test_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using GraphNeuralNetworks
using Test
using Statistics, Random
using Flux
using Functors: fmapstructure_with_path
using Functors: fmap, fmapstructure_with_path
using Graphs
using ChainRulesTestUtils, FiniteDifferences
using Zygote: Zygote
Expand All @@ -35,6 +35,13 @@ if TEST_MOONCAKE
import Mooncake
end

# Enzyme runs only in the CI job on the latest Julia release, to keep the 1.10
# job's run time down.
const TEST_ENZYME = VERSION >= v"1.12"
if TEST_ENZYME
import Enzyme # loads FluxEnzymeExt, which backs Flux.AutoEnzyme()
end

# from Base
export mean, randn, SparseArrays, AbstractSparseMatrix

Expand All @@ -49,7 +56,7 @@ export random_regular_graph, erdos_renyi

# from this module
export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS,
test_gradients, finitediff_withgradient,
test_gradients, finitediff_withgradient,
check_equal_leaves, gpu_backend, TEST_MOONCAKE


Expand Down Expand Up @@ -95,15 +102,25 @@ function test_gradients(
test_grad_x = true,
# Reference AD: finite differences for CPU tests, Zygote (on CPU) for GPU tests.
reference = test_gpu ? Flux.AutoZygote() : :finitediff,
ad_backends = test_gpu ? [] : [Flux.AutoZygote(), Flux.AutoMooncake()],
ad_backends = test_gpu ? [] :
[Flux.AutoZygote(), Flux.AutoMooncake(), Flux.AutoEnzyme()],
loss = (f, g, xs...) -> mean(f(g, xs...)),
# false skips Enzyme on :dense graphs, where `propagate(copy_xj, g, +)`
# multiplies by the union-typed `adjacency_matrix(g; weighted = false)`
# and Enzyme throws an IllegalTypeAnalysisException.
enzyme_dense = true,
)

# Mooncake requires Julia >= 1.12 and errors on sparse graph internals.
if !TEST_MOONCAKE || graph.graph isa AbstractSparseMatrix
ad_backends = filter(b -> !(b isa Flux.AutoMooncake), ad_backends)
end

# Other layers Enzyme cannot handle opt out per call site via `ad_backends`.
if !TEST_ENZYME || (!enzyme_dense && get_graph_type(graph) == :dense)
ad_backends = filter(b -> !(b isa Flux.AutoEnzyme), ad_backends)
end

## Let's make sure first that the forward pass works.
l = loss(f, graph, xs...)
@assert l isa Number
Expand Down Expand Up @@ -149,7 +166,10 @@ function test_gradients(
f64 = f |> Flux.f64
ps, re = Flux.destructure(f64)
y_fd, g_fd = finitediff_withgradient(ps -> loss(re(ps), graph, xs...), ps)
y_fd, (re(g_fd[1]),)
# Rebuild from a zeroed model, so non-trainable arrays (e.g. BatchNorm's
# running stats) compare as zero gradients instead of as model values.
_, re0 = Flux.destructure(fmap(x -> x isa AbstractArray ? zero(x) : x, f64))
y_fd, (re0(g_fd[1]),)
else
Flux.withgradient(f -> loss(f, graph, xs...), reference, f)
end
Expand Down