|
| 1 | +@testitem "GNNGraphsMooncakeExt zero-derivative rules" setup=[GraphsTestModule] begin |
| 2 | + using .GraphsTestModule |
| 3 | + import Mooncake |
| 4 | + import NNlib |
| 5 | + |
| 6 | + # Loading Mooncake triggers the extension. |
| 7 | + @test Base.get_extension(GNNGraphs, :GNNGraphsMooncakeExt) !== nothing |
| 8 | + |
| 9 | + # Mooncake is only exercised on Julia >= 1.12, as in the other test suites. |
| 10 | + if VERSION >= v"1.12" |
| 11 | + function mooncake_gradient(f, x) |
| 12 | + cache = Mooncake.prepare_gradient_cache(f, x) |
| 13 | + _, grads = Mooncake.value_and_gradient!!(cache, f, x) |
| 14 | + return grads[2] |
| 15 | + end |
| 16 | + |
| 17 | + rng = MersenneTwister(17) |
| 18 | + |
| 19 | + # Gradients through `add_self_loops` (zero-derivative rule). |
| 20 | + for graph_type in GRAPH_TYPES |
| 21 | + g = rand_graph(rng, 6, 10; graph_type) |
| 22 | + x = randn(rng, Float32, 3, g.num_nodes) |
| 23 | + loss(x) = sum(abs2, NNlib.gather(x, edge_index(add_self_loops(g))[1])) |
| 24 | + gm = mooncake_gradient(loss, x) |
| 25 | + @test gm≈gradient(loss, x)[1] rtol=1e-4 |
| 26 | + @test gm≈ngradient(loss, x)[1] rtol=1e-4 |
| 27 | + end |
| 28 | + |
| 29 | + # Gradients through `edge_index` on adjacency-matrix graphs. |
| 30 | + for graph_type in (:dense, :sparse) |
| 31 | + g = rand_graph(rng, 6, 10; graph_type) |
| 32 | + x = randn(rng, Float32, 3, g.num_nodes) |
| 33 | + loss(x) = sum(abs2, NNlib.gather(x, edge_index(g)[1])) |
| 34 | + gm = mooncake_gradient(loss, x) |
| 35 | + @test gm≈gradient(loss, x)[1] rtol=1e-4 |
| 36 | + @test gm≈ngradient(loss, x)[1] rtol=1e-4 |
| 37 | + end |
| 38 | + |
| 39 | + # Float adjacency: edge weights stay on the AD path. |
| 40 | + A = Float32[0 1 0 2; 1 0 3 0; 0 3 0 1; 2 0 1 0] |
| 41 | + loss_w(A) = sum(abs2, get_edge_weight(GNNGraph(A, graph_type = :dense))) |
| 42 | + gw = mooncake_gradient(loss_w, copy(A)) |
| 43 | + @test gw≈2 .* A rtol=1e-4 |
| 44 | + @test gw≈gradient(loss_w, A)[1] rtol=1e-4 |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +@testitem "GNNGraphsMooncakeExt on GPU" setup=[GraphsTestModule] tags=[:gpu] begin |
| 49 | + using .GraphsTestModule |
| 50 | + import Mooncake |
| 51 | + import NNlib |
| 52 | + |
| 53 | + dev = gpu_device(force = true) |
| 54 | + # Mooncake's GPU rules are CUDA-only. |
| 55 | + if VERSION >= v"1.12" && dev isa CUDADevice |
| 56 | + function mooncake_gradient(f, x) |
| 57 | + cache = Mooncake.prepare_gradient_cache(f, x) |
| 58 | + _, grads = Mooncake.value_and_gradient!!(cache, f, x) |
| 59 | + return grads[2] |
| 60 | + end |
| 61 | + |
| 62 | + rng = MersenneTwister(17) |
| 63 | + |
| 64 | + # Without the rules Mooncake errors here instead of returning a gradient. |
| 65 | + for graph_type in (:coo, :dense) |
| 66 | + g = rand_graph(rng, 6, 10; graph_type) |
| 67 | + x = randn(rng, Float32, 3, g.num_nodes) |
| 68 | + g_gpu, x_gpu = dev(g), dev(x) |
| 69 | + loss(x) = sum(abs2, NNlib.gather(x, edge_index(add_self_loops(g))[1])) |
| 70 | + loss_gpu(x) = sum(abs2, NNlib.gather(x, edge_index(add_self_loops(g_gpu))[1])) |
| 71 | + @test Array(mooncake_gradient(loss_gpu, x_gpu))≈mooncake_gradient(loss, x) rtol=1e-4 |
| 72 | + end |
| 73 | + |
| 74 | + # Float adjacency: edge weights stay on the AD path. |
| 75 | + A = Float32[0 1 0 2; 1 0 3 0; 0 3 0 1; 2 0 1 0] |
| 76 | + loss_w(A) = sum(abs2, get_edge_weight(GNNGraph(A, graph_type = :dense))) |
| 77 | + @test Array(mooncake_gradient(loss_w, dev(A)))≈2 .* A rtol=1e-4 |
| 78 | + end |
| 79 | +end |
0 commit comments