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
18 changes: 9 additions & 9 deletions GNNlib/test/msgpass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ 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_mooncake=TEST_MOONCAKE)
test_gradients(f, g, g.x; test_grad_f=false)
end
end

@testset "copy_xj mean" begin
for g in TEST_GRAPHS
f(g, x) = propagate(copy_xj, g, mean, xj = x)
test_gradients(f, g, g.x; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
test_gradients(f, g, g.x; test_grad_f=false)
end
end

@testset "e_mul_xj +" begin
for g in TEST_GRAPHS
e = rand(Float32, size(g.x, 1), g.num_edges)
f(g, x, e) = propagate(e_mul_xj, g, +; xj = x, e)
test_gradients(f, g, g.x, e; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
test_gradients(f, g, g.x, e; test_grad_f=false)
end
end

Expand All @@ -157,7 +157,7 @@ end
for g in TEST_GRAPHS
e = rand(Float32, g.num_edges)
f(g, x, e) = propagate(e_mul_xj, g, +; xj = x, e)
test_gradients(f, g, g.x, e; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
test_gradients(f, g, g.x, e; test_grad_f=false)
end
end

Expand All @@ -168,7 +168,7 @@ end
g = set_edge_weight(g, w)
return propagate(w_mul_xj, g, +, xj = x)
end
test_gradients(f, g, g.x, w; test_grad_f=false, test_mooncake=TEST_MOONCAKE)
test_gradients(f, g, g.x, w; test_grad_f=false)
end
end
end
Expand All @@ -182,7 +182,7 @@ end
broken = false
f(g, x) = propagate(copy_xj, g, +, xj = x)
@test test_gradients(
f, g, g.x; test_gpu=true, test_grad_f=false, compare_finite_diff=false
f, g, g.x; test_gpu=true, test_grad_f=false
) broken=broken
end
end
Expand All @@ -193,7 +193,7 @@ end
broken = false
f(g, x) = propagate(copy_xj, g, mean, xj = x)
@test test_gradients(
f, g, g.x; test_gpu=true, test_grad_f=false, compare_finite_diff=false
f, g, g.x; test_gpu=true, test_grad_f=false
) broken=broken
end
end
Expand All @@ -205,7 +205,7 @@ end
e = rand(Float32, size(g.x, 1), g.num_edges)
f(g, x, e) = propagate(e_mul_xj, g, +; xj = x, e)
@test test_gradients(
f, g, g.x, e; test_gpu=true, test_grad_f=false, compare_finite_diff=false
f, g, g.x, e; test_gpu=true, test_grad_f=false
) broken=broken
end
end
Expand All @@ -221,7 +221,7 @@ end
# broken = get_graph_type(g) == :sparse
broken = true
@test test_gradients(
f, g, g.x, w; test_gpu=true, test_grad_f=false, compare_finite_diff=false
f, g, g.x, w; test_gpu=true, test_grad_f=false
) broken=broken
end
end
Expand Down
64 changes: 33 additions & 31 deletions GNNlib/test/test_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ function check_equal_leaves(a, b; rtol=1e-4, atol=1e-4)
@assert equal
end

# Gradient from a generic AD backend, compared against the reference (y, g).
function test_gradients_with_backend(backend, loss_fn, y, g, args...; rtol, atol)
y2, g2 = Flux.withgradient(loss_fn, backend, args...)
@assert isapprox(y, y2; rtol, atol)
check_equal_leaves(g2, g; rtol, atol)
end

function test_gradients(
f,
graph::GNNGraph,
Expand All @@ -86,14 +93,15 @@ function test_gradients(
test_gpu = false,
test_grad_f = true,
test_grad_x = true,
compare_finite_diff = true,
test_mooncake = false,
# 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()],
loss = (f, g, xs...) -> mean(f(g, xs...)),
)

if !test_gpu && !compare_finite_diff && !test_mooncake
error("You should either compare finite diff vs CPU AD, \
CPU AD vs GPU AD, or test Mooncake AD.")
# 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

## Let's make sure first that the forward pass works.
Expand All @@ -110,24 +118,19 @@ function test_gradients(
end

if test_grad_x
# Zygote gradient with respect to input.
y, g = Zygote.withgradient((xs...) -> loss(f, graph, xs...), xs...)

if compare_finite_diff
# Reference gradient with respect to input.
y, g = if reference === :finitediff
# Cast to Float64 to avoid precision issues.
f64 = f |> Flux.f64
xs64 = xs .|> Flux.f64
y_fd, g_fd = finitediff_withgradient((xs...) -> loss(f64, graph, xs...), xs64...)
@assert isapprox(y, y_fd; rtol, atol)
check_equal_leaves(g, g_fd; rtol, atol)
finitediff_withgradient((xs...) -> loss(f64, graph, xs...), xs64...)
else
Flux.withgradient((xs...) -> loss(f, graph, xs...), reference, xs...)
end
@assert isapprox(l, y; rtol, atol)

if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals
# Mooncake gradient with respect to input via Flux integration, compared against Zygote.
loss_mc_x = (xs...) -> loss(f, graph, xs...)
y_mc, g_mc = Flux.withgradient(loss_mc_x, Flux.AutoMooncake(), xs...)
@assert isapprox(y, y_mc; rtol, atol)
check_equal_leaves(g, g_mc; rtol, atol)
for backend in ad_backends
test_gradients_with_backend(backend, (xs...) -> loss(f, graph, xs...), y, g, xs...; rtol, atol)
end

if test_gpu
Expand All @@ -140,24 +143,23 @@ function test_gradients(
end

if test_grad_f
# Zygote gradient with respect to f.
y, g = Zygote.withgradient(f -> loss(f, graph, xs...), f)

if compare_finite_diff
# Reference gradient with respect to f.
y, g = if reference === :finitediff
# Cast to Float64 to avoid precision issues.
f64 = f |> Flux.f64
ps, re = Flux.destructure(f64)
y_fd, g_fd = finitediff_withgradient(ps -> loss(re(ps),graph, xs...), ps)
g_fd = (re(g_fd[1]),)
@assert isapprox(y, y_fd; rtol, atol)
check_equal_leaves(g, g_fd; rtol, atol)
y_fd, g_fd = finitediff_withgradient(ps -> loss(re(ps), graph, xs...), ps)
y_fd, (re(g_fd[1]),)
else
Flux.withgradient(f -> loss(f, graph, xs...), reference, f)
end
@assert isapprox(l, y; rtol, atol)

if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals
# Mooncake gradient with respect to f via Flux integration, compared against Zygote.
y_mc, g_mc = Flux.withgradient(f -> loss(f, graph, xs...), Flux.AutoMooncake(), f)
@assert isapprox(y, y_mc; rtol, atol)
check_equal_leaves(g, g_mc; rtol, atol)
# Skip comparison for parameterless layers (no gradients wrt f).
if !isempty(Flux.trainables(f))
for backend in ad_backends
test_gradients_with_backend(backend, f -> loss(f, graph, xs...), y, g, f; rtol, atol)
end
end

if test_gpu
Expand Down
4 changes: 2 additions & 2 deletions GraphNeuralNetworks/test/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

Flux.testmode!(gnn)

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

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

Flux.trainmode!(gnn)

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

Expand Down
Loading
Loading