|
23 | 23 | Enzyme.autodiff(Reverse, loss_w, Active, Duplicated(g, dg)) |
24 | 24 | @test nonzeros(dg.graph) ≈ 2 .* nonzeros(S) |
25 | 25 | end |
| 26 | + |
| 27 | +@testitem "Enzyme treats scaled_laplacian as constant" setup=[GraphsTestModule] begin |
| 28 | + using .GraphsTestModule |
| 29 | + using Enzyme: Enzyme, Const, Reverse |
| 30 | + |
| 31 | + # Without GNNGraphsEnzymeCoreExt Enzyme differentiates the Krylov eigensolve |
| 32 | + # inside `scaled_laplacian` and fails; ChainRules already treats it as constant. |
| 33 | + # Bidirected cycle: symmetric and without isolated nodes. |
| 34 | + src = [1, 2, 3, 4, 5, 6, 7, 8] |
| 35 | + dst = [2, 3, 4, 5, 6, 7, 8, 1] |
| 36 | + g = GNNGraph(vcat(src, dst), vcat(dst, src)) |
| 37 | + x = randn(MersenneTwister(17), Float32, 3, g.num_nodes) |
| 38 | + loss(x, g) = sum(abs2, x * scaled_laplacian(g, Float32)) |
| 39 | + |
| 40 | + grad = Enzyme.gradient(Reverse, loss, x, Const(g))[1] |
| 41 | + gfd = ngradient(x -> loss(x, g), x)[1] |
| 42 | + @test grad ≈ gfd rtol = 1e-4 |
| 43 | +end |
0 commit comments