From 140168e1a894e7c6f57642f2a5168810aa094c7e Mon Sep 17 00:00:00 2001 From: Parvm1102 Date: Tue, 7 Jul 2026 20:11:15 +0530 Subject: [PATCH 1/3] fix: made the gradient testing more generic Signed-off-by: Parvm1102 --- GNNlib/test/test_module.jl | 30 ++++++++++++++++--------- GraphNeuralNetworks/test/test_module.jl | 30 ++++++++++++++++--------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/GNNlib/test/test_module.jl b/GNNlib/test/test_module.jl index 18fe42dca..02162294d 100644 --- a/GNNlib/test/test_module.jl +++ b/GNNlib/test/test_module.jl @@ -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(g, g2; rtol, atol) +end + function test_gradients( f, graph::GNNGraph, @@ -96,6 +103,12 @@ function test_gradients( CPU AD vs GPU AD, or test Mooncake AD.") end + # AD backends to compare against the Zygote reference gradient. + ad_backends = [] + if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals + push!(ad_backends, Flux.AutoMooncake()) + end + ## Let's make sure first that the forward pass works. l = loss(f, graph, xs...) @assert l isa Number @@ -122,12 +135,9 @@ function test_gradients( check_equal_leaves(g, g_fd; rtol, atol) end - 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 + # AD backend gradient with respect to input. + test_gradients_with_backend(backend, (xs...) -> loss(f, graph, xs...), y, g, xs...; rtol, atol) end if test_gpu @@ -153,11 +163,9 @@ function test_gradients( check_equal_leaves(g, g_fd; rtol, atol) end - 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) + for backend in ad_backends + # AD backend gradient with respect to f. + test_gradients_with_backend(backend, f -> loss(f, graph, xs...), y, g, f; rtol, atol) end if test_gpu diff --git a/GraphNeuralNetworks/test/test_module.jl b/GraphNeuralNetworks/test/test_module.jl index b4612e04f..6f4c701ee 100644 --- a/GraphNeuralNetworks/test/test_module.jl +++ b/GraphNeuralNetworks/test/test_module.jl @@ -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(g, g2; rtol, atol) +end + function test_gradients( f, graph::GNNGraph, @@ -96,6 +103,12 @@ function test_gradients( CPU AD vs GPU AD, or test Mooncake AD.") end + # AD backends to compare against the Zygote reference gradient. + ad_backends = [] + if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals + push!(ad_backends, Flux.AutoMooncake()) + end + ## Let's make sure first that the forward pass works. l = loss(f, graph, xs...) @assert l isa Number @@ -122,12 +135,9 @@ function test_gradients( check_equal_leaves(g, g_fd; rtol, atol) end - 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 + # AD backend gradient with respect to input. + test_gradients_with_backend(backend, (xs...) -> loss(f, graph, xs...), y, g, xs...; rtol, atol) end if test_gpu @@ -153,11 +163,9 @@ function test_gradients( check_equal_leaves(g, g_fd; rtol, atol) end - 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) + for backend in ad_backends + # AD backend gradient with respect to f. + test_gradients_with_backend(backend, f -> loss(f, graph, xs...), y, g, f; rtol, atol) end if test_gpu From b4f23b4aaa0d1b29e004e0666dfae08ea4b37ac3 Mon Sep 17 00:00:00 2001 From: Parvm1102 Date: Wed, 8 Jul 2026 15:01:14 +0530 Subject: [PATCH 2/3] Refactor gradient testing to utilize AD_BACKENDS instead of TEST_MOONCAKE across various test files Signed-off-by: Parvm1102 --- GNNlib/test/msgpass.jl | 10 ++-- GNNlib/test/test_module.jl | 18 +++--- GraphNeuralNetworks/test/layers/basic.jl | 4 +- GraphNeuralNetworks/test/layers/conv.jl | 54 ++++++++--------- GraphNeuralNetworks/test/layers/pool.jl | 4 +- .../test/layers/temporalconv.jl | 58 +++++++++---------- GraphNeuralNetworks/test/test_module.jl | 18 +++--- 7 files changed, 85 insertions(+), 81 deletions(-) diff --git a/GNNlib/test/msgpass.jl b/GNNlib/test/msgpass.jl index 6fb0b0b1d..31791b438 100644 --- a/GNNlib/test/msgpass.jl +++ b/GNNlib/test/msgpass.jl @@ -132,14 +132,14 @@ 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, ad_backends=AD_BACKENDS) 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, ad_backends=AD_BACKENDS) end end @@ -147,7 +147,7 @@ end 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, ad_backends=AD_BACKENDS) end end @@ -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, ad_backends=AD_BACKENDS) end end @@ -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, ad_backends=AD_BACKENDS) end end end diff --git a/GNNlib/test/test_module.jl b/GNNlib/test/test_module.jl index 02162294d..074e19fb8 100644 --- a/GNNlib/test/test_module.jl +++ b/GNNlib/test/test_module.jl @@ -48,10 +48,13 @@ if TEST_MOONCAKE import Mooncake end +# AD backends compared against the Zygote reference in `test_gradients`. +const AD_BACKENDS = TEST_MOONCAKE ? [Flux.AutoMooncake()] : [] + # from this module export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS, test_gradients, finitediff_withgradient, - check_equal_leaves, gpu_backend, TEST_MOONCAKE + check_equal_leaves, gpu_backend, TEST_MOONCAKE, AD_BACKENDS const D_IN = 3 @@ -94,19 +97,18 @@ function test_gradients( test_grad_f = true, test_grad_x = true, compare_finite_diff = true, - test_mooncake = false, + ad_backends = [], loss = (f, g, xs...) -> mean(f(g, xs...)), ) - if !test_gpu && !compare_finite_diff && !test_mooncake + if !test_gpu && !compare_finite_diff && isempty(ad_backends) error("You should either compare finite diff vs CPU AD, \ - CPU AD vs GPU AD, or test Mooncake AD.") + CPU AD vs GPU AD, or test an AD backend.") end - # AD backends to compare against the Zygote reference gradient. - ad_backends = [] - if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals - push!(ad_backends, Flux.AutoMooncake()) + # Mooncake's friendly tangents currently error on sparse graph internals. + if 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. diff --git a/GraphNeuralNetworks/test/layers/basic.jl b/GraphNeuralNetworks/test/layers/basic.jl index 47b111535..bce86b75d 100644 --- a/GraphNeuralNetworks/test/layers/basic.jl +++ b/GraphNeuralNetworks/test/layers/basic.jl @@ -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) @testset "constructor with names" begin m = GNNChain(GCNConv(din => d), @@ -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) end end diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index 96137a3ce..60a3fccd8 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -10,20 +10,20 @@ end l = GCNConv(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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end l = GCNConv(D_IN => D_OUT, tanh, bias = false) 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end l = GCNConv(D_IN => D_OUT, add_self_loops = false) for g in TEST_GRAPHS has_isolated_nodes(g) && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -49,7 +49,7 @@ end l = GCNConv(1 => 1, add_self_loops = false, use_edge_weight = true) @test gradient(w -> sum(l(g, x, w)), w)[1] isa AbstractVector{Float32} # redundant test but more explicit @test size(l(g, x, w)) == (1, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end @testset "conv_weight" begin @@ -86,7 +86,7 @@ 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, test_mooncake = false) + test_gradients(l, g, g.x, rtol = RTOL_LOW) end @testset "bias=false" begin @@ -124,13 +124,13 @@ 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end l = GraphConv(D_IN => D_OUT, tanh, bias = false, aggr = mean) 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end @testset "bias=false" begin @@ -157,7 +157,7 @@ end l = GATConv(D_IN => D_OUT; heads, concat, dropout=0) for g in TEST_GRAPHS @test size(l(g, g.x)) == (concat ? heads * D_OUT : D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) end end @@ -166,7 +166,7 @@ end l = GATConv((D_IN, ein) => D_OUT, add_self_loops = false, dropout=0) g = GNNGraph(TEST_GRAPHS[1], edata = rand(Float32, ein, TEST_GRAPHS[1].num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) end @testset "num params" begin @@ -197,7 +197,7 @@ end l = GATv2Conv(D_IN => D_OUT, tanh; heads, concat, dropout=0) for g in TEST_GRAPHS @test size(l(g, g.x)) == (concat ? heads * D_OUT : D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, atol=ATOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) end end @@ -206,7 +206,7 @@ end l = GATv2Conv((D_IN, ein) => D_OUT, add_self_loops = false, dropout=0) g = GNNGraph(TEST_GRAPHS[1], edata = rand(Float32, ein, TEST_GRAPHS[1].num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, atol=ATOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) end @testset "num params" begin @@ -239,7 +239,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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -260,7 +260,7 @@ end l = EdgeConv(Dense(2 * D_IN, D_OUT), 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -281,7 +281,7 @@ end l = GINConv(nn, 0.01, aggr = mean) 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end @test !in(:eps, Flux.trainable(l)) @@ -307,7 +307,7 @@ end for g in TEST_GRAPHS g = GNNGraph(g, edata = rand(Float32, edim, g.num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -332,7 +332,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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -351,7 +351,7 @@ end l = ResGatedGraphConv(D_IN => D_OUT, tanh, bias = 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -411,7 +411,7 @@ end Flux.trainable(l) == (; β = [1f0]) for g in TEST_GRAPHS @test size(l(g, g.x)) == (D_IN, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end @@ -437,7 +437,7 @@ end y = l(g, x, e) return mean(y[1]) + sum(y[2]) end - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss, ad_backends = AD_BACKENDS) end end @@ -491,13 +491,13 @@ 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end end @@ -520,13 +520,13 @@ 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) 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_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) end end end @@ -572,7 +572,7 @@ end for g in TEST_GRAPHS g = GNNGraph(g, ndata = rand(Float32, D_IN * heads, g.num_nodes)) @test size(l(g, g.x)) == (D_IN * heads, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) end # used like in Shi et al., 2021 l = TransformerConv((D_IN, ein) => D_IN; heads, gating = true, @@ -580,7 +580,7 @@ end for g in TEST_GRAPHS g = GNNGraph(g, edata = rand(Float32, ein, g.num_edges)) @test size(l(g, g.x, g.e)) == (D_IN * heads, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) end # test averaging heads l = TransformerConv(D_IN => D_IN; heads, concat = false, @@ -588,7 +588,7 @@ end root_weight = false) for g in TEST_GRAPHS @test size(l(g, g.x)) == (D_IN, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) end end @@ -616,7 +616,7 @@ end l = DConv(D_IN => D_OUT, k) 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_mooncake = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end end diff --git a/GraphNeuralNetworks/test/layers/pool.jl b/GraphNeuralNetworks/test/layers/pool.jl index 7300b9aa3..37bd7f63d 100644 --- a/GraphNeuralNetworks/test/layers/pool.jl +++ b/GraphNeuralNetworks/test/layers/pool.jl @@ -19,7 +19,7 @@ @test u[:, [1]] ≈ sum(g.ndata.x[:, 1:n], dims = 2) @test p(g).gdata.u == u - test_gradients(p, g, g.x, rtol = 1e-5, test_mooncake = TEST_MOONCAKE) + test_gradients(p, g, g.x, rtol = 1e-5, ad_backends = AD_BACKENDS) end end @@ -42,7 +42,7 @@ end for i in 1:ng]) @test size(p(g, g.x)) == (chout, ng) - test_gradients(p, g, g.x, rtol = 1e-5, test_mooncake = TEST_MOONCAKE) + test_gradients(p, g, g.x, rtol = 1e-5, ad_backends = AD_BACKENDS) end end diff --git a/GraphNeuralNetworks/test/layers/temporalconv.jl b/GraphNeuralNetworks/test/layers/temporalconv.jl index 6489cba98..b9758bca7 100644 --- a/GraphNeuralNetworks/test/layers/temporalconv.jl +++ b/GraphNeuralNetworks/test/layers/temporalconv.jl @@ -32,9 +32,9 @@ end @test y === h @test size(h) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) # with initial state - test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) # Test with custom activation function custom_activation = tanh @@ -45,9 +45,9 @@ end # Test that outputs differ when using different activation functions @test !isapprox(y, y_custom, rtol=RTOL_HIGH) # with no initial state - test_gradients(cell_custom, g, g.x, loss=cell_loss, rtol=RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(cell_custom, g, g.x, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) # with initial state - test_gradients(cell_custom, g, g.x, h_custom, loss=cell_loss, rtol=RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(cell_custom, g, g.x, h_custom, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) end @testitem "TGCN" setup=[TemporalConvTestModule, TestModule] begin @@ -61,9 +61,9 @@ end @test layer isa GNNRecurrence @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(layer, g, x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) # with initial state - test_gradients(layer, g, x, state0, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(layer, g, x, state0, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) # Test with custom activation function custom_activation = tanh @@ -74,15 +74,15 @@ end # Test that outputs differ when using different activation functions @test !isapprox(y, y_custom, rtol = RTOL_HIGH) # with no initial state - test_gradients(layer_custom, g, x, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(layer_custom, g, x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) # with initial state - test_gradients(layer_custom, g, x, state0, rtol = RTOL_HIGH, test_mooncake = TEST_MOONCAKE) + test_gradients(layer_custom, g, x, state0, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) # interplay with GNNChain model = GNNChain(TGCN(in_channel => out_channel), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_HIGH, atol = ATOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(model, g, x, rtol = RTOL_HIGH, atol = ATOL_LOW, ad_backends = AD_BACKENDS) end @testitem "GConvLSTMCell" setup=[TemporalConvTestModule, TestModule] begin @@ -93,9 +93,9 @@ end @test size(h) == (out_channel, g.num_nodes) @test size(c) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(cell, g, g.x, (h, c), loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, (h, c), loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) end @testitem "GConvLSTM" setup=[TemporalConvTestModule, TestModule] begin @@ -107,15 +107,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) # interplay with GNNChain model = GNNChain(GConvLSTM(in_channel => out_channel, 2), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW, test_mooncake = false) + test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW) end @testitem "GConvGRUCell" setup=[TemporalConvTestModule, TestModule] begin @@ -125,9 +125,9 @@ end @test y === h @test size(h) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) end @@ -140,15 +140,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) # interplay with GNNChain model = GNNChain(GConvGRU(in_channel => out_channel, 2), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW, test_mooncake = false) + test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW) end @testitem "DCGRUCell" setup=[TemporalConvTestModule, TestModule] begin @@ -158,9 +158,9 @@ end @test y === h @test size(h) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) end @testitem "DCGRU" setup=[TemporalConvTestModule, TestModule] begin @@ -172,15 +172,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) # interplay with GNNChain model = GNNChain(DCGRU(in_channel => out_channel, 2), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW, test_mooncake = false) + test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW) end @testitem "EvolveGCNOCell" setup=[TemporalConvTestModule, TestModule] begin @@ -189,9 +189,9 @@ 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, test_mooncake = false) + test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(cell, g, g.x, state, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = false) + test_gradients(cell, g, g.x, state, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) end @testitem "EvolveGCNO" setup=[TemporalConvTestModule, TestModule] begin @@ -203,15 +203,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) # interplay with GNNChain model = GNNChain(EvolveGCNO(in_channel => out_channel), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, test_mooncake = TEST_MOONCAKE) + test_gradients(model, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) end # @testitem "GINConv" setup=[TemporalConvTestModule, TestModule] begin diff --git a/GraphNeuralNetworks/test/test_module.jl b/GraphNeuralNetworks/test/test_module.jl index 6f4c701ee..6dff7d223 100644 --- a/GraphNeuralNetworks/test/test_module.jl +++ b/GraphNeuralNetworks/test/test_module.jl @@ -35,6 +35,9 @@ if TEST_MOONCAKE import Mooncake end +# AD backends compared against the Zygote reference in `test_gradients`. +const AD_BACKENDS = TEST_MOONCAKE ? [Flux.AutoMooncake()] : [] + # from Base export mean, randn, SparseArrays, AbstractSparseMatrix @@ -50,7 +53,7 @@ export random_regular_graph, erdos_renyi # from this module export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS, test_gradients, finitediff_withgradient, - check_equal_leaves, gpu_backend, TEST_MOONCAKE + check_equal_leaves, gpu_backend, TEST_MOONCAKE, AD_BACKENDS const D_IN = 3 @@ -94,19 +97,18 @@ function test_gradients( test_grad_f = true, test_grad_x = true, compare_finite_diff = true, - test_mooncake = false, + ad_backends = [], loss = (f, g, xs...) -> mean(f(g, xs...)), ) - if !test_gpu && !compare_finite_diff && !test_mooncake + if !test_gpu && !compare_finite_diff && isempty(ad_backends) error("You should either compare finite diff vs CPU AD, \ - CPU AD vs GPU AD, or test Mooncake AD.") + CPU AD vs GPU AD, or test an AD backend.") end - # AD backends to compare against the Zygote reference gradient. - ad_backends = [] - if test_mooncake && !(graph.graph isa AbstractSparseMatrix) # Mooncake friendly tangents currently error on sparse graph internals - push!(ad_backends, Flux.AutoMooncake()) + # Mooncake's friendly tangents currently error on sparse graph internals. + if 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. From 5445ec7dfa391ee5b4f9a5ef2b76de065206a5c8 Mon Sep 17 00:00:00 2001 From: Parvm1102 Date: Sat, 11 Jul 2026 22:57:10 +0530 Subject: [PATCH 3/3] Fix: AD gradients now do not need to be passed every time Signed-off-by: Parvm1102 --- GNNlib/test/msgpass.jl | 18 ++-- GNNlib/test/test_module.jl | 58 +++++------- GraphNeuralNetworks/test/layers/basic.jl | 4 +- GraphNeuralNetworks/test/layers/conv.jl | 92 +++++++++---------- GraphNeuralNetworks/test/layers/pool.jl | 4 +- .../test/layers/temporalconv.jl | 58 ++++++------ GraphNeuralNetworks/test/runtests.jl | 2 - GraphNeuralNetworks/test/test_module.jl | 58 +++++------- 8 files changed, 138 insertions(+), 156 deletions(-) diff --git a/GNNlib/test/msgpass.jl b/GNNlib/test/msgpass.jl index 31791b438..12c207507 100644 --- a/GNNlib/test/msgpass.jl +++ b/GNNlib/test/msgpass.jl @@ -132,14 +132,14 @@ 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, ad_backends=AD_BACKENDS) + 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, ad_backends=AD_BACKENDS) + test_gradients(f, g, g.x; test_grad_f=false) end end @@ -147,7 +147,7 @@ end 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, ad_backends=AD_BACKENDS) + test_gradients(f, g, g.x, e; test_grad_f=false) end end @@ -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, ad_backends=AD_BACKENDS) + test_gradients(f, g, g.x, e; test_grad_f=false) end end @@ -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, ad_backends=AD_BACKENDS) + test_gradients(f, g, g.x, w; test_grad_f=false) end end end @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/GNNlib/test/test_module.jl b/GNNlib/test/test_module.jl index 074e19fb8..3478ca9ff 100644 --- a/GNNlib/test/test_module.jl +++ b/GNNlib/test/test_module.jl @@ -48,13 +48,10 @@ if TEST_MOONCAKE import Mooncake end -# AD backends compared against the Zygote reference in `test_gradients`. -const AD_BACKENDS = TEST_MOONCAKE ? [Flux.AutoMooncake()] : [] - # from this module export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS, test_gradients, finitediff_withgradient, - check_equal_leaves, gpu_backend, TEST_MOONCAKE, AD_BACKENDS + check_equal_leaves, gpu_backend, TEST_MOONCAKE const D_IN = 3 @@ -85,7 +82,7 @@ end 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(g, g2; rtol, atol) + check_equal_leaves(g2, g; rtol, atol) end function test_gradients( @@ -96,18 +93,14 @@ function test_gradients( test_gpu = false, test_grad_f = true, test_grad_x = true, - compare_finite_diff = true, - ad_backends = [], + # 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 && isempty(ad_backends) - error("You should either compare finite diff vs CPU AD, \ - CPU AD vs GPU AD, or test an AD backend.") - end - - # Mooncake's friendly tangents currently error on sparse graph internals. - if graph.graph isa AbstractSparseMatrix + # 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 @@ -125,20 +118,18 @@ 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) for backend in ad_backends - # AD backend gradient with respect to input. test_gradients_with_backend(backend, (xs...) -> loss(f, graph, xs...), y, g, xs...; rtol, atol) end @@ -152,22 +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) - for backend in ad_backends - # AD backend gradient with respect to f. - test_gradients_with_backend(backend, f -> loss(f, graph, xs...), y, g, f; 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 diff --git a/GraphNeuralNetworks/test/layers/basic.jl b/GraphNeuralNetworks/test/layers/basic.jl index bce86b75d..27a39c3a8 100644 --- a/GraphNeuralNetworks/test/layers/basic.jl +++ b/GraphNeuralNetworks/test/layers/basic.jl @@ -18,7 +18,7 @@ Flux.testmode!(gnn) - test_gradients(gnn, g, x, rtol = 1e-5) + test_gradients(gnn, g, x, rtol = 1e-5, ad_backends = [Flux.AutoZygote()]) @testset "constructor with names" begin m = GNNChain(GCNConv(din => d), @@ -53,7 +53,7 @@ Flux.trainmode!(gnn) - test_gradients(gnn, g, x, rtol = 1e-4, atol=1e-4) + test_gradients(gnn, g, x, rtol = 1e-4, atol=1e-4, ad_backends = [Flux.AutoZygote()]) end end diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index 60a3fccd8..be7c4f1d5 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -10,20 +10,20 @@ end l = GCNConv(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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end l = GCNConv(D_IN => D_OUT, tanh, bias = false) 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end l = GCNConv(D_IN => D_OUT, add_self_loops = false) for g in TEST_GRAPHS has_isolated_nodes(g) && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end @@ -49,7 +49,7 @@ end l = GCNConv(1 => 1, add_self_loops = false, use_edge_weight = true) @test gradient(w -> sum(l(g, x, w)), w)[1] isa AbstractVector{Float32} # redundant test but more explicit @test size(l(g, x, w)) == (1, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end @testset "conv_weight" begin @@ -72,7 +72,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -86,7 +86,7 @@ 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) + test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = [Flux.AutoZygote()]) end @testset "bias=false" begin @@ -114,7 +114,7 @@ end broken = false end @test test_gradients( - l, g, g.x, rtol = RTOL_LOW, test_gpu = true, compare_finite_diff = false + l, g, g.x, rtol = RTOL_LOW, test_gpu = true ) broken=broken end end @@ -124,13 +124,13 @@ 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end l = GraphConv(D_IN => D_OUT, tanh, bias = false, aggr = mean) 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end @testset "bias=false" begin @@ -146,7 +146,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -157,7 +157,7 @@ end l = GATConv(D_IN => D_OUT; heads, concat, dropout=0) for g in TEST_GRAPHS @test size(l(g, g.x)) == (concat ? heads * D_OUT : D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_LOW) end end @@ -166,7 +166,7 @@ end l = GATConv((D_IN, ein) => D_OUT, add_self_loops = false, dropout=0) g = GNNGraph(TEST_GRAPHS[1], edata = rand(Float32, ein, TEST_GRAPHS[1].num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW) end @testset "num params" begin @@ -186,7 +186,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (concat ? heads * D_OUT : D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_LOW, test_gpu = true) end end end @@ -197,7 +197,7 @@ end l = GATv2Conv(D_IN => D_OUT, tanh; heads, concat, dropout=0) for g in TEST_GRAPHS @test size(l(g, g.x)) == (concat ? heads * D_OUT : D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_LOW, atol=ATOL_LOW) end end @@ -206,7 +206,7 @@ end l = GATv2Conv((D_IN, ein) => D_OUT, add_self_loops = false, dropout=0) g = GNNGraph(TEST_GRAPHS[1], edata = rand(Float32, ein, TEST_GRAPHS[1].num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, atol=ATOL_LOW) end @testset "num params" begin @@ -226,7 +226,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (concat ? heads * D_OUT : D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, atol=ATOL_LOW, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_LOW, atol=ATOL_LOW, test_gpu = true) end end end @@ -239,7 +239,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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end @@ -251,7 +251,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -260,7 +260,7 @@ end l = EdgeConv(Dense(2 * D_IN, D_OUT), 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end @@ -270,7 +270,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -281,7 +281,7 @@ end l = GINConv(nn, 0.01, aggr = mean) 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end @test !in(:eps, Flux.trainable(l)) @@ -294,7 +294,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -307,7 +307,7 @@ end for g in TEST_GRAPHS g = GNNGraph(g, edata = rand(Float32, edim, g.num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH) end end @@ -320,7 +320,7 @@ end g.graph isa AbstractSparseMatrix && continue g = GNNGraph(g, edata = rand(Float32, edim, g.num_edges)) @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_gpu = true) end end @@ -332,7 +332,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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end @@ -342,7 +342,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -351,7 +351,7 @@ end l = ResGatedGraphConv(D_IN => D_OUT, tanh, bias = 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end @@ -361,7 +361,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -392,7 +392,7 @@ end # g.graph isa AbstractSparseMatrix && continue # g = GNNGraph(g, edata = rand(Float32, edim, g.num_edges)) # @test size(l(g, g.x, g.e)) == (D_OUT, g.num_nodes) -# test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) +# test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_gpu = true) # end # end @@ -411,7 +411,7 @@ end Flux.trainable(l) == (; β = [1f0]) for g in TEST_GRAPHS @test size(l(g, g.x)) == (D_IN, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end @@ -421,7 +421,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_IN, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -437,7 +437,7 @@ end y = l(g, x, e) return mean(y[1]) + sum(y[2]) end - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss) end end @@ -454,7 +454,7 @@ end y = l(g, x, e) return mean(y[1]) + sum(y[2]) end - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW; loss, test_gpu = true) end end @@ -480,7 +480,7 @@ end # g.graph isa AbstractSparseMatrix && continue # g = GNNGraph(g, edata = rand(Float32, ein_channel, g.num_edges)) # y = l(g, g.x, g.e) -# test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) +# test_gradients(l, g, g.x, g.e, rtol = RTOL_HIGH, test_gpu = true) # end # end @@ -491,13 +491,13 @@ 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end end @@ -509,7 +509,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -520,13 +520,13 @@ 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) 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, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_HIGH) end end end @@ -538,7 +538,7 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end @@ -572,7 +572,7 @@ end for g in TEST_GRAPHS g = GNNGraph(g, ndata = rand(Float32, D_IN * heads, g.num_nodes)) @test size(l(g, g.x)) == (D_IN * heads, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_LOW) end # used like in Shi et al., 2021 l = TransformerConv((D_IN, ein) => D_IN; heads, gating = true, @@ -580,7 +580,7 @@ end for g in TEST_GRAPHS g = GNNGraph(g, edata = rand(Float32, ein, g.num_edges)) @test size(l(g, g.x, g.e)) == (D_IN * heads, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW) end # test averaging heads l = TransformerConv(D_IN => D_IN; heads, concat = false, @@ -588,7 +588,7 @@ end root_weight = false) for g in TEST_GRAPHS @test size(l(g, g.x)) == (D_IN, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(l, g, g.x, rtol = RTOL_LOW) end end @@ -604,7 +604,7 @@ end g.graph isa AbstractSparseMatrix && continue g = GNNGraph(g, edata = rand(Float32, ein, g.num_edges)) @test size(l(g, g.x, g.e)) == (D_IN * heads, g.num_nodes) - test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, g.e, rtol = RTOL_LOW, test_gpu = true) end end @@ -616,7 +616,7 @@ end l = DConv(D_IN => D_OUT, k) 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()]) end end end @@ -627,6 +627,6 @@ end for g in TEST_GRAPHS g.graph isa AbstractSparseMatrix && continue @test size(l(g, g.x)) == (D_OUT, g.num_nodes) - test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true, compare_finite_diff = false) + test_gradients(l, g, g.x, rtol = RTOL_HIGH, test_gpu = true) end end diff --git a/GraphNeuralNetworks/test/layers/pool.jl b/GraphNeuralNetworks/test/layers/pool.jl index 37bd7f63d..fa1475b20 100644 --- a/GraphNeuralNetworks/test/layers/pool.jl +++ b/GraphNeuralNetworks/test/layers/pool.jl @@ -19,7 +19,7 @@ @test u[:, [1]] ≈ sum(g.ndata.x[:, 1:n], dims = 2) @test p(g).gdata.u == u - test_gradients(p, g, g.x, rtol = 1e-5, ad_backends = AD_BACKENDS) + test_gradients(p, g, g.x, rtol = 1e-5) end end @@ -42,7 +42,7 @@ end for i in 1:ng]) @test size(p(g, g.x)) == (chout, ng) - test_gradients(p, g, g.x, rtol = 1e-5, ad_backends = AD_BACKENDS) + test_gradients(p, g, g.x, rtol = 1e-5) end end diff --git a/GraphNeuralNetworks/test/layers/temporalconv.jl b/GraphNeuralNetworks/test/layers/temporalconv.jl index b9758bca7..47ab38813 100644 --- a/GraphNeuralNetworks/test/layers/temporalconv.jl +++ b/GraphNeuralNetworks/test/layers/temporalconv.jl @@ -32,9 +32,9 @@ end @test y === h @test size(h) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_HIGH) # with initial state - test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_HIGH) # Test with custom activation function custom_activation = tanh @@ -45,9 +45,9 @@ end # Test that outputs differ when using different activation functions @test !isapprox(y, y_custom, rtol=RTOL_HIGH) # with no initial state - test_gradients(cell_custom, g, g.x, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(cell_custom, g, g.x, loss=cell_loss, rtol=RTOL_HIGH) # with initial state - test_gradients(cell_custom, g, g.x, h_custom, loss=cell_loss, rtol=RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(cell_custom, g, g.x, h_custom, loss=cell_loss, rtol=RTOL_HIGH) end @testitem "TGCN" setup=[TemporalConvTestModule, TestModule] begin @@ -61,9 +61,9 @@ end @test layer isa GNNRecurrence @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(layer, g, x, rtol = RTOL_HIGH) # with initial state - test_gradients(layer, g, x, state0, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(layer, g, x, state0, rtol = RTOL_HIGH) # Test with custom activation function custom_activation = tanh @@ -74,15 +74,15 @@ end # Test that outputs differ when using different activation functions @test !isapprox(y, y_custom, rtol = RTOL_HIGH) # with no initial state - test_gradients(layer_custom, g, x, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(layer_custom, g, x, rtol = RTOL_HIGH) # with initial state - test_gradients(layer_custom, g, x, state0, rtol = RTOL_HIGH, ad_backends = AD_BACKENDS) + test_gradients(layer_custom, g, x, state0, rtol = RTOL_HIGH) # interplay with GNNChain model = GNNChain(TGCN(in_channel => out_channel), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_HIGH, atol = ATOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(model, g, x, rtol = RTOL_HIGH, atol = ATOL_LOW) end @testitem "GConvLSTMCell" setup=[TemporalConvTestModule, TestModule] begin @@ -93,9 +93,9 @@ end @test size(h) == (out_channel, g.num_nodes) @test size(c) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) + 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, (h, c), loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(cell, g, g.x, (h, c), loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) end @testitem "GConvLSTM" setup=[TemporalConvTestModule, TestModule] begin @@ -107,15 +107,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) # interplay with GNNChain model = GNNChain(GConvLSTM(in_channel => out_channel, 2), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW) + test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW, ad_backends = [Flux.AutoZygote()]) end @testitem "GConvGRUCell" setup=[TemporalConvTestModule, TestModule] begin @@ -125,9 +125,9 @@ end @test y === h @test size(h) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) + 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, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) end @@ -140,15 +140,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) # interplay with GNNChain model = GNNChain(GConvGRU(in_channel => out_channel, 2), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW) + test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW, ad_backends = [Flux.AutoZygote()]) end @testitem "DCGRUCell" setup=[TemporalConvTestModule, TestModule] begin @@ -158,9 +158,9 @@ end @test y === h @test size(h) == (out_channel, g.num_nodes) # with no initial state - test_gradients(cell, g, g.x, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) + 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, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(cell, g, g.x, h, loss=cell_loss, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) end @testitem "DCGRU" setup=[TemporalConvTestModule, TestModule] begin @@ -172,15 +172,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = [Flux.AutoZygote()]) # interplay with GNNChain model = GNNChain(DCGRU(in_channel => out_channel, 2), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW) + test_gradients(model, g, x, rtol = RTOL_LOW, atol = ATOL_LOW, ad_backends = [Flux.AutoZygote()]) end @testitem "EvolveGCNOCell" setup=[TemporalConvTestModule, TestModule] begin @@ -189,9 +189,9 @@ 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) + 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) + 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 @@ -203,15 +203,15 @@ end y = layer(g, x) @test size(y) == (out_channel, timesteps, g.num_nodes) # with no initial state - test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(layer, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) # with initial state - test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(layer, g, x, state0, rtol=RTOL_LOW, atol=ATOL_LOW) # interplay with GNNChain model = GNNChain(EvolveGCNO(in_channel => out_channel), Dense(out_channel, 1)) y = model(g, x) @test size(y) == (1, timesteps, g.num_nodes) - test_gradients(model, g, x, rtol=RTOL_LOW, atol=ATOL_LOW, ad_backends = AD_BACKENDS) + test_gradients(model, g, x, rtol=RTOL_LOW, atol=ATOL_LOW) end # @testitem "GINConv" setup=[TemporalConvTestModule, TestModule] begin diff --git a/GraphNeuralNetworks/test/runtests.jl b/GraphNeuralNetworks/test/runtests.jl index cdd695805..0a1dfeec6 100644 --- a/GraphNeuralNetworks/test/runtests.jl +++ b/GraphNeuralNetworks/test/runtests.jl @@ -4,8 +4,6 @@ using TestItemRunner ## for how to run the tests within VS Code. ## See test_module.jl for the test infrastructure. -const TEST_MOONCAKE = VERSION >= v"1.12" - ## Uncomment below to change the default test settings # ENV["GNN_TEST_CPU"] = "false" # ENV["GNN_TEST_CUDA"] = "true" diff --git a/GraphNeuralNetworks/test/test_module.jl b/GraphNeuralNetworks/test/test_module.jl index 6dff7d223..de5e714d1 100644 --- a/GraphNeuralNetworks/test/test_module.jl +++ b/GraphNeuralNetworks/test/test_module.jl @@ -35,9 +35,6 @@ if TEST_MOONCAKE import Mooncake end -# AD backends compared against the Zygote reference in `test_gradients`. -const AD_BACKENDS = TEST_MOONCAKE ? [Flux.AutoMooncake()] : [] - # from Base export mean, randn, SparseArrays, AbstractSparseMatrix @@ -53,7 +50,7 @@ export random_regular_graph, erdos_renyi # from this module export D_IN, D_OUT, GRAPH_TYPES, TEST_GRAPHS, test_gradients, finitediff_withgradient, - check_equal_leaves, gpu_backend, TEST_MOONCAKE, AD_BACKENDS + check_equal_leaves, gpu_backend, TEST_MOONCAKE const D_IN = 3 @@ -85,7 +82,7 @@ end 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(g, g2; rtol, atol) + check_equal_leaves(g2, g; rtol, atol) end function test_gradients( @@ -96,18 +93,14 @@ function test_gradients( test_gpu = false, test_grad_f = true, test_grad_x = true, - compare_finite_diff = true, - ad_backends = [], + # 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 && isempty(ad_backends) - error("You should either compare finite diff vs CPU AD, \ - CPU AD vs GPU AD, or test an AD backend.") - end - - # Mooncake's friendly tangents currently error on sparse graph internals. - if graph.graph isa AbstractSparseMatrix + # 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 @@ -125,20 +118,18 @@ 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) for backend in ad_backends - # AD backend gradient with respect to input. test_gradients_with_backend(backend, (xs...) -> loss(f, graph, xs...), y, g, xs...; rtol, atol) end @@ -152,22 +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) - for backend in ad_backends - # AD backend gradient with respect to f. - test_gradients_with_backend(backend, f -> loss(f, graph, xs...), y, g, f; 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